def test_call(self, mock_backup_helper_backup): path = "/some/path" bh = utils.BackupHelper() self.assertEqual(bh, bh(path)) mock_backup_helper_backup.assert_called_once_with(path)
def test_context_manager(self, mock_backup_helper_rollback): bh = utils.BackupHelper() with bh: pass self.assertFalse(mock_backup_helper_rollback.called) bh = utils.BackupHelper() try: with bh: raise RuntimeError() except RuntimeError: # it is expected behaviour pass else: self.fail("BackupHelper context manager should not hide " "an exception") self.assertTrue(mock_backup_helper_rollback.called)
def test___del__(self, mock_rmtree, mock_exists): paths = {"original_path": "/tmp/backup_of_something", "another_path": "/tmp/backup_of_another_thing"} bh = utils.BackupHelper() bh._stored_data = paths del bh self.assertEqual([mock.call(p) for p in paths.values()], mock_rmtree.call_args_list)
def test_backup_failed_while_copy(self, mock_copytree, mock_generate_random_path, mock_backup_helper_rollback): backup_dir = "another_dir" mock_generate_random_path.side_effect = ("base_tmp_dir", backup_dir) mock_copytree.side_effect = RuntimeError bh = utils.BackupHelper() path = "some_path" self.assertRaises(RuntimeError, bh.backup, path) mock_copytree.assert_called_once_with(path, backup_dir, symlinks=True) self.assertTrue(not bh._stored_data) mock_backup_helper_rollback.assert_called_once_with()
def test_backup(self, mock_copytree, mock_generate_random_path): backup_dir = "another_dir" mock_generate_random_path.side_effect = ("base_tmp_dir", backup_dir) bh = utils.BackupHelper() path = "some_path" bh.backup(path) mock_copytree.assert_called_once_with(path, backup_dir, symlinks=True) self.assertEqual(backup_dir, bh._stored_data[path]) mock_copytree.reset_mock() self.assertRaises(exceptions.RallyException, bh.backup, path) self.assertFalse(mock_copytree.called)
def test_rollback(self, mock_copytree, mock_rmtree, mock_exists): bh = utils.BackupHelper() original_path = "/some/original/path" tmp_path = "/temporary/location/for/backup" path = {original_path: tmp_path} bh._stored_data = path rollback_method = mock.MagicMock() args = (1, 2, 3) kwargs = {"arg1": "value"} bh.add_rollback_action(rollback_method, *args, **kwargs) bh.rollback() mock_rmtree.assert_called_once_with(original_path) mock_copytree.assert_called_once_with(tmp_path, original_path, symlinks=True) self.assertTrue(not bh._stored_data) rollback_method.assert_called_once_with(*args, **kwargs)
def test___init__(self, mock_generate_random_path): utils.BackupHelper() mock_generate_random_path.assert_called_once_with() self.mock_mkdir.assert_called_once_with( mock_generate_random_path.return_value)
def update(cls, verifier_id, system_wide=None, version=None, update_venv=False): """Update a verifier. :param verifier_id: Verifier name or UUID :param system_wide: Switch to using the system-wide environment :param version: Branch, tag or commit ID to checkout :param update_venv: Update the virtual environment for verifier """ if system_wide is None and version is None and not update_venv: # nothing to update raise exceptions.RallyException( "At least one of the following parameters should be " "specified: 'system_wide', 'version', 'update_venv'.") verifier = cls.get(verifier_id) LOG.info("Updating verifier %s.", verifier) if verifier.status != consts.VerifierStatus.INSTALLED: raise exceptions.RallyException( "Failed to update verifier %s because verifier is in '%s' " "status, but should be in '%s'." % (verifier, verifier.status, consts.VerifierStatus.INSTALLED)) system_wide_in_use = (system_wide or (system_wide is None and verifier.system_wide)) if update_venv and system_wide_in_use: raise exceptions.RallyException( "It is impossible to update the virtual environment for " "verifier %s when it uses the system-wide environment." % verifier) # store original status to set it again after updating or rollback original_status = verifier.status verifier.update_status(consts.VerifierStatus.UPDATING) properties = {} # store new verifier properties to update old ones sw_is_checked = False if version: properties["version"] = version backup = utils.BackupHelper() rollback_msg = ("Failed to update verifier %s. It has been " "rollbacked to the previous state." % verifier) backup.add_rollback_action(LOG.info, rollback_msg) backup.add_rollback_action(verifier.update_status, original_status) with backup(verifier.manager.repo_dir): verifier.manager.checkout(version) if system_wide_in_use: verifier.manager.check_system_wide() sw_is_checked = True if system_wide is not None: if system_wide == verifier.system_wide: LOG.info( "Verifier %s is already switched to system_wide=%s. " "Nothing will be changed.", verifier, verifier.system_wide) else: properties["system_wide"] = system_wide if not system_wide: update_venv = True # we need to install a virtual env else: # NOTE(andreykurilin): should we remove previously created # virtual environment?! if not sw_is_checked: verifier.manager.check_system_wide() if update_venv: backup = utils.BackupHelper() rollback_msg = ("Failed to update the virtual environment for " "verifier %s. It has been rollbacked to the " "previous state." % verifier) backup.add_rollback_action(LOG.info, rollback_msg) backup.add_rollback_action(verifier.update_status, original_status) with backup(verifier.manager.venv_dir): verifier.manager.install_venv() properties["status"] = original_status # change verifier status back verifier.update_properties(**properties) LOG.info("Verifier %s has been successfully updated!", verifier) return verifier.uuid