diff --git a/repos/system_upgrade/common/actors/removeobsoletegpgkeys/libraries/removeobsoleterpmgpgkeys.py b/repos/system_upgrade/common/actors/removeobsoletegpgkeys/libraries/removeobsoleterpmgpgkeys.py index bda7efa383..198c436850 100644 --- a/repos/system_upgrade/common/actors/removeobsoletegpgkeys/libraries/removeobsoleterpmgpgkeys.py +++ b/repos/system_upgrade/common/actors/removeobsoletegpgkeys/libraries/removeobsoleterpmgpgkeys.py @@ -12,14 +12,14 @@ def _get_obsolete_keys(): distribution = api.current_actor().configuration.os_release.release_id obsoleted_keys_map = get_distribution_data(distribution).get('obsoleted-keys', {}) keys = [] - try: - for version in range(7, int(get_target_major_version()) + 1): + for version in range(7, int(get_target_major_version()) + 1): + try: for key in obsoleted_keys_map[str(version)]: name, version, release = key.rsplit("-", 2) if has_package(InstalledRPM, name, version=version, release=release): keys.append(key) - except KeyError: - pass + except KeyError: + pass return keys diff --git a/repos/system_upgrade/common/actors/removeobsoletegpgkeys/tests/test_removeobsoleterpmgpgkeys.py b/repos/system_upgrade/common/actors/removeobsoletegpgkeys/tests/test_removeobsoleterpmgpgkeys.py index 4d9a0e844b..113d283420 100644 --- a/repos/system_upgrade/common/actors/removeobsoletegpgkeys/tests/test_removeobsoleterpmgpgkeys.py +++ b/repos/system_upgrade/common/actors/removeobsoletegpgkeys/tests/test_removeobsoleterpmgpgkeys.py @@ -76,6 +76,81 @@ def get_target_major_version_mocked(): assert set(keys) == set(expected) +@pytest.mark.parametrize( + "version, data, expected", + [ + (10, {"8": ["gpg-pubkey-888-abc"], "10": ["gpg-pubkey-10-10"]}, ["gpg-pubkey-888-abc", "gpg-pubkey-10-10"]), + (9, {"8": ["gpg-pubkey-888-abc"], "9": ["gpg-pubkey-999-def"]}, ["gpg-pubkey-999-def", "gpg-pubkey-888-abc"]), + (8, {"8": ["gpg-pubkey-888-abc"], "9": ["gpg-pubkey-999-def"]}, ["gpg-pubkey-888-abc"]) + ] +) +def test_get_obsolete_keys_incomplete_data(monkeypatch, version, data, expected): + def get_target_major_version_mocked(): + return version + + def get_distribution_data_mocked(_distro): + return {'obsoleted-keys': data} + + def has_package_mocked(*args, **kwargs): + return True + + monkeypatch.setattr( + removeobsoleterpmgpgkeys, + "get_target_major_version", + get_target_major_version_mocked, + ) + + monkeypatch.setattr( + removeobsoleterpmgpgkeys, + "get_distribution_data", + get_distribution_data_mocked, + ) + + monkeypatch.setattr( + removeobsoleterpmgpgkeys, + "has_package", + has_package_mocked, + ) + + monkeypatch.setattr( + api, + "current_actor", + CurrentActorMocked(), + ) + + keys = removeobsoleterpmgpgkeys._get_obsolete_keys() + assert set(keys) == set(expected) + + +def test_get_obsolete_keys_empty_data(monkeypatch): + def get_target_major_version_mocked(): + return 9 + + def get_distribution_data_mocked(_distro): + return {} + + monkeypatch.setattr( + removeobsoleterpmgpgkeys, + "get_target_major_version", + get_target_major_version_mocked, + ) + + monkeypatch.setattr( + removeobsoleterpmgpgkeys, + "get_distribution_data", + get_distribution_data_mocked, + ) + + monkeypatch.setattr( + api, + "current_actor", + CurrentActorMocked(), + ) + + keys = removeobsoleterpmgpgkeys._get_obsolete_keys() + assert set(keys) == set() + + @pytest.mark.parametrize( "keys, should_register", [