def test_extract_paths_files_dirs(self, files, dirs, mock_gfs_fs): def mock_is_file(fname, **kwargs): return len([src for src, _ in files if src == fname]) == 1 def mock_is_dir(dname, **kwargs): return len([src for src, _ in dirs if src == dname]) == 1 joined = files + dirs shuffle(joined) with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs'): mock_path_isdir.return_value = True mock_gfs_fs.is_file.side_effect = mock_is_file mock_gfs_fs.is_dir.side_effect = mock_is_dir guestfs_tools.extract_paths('a', 'b', joined, ignore_nopath=False) assert sorted(mock_path_isdir.mock_calls) == sorted( [call(host_path) for _, host_path in dirs]) assert sorted(mock_gfs_fs.download.mock_calls) == sorted([ call(guest_path, host_path) for guest_path, host_path in files ]) assert sorted(mock_gfs_fs.copy_out.mock_calls) == sorted([ call(guest_path, host_path) for guest_path, host_path in dirs ])
def test_extract_paths_files_dirs(self, files, dirs, mock_gfs_fs): def mock_is_file(fname, **kwargs): return len([src for src, _ in files if src == fname]) == 1 def mock_is_dir(dname, **kwargs): return len([src for src, _ in dirs if src == dname]) == 1 joined = files + dirs shuffle(joined) with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs'): mock_path_isdir.return_value = True mock_gfs_fs.is_file.side_effect = mock_is_file mock_gfs_fs.is_dir.side_effect = mock_is_dir guestfs_tools.extract_paths( 'a', 'b', joined, ignore_nopath=False ) assert sorted(mock_path_isdir.mock_calls) == sorted( [call(host_path) for _, host_path in dirs] ) assert sorted(mock_gfs_fs.download.mock_calls) == sorted( [ call(guest_path, host_path) for guest_path, host_path in files ] ) assert sorted(mock_gfs_fs.copy_out.mock_calls) == sorted( [ call(guest_path, host_path) for guest_path, host_path in dirs ] )
def extract_paths_dead(self, paths, ignore_nopath): """ Extract the given paths from the domain using guestfs. Using guestfs can have side-effects and should be used as a second option, mainly when SSH is not available. Args: paths(list of str): paths to extract ignore_nopath(boolean): if True will ignore none existing paths. Returns: None Raises: :exc:`~lago.utils.LagoException`: if :mod:`guestfs` is not importable. :exc:`~lago.plugins.vm.ExtractPathNoPathError`: if a none existing path was found on the VM, and `ignore_nopath` is True. :exc:`~lago.plugins.vm.ExtractPathError`: on failure extracting the files. """ if not self._has_guestfs: raise LagoException(('guestfs module not available, cannot ' )('extract files with libguestfs')) LOGGER.debug('%s: attempting to extract files with libguestfs', self.vm.name()) guestfs_tools.extract_paths( disk_path=self.vm.spec['disks'][0]['path'], disk_root=self.vm.spec['disks'][0]['metadata'].get( 'root-partition', 'root'), paths=paths, ignore_nopath=ignore_nopath)
def test_extract_paths_nodir_created(self, mock_gfs_fs): with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs') as mock_makedirs: mock_path_isdir.return_value = False mock_gfs_fs.is_file.return_value = False mock_gfs_fs.is_dir.return_value = True guestfs_tools.extract_paths('a', 'b', [('src-dir', 'dst-dir')], ignore_nopath=False) assert mock_makedirs.mock_calls == [call('dst-dir')]
def test_extract_paths_nodir_created(self, mock_gfs_fs): with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs') as mock_makedirs: mock_path_isdir.return_value = False mock_gfs_fs.is_file.return_value = False mock_gfs_fs.is_dir.return_value = True guestfs_tools.extract_paths( 'a', 'b', [('src-dir', 'dst-dir')], ignore_nopath=False ) assert mock_makedirs.mock_calls == [call('dst-dir')]
def test_extract_paths_no_file_guest_skipped(self, mock_gfs_fs): with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs'): mock_path_isdir.return_value = False mock_gfs_fs.is_file.return_value = False mock_gfs_fs.is_dir.return_value = False guestfs_tools.extract_paths('a', 'b', [('src', 'dst')], ignore_nopath=True) assert mock_gfs_fs.is_file.call_count == 1 assert mock_gfs_fs.is_dir.call_count == 1
def test_extract_paths_dir_raises(self, mock_gfs_fs): with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs'): mock_path_isdir.return_value = True mock_gfs_fs.is_file.return_value = False mock_gfs_fs.copy_out.side_effect = RuntimeError('mock') with pytest.raises(GuestFSError): guestfs_tools.extract_paths('a', 'b', [('src-dir', 'dst-dir')], ignore_nopath=False) assert mock_gfs_fs.copy_out.call_count == 1
def test_extract_paths_no_file_guest_skipped(self, mock_gfs_fs): with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs'): mock_path_isdir.return_value = False mock_gfs_fs.is_file.return_value = False mock_gfs_fs.is_dir.return_value = False guestfs_tools.extract_paths( 'a', 'b', [('src', 'dst')], ignore_nopath=True ) assert mock_gfs_fs.is_file.call_count == 1 assert mock_gfs_fs.is_dir.call_count == 1
def test_extract_paths_no_file_guest_raises(self, mock_gfs_fs): with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs'): mock_path_isdir.return_value = False mock_gfs_fs.is_file.return_value = False mock_gfs_fs.is_dir.return_value = False with pytest.raises(ExtractPathNoPathError): guestfs_tools.extract_paths('a', 'b', [('src', 'dst')], ignore_nopath=False) assert mock_gfs_fs.is_file.call_count == 1 assert mock_gfs_fs.is_dir.call_count == 1
def test_extract_paths_no_file_guest_raises(self, mock_gfs_fs): with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs'): mock_path_isdir.return_value = False mock_gfs_fs.is_file.return_value = False mock_gfs_fs.is_dir.return_value = False with pytest.raises(ExtractPathNoPathError): guestfs_tools.extract_paths( 'a', 'b', [('src', 'dst')], ignore_nopath=False ) assert mock_gfs_fs.is_file.call_count == 1 assert mock_gfs_fs.is_dir.call_count == 1
def test_extract_paths_dir_raises(self, mock_gfs_fs): with patch('lago.guestfs_tools.os.path.isdir') as mock_path_isdir: with patch('lago.guestfs_tools.os.makedirs'): mock_path_isdir.return_value = True mock_gfs_fs.is_file.return_value = False mock_gfs_fs.copy_out.side_effect = RuntimeError('mock') with pytest.raises(GuestFSError): guestfs_tools.extract_paths( 'a', 'b', [('src-dir', 'dst-dir')], ignore_nopath=False ) assert mock_gfs_fs.copy_out.call_count == 1
def extract_paths_dead(self, paths, ignore_nopath): """ Extract the given paths from the domain using guestfs. Using guestfs can have side-effects and should be used as a second option, mainly when SSH is not available. Args: paths(list of str): paths to extract ignore_nopath(boolean): if True will ignore none existing paths. Returns: None Raises: :exc:`~lago.utils.LagoException`: if :mod:`guestfs` is not importable. :exc:`~lago.plugins.vm.ExtractPathNoPathError`: if a none existing path was found on the VM, and `ignore_nopath` is True. :exc:`~lago.plugins.vm.ExtractPathError`: on failure extracting the files. """ if not self._has_guestfs: raise LagoException( ('guestfs module not available, cannot ' )('extract files with libguestfs') ) LOGGER.debug( '%s: attempting to extract files with libguestfs', self.vm.name() ) guestfs_tools.extract_paths( disk_path=self.vm.spec['disks'][0]['path'], disk_root=self.vm.spec['disks'][0]['metadata'].get( 'root-partition', 'root' ), paths=paths, ignore_nopath=ignore_nopath )