def setUp(self): super(RepoUnitAssociationManagerTests, self).setUp() database.update_database([TYPE_1_DEF, TYPE_2_DEF, MOCK_TYPE_DEF]) mock_plugins.install() self.manager = association_manager.RepoUnitAssociationManager() self.content_manager = content_cud_manager.ContentManager() # Set up a valid configured repo for the tests self.repo_id = 'associate-repo' with mock.patch('pulp.server.controllers.importer.model.Repository'): importer_controller.set_importer(self.repo_id, 'mock-importer', {}) # Create units that can be associated to a repo self.unit_type_id = 'mock-type' self.unit_id = 'test-unit-id' self.unit_key = {'key-1': 'test-unit'} self.content_manager.add_content_unit(self.unit_type_id, self.unit_id, self.unit_key) self.unit_id_2 = 'test-unit-id-2' self.unit_key_2 = {'key-1': 'test-unit-2'} self.content_manager.add_content_unit(self.unit_type_id, self.unit_id_2, self.unit_key_2)
def test_import_uploaded_unit_importer_error(self, mock_repo_qs): importer_controller.set_importer('repo-u', 'mock-importer', {}) mock_plugins.MOCK_IMPORTER.upload_unit.side_effect = Exception() upload_id = self.upload_manager.initialize_upload() self.assertRaises(PulpExecutionException, self.upload_manager.import_uploaded_unit, 'repo-u', 'mock-type', {}, {}, upload_id)
def test_associate_from_repo_missing_source(self, mock_repo, mock_crit): importer_controller.set_importer('dest_repo', 'mock-importer', {}) try: self.manager.associate_from_repo('missing', 'dest_repo', mock_crit) self.fail('Exception expected') except exceptions.MissingResource, e: self.assertTrue('missing' == e.resources['repo_id'])
def test_associate_from_repo_missing_destination(self, mock_repo, mock_crit): importer_controller.set_importer('source_repo', 'mock-importer', {}) try: self.manager.associate_from_repo('source_repo', 'missing', mock_crit) self.fail('Exception expected') except exceptions.MissingResource, e: self.assertTrue('missing' == e.resources['repo_id'])
def test_import_uploaded_unit_importer_error_reraise_pulp_exception( self, mock_repo_qs): importer_controller.set_importer('repo-u', 'mock-importer', {}) mock_plugins.MOCK_IMPORTER.upload_unit.side_effect = InvalidValue( ['filename']) upload_id = self.upload_manager.initialize_upload() self.assertRaises(InvalidValue, self.upload_manager.import_uploaded_unit, 'repo-u', 'mock-type', {}, {}, upload_id)
def test_import_uploaded_unit_success_flag_false(self, mock_repo_qs): """Test that exception is raised if upload report indicates failure.""" importer_controller.set_importer('repo-u', 'mock-importer', {}) importer_return_report = {'success_flag': False, 'summary': '', 'details': {}} mock_plugins.MOCK_IMPORTER.upload_unit.side_effect = None mock_plugins.MOCK_IMPORTER.upload_unit.return_value = importer_return_report upload_id = self.upload_manager.initialize_upload() with self.assertRaises(PulpCodedException) as cm: self.upload_manager.import_uploaded_unit('repo-u', 'mock-type', {}, {}, upload_id) self.assertEqual('PLP0047', cm.exception.error_code.code)
def test_plugin_is_invalid_importer(self, m_validate_conf, m_model, m_plug_api, m_clean, mock_remove, mock_plug_call_config, *_): """ If the plugin_api returns that an importer is invalid, raise. """ m_plug_api.is_valid_importer.return_value = False try: importer.set_importer('m_id', 'm_type', 'm_conf') except exceptions.PulpCodedValidationException, e: pass
def setUp(self, mock_repo_qs, mock_imp_qs, mock_remove): super(RepoSyncConduitTests, self).setUp() mock_plugins.install() types_database.update_database([TYPE_1_DEF, TYPE_2_DEF]) self.association_manager = association_manager.RepoUnitAssociationManager() self.association_query_manager = association_query_manager.RepoUnitAssociationQueryManager() self.content_manager = content_manager.ContentManager() self.query_manager = query_manager.ContentQueryManager() self.conduit = RepoSyncConduit('repo-1', 'test-importer', 'abc123') importer_controller.set_importer('repo-1', 'mock-importer', {})
def test_import_uploaded_unit(self, mock_repo_qs, mock_rebuild): importer_controller.set_importer('repo-u', 'mock-importer', {}) key = {'key': 'value'} metadata = {'k1': 'v1'} timestamp_pre_upload = dateutils.now_utc_datetime_with_tzinfo() mock_repo = mock_repo_qs.get_repo_or_missing_resource.return_value importer_return_report = { 'success_flag': True, 'summary': '', 'details': {} } mock_plugins.MOCK_IMPORTER.upload_unit.return_value = importer_return_report upload_id = self.upload_manager.initialize_upload() file_path = self.upload_manager._upload_file_path(upload_id) fake_user = model.User('import-user', '') manager_factory.principal_manager().set_principal(principal=fake_user) response = self.upload_manager.import_uploaded_unit( 'repo-u', 'mock-type', key, metadata, upload_id) # import_uploaded_unit() should have returned our importer_return_report self.assertTrue(response is importer_return_report) call_args = mock_plugins.MOCK_IMPORTER.upload_unit.call_args[0] self.assertTrue(call_args[0] is mock_repo.to_transfer_repo()) self.assertEqual(call_args[1], 'mock-type') self.assertEqual(call_args[2], key) self.assertEqual(call_args[3], metadata) self.assertEqual(call_args[4], file_path) conduit = call_args[5] self.assertTrue(isinstance(conduit, UploadConduit)) self.assertEqual(call_args[5].repo_id, 'repo-u') # It is now platform's responsibility to update plugin content unit counts self.assertTrue(mock_rebuild.called, "rebuild_content_unit_counts must be called") # Make sure that the last_unit_added timestamp was updated self.assertTrue(mock_repo.last_unit_added > timestamp_pre_upload) # Clean up mock_plugins.MOCK_IMPORTER.upload_unit.return_value = None manager_factory.principal_manager().set_principal(principal=None)
def test_associate_from_repo_no_matching_units(self, mock_importer, mock_plugin, mock_repo, mock_crit, mock_rebuild_count): mock_imp_inst = mock.MagicMock() mock_plugin.get_importer_by_id.return_value = (mock_imp_inst, mock.MagicMock()) source_repo = mock.MagicMock(repo_id='source-repo') dest_repo = mock.MagicMock(repo_id='dest-repo') with mock.patch('pulp.server.controllers.importer.remove_importer'): importer_controller.set_importer(source_repo, 'mock-importer', {}) importer_controller.set_importer(dest_repo, 'mock-importer', {}) mock_plugins.MOCK_IMPORTER.import_units.return_value = [] ret = self.manager.associate_from_repo('source_repo', 'dest_repo', mock_crit) self.assertEqual(1, mock_imp_inst.import_units.call_count) self.assertEqual(ret.get('units_successful'), [])
def test_duplicate(self, m_validate_conf, m_model, m_plug_api, m_clean, mock_remove, mock_plug_call_config, *_): """ Ensure that if an importer already exists, it is replaced by the new one. """ mock_repo = m_model.Repository.objects.get_repo_or_missing_resource.return_value mock_importer = m_model.Importer.return_value mock_imp_inst = mock.MagicMock() mock_call_config = mock_plug_call_config.return_value mock_remove.side_effect = exceptions.MissingResource mock_plugin_config = mock.MagicMock() m_plug_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) result = importer.set_importer('mrepo', 'mtype', 'm_conf') m_clean.assert_called_once_with('m_conf') mock_plug_call_config.assert_called_once_with(mock_plugin_config, m_clean.return_value) mock_remove.assert_called_once_with('mrepo') mock_imp_inst.importer_added.assert_called_once_with( mock_repo.to_transfer_repo(), mock_call_config) mock_importer.save.assert_called_once_with() m_model.Importer.SERIALIZER.assert_called_once_with(mock_importer) self.assertTrue( result is m_model.Importer.SERIALIZER.return_value.data)
def test_associate_from_repo_importer_error(self, mock_repo, mock_crit): importer_controller.set_importer('source_repo', 'mock-importer', {}) importer_controller.set_importer('dest_repo', 'mock-importer', {}) mock_plugins.MOCK_IMPORTER.import_units.side_effect = Exception() self.content_manager.add_content_unit('mock-type', 'unit-1', {'key-1': 'unit-1'}) self.manager.associate_unit_by_id('source_repo', 'mock-type', 'unit-1') # Test try: self.manager.associate_from_repo('source_repo', 'dest_repo', mock_crit) self.fail('Exception expected') except exceptions.PulpExecutionException: pass # Cleanup mock_plugins.MOCK_IMPORTER.import_units.side_effect = None
def test_import_uploaded_unit(self, mock_repo_qs, mock_rebuild): importer_controller.set_importer('repo-u', 'mock-importer', {}) key = {'key': 'value'} metadata = {'k1': 'v1'} timestamp_pre_upload = dateutils.now_utc_datetime_with_tzinfo() mock_repo = mock_repo_qs.get_repo_or_missing_resource.return_value importer_return_report = {'success_flag': True, 'summary': '', 'details': {}} mock_plugins.MOCK_IMPORTER.upload_unit.return_value = importer_return_report upload_id = self.upload_manager.initialize_upload() file_path = self.upload_manager._upload_file_path(upload_id) fake_user = model.User('import-user', '') manager_factory.principal_manager().set_principal(principal=fake_user) response = self.upload_manager.import_uploaded_unit('repo-u', 'mock-type', key, metadata, upload_id) # import_uploaded_unit() should have returned our importer_return_report self.assertTrue(response is importer_return_report) call_args = mock_plugins.MOCK_IMPORTER.upload_unit.call_args[0] self.assertTrue(call_args[0] is mock_repo.to_transfer_repo()) self.assertEqual(call_args[1], 'mock-type') self.assertEqual(call_args[2], key) self.assertEqual(call_args[3], metadata) self.assertEqual(call_args[4], file_path) conduit = call_args[5] self.assertTrue(isinstance(conduit, UploadConduit)) self.assertEqual(call_args[5].repo_id, 'repo-u') # It is now platform's responsibility to update plugin content unit counts self.assertTrue(mock_rebuild.called, "rebuild_content_unit_counts must be called") # Make sure that the last_unit_added timestamp was updated self.assertTrue(mock_repo.last_unit_added > timestamp_pre_upload) # Clean up mock_plugins.MOCK_IMPORTER.upload_unit.return_value = None manager_factory.principal_manager().set_principal(principal=None)
def test_associate_from_repo_return_tuple(self, mock_importer, mock_plugin, mock_repo, mock_crit, mock_rebuild_count, mock_last_unit_added): mock_imp_inst = mock.MagicMock() mock_plugin.get_importer_by_id.return_value = (mock_imp_inst, mock.MagicMock()) source_repo = mock.MagicMock(repo_id='source-repo') dest_repo = mock.MagicMock(repo_id='dest-repo') with mock.patch('pulp.server.controllers.importer.remove_importer'): importer_controller.set_importer(source_repo, 'mock-importer', {}) importer_controller.set_importer(dest_repo, 'mock-importer', {}) mock_imp_inst.import_units.return_value = (list(), list()) ret = self.manager.associate_from_repo('source_repo', 'dest_repo', mock_crit) mock_last_unit_added.assert_called_once() self.assertEqual(1, mock_imp_inst.import_units.call_count) self.assertEqual(ret.get('units_successful'), []) self.assertEqual(ret.get('units_failed_signature_filter'), [])
def test_as_expected(self, m_validate_conf, m_model, m_plug_api, m_clean, mock_remove, mock_plug_call_config, *_): """ Ensure that if an importer does not already exist, it is created. """ mock_repo = m_model.Repository.objects.get_repo_or_missing_resource.return_value mock_importer = m_model.Importer.return_value mock_imp_inst = mock.MagicMock() mock_call_config = mock_plug_call_config.return_value mock_plugin_config = mock.MagicMock() m_plug_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) result = importer.set_importer("mrepo", "mtype", "m_conf") mock_remove.assert_called_once_with("mrepo") mock_imp_inst.importer_added.assert_called_once_with(mock_repo.to_transfer_repo(), mock_call_config) m_clean.assert_called_once_with("m_conf") mock_plug_call_config.assert_called_once_with(mock_plugin_config, m_clean.return_value) mock_importer.save.assert_called_once_with() m_model.Importer.SERIALIZER.assert_called_once_with(mock_importer) self.assertTrue(result is m_model.Importer.SERIALIZER.return_value.data)
def test_duplicate(self, m_validate_conf, m_model, m_plug_api, m_clean, mock_remove, mock_plug_call_config, *_): """ Ensure that if an importer already exists, it is replaced by the new one. """ mock_repo = m_model.Repository.objects.get_repo_or_missing_resource.return_value mock_importer = m_model.Importer.return_value mock_imp_inst = mock.MagicMock() mock_call_config = mock_plug_call_config.return_value mock_remove.side_effect = exceptions.MissingResource mock_plugin_config = mock.MagicMock() m_plug_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) result = importer.set_importer("mrepo", "mtype", "m_conf") m_clean.assert_called_once_with("m_conf") mock_plug_call_config.assert_called_once_with(mock_plugin_config, m_clean.return_value) mock_remove.assert_called_once_with("mrepo") mock_imp_inst.importer_added.assert_called_once_with(mock_repo.to_transfer_repo(), mock_call_config) mock_importer.save.assert_called_once_with() m_model.Importer.SERIALIZER.assert_called_once_with(mock_importer) self.assertTrue(result is m_model.Importer.SERIALIZER.return_value.data)
def test_as_expected(self, m_validate_conf, m_model, m_plug_api, m_clean, mock_remove, mock_plug_call_config, *_): """ Ensure that if an importer does not already exist, it is created. """ mock_repo = m_model.Repository.objects.get_repo_or_missing_resource.return_value mock_importer = m_model.Importer.return_value mock_imp_inst = mock.MagicMock() mock_call_config = mock_plug_call_config.return_value mock_plugin_config = mock.MagicMock() m_plug_api.get_importer_by_id.return_value = (mock_imp_inst, mock_plugin_config) result = importer.set_importer('mrepo', 'mtype', 'm_conf') mock_remove.assert_called_once_with('mrepo') mock_imp_inst.importer_added.assert_called_once_with(mock_repo.to_transfer_repo(), mock_call_config) m_clean.assert_called_once_with('m_conf') mock_plug_call_config.assert_called_once_with(mock_plugin_config, m_clean.return_value) mock_importer.save.assert_called_once_with() m_model.Importer.SERIALIZER.assert_called_once_with(mock_importer) self.assertTrue(result is m_model.Importer.SERIALIZER.return_value.data)
def test_associate_from_repo_dest_unsupported_types(self, mock_repo_qs, mock_crit): importer_controller.set_importer('source_repo', 'mock-importer', {}) self.assertRaises(exceptions.MissingResource, self.manager.associate_from_repo, 'source_repo', 'dest_repo', mock_crit)
# Note: the repo must be saved before the importer and distributor controllers can be called # because the first thing that they do is validate that the repo exists. repo = model.Repository(repo_id=repo_id, display_name=display_name, description=description, notes=notes) try: repo.save() except NotUniqueError: raise pulp_exceptions.DuplicateResource(repo_id) except ValidationError, e: raise pulp_exceptions.InvalidValue(e.to_dict().keys()) # Add the importer. Delete the repository if this fails. if importer_type_id is not None: try: importer_controller.set_importer(repo_id, importer_type_id, importer_repo_plugin_config) except Exception: _logger.exception( 'Exception adding importer to repo [%s]; the repo will be deleted' % repo_id) repo.delete() raise # Add the distributors. Delete the repository if this fails. for distributor in distributor_list or []: type_id = distributor.get('distributor_type_id') plugin_config = distributor.get('distributor_config') auto_publish = distributor.get('auto_publish', False) dist_id = distributor.get('distributor_id') try: dist_controller.add_distributor(repo_id, type_id, plugin_config, auto_publish, dist_id)
def test_is_valid_upload(self, mock_repo_qs): importer_controller.set_importer('repo-u', 'mock-importer', {}) valid = self.upload_manager.is_valid_upload('repo-u', 'mock-type') self.assertTrue(valid)
def test_is_valid_upload_unsupported_type(self, mock_repo_qs): importer_controller.set_importer('repo-u', 'mock-importer', {}) # Test self.assertRaises(PulpDataException, self.upload_manager.is_valid_upload, 'repo-u', 'fake-type')
def test_associate_from_repo_dest_unsupported_types( self, mock_repo_qs, mock_crit): importer_controller.set_importer('source_repo', 'mock-importer', {}) self.assertRaises(exceptions.MissingResource, self.manager.associate_from_repo, 'source_repo', 'dest_repo', mock_crit)
def test_import_uploaded_unit_importer_error_reraise_pulp_exception(self, mock_repo_qs): importer_controller.set_importer('repo-u', 'mock-importer', {}) mock_plugins.MOCK_IMPORTER.upload_unit.side_effect = InvalidValue(['filename']) upload_id = self.upload_manager.initialize_upload() self.assertRaises(InvalidValue, self.upload_manager.import_uploaded_unit, 'repo-u', 'mock-type', {}, {}, upload_id)