Skip to content

Commit

Permalink
1118501 - updating logic to form consumer profile lookup table with t…
Browse files Browse the repository at this point in the history
…he newest rpm, so that in case of multiple packages with same name and arch, applicability logic does not fail
  • Loading branch information
Sayli Karmarkar committed Jul 25, 2014
1 parent 7053e68 commit 9bd12ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
11 changes: 8 additions & 3 deletions plugins/pulp_rpm/plugins/profilers/yum.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ def _form_lookup_key(rpm):
def _form_lookup_table(rpms):
"""
Build a dictionary mapping RPM names and arches (generated with the _form_lookup_key()
method) to the full unit key for each RPM.
method) to the full unit key for each RPM. In case of multiple rpms with same name and
arch, unit key of the newest rpm is stored as the value.
:param rpms: A list of RPM unit keys
:type rpms: list
Expand All @@ -215,9 +216,13 @@ def _form_lookup_table(rpms):
"""
lookup = {}
for rpm in rpms:
# Since only one name.arch is allowed to be installed on a machine,
# we will use "name arch" as a key in the lookup table
key = YumProfiler._form_lookup_key(rpm)
# In case of duplicate key, replace the value only if the rpm is newer
# than the old value.
if key in lookup:
existing_unit = lookup[key]
if not util.is_rpm_newer(rpm, existing_unit):
continue
lookup[key] = rpm
return lookup

Expand Down
33 changes: 33 additions & 0 deletions plugins/test/unit/plugins/profilers/test_yum.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ def get_test_profile_been_updated(self, arch="x86_64"):
bar = self.create_profile_entry("patb", 0, "0.1", "2", arch, "Test Vendor")
return {TYPE_ID_RPM:[foo, bar]}

def get_test_profile_with_duplicate_packages(self, arch="x86_64"):
foo = self.create_profile_entry("emoticons", 0, "0.0.1", "1", arch, "Test Vendor")
bar = self.create_profile_entry("patb", 0, "0.0.1", "1", arch, "Test Vendor")
newer_bar = self.create_profile_entry("patb", 0, "0.0.2", "1", arch, "Test Vendor")
return {TYPE_ID_RPM: [foo, newer_bar, bar]}

def test_metadata(self):
"""
Test the metadata() method.
Expand All @@ -352,6 +358,17 @@ def test_metadata(self):
self.assertTrue(TYPE_ID_RPM in data["types"])
self.assertTrue(TYPE_ID_ERRATA in data["types"])

def test_form_lookup_table(self):
"""
Test that form_lookup_table creates a table with the latest rpm in the profile as a value
corresponding to the rpm name and arch.
"""
test_profile = self.get_test_profile_with_duplicate_packages()
consumer_lookup = YumProfiler._form_lookup_table(test_profile[TYPE_ID_RPM])
self.assertEqual(len(consumer_lookup), 2)
self.assertEqual(consumer_lookup['patb x86_64'],
self.create_profile_entry("patb", 0, "0.0.2", "1", "x86_64", "Test Vendor"))

def test_rpm_applicable_to_consumer(self):
rpm = {}
prof = YumProfiler()
Expand All @@ -364,6 +381,22 @@ def test_rpm_applicable_to_consumer(self):
applicable = prof._is_rpm_applicable(rpm, self.test_consumer_lookup)
self.assertTrue(applicable)

def test_rpm_applicable_with_profile_containing_duplicate_packages(self):
"""
If a consumer profile contains multiple rpms with same name and arch (eg. in case of
kernel rpms), make sure that the applicability calculations take into consideration
the newest rpm installed.
"""
consumer_profile = self.get_test_profile_with_duplicate_packages()
test_consumer_lookup = YumProfiler._form_lookup_table(consumer_profile[TYPE_ID_RPM])
rpm = self.create_profile_entry("patb", 0, "0.0.2", "1", "x86_64", "Test Vendor")
yum_profiler = YumProfiler()
applicable = yum_profiler._is_rpm_applicable(rpm, test_consumer_lookup)
self.assertFalse(applicable)
newer_rpm = self.create_profile_entry("patb", 0, "0.0.3", "1", "x86_64", "Test Vendor")
applicable = yum_profiler._is_rpm_applicable(newer_rpm, test_consumer_lookup)
self.assertTrue(applicable)

def test_unit_applicable_true(self):
rpm_unit_key = self.create_profile_entry("emoticons", 0, "0.1", "2", "x86_64",
"Test Vendor")
Expand Down

0 comments on commit 9bd12ee

Please sign in to comment.