-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RHELC-1333] Port lock_releasever_in_rhel_repositories to Action fram…
…ework (#1326) * Port lock_releasever_in_rhel_repositories to Action framework. * Added unit test, changed failure result to warning. * Remove unused imports. * Update convert2rhel/actions/post_conversion/lock_releasever.py Co-authored-by: Rodolfo Olivieri <[email protected]> * Simplify control flow per r0x0d suggestion. * Paging Dr. DeMorgan: fixed an incorrect if-test. * Update convert2rhel/actions/post_conversion/lock_releasever.py Co-authored-by: Adam Hošek <[email protected]> * Update convert2rhel/actions/post_conversion/lock_releasever.py Co-authored-by: Adam Hošek <[email protected]> * Move to conversion folder * Remove post_ponr_conversion and mocks * Apply suggestions from code review Co-authored-by: Adam Hošek <[email protected]> * Fix unit tests after applying suggestions --------- Co-authored-by: Rodolfo Olivieri <[email protected]> Co-authored-by: Preston Watson <[email protected]> Co-authored-by: Adam Hošek <[email protected]> Co-authored-by: Rodolfo Olivieri <[email protected]>
- Loading branch information
1 parent
e07bc6b
commit 6c8df0f
Showing
6 changed files
with
158 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright(C) 2024 Red Hat, Inc. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
__metaclass__ = type | ||
|
||
import logging | ||
|
||
from convert2rhel import actions, utils | ||
from convert2rhel.systeminfo import system_info | ||
from convert2rhel.toolopts import tool_opts | ||
|
||
|
||
loggerinst = logging.getLogger(__name__) | ||
|
||
|
||
class LockReleaseverInRHELRepositories(actions.Action): | ||
id = "LOCK_RELEASEVER_IN_RHEL_REPOSITORIES" | ||
dependencies = () | ||
|
||
def run(self): | ||
"""Lock the releasever in the RHEL repositories located under /etc/yum.repos.d/redhat.repo | ||
After converting to a RHEL EUS minor version, we need to lock the releasever in the redhat.repo file to prevent | ||
future errors such as, running `yum update` and not being able to find the repositories metadata. | ||
.. note:: | ||
This function should only run if the system corresponds to a RHEL EUS version to make sure the converted system | ||
keeps receiving updates for the specific EUS minor version instead of the latest minor version which is the | ||
default. | ||
""" | ||
super(LockReleaseverInRHELRepositories, self).run() | ||
loggerinst.task("Convert: Lock releasever in RHEL repositories") | ||
# We only lock the releasever on rhel repos if we detect that the running system is an EUS correspondent and if | ||
# rhsm is used, otherwise, there's no need to lock the releasever as the subscription-manager won't be | ||
# available. | ||
if not system_info.eus_system or tool_opts.no_rhsm: | ||
loggerinst.info("Skipping locking RHEL repositories to a specific EUS minor version.") | ||
self.add_message( | ||
id="SKIPPED_LOCK_RELEASEVER_IN_RHEL_REPOSITORIES", | ||
level="INFO", | ||
title="Skipped releasever lock", | ||
description="Releasever lock is needed only when converting to RHEL EUS using RHSM.", | ||
) | ||
return | ||
loggerinst.info( | ||
"Updating /etc/yum.repos.d/rehat.repo to point to RHEL %s instead of the default latest minor version." | ||
% system_info.releasever | ||
) | ||
cmd = [ | ||
"subscription-manager", | ||
"release", | ||
"--set=%s" % system_info.releasever, | ||
] | ||
_, ret_code = utils.run_subprocess(cmd, print_output=False) | ||
if ret_code != 0: | ||
loggerinst.warning("Locking RHEL repositories failed.") | ||
return | ||
loggerinst.info("RHEL repositories locked to the %s minor version." % system_info.releasever) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
convert2rhel/unit_tests/actions/conversion/lock_releasever_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright(C) 2024 Red Hat, Inc. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
__metaclass__ = type | ||
|
||
|
||
import pytest | ||
import six | ||
|
||
from convert2rhel import actions, utils | ||
from convert2rhel.actions.conversion import lock_releasever | ||
from convert2rhel.systeminfo import Version, system_info | ||
from convert2rhel.unit_tests import RunSubprocessMocked | ||
from convert2rhel.unit_tests.conftest import centos8 | ||
|
||
|
||
six.add_move(six.MovedModule("mock", "mock", "unittest.mock")) | ||
|
||
from convert2rhel import unit_tests | ||
|
||
|
||
@pytest.fixture | ||
def lock_releasever_in_rhel_repositories_instance(): | ||
return lock_releasever.LockReleaseverInRHELRepositories() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("subprocess", "expected"), | ||
( | ||
(("output", 0), "RHEL repositories locked"), | ||
(("output", 1), "Locking RHEL repositories failed"), | ||
), | ||
) | ||
@centos8 | ||
def test_lock_releasever_in_rhel_repositories( | ||
lock_releasever_in_rhel_repositories_instance, subprocess, expected, monkeypatch, caplog, pretend_os | ||
): | ||
cmd = ["subscription-manager", "release", "--set=%s" % system_info.releasever] | ||
run_subprocess_mock = RunSubprocessMocked( | ||
side_effect=unit_tests.run_subprocess_side_effect( | ||
(cmd, subprocess), | ||
) | ||
) | ||
version = Version(8, 6) | ||
monkeypatch.setattr(system_info, "version", version) | ||
monkeypatch.setattr( | ||
utils, | ||
"run_subprocess", | ||
value=run_subprocess_mock, | ||
) | ||
monkeypatch.setattr(system_info, "eus_system", value=True) | ||
lock_releasever_in_rhel_repositories_instance.run() | ||
|
||
assert expected in caplog.records[-1].message | ||
assert run_subprocess_mock.call_count == 1 | ||
|
||
|
||
def test_lock_releasever_in_rhel_repositories_not_eus(lock_releasever_in_rhel_repositories_instance, caplog): | ||
lock_releasever_in_rhel_repositories_instance.run() | ||
expected = set( | ||
( | ||
actions.ActionMessage( | ||
level="INFO", | ||
id="SKIPPED_LOCK_RELEASEVER_IN_RHEL_REPOSITORIES", | ||
title="Skipped releasever lock", | ||
description="Releasever lock is needed only when converting to RHEL EUS using RHSM.", | ||
diagnosis=None, | ||
remediations=None, | ||
), | ||
) | ||
) | ||
assert "Skipping locking RHEL repositories to a specific EUS minor version." in caplog.records[-1].message | ||
assert expected.issuperset(lock_releasever_in_rhel_repositories_instance.messages) | ||
assert expected.issubset(lock_releasever_in_rhel_repositories_instance.messages) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters