Skip to content

Commit

Permalink
870495 - Call sync conduit's save_unit() for pre-existing errata.
Browse files Browse the repository at this point in the history
  • Loading branch information
Randy Barlow committed Nov 7, 2012
1 parent af3012d commit 8cad540
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 16 deletions.
29 changes: 17 additions & 12 deletions pulp_rpm/plugins/importers/yum_importer/errata.py
Expand Up @@ -87,26 +87,28 @@ def get_orphaned_errata(available_errata, existing_errata):
orphaned_errata[key] = existing_errata[key]
return orphaned_errata


def get_new_errata_units(available_errata, sync_conduit):
"""
Determines which errata to add or remove and will initialize new units
@param available_errata a dict of available errata
@type available_errata {}
@param sync_conduit
@type sync_conduit pulp.server.content.conduits.repo_sync.RepoSyncConduit
@return a tuple of 2 dictionaries. First dict is of new errata, second dict is of new units
@rtype ({}, {}, pulp.server.content.conduits.repo_sync.RepoSyncConduit)
Determines which errata to add or remove and will initialize new units
:param available_errata: a dict of available errata
:type available_errata: dict
:param sync_conduit: The sync conduit to save new units to
:type sync_conduit: pulp.server.content.conduits.repo_sync.RepoSyncConduit
:return: A tuple of 2 dictionaries and the sync conduit. First dict is of new
errata, second dict is of new units, and I (rbarlow) am not sure why we
are returning the sync conduit, but apparently we are.
:rtype: tuple
"""
new_errata = {}
new_units = {}
for key in available_errata:
criteria = Criteria(filters={'id' : key})
try:
# errata id is unique and will return only 1 entry if matched
existing_erratum = sync_conduit.search_all_units(type_id=TYPE_ID_ERRATA, criteria=criteria)[0]
existing_erratum = sync_conduit.search_all_units(type_id=TYPE_ID_ERRATA,
criteria=criteria)[0]
except IndexError:
existing_erratum = None
if existing_erratum:
Expand All @@ -122,9 +124,11 @@ def get_new_errata_units(available_errata, sync_conduit):
if elist['name'] not in coll_names:
# merge the pkglist and save
existing_erratum.metadata['pkglist'].append(elist)
#sync_conduit.save_unit(existing_erratum)
new_units[key] = existing_erratum
new_errata[key] = available_errata[key]
# We need to save this erratum, so that the repo_content_units collection is sure to
# have an entry for the repo for this sync_conduit
sync_conduit.save_unit(existing_erratum)
continue
# If we're here, the existing erratum is outdated or doesnt exist. Let's create/update
# new available erratum.
Expand All @@ -135,6 +139,7 @@ def get_new_errata_units(available_errata, sync_conduit):
new_units[key] = sync_conduit.init_unit(TYPE_ID_ERRATA, unit_key, metadata, None)
return new_errata, new_units, sync_conduit


def form_errata_unit_key(erratum):
unit_key = {}
for key in UNIT_KEY_ERRATA:
Expand Down
7 changes: 5 additions & 2 deletions pulp_rpm/test/unit/importer_mocks.py
Expand Up @@ -38,11 +38,13 @@ def get_units(criteria=None):
ret_val.append(u)
return ret_val

def serach_all_units(type_id=None, criteria=None):
def serach_all_units(type_id, criteria):
ret_val = []
if existing_units:
for u in existing_units:
ret_val.append(u)
if u.type_id == type_id:
if u.unit_key['id'] == criteria['filters']['id']:
ret_val.append(u)
return ret_val

sync_conduit = mock.Mock(spec=RepoSyncConduit)
Expand All @@ -52,6 +54,7 @@ def serach_all_units(type_id=None, criteria=None):

return sync_conduit


def get_import_conduit(source_units=None, existing_units=None):
def get_source_units(criteria=None):
units = []
Expand Down
87 changes: 85 additions & 2 deletions pulp_rpm/test/unit/test_errata.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2011 Red Hat, Inc.
# Copyright © 2012 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
Expand All @@ -25,8 +25,9 @@
from yum_importer import errata
from yum_importer import importer_rpm
from yum_importer.importer import YumImporter
from pulp.plugins.model import Repository, Unit

from pulp.plugins.model import Repository, Unit
from pulp.server.db.model.repository import RepoContentUnit
from pulp_rpm.common.ids import TYPE_ID_RPM, TYPE_ID_IMPORTER_YUM, TYPE_ID_ERRATA
import rpm_support_base

Expand Down Expand Up @@ -76,6 +77,65 @@ def test_errata_sync(self):
self.assertEquals(details["num_security_errata"], 7)
self.assertEquals(details["num_enhancement_errata"], 9)

def test_errata_sync_with_repos_that_share_upstream_url(self):
# This test is for https://bugzilla.redhat.com/show_bug.cgi?id=870495
feed_url = "http://example.com/test_repo/"

# Set up repo_1 and sync it
importer_1 = YumImporter()
repo_1 = mock.Mock(spec=Repository)
repo_1.working_dir = self.working_dir
repo_1.id = "test_repo_1"
sync_conduit_1 = importer_mocks.get_sync_conduit()
config_1 = importer_mocks.get_basic_config(feed_url=feed_url)
self.simulate_sync(repo_1, self.repo_dir)
importer_errata_1 = errata.ImporterErrata()
status_1, summary_1, details_1 = importer_errata_1.sync(repo_1, sync_conduit_1, config_1)
self.assertTrue(status_1)
self.assertTrue(summary_1 is not None)
self.assertTrue(details_1 is not None)
self.assertEquals(summary_1["num_new_errata"], 52)
self.assertEquals(summary_1["num_existing_errata"], 0)
self.assertEquals(summary_1["num_orphaned_errata"], 0)
self.assertEquals(details_1["num_bugfix_errata"], 36)
self.assertEquals(details_1["num_security_errata"], 7)
self.assertEquals(details_1["num_enhancement_errata"], 9)
# We should have called save_unit() once for each errata, in sync().
self.assertEqual(len(sync_conduit_1.save_unit.mock_calls), 52)

# Now let's set up another repo with the same URL, and then sync. We should get the same
# errata.
importer_2 = YumImporter()
repo_2 = mock.Mock(spec=Repository)
working_dir_2 = os.path.join(self.temp_dir, "working_2")
os.makedirs(working_dir_2)
repo_2.working_dir = working_dir_2
repo_2.id = "test_repo_2"
unit_key = {'id': "RHBA-2007:0112"}
metadata = {'updated' : "2007-03-14 00:00:00",
'pkglist': [{'name': 'RHEL Virtualization (v. 5 for 32-bit x86)'}]}
existing_units = [Unit(TYPE_ID_ERRATA, unit_key, metadata, '')]
existing_units[0].updated = metadata['updated']
sync_conduit_2 = importer_mocks.get_sync_conduit(existing_units=existing_units)
config_2 = importer_mocks.get_basic_config(feed_url=feed_url)
self.simulate_sync(repo_2, self.repo_dir)
importer_errata_2 = errata.ImporterErrata()
status_2, summary_2, details_2 = importer_errata_2.sync(repo_2, sync_conduit_2, config_2)
self.assertTrue(status_2)
self.assertTrue(summary_2 is not None)
self.assertTrue(details_2 is not None)
self.assertEquals(summary_2["num_new_errata"], 51)
self.assertEquals(summary_2["num_existing_errata"], 1)
self.assertEquals(summary_2["num_orphaned_errata"], 0)
self.assertEquals(details_2["num_bugfix_errata"], 35)
self.assertEquals(details_2["num_security_errata"], 7)
self.assertEquals(details_2["num_enhancement_errata"], 9)

# There should be the same number of calls to save_unit() as there are errata,
# because sync() calls it once for each of the 51 new erratum, and get_new_errata_units()
# also calls it once for the one errata that already existed
self.assertEqual(len(sync_conduit_2.save_unit.mock_calls), 52)

def test_get_available_errata(self):
errata_items_found = errata.get_available_errata(self.repo_dir)
self.assertEqual(52, len(errata_items_found))
Expand Down Expand Up @@ -107,6 +167,29 @@ def test_new_errata_units(self):
sync_conduit)
self.assertEquals(len(available_errata), len(new_errata))

def test_get_new_errata_units_saves_existing_units(self):
# This test is for https://bugzilla.redhat.com/show_bug.cgi?id=870495
available_errata = errata.get_available_errata(self.repo_dir)
self.assertEqual(52, len(available_errata))
unit_key = {'id': "RHBA-2007:0112"}
metadata = {'updated' : "2007-03-14 00:00:00",
'pkglist': [{'name': 'RHEL Virtualization (v. 5 for 32-bit x86)'}]}
existing_units = [Unit(TYPE_ID_ERRATA, unit_key, metadata, '')]
existing_units[0].updated = metadata['updated']
sync_conduit = importer_mocks.get_sync_conduit(existing_units=existing_units)
created_existing_units = errata.get_existing_errata(sync_conduit)
self.assertEquals(len(created_existing_units), 1)
self.assertEquals(len(existing_units), len(created_existing_units))
new_errata, new_units, sync_conduit = errata.get_new_errata_units(available_errata,
sync_conduit)
# The one pre-existing errata makes the number of new errata one less
self.assertEquals(len(new_errata), len(available_errata) - 1)
# The one existing unit that we passed in as an existing unit should cause save_unit() to be
# called one time
self.assertEqual(len(sync_conduit.save_unit.mock_calls), 1)
# Assert that save_unit was called with the pre-existing errata
self.assertEqual(sync_conduit.save_unit.mock_calls[0][1][0], existing_units[0])

def test_update_errata_units(self):
# existing errata is older than available; should purge and resync
available_errata = errata.get_available_errata(self.repo_dir)
Expand Down

0 comments on commit 8cad540

Please sign in to comment.