def test_unbind_w_error(self, m_exception, m_repo_qs, m_dist_qs, m_managers, m_plug_api, m_plug_call_conf, m_task): """ Test handling of errors raised by unbind. """ m_dist_inst = mock.MagicMock() m_plug_config = mock.MagicMock() m_plug_api.get_distributor_by_id.return_value = (m_dist_inst, m_plug_config) m_bind_man = m_managers.consumer_bind_manager.return_value m_bind = { 'consumer_id': 'cid', 'repo_id': 'rid', 'distributor_id': 'did' } m_bind_man.find_by_distributor.return_value = [m_bind] m_bind_man.unbind.side_effect = Exception('test e') result = distributor.delete('rid', 'did') m_bind_man.unbind.assert_called_once_with(m_bind['consumer_id'], m_bind['repo_id'], m_bind['distributor_id'], {}) m_task.assert_called_once_with(error=m_exception.return_value, spawned_tasks=[]) self.assertTrue(result is m_task.return_value)
def test_bindings(self, m_repo_qs, m_dist_qs, m_managers, m_plug_api, m_plug_call_conf, m_task): """ Test that consumers are unbound after a distributor is removed. """ m_dist_inst = mock.MagicMock() m_plug_config = mock.MagicMock() m_plug_api.get_distributor_by_id.return_value = (m_dist_inst, m_plug_config) m_bind_man = m_managers.consumer_bind_manager.return_value m_bind = { 'consumer_id': 'cid', 'repo_id': 'rid', 'distributor_id': 'did' } m_bind_man.find_by_distributor.return_value = [m_bind] m_bind_man.unbind.return_value = None result = distributor.delete('rid', 'did') m_bind_man.unbind.assert_called_once_with(m_bind['consumer_id'], m_bind['repo_id'], m_bind['distributor_id'], {}) m_task.assert_called_once_with(error=None, spawned_tasks=[]) self.assertTrue(result is m_task.return_value)
def test_unbind_w_spawned_tasks(self, m_repo_qs, m_dist_qs, m_managers, m_plug_api, m_plug_call_conf, m_task): """ Ensure that when unbind spawns tasks, they are included in the result. """ m_dist_inst = mock.MagicMock() m_plug_config = mock.MagicMock() m_plug_api.get_distributor_by_id.return_value = (m_dist_inst, m_plug_config) m_bind_man = m_managers.consumer_bind_manager.return_value m_bind = { 'consumer_id': 'cid', 'repo_id': 'rid', 'distributor_id': 'did' } m_bind_man.find_by_distributor.return_value = [m_bind] m_bind_man.unbind.return_value = mock.MagicMock( spawned_tasks=['another_task']) result = distributor.delete('rid', 'did') m_bind_man.unbind.assert_called_once_with(m_bind['consumer_id'], m_bind['repo_id'], m_bind['distributor_id'], {}) m_task.assert_called_once_with(error=None, spawned_tasks=['another_task']) self.assertTrue(result is m_task.return_value)
def test_distributor_delete_with_agent_errors(self, mock_dist_manager, mock_bind_manager, mock_unbind): mock_bind_manager.return_value.find_by_repo.return_value = [{ 'consumer_id': 'foo', 'repo_id': 'repo-foo', 'distributor_id': 'dist-id' }] mock_bind_manager.return_value.find_by_distributor.return_value = [{ 'consumer_id': 'foo', 'repo_id': 'repo-foo', 'distributor_id': 'dist-id' }] side_effect_exception = PulpException('foo') mock_unbind.side_effect = side_effect_exception result = dist_controller.delete('foo-id', 'bar-id') mock_unbind.assert_called_once_with('foo', 'repo-foo', 'dist-id', ANY) self.assertTrue(isinstance(result.error, PulpException)) self.assertEquals(result.error.error_code, error_codes.PLP0003) self.assertEquals(result.error.child_exceptions[0], side_effect_exception)
def test_distributor_delete_with_bindings(self, mock_dist_manager, mock_bind_manager, mock_unbind): mock_bind_manager.return_value.find_by_distributor.return_value = [ {'consumer_id': 'foo', 'repo_id': 'repo-foo', 'distributor_id': 'dist-id'}] mock_unbind.return_value = TaskResult(spawned_tasks=[{'task_id': 'foo-request-id'}]) result = dist_controller.delete('foo-id', 'bar-id') mock_dist_manager.return_value.remove_distributor.assert_called_with('foo-id', 'bar-id') mock_unbind.assert_called_once_with('foo', 'repo-foo', 'dist-id', ANY) self.assertEquals(result.spawned_tasks[0], {'task_id': 'foo-request-id'})
def test_distributor_delete_with_agent_errors(self, mock_dist_manager, mock_bind_manager, mock_unbind): mock_bind_manager.return_value.find_by_repo.return_value = [ {'consumer_id': 'foo', 'repo_id': 'repo-foo', 'distributor_id': 'dist-id'}] mock_bind_manager.return_value.find_by_distributor.return_value = [ {'consumer_id': 'foo', 'repo_id': 'repo-foo', 'distributor_id': 'dist-id'}] side_effect_exception = PulpException('foo') mock_unbind.side_effect = side_effect_exception result = dist_controller.delete('foo-id', 'bar-id') mock_unbind.assert_called_once_with('foo', 'repo-foo', 'dist-id', ANY) self.assertTrue(isinstance(result.error, PulpException)) self.assertEquals(result.error.error_code, error_codes.PLP0003) self.assertEquals(result.error.child_exceptions[0], side_effect_exception)
def test_bindings(self, m_repo_qs, m_dist_qs, m_managers, m_plug_api, m_plug_call_conf, m_task): """ Test that consumers are unbound after a distributor is removed. """ m_dist_inst = mock.MagicMock() m_plug_config = mock.MagicMock() m_plug_api.get_distributor_by_id.return_value = (m_dist_inst, m_plug_config) m_bind_man = m_managers.consumer_bind_manager.return_value m_bind = {'consumer_id': 'cid', 'repo_id': 'rid', 'distributor_id': 'did'} m_bind_man.find_by_distributor.return_value = [m_bind] m_bind_man.unbind.return_value = None result = distributor.delete('rid', 'did') m_bind_man.unbind.assert_called_once_with(m_bind['consumer_id'], m_bind['repo_id'], m_bind['distributor_id'], {}) m_task.assert_called_once_with(error=None, spawned_tasks=[]) self.assertTrue(result is m_task.return_value)
def test_unbind_w_error(self, m_exception, m_repo_qs, m_dist_qs, m_managers, m_plug_api, m_plug_call_conf, m_task): """ Test handling of errors raised by unbind. """ m_dist_inst = mock.MagicMock() m_plug_config = mock.MagicMock() m_plug_api.get_distributor_by_id.return_value = (m_dist_inst, m_plug_config) m_bind_man = m_managers.consumer_bind_manager.return_value m_bind = {'consumer_id': 'cid', 'repo_id': 'rid', 'distributor_id': 'did'} m_bind_man.find_by_distributor.return_value = [m_bind] m_bind_man.unbind.side_effect = Exception('test e') result = distributor.delete('rid', 'did') m_bind_man.unbind.assert_called_once_with(m_bind['consumer_id'], m_bind['repo_id'], m_bind['distributor_id'], {}) m_task.assert_called_once_with(error=m_exception.return_value, spawned_tasks=[]) self.assertTrue(result is m_task.return_value)
def test_unbind_w_spawned_tasks(self, m_repo_qs, m_dist_qs, m_managers, m_plug_api, m_plug_call_conf, m_task): """ Ensure that when unbind spawns tasks, they are included in the result. """ m_dist_inst = mock.MagicMock() m_plug_config = mock.MagicMock() m_plug_api.get_distributor_by_id.return_value = (m_dist_inst, m_plug_config) m_bind_man = m_managers.consumer_bind_manager.return_value m_bind = {'consumer_id': 'cid', 'repo_id': 'rid', 'distributor_id': 'did'} m_bind_man.find_by_distributor.return_value = [m_bind] m_bind_man.unbind.return_value = mock.MagicMock(spawned_tasks=['another_task']) result = distributor.delete('rid', 'did') m_bind_man.unbind.assert_called_once_with(m_bind['consumer_id'], m_bind['repo_id'], m_bind['distributor_id'], {}) m_task.assert_called_once_with(error=None, spawned_tasks=['another_task']) self.assertTrue(result is m_task.return_value)
def test_expected(self, m_repo_qs, m_dist_qs, m_managers, m_plug_api, m_plug_call_conf, m_task): """ Test removal of a distributor with minimal valid arguments. """ m_repo_obj = m_repo_qs.get_repo_or_missing_resource.return_value m_dist_inst = mock.MagicMock() m_plug_config = mock.MagicMock() m_plug_api.get_distributor_by_id.return_value = (m_dist_inst, m_plug_config) m_repo_pub_sched_man = m_managers.repo_publish_schedule_manager.return_value m_bind_man = m_managers.consumer_bind_manager.return_value m_bind_man.find_by_distributor.return_value = [] result = distributor.delete('rid', 'did') m_repo_pub_sched_man.delete_by_distributor_id.assert_called_once_with('rid', 'did') m_dist_inst.distributor_removed.assert_called_once_with( m_repo_obj.to_transfer_repo.return_value, m_plug_call_conf.return_value) m_dist_qs.get_or_404.return_value.delete.assert_called_once_with() m_task.assert_called_once_with(error=None, spawned_tasks=[]) self.assertTrue(result is m_task.return_value)
def test_distributor_delete_with_bindings(self, mock_dist_manager, mock_bind_manager, mock_unbind): mock_bind_manager.return_value.find_by_distributor.return_value = [{ 'consumer_id': 'foo', 'repo_id': 'repo-foo', 'distributor_id': 'dist-id' }] mock_unbind.return_value = TaskResult( spawned_tasks=[{ 'task_id': 'foo-request-id' }]) result = dist_controller.delete('foo-id', 'bar-id') mock_dist_manager.return_value.remove_distributor.assert_called_with( 'foo-id', 'bar-id') mock_unbind.assert_called_once_with('foo', 'repo-foo', 'dist-id', ANY) self.assertEquals(result.spawned_tasks[0], {'task_id': 'foo-request-id'})
error_tuples = [] # tuple of failed step and exception arguments # Inform the importer repo_importer = model.Importer.objects(repo_id=repo_id).first() if repo_importer is not None: try: importer_controller.remove_importer(repo_id) except Exception, e: _logger.exception('Error received removing importer [%s] from repo [%s]' % ( repo_importer.importer_type_id, repo_id)) error_tuples.append(e) # Inform all distributors for distributor in model.Distributor.objects(repo_id=repo_id): try: dist_controller.delete(distributor.repo_id, distributor.distributor_id) except Exception, e: _logger.exception('Error received removing distributor [%s] from repo [%s]' % ( distributor.id, repo_id)) error_tuples.append(e) # Database Updates repo = model.Repository.objects.get_repo_or_missing_resource(repo_id) repo.delete() try: # Remove all importers and distributors from the repo. This is likely already done by the # calls to other methods in this manager, but in case those failed we still want to attempt # to keep the database clean. model.Distributor.objects(repo_id=repo_id).delete() model.Importer.objects(repo_id=repo_id).delete()
def test_distributor_delete_no_bindings(self, mock_dist_manager, mock_bind_manager): result = dist_controller.delete('foo-id', 'bar-id') mock_dist_manager.return_value.remove_distributor.assert_called_with( 'foo-id', 'bar-id') self.assertTrue(isinstance(result, TaskResult))
def test_distributor_delete_no_bindings(self, mock_dist_manager, mock_bind_manager): result = dist_controller.delete('foo-id', 'bar-id') mock_dist_manager.return_value.remove_distributor.assert_called_with('foo-id', 'bar-id') self.assertTrue(isinstance(result, TaskResult))