Exemple #1
0
def test_file_data_no_data():
    with tempfile.TemporaryDirectory() as d:
        path = os.path.join(d, 'test')
        # file does not exist
        data = filedata.FileData(path, compress=False)
        with pytest.raises(exception.ExternalDataFailed):
            data.retrieve()
        # file is empty
        open(path, 'w').write('')
        data = filedata.FileData(path, compress=False, allow_empty=False)
        with pytest.raises(exception.ExternalDataFailed):
            data.retrieve()
        data = filedata.FileData(path, compress=False, allow_empty=True)
        assert data.retrieve() == ''
Exemple #2
0
def write_nvram_data(vm_id, nvram_data):
    nvram_data = password.unprotect(nvram_data)
    nvram_path = filedata.nvram_path(vm_id)
    # Create the file with restricted permissions owned by root
    if os.path.exists(nvram_path):
        os.remove(nvram_path)
    fd = os.open(nvram_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, mode=0o600)
    os.close(fd)
    # Write content
    accessor = filedata.FileData(nvram_path)
    accessor.store(nvram_data)
Exemple #3
0
def test_file_data_conditional_read(last_modified, is_none):
    with tempfile.TemporaryDirectory() as d:
        path = os.path.join(d, 'test')
        open(path, 'w').write(FILE_DATA)
        data = filedata.FileData(path, compress=True)
        if last_modified is None:
            last_modified = data.last_modified()
        encoded = data.retrieve(last_modified=last_modified)
        if is_none:
            assert encoded is None
        else:
            assert encoded == ENCODED_DATA_BZ2
Exemple #4
0
def test_file_data_read():
    with tempfile.TemporaryDirectory() as d:
        path = os.path.join(d, 'test')
        open(path, 'w').write(FILE_DATA)
        data = filedata.FileData(path, compress=False)
        assert data.retrieve() == ENCODED_DATA
Exemple #5
0
def test_file_data_modified():
    with tempfile.TemporaryDirectory() as d:
        path = os.path.join(d, 'test')
        open(path, 'w').write(FILE_DATA)
        data = filedata.FileData(path, compress=False)
        assert data.last_modified() == os.stat(path).st_mtime
Exemple #6
0
def test_file_data_write():
    with tempfile.TemporaryDirectory() as d:
        path = os.path.join(d, 'test')
        data = filedata.FileData(path)
        data.store(ENCODED_DATA)
        assert open(path).read() == FILE_DATA
Exemple #7
0
def read_nvram_data(vm_id, last_modified):
    accessor = filedata.FileData(filedata.nvram_path(vm_id))
    currently_modified = accessor.last_modified()
    data = accessor.retrieve(last_modified=last_modified)
    return password.ProtectedPassword(data), currently_modified