Skip to content

Commit

Permalink
Include most original NVR in bundle rebuilds
Browse files Browse the repository at this point in the history
We previously added a feature to automatically include previous rebuilds
in the NVR mapping during a bundle rebuild.

However, our implementation was missing the most original NVR, as it was
not an NVR rebuild itself.

This commit makes a slight change in the logic so that the most original
NVR is also included.

There is also a refactoring to make the code simpler, and also
adjustments to its tests to make them more easily readable.

JIRA: CWFHEALTH-2264
  • Loading branch information
FernandesMF committed Sep 25, 2023
1 parent 5ecae6c commit 63425dd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
22 changes: 8 additions & 14 deletions freshmaker/handlers/botas/botas_shipped_advisory.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,20 +648,14 @@ def _completion_with_previous_rebuilds(self, nvrs_mapping: dict[str, str]) -> di
"""
nvr_mappings_to_add = {}
for original_nvr, latest_nvr in nvrs_mapping.items():
# The nvrs_mapping is construced by checking database, so the latest_rebuild always
# exists and has only one record.
latest_rebuild = ArtifactBuild.query.filter_by(rebuilt_nvr=latest_nvr).first()

# Check whether the original NVR is also a freshmaker rebuild
former_build = ArtifactBuild.query.filter_by(
rebuilt_nvr=latest_rebuild.original_nvr
).first()
while former_build:
nvr_mappings_to_add[former_build.rebuilt_nvr] = latest_nvr
log.info(f"Found former rebuild '{former_build.rebuilt_nvr}' of '{latest_nvr}'")
former_build = ArtifactBuild.query.filter_by(
rebuilt_nvr=former_build.original_nvr
).first()
rebuilt_nvr = original_nvr
while rebuilt_nvr:
build = ArtifactBuild.query.filter_by(rebuilt_nvr=rebuilt_nvr).first()
if not build:
break
log.info(f"Found former rebuild '{build.rebuilt_nvr}' of '{latest_nvr}'")
nvr_mappings_to_add[build.original_nvr] = latest_nvr
rebuilt_nvr = build.original_nvr

return {**nvrs_mapping, **nvr_mappings_to_add}

Expand Down
69 changes: 44 additions & 25 deletions tests/handlers/botas/test_botas_shipped_advisory.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,8 @@ def test_handle_bundle_rebuild_manual_nvr_override(self, get_dependent_event_bui
"""Test handling of MANUALLY triggered bundle rebuild with manual nvr override.
Bundle 1 images are left alone, bundle 2 images are manually overriden, and there
is an override that is introduced manually in the request (#FIXME specify)
is an override that is introduced manually in the request (in the
'bundle_related_image_overrides' field of the request data).
"""
# operators mapping
nvr_to_digest = {
Expand Down Expand Up @@ -1283,64 +1284,82 @@ def test_create_original_to_rebuilt_nvrs_map(self, get_original_build):
"freshmaker.handlers.botas.botas_shipped_advisory.HandleBotasAdvisory.get_published_original_nvr"
)
def test_bundle_include_previous_builds(self, get_original_build):
get_original_build.side_effect = ["original_1", "original_2"]
get_original_build.side_effect = ["foo-1-100.1004", "bar-2-200.2007"]
self.handler.event = BotasErrataShippedEvent("test_msg_id", self.botas_advisory)
self.botas_advisory._builds = {
"product_name": {
"builds": [
{"some_name-2-12345": {"nvr": "some_name-2-12345"}},
{"some_name_two-2-2": {"nvr": "some_name_two-2-2"}},
{"foo-1-100.1005": {"nvr": "foo-1-100.1005"}},
{"bar-2-200.2008": {"nvr": "bar-2-200.2008"}},
]
}
}
self.get_blocking_advisories.return_value = {"some_name-1-1", "some_name-2-1"}
self.get_blocking_advisories.return_value = {"foo-3-300", "bar-2-300"}
db_event = Event.get_or_create_from_event(db.session, self.handler.event)
ArtifactBuild.create(
db.session,
db_event,
"ed0",
"ed1",
"image",
original_nvr="some_name-2-12344",
rebuilt_nvr="some_name-2-12345",
original_nvr="foo-1-100.1003",
rebuilt_nvr="foo-1-100.1004",
)
ArtifactBuild.create(
db.session,
db_event,
"ed1",
"ed2",
"image",
original_nvr="some_name-2-12343",
rebuilt_nvr="some_name-2-12344",
original_nvr="foo-1-100.1002",
rebuilt_nvr="foo-1-100.1003",
)
ArtifactBuild.create(
db.session,
db_event,
"ed2",
"ed3",
"image",
original_nvr="some_name-2-12342",
rebuilt_nvr="some_name-2-12343",
original_nvr="foo-1-100",
rebuilt_nvr="foo-1-100.1002",
)
ArtifactBuild.create(db.session, db_event, "ed2", "image", rebuilt_nvr="some_name-2-12342")
ArtifactBuild.create(
db.session,
db_event,
"ed3",
"ed4",
"image",
original_nvr="some_name_two-2-1",
rebuilt_nvr="some_name_two-2-2",
original_nvr="bar-2-200.2006",
rebuilt_nvr="bar-2-200.2007",
)
ArtifactBuild.create(
db.session,
db_event,
"ed5",
"image",
original_nvr="bar-2-200.2005",
rebuilt_nvr="bar-2-200.2006",
),
ArtifactBuild.create(
db.session,
db_event,
"ed6",
"image",
original_nvr="bar-2-200",
rebuilt_nvr="bar-2-200.2005",
),
expected_map = {
"original_1": "some_name-2-12345",
"original_2": "some_name_two-2-2",
"some_name-2-1": "some_name-2-12345",
"some_name-2-12344": "some_name-2-12345",
"some_name-2-12343": "some_name-2-12345",
"some_name-2-12342": "some_name-2-12345",
"bar-2-200": "bar-2-200.2008",
"bar-2-200.2005": "bar-2-200.2008",
"bar-2-200.2006": "bar-2-200.2008",
"bar-2-200.2007": "bar-2-200.2008",
"bar-2-300": "bar-2-200.2008",
"foo-1-100": "foo-1-100.1005",
"foo-1-100.1002": "foo-1-100.1005",
"foo-1-100.1003": "foo-1-100.1005",
"foo-1-100.1004": "foo-1-100.1005",
}

mapping = self.handler._create_original_to_rebuilt_nvrs_map()

self.assertEqual(get_original_build.call_count, 2)
self.assertEqual(mapping, expected_map)
self.assertDictEqual(mapping, expected_map)

@patch("freshmaker.image.ContainerImage.get_additional_data_from_koji")
def test_prepare_builds(self, get_koji_data):
Expand Down

0 comments on commit 63425dd

Please sign in to comment.