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 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 test_get_profiles(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.create(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) profiles = manager.get_profiles(self.CONSUMER_ID) # Verify profiles = sorted(profiles) self.assertEquals(len(profiles), 2) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) expected_hash = UnitProfile.calculate_hash(self.PROFILE_1) self.assertEqual(profiles[0]['profile_hash'], expected_hash) self.assertEquals(profiles[1]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[1]['content_type'], self.TYPE_2) self.assertEquals(profiles[1]['profile'], self.PROFILE_2) expected_hash = UnitProfile.calculate_hash(self.PROFILE_2) self.assertEqual(profiles[1]['profile_hash'], expected_hash)
def test_get_profiles(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.create(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) profiles = manager.get_profiles(self.CONSUMER_ID) # Verify profiles = sorted(profiles) self.assertEquals(len(profiles), 2) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) expected_hash = UnitProfile.calculate_hash(self.PROFILE_1) self.assertEqual(profiles[0]['profile_hash'], expected_hash) self.assertEquals(profiles[1]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[1]['content_type'], self.TYPE_2) self.assertEquals(profiles[1]['profile'], self.PROFILE_2) expected_hash = UnitProfile.calculate_hash(self.PROFILE_2) self.assertEqual(profiles[1]['profile_hash'], expected_hash)
def test_multiple_types(self): # Setup self.populate() collection = UnitProfile.get_collection() # Test manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Verify cursor = collection.find({'consumer_id': self.CONSUMER_ID}) cursor.sort('content_type', pymongo.ASCENDING) profiles = list(cursor) # Type_1 self.assertEquals(len(profiles), 2) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) expected_hash = UnitProfile.calculate_hash(self.PROFILE_1) self.assertEqual(profiles[0]['profile_hash'], expected_hash) self.assertEquals(profiles[1]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[1]['content_type'], self.TYPE_2) self.assertEquals(profiles[1]['profile'], self.PROFILE_2) expected_hash = UnitProfile.calculate_hash(self.PROFILE_2) self.assertEqual(profiles[1]['profile_hash'], expected_hash)
def test_fetch_by_type2(self): # Setup self.populate() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Test profile = manager.get_profile(self.CONSUMER_ID, self.TYPE_2) # Verify self.assertTrue(profile is not None) self.assertEquals(profile['consumer_id'], self.CONSUMER_ID) self.assertEquals(profile['content_type'], self.TYPE_2) self.assertEquals(profile['profile'], self.PROFILE_2) expected_hash = UnitProfile.calculate_hash(self.PROFILE_2) self.assertEqual(profile['profile_hash'], expected_hash)
def test_multiple_types(self): # Setup self.populate() collection = UnitProfile.get_collection() # Test manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) # Verify cursor = collection.find({'consumer_id': self.CONSUMER_ID}) cursor.sort('content_type', pymongo.ASCENDING) profiles = list(cursor) # Type_1 self.assertEquals(len(profiles), 2) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) expected_hash = UnitProfile.calculate_hash(self.PROFILE_1) self.assertEqual(profiles[0]['profile_hash'], expected_hash) self.assertEquals(profiles[1]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[1]['content_type'], self.TYPE_2) self.assertEquals(profiles[1]['profile'], self.PROFILE_2) expected_hash = UnitProfile.calculate_hash(self.PROFILE_2) self.assertEqual(profiles[1]['profile_hash'], expected_hash)
def test_create(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) # Verify collection = UnitProfile.get_collection() cursor = collection.find({'consumer_id': self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 1) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) expected_hash = UnitProfile.calculate_hash(self.PROFILE_1) self.assertEqual(profiles[0]['profile_hash'], expected_hash)
def test_create(self): # Setup self.populate() # Test manager = factory.consumer_profile_manager() manager.create(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) # Verify collection = UnitProfile.get_collection() cursor = collection.find({'consumer_id': self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 1) self.assertEquals(profiles[0]['consumer_id'], self.CONSUMER_ID) self.assertEquals(profiles[0]['content_type'], self.TYPE_1) self.assertEquals(profiles[0]['profile'], self.PROFILE_1) expected_hash = UnitProfile.calculate_hash(self.PROFILE_1) self.assertEqual(profiles[0]['profile_hash'], expected_hash)
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 """ manager = factory.consumer_manager() manager.get_consumer(consumer_id) 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(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 """ manager = factory.consumer_manager() manager.get_consumer(consumer_id) 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 test_delete(self): # Setup self.populate() collection = UnitProfile.get_collection() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) manager.update(self.CONSUMER_ID, self.TYPE_2, self.PROFILE_2) collection = UnitProfile.get_collection() cursor = collection.find({"consumer_id": self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 2) # Test manager.delete(self.CONSUMER_ID, self.TYPE_1) # Verify collection = UnitProfile.get_collection() cursor = collection.find({"consumer_id": self.CONSUMER_ID}) profiles = list(cursor) self.assertEquals(len(profiles), 1) self.assertEquals(profiles[0]["consumer_id"], self.CONSUMER_ID) self.assertEquals(profiles[0]["content_type"], self.TYPE_2) self.assertEquals(profiles[0]["profile"], self.PROFILE_2) expected_hash = UnitProfile.calculate_hash(self.PROFILE_2) self.assertEqual(profiles[0]["profile_hash"], expected_hash)
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_migration(self): """ Assert that the migration adds the appropriate hashes to the three consumers. """ consumer_unit_profiles = [ { "consumer_id": "consumer_1", "content_type": "rpm", "profile": [ { "name": "Package A", "epoch": 0, "version": "1.0.1", "release": "1.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package B", "epoch": 0, "version": "2.0.3", "release": "3.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package C", "epoch": 0, "version": "1.3.6", "release": "2.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, ], }, { "consumer_id": "consumer_2", "content_type": "rpm", "profile": [ { "name": "Package B", "epoch": 0, "version": "2.0.3", "release": "3.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package A", "epoch": 0, "version": "1.0.1", "release": "1.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package C", "epoch": 0, "version": "1.3.6", "release": "2.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, ], }, { "consumer_id": "consumer_3", "content_type": "rpm", "profile": [ { "name": "Package A", "epoch": 0, "version": "1.0.1", "release": "1.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package B", "epoch": 0, "version": "2.0.3", "release": "3.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package C", "epoch": 0, "version": "1.3.6", "release": "2.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package D", "epoch": 1, "version": "12.1.6", "release": "27.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, ], }, { "consumer_id": "consumer_3", "content_type": "some_other_type_that_should_be_left_alone", "profile": [ { "name": "Package A", "epoch": 0, "version": "1.0.1", "release": "1.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package B", "epoch": 0, "version": "2.0.3", "release": "3.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, { "name": "Package C", "epoch": 0, "version": "1.3.6", "release": "2.el6", "arch": "x86_64", "vendor": "Red Hat, Inc.", }, ], }, ] self.collection.insert(consumer_unit_profiles) migration_module = _import_all_the_way("pulp_rpm.migrations.0014_add_consumer_profile_hash") # Run the migration migration_module.migrate("arg_1", kwarg_1="kwarg_1") # Get the profiles consumer_1_profile = self.collection.find_one({"consumer_id": "consumer_1"}) consumer_2_profile = self.collection.find_one({"consumer_id": "consumer_2"}) consumer_3_rpm_profile = self.collection.find_one({"consumer_id": "consumer_3", "content_type": "rpm"}) consumer_3_other_profile = self.collection.find_one( {"consumer_id": "consumer_3", "content_type": "some_other_type_that_should_be_left_alone"} ) # Consumer 1 and Consumer 2 should have the same hashes, even though the RPMs were recorded # in a different order self.assertEqual(consumer_1_profile["profile_hash"], consumer_2_profile["profile_hash"]) # Consumer 3 should have a different hash, since it has an additional package self.assertNotEqual(consumer_1_profile["profile_hash"], consumer_3_rpm_profile["profile_hash"]) # Consumer 3's non-RPM profile should not have a hash self.assertTrue("profile_hash" not in consumer_3_other_profile) # Now, let's make sure the hashes are actually correct. We only have to check 1 and 3, since # we already asserted that 1 is equal to 2 profiler = yum.YumProfiler() for profile in [consumer_1_profile, consumer_3_rpm_profile]: sorted_profile = profiler.update_profile(None, profile["profile"], None, None) expected_hash = UnitProfile.calculate_hash(profile["profile"]) self.assertEqual(profile["profile_hash"], expected_hash)
def test_migration(self): """ Assert that the migration adds the appropriate hashes to the three consumers. """ consumer_unit_profiles = [ {'consumer_id': 'consumer_1', 'content_type': 'rpm', 'profile': [{'name': 'Package A', 'epoch': 0, 'version': '1.0.1', 'release': '1.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package B', 'epoch': 0, 'version': '2.0.3', 'release': '3.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package C', 'epoch': 0, 'version': '1.3.6', 'release': '2.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}]}, {'consumer_id': 'consumer_2', 'content_type': 'rpm', 'profile': [{'name': 'Package B', 'epoch': 0, 'version': '2.0.3', 'release': '3.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package A', 'epoch': 0, 'version': '1.0.1', 'release': '1.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package C', 'epoch': 0, 'version': '1.3.6', 'release': '2.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}]}, {'consumer_id': 'consumer_3', 'content_type': 'rpm', 'profile': [{'name': 'Package A', 'epoch': 0, 'version': '1.0.1', 'release': '1.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package B', 'epoch': 0, 'version': '2.0.3', 'release': '3.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package C', 'epoch': 0, 'version': '1.3.6', 'release': '2.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package D', 'epoch': 1, 'version': '12.1.6', 'release': '27.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}]}, {'consumer_id': 'consumer_3', 'content_type': 'some_other_type_that_should_be_left_alone', 'profile': [{'name': 'Package A', 'epoch': 0, 'version': '1.0.1', 'release': '1.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package B', 'epoch': 0, 'version': '2.0.3', 'release': '3.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}, {'name': 'Package C', 'epoch': 0, 'version': '1.3.6', 'release': '2.el6', 'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}]}, ] self.collection.insert(consumer_unit_profiles) migration_module = _import_all_the_way( 'pulp_rpm.plugins.migrations.0014_add_consumer_profile_hash') # Run the migration migration_module.migrate('arg_1', kwarg_1='kwarg_1') # Get the profiles consumer_1_profile = self.collection.find_one({'consumer_id': 'consumer_1'}) consumer_2_profile = self.collection.find_one({'consumer_id': 'consumer_2'}) consumer_3_rpm_profile = self.collection.find_one({'consumer_id': 'consumer_3', 'content_type': 'rpm'}) consumer_3_other_profile = self.collection.find_one( {'consumer_id': 'consumer_3', 'content_type': 'some_other_type_that_should_be_left_alone'}) # Consumer 1 and Consumer 2 should have the same hashes, even though the RPMs were recorded # in a different order self.assertEqual(consumer_1_profile['profile_hash'], consumer_2_profile['profile_hash']) # Consumer 3 should have a different hash, since it has an additional package self.assertNotEqual(consumer_1_profile['profile_hash'], consumer_3_rpm_profile['profile_hash']) # Consumer 3's non-RPM profile should not have a hash self.assertTrue('profile_hash' not in consumer_3_other_profile) # Now, let's make sure the hashes are actually correct. We only have to check 1 and 3, since # we already asserted that 1 is equal to 2 profiler = yum.YumProfiler() for profile in [consumer_1_profile, consumer_3_rpm_profile]: profiler.update_profile(None, profile['profile'], None, None) expected_hash = UnitProfile.calculate_hash(profile['profile']) self.assertEqual(profile['profile_hash'], expected_hash)