def test_content_install(self): # Setup self.populate() # Test units = [ {'type_id':'rpm', 'unit_key':{'name':'zsh', 'version':'1.0'}}, {'type_id':'rpm', 'unit_key':{'name':'bar', 'version':'1.0'}}, {'type_id':'rpm', 'unit_key':{'name':'abc', 'version':'1.0'}}, {'type_id':'mock-type', 'unit_key':{'name':'monster', 'version':'5.0'}}, {'type_id':'unsupported', 'unit_key':{'name':'xxx', 'version':'1.0'}}, ] options = dict(importkeys=True) manager = factory.consumer_agent_manager() manager.install_content(self.CONSUMER_ID, units, options) # Verify profiler = plugin_api.get_profiler_by_type('rpm')[0] pargs = profiler.install_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[:3]) self.assertEquals(pargs[2], options) profiler = plugin_api.get_profiler_by_type('mock-type')[0] pargs = profiler.install_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[3:4]) self.assertEquals(pargs[2], options)
def test_content_install(self): # Setup self.populate() # Test units = [ {"type_id": "rpm", "unit_key": {"name": "zsh", "version": "1.0"}}, {"type_id": "rpm", "unit_key": {"name": "bar", "version": "1.0"}}, {"type_id": "rpm", "unit_key": {"name": "abc", "version": "1.0"}}, {"type_id": "mock-type", "unit_key": {"name": "monster", "version": "5.0"}}, {"type_id": "unsupported", "unit_key": {"name": "xxx", "version": "1.0"}}, ] options = dict(importkeys=True) manager = factory.consumer_agent_manager() manager.install_content(self.CONSUMER_ID, units, options) # Verify # agent call params = mock_agent.Content.install.call_args[0] self.assertEqual(sorted(params[0]), sorted(units)) self.assertEqual(params[1], options) # profiler call profiler = plugin_api.get_profiler_by_type("rpm")[0] pargs = profiler.install_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[:3]) self.assertEquals(pargs[2], options) profiler = plugin_api.get_profiler_by_type("mock-type")[0] pargs = profiler.install_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[3:4]) self.assertEquals(pargs[2], options)
def test_profiler_no_exception(self): # Setup self.populate() profiler, cfg = plugins.get_profiler_by_type('rpm') profiler.find_applicable_units = Mock(side_effect=KeyError) # Test user_specified_unit_criteria = { 'rpm': { "filters": { "name": { "$in": ['zsh', 'ksh'] } } }, 'mock-type': { "filters": { "name": { "$in": ['abc', 'def'] } } } } unit_criteria = {} for type_id, criteria in user_specified_unit_criteria.items(): unit_criteria[type_id] = Criteria.from_client_input(criteria) manager = factory.consumer_applicability_manager() result = manager.find_applicable_units(self.CONSUMER_CRITERIA, self.REPO_CRITERIA, unit_criteria) self.assertTrue(result == {})
def update(self, consumer_id, content_type, profile): """ Update a unit profile. Created if not already exists. @param consumer_id: uniquely identifies the consumer. @type consumer_id: str @param content_type: The profile (content) type ID. @type content_type: str @param profile: The unit profile @type profile: object """ try: profiler, config = plugin_api.get_profiler_by_type(content_type) except plugin_exceptions.PluginNotFound: # Not all profile types have a type specific profiler, so let's use the baseclass # Profiler profiler, config = (Profiler(), {}) consumer = factory.consumer_manager().get_consumer(consumer_id) # Allow the profiler a chance to update the profile before we save it profile = profiler.update_profile(consumer, content_type, profile, config) try: p = self.get_profile(consumer_id, content_type) p['profile'] = profile # We store the profile's hash anytime the profile gets altered p['profile_hash'] = UnitProfile.calculate_hash(profile) except MissingResource: p = UnitProfile(consumer_id, content_type, profile) collection = UnitProfile.get_collection() collection.save(p, safe=True) return p
def update(consumer_id, content_type, profile): """ Update a unit profile. Created if not already exists. :param consumer_id: uniquely identifies the consumer. :type consumer_id: str :param content_type: The profile (content) type ID. :type content_type: str :param profile: The unit profile :type profile: object """ try: profiler, config = plugin_api.get_profiler_by_type(content_type) except plugin_exceptions.PluginNotFound: # Not all profile types have a type specific profiler, so let's use the baseclass # Profiler profiler, config = (Profiler(), {}) consumer = factory.consumer_manager().get_consumer(consumer_id) # Allow the profiler a chance to update the profile before we save it profile = profiler.update_profile(consumer, content_type, profile, config) try: p = ProfileManager.get_profile(consumer_id, content_type) p['profile'] = profile # We store the profile's hash anytime the profile gets altered p['profile_hash'] = UnitProfile.calculate_hash(profile) except MissingResource: p = UnitProfile(consumer_id, content_type, profile) collection = UnitProfile.get_collection() collection.save(p, safe=True) return p
def setUp(self): base.PulpServerTests.setUp(self) Consumer.get_collection().remove() UnitProfile.get_collection().remove() plugins._create_manager() mock_plugins.install() profiler, cfg = plugins.get_profiler_by_type('rpm') profiler.find_applicable_units = \ Mock(side_effect=lambda i,r,t,u,c,x: [ApplicabilityReport('mysummary', 'mydetails')])
def setUp(self): base.PulpWebserviceTests.setUp(self) Consumer.get_collection().remove() UnitProfile.get_collection().remove() plugin_api._create_manager() mock_plugins.install() profiler = plugin_api.get_profiler_by_type('errata')[0] profiler.unit_applicable = \ mock.Mock(side_effect=lambda i,u,c,x: ApplicabilityReport(u, True, self.SUMMARY, self.DETAILS))
def setUp(self): base.PulpServerTests.setUp(self) Consumer.get_collection().remove() UnitProfile.get_collection().remove() plugins._create_manager() mock_plugins.install() profiler, cfg = plugins.get_profiler_by_type('rpm') profiler.units_applicable = \ Mock(side_effect=lambda i,r,t,u,c,x: [ApplicabilityReport('mysummary', 'mydetails')])
def test_profiler_no_exception(self): # Setup self.populate() profiler, cfg = plugins.get_profiler_by_type('rpm') profiler.units_applicable = Mock(side_effect=KeyError) # Test units = {'rpm': [{'name':'zsh'}, {'name':'ksh'}], 'mock-type': [{'name':'abc'}, {'name':'def'}] } manager = factory.consumer_applicability_manager() result = manager.units_applicable(self.CONSUMER_CRITERIA, self.REPO_CRITERIA, units) self.assertTrue('test-1' in result.keys()) self.assertTrue('test-2' in result.keys())
def test_profiler_no_exception(self): # Setup self.populate() profiler, cfg = plugins.get_profiler_by_type('rpm') profiler.find_applicable_units = Mock(side_effect=KeyError) # Test user_specified_unit_criteria = {'rpm': {"filters": {"name": {"$in":['zsh','ksh']}}}, 'mock-type': {"filters": {"name": {"$in":['abc','def']}}} } unit_criteria = {} for type_id, criteria in user_specified_unit_criteria.items(): unit_criteria[type_id] = Criteria.from_client_input(criteria) manager = factory.consumer_applicability_manager() result = manager.find_applicable_units(self.CONSUMER_CRITERIA, self.REPO_CRITERIA, unit_criteria) self.assertTrue(result == {})
def _profiler(typeid): """ Find the profiler. Returns the Profiler base class when not matched. :param typeid: The content type ID. :type typeid: str :return: (profiler, cfg) :rtype: tuple """ try: plugin, cfg = plugin_api.get_profiler_by_type(typeid) except plugin_exceptions.PluginNotFound: plugin = Profiler() cfg = {} return plugin, cfg
def __profiler(self, typeid): """ Find the profiler. Returns the Profiler base class when not matched. @param typeid: The content type ID. @type typeid: str @return: (profiler, cfg) @rtype: tuple """ try: plugin, cfg = plugin_api.get_profiler_by_type(typeid) except plugin_exceptions.PluginNotFound: plugin = Profiler() cfg = {} return plugin, cfg
def test_content_uninstall(self): # Setup self.populate() # Test manager = factory.consumer_agent_manager() unit = dict(type_id='rpm', unit_key=dict(name='zsh')) units = [unit,] options = {} manager.uninstall_content(self.CONSUMER_ID, units, options) # verify profiler = plugin_api.get_profiler_by_type('rpm')[0] pargs = profiler.uninstall_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[:3]) self.assertEquals(pargs[2], options)
def test_profiler_exception(self): # Setup self.populate() profiler, cfg = plugins.get_profiler_by_type('rpm') profiler.unit_applicable = Mock(side_effect=KeyError) # Test units = [ {'type_id':'rpm', 'unit_key':{'name':'zsh'}}, {'type_id':'rpm', 'unit_key':{'name':'ksh'}}, {'type_id':'mock-type', 'unit_key':{'name':'abc'}}, {'type_id':'mock-type', 'unit_key':{'name':'def'}} ] manager = factory.consumer_applicability_manager() self.assertRaises( PulpExecutionException, manager.units_applicable, self.CRITERIA, units)
def test_profilers(self): # listing profilers = api.list_profilers() self.assertEqual(len(profilers), 1) self.assertEqual(profilers, {PROFILER_ID: METADATA}) # list types self.assertEqual(api.list_profiler_types(PROFILER_ID), METADATA) # by id profiler = api.get_profiler_by_id(PROFILER_ID) self.assertFalse(profiler is None) self.assertTrue(isinstance(profiler[0], MockProfiler)) self.assertRaises(PluginNotFound, api.get_profiler_by_id, 'not-valid') # by type profiler = api.get_profiler_by_type(TYPES[0]) self.assertFalse(profiler is None) self.assertTrue(isinstance(profiler[0], MockProfiler)) self.assertRaises(PluginNotFound, api.get_profiler_by_type, 'not-valid') # is_valid self.assertTrue(api.is_valid_profiler(PROFILER_ID)) self.assertFalse(api.is_valid_profiler('not-valid'))
def test_content_update(self): # Setup self.populate() # Test unit = dict(type_id='rpm', unit_key=dict(name='zsh')) units = [unit,] options = {} manager = factory.consumer_agent_manager() manager.update_content(self.CONSUMER_ID, units, options) # Verify # # agent call params = mock_agent.Content.update.call_args[0] self.assertEqual(params[0], units) self.assertEqual(params[1], options) # profiler call profiler = plugin_api.get_profiler_by_type('rpm')[0] pargs = profiler.update_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[:3]) self.assertEquals(pargs[2], options)
def test_content_uninstall(self): # Setup self.populate() # Test manager = factory.consumer_agent_manager() unit = dict(type_id="rpm", unit_key=dict(name="zsh")) units = [unit] options = {} manager.uninstall_content(self.CONSUMER_ID, units, options) # Verify # agent call params = mock_agent.Content.uninstall.call_args[0] self.assertEqual(params[0], units) self.assertEqual(params[1], options) # profiler call profiler = plugin_api.get_profiler_by_type("rpm")[0] pargs = profiler.uninstall_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[:3]) self.assertEquals(pargs[2], options)
def test_content_uninstall(self): # Setup self.populate() # Test manager = factory.consumer_agent_manager() unit = dict(type_id='rpm', unit_key=dict(name='zsh')) units = [ unit, ] options = {} manager.uninstall_content(self.CONSUMER_ID, units, options) # Verify # agent call params = mock_agent.Content.uninstall.call_args[0] self.assertEqual(params[0], units) self.assertEqual(params[1], options) # profiler call profiler = plugin_api.get_profiler_by_type('rpm')[0] pargs = profiler.uninstall_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[:3]) self.assertEquals(pargs[2], options)
def update(consumer_id, content_type, profile): """ Update a unit profile. Created if not already exists. :param consumer_id: uniquely identifies the consumer. :type consumer_id: str :param content_type: The profile (content) type ID. :type content_type: str :param profile: The unit profile :type profile: object """ consumer = factory.consumer_manager().get_consumer(consumer_id) try: profiler, config = plugin_api.get_profiler_by_type(content_type) except plugin_exceptions.PluginNotFound: # Not all profile types have a type specific profiler, so let's use the baseclass # Profiler profiler, config = (Profiler(), {}) # Allow the profiler a chance to update the profile before we save it if profile is None: raise MissingValue('profile') profile = profiler.update_profile(consumer, content_type, profile, config) try: p = ProfileManager.get_profile(consumer_id, content_type) p['profile'] = profile # We store the profile's hash anytime the profile gets altered p['profile_hash'] = UnitProfile.calculate_hash(profile) except MissingResource: p = UnitProfile(consumer_id, content_type, profile) collection = UnitProfile.get_collection() collection.save(p) history_manager = factory.consumer_history_manager() history_manager.record_event( consumer_id, 'unit_profile_changed', {'profile_content_type': content_type}) return p
def test_applicability(self): # Setup self.populate() # Test units = [ {'type_id':'rpm', 'unit_key':{'name':'zsh'}}, {'type_id':'rpm', 'unit_key':{'name':'ksh'}}, {'type_id':'mock-type', 'unit_key':{'name':'abc'}}, {'type_id':'mock-type', 'unit_key':{'name':'def'}} ] manager = factory.consumer_applicability_manager() applicability = manager.units_applicable(self.CRITERIA, units) # verify self.assertEquals(len(applicability), 2) for id in self.CONSUMER_IDS: for report in applicability[id]: if report.unit in units[1:2]: self.assertTrue(report.applicable) self.assertEquals(report.summary, 'mysummary') self.assertEquals(report.details, 'mydetails') continue if report.unit in units[2:3]: self.assertFalse(report.applicable) self.assertEquals(report.summary, 'mocked') self.assertEquals(report.details, None) continue profiler, cfg = plugins.get_profiler_by_type('rpm') call = 0 args = [c[0] for c in profiler.unit_applicable.call_args_list] for id in self.CONSUMER_IDS: for unit in units[0:2]: self.assertEquals(args[call][0].id, id) self.assertEquals(args[call][0].profiles, {'rpm':self.PROFILE}) self.assertEquals(args[call][1], unit) self.assertEquals(args[call][2], cfg) self.assertEquals(args[call][3].__class__, ProfilerConduit) call += 1
def test_content_install(self): # Setup self.populate() # Test units = [ { 'type_id': 'rpm', 'unit_key': { 'name': 'zsh', 'version': '1.0' } }, { 'type_id': 'rpm', 'unit_key': { 'name': 'bar', 'version': '1.0' } }, { 'type_id': 'rpm', 'unit_key': { 'name': 'abc', 'version': '1.0' } }, { 'type_id': 'mock-type', 'unit_key': { 'name': 'monster', 'version': '5.0' } }, { 'type_id': 'unsupported', 'unit_key': { 'name': 'xxx', 'version': '1.0' } }, ] options = dict(importkeys=True) manager = factory.consumer_agent_manager() manager.install_content(self.CONSUMER_ID, units, options) # Verify # agent call params = mock_agent.Content.install.call_args[0] self.assertEqual(sorted(params[0]), sorted(units)) self.assertEqual(params[1], options) # profiler call profiler = plugin_api.get_profiler_by_type('rpm')[0] pargs = profiler.install_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[:3]) self.assertEquals(pargs[2], options) profiler = plugin_api.get_profiler_by_type('mock-type')[0] pargs = profiler.install_units.call_args[0] self.assertEquals(pargs[0].id, self.CONSUMER_ID) self.assertEquals(pargs[0].profiles, {}) self.assertEquals(pargs[1], units[3:4]) self.assertEquals(pargs[2], options)