Skip to content

Commit

Permalink
Merge branch 'main' into fix-enabled-repos-check
Browse files Browse the repository at this point in the history
  • Loading branch information
kokesak authored Apr 29, 2024
2 parents b270438 + 8141cef commit 9c3c798
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/alma9/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pylint==3.1.0
astroid==3.1.0
pre-commit==3.7.0
pytest==8.1.1
pytest==8.2.0
pytest-cov==5.0.0
coverage[toml]==7.5.0
2 changes: 1 addition & 1 deletion .github/workflows/build_images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: Checkout Code
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/label-commenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
with:
sparse-checkout: |
.github/label-commenter-config.yml
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: Checkout Code
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ PYLINT_ARGS ?=
VENV ?= .venv3
PRE_COMMIT ?= pre-commit
SHOW_CAPTURE ?= no
PYTEST_ARGS ?= -p no:cacheprovider
PYTEST_ARGS ?= --override-ini=addopts= -p no:cacheprovider
BUILD_IMAGES ?= 1

ifdef KEEP_TEST_CONTAINER
Expand Down
32 changes: 17 additions & 15 deletions convert2rhel/actions/pre_ponr_changes/backup_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os
import re

from convert2rhel import actions, backup, exceptions, repo
from convert2rhel import actions, backup, exceptions, subscription
from convert2rhel.backup.files import MissingFile, RestorableFile
from convert2rhel.logger import LOG_DIR
from convert2rhel.redhatrelease import os_release_file, system_release_file
Expand Down Expand Up @@ -75,28 +75,30 @@ class BackupRepository(actions.Action):
id = "BACKUP_REPOSITORY"

def run(self):
"""Backup repository files before starting conversion process"""
"""Backup .repo files in /etc/yum.repos.d/ so the repositories can be restored on rollback."""
loggerinst.task("Prepare: Backup Repository Files")

super(BackupRepository, self).run()

self.backup_yum_repos()

def backup_yum_repos(self):
"""Backup .repo files in /etc/yum.repos.d/ so the repositories can be restored on rollback."""
loggerinst.info("Backing up .repo files from %s." % DEFAULT_YUM_REPOFILE_DIR)

repo_files_backed_up = False
if not os.listdir(DEFAULT_YUM_REPOFILE_DIR):
loggerinst.info("Repository folder %s seems to be empty.", DEFAULT_YUM_REPOFILE_DIR)

for repo in os.listdir(DEFAULT_YUM_REPOFILE_DIR):
if repo.endswith(".repo") and repo != "redhat.repo":
repo_path = os.path.join(DEFAULT_YUM_REPOFILE_DIR, repo)
restorable_file = RestorableFile(repo_path)
backup.backup_control.push(restorable_file)
repo_files_backed_up = True

if not repo_files_backed_up:
loggerinst.info("No .repo files backed up.")
# backing up redhat.repo so repo files are properly backed up when doing satellite conversions

if not repo.endswith(".repo"):
loggerinst.info("Skipping backup as %s is not a repository file." % repo)
continue

if not subscription.should_subscribe() and repo == "redhat.repo":
loggerinst.info("Skipping backup of redhat.repo as it is not needed.")
continue

repo_path = os.path.join(DEFAULT_YUM_REPOFILE_DIR, repo)
restorable_file = RestorableFile(repo_path)
backup.backup_control.push(restorable_file)


class BackupYumVariables(actions.Action):
Expand Down
45 changes: 14 additions & 31 deletions convert2rhel/backup/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
class RestorablePackage(RestorableChange):
def __init__(self, pkgs, reposdir=None, set_releasever=False, custom_releasever=None, varsdir=None):
"""
Keep control of systme packages before their removal to backup and
Keep control of system packages before their removal to backup and
restore in case of rollback.
:param pkgs list[str]: List of packages to backup.
Expand Down Expand Up @@ -135,37 +135,20 @@ def enable(self):
if system_info.eus_system and system_info.id == "centos":
self.reposdir = get_hardcoded_repofiles_dir()

if not system_info.has_internet_access:
if self.reposdir:
loggerinst.debug(
"Not using repository files stored in %s due to the absence of internet access." % self.reposdir
)

for pkg in self.pkgs:
self._backedup_pkgs_paths.append(
utils.download_pkg(
pkg=pkg,
dest=BACKUP_DIR,
set_releasever=self.set_releasever,
custom_releasever=self.custom_releasever,
varsdir=self.varsdir,
)
)
else:
if self.reposdir:
loggerinst.debug("Using repository files stored in %s." % self.reposdir)

for pkg in self.pkgs:
self._backedup_pkgs_paths.append(
utils.download_pkg(
pkg=pkg,
dest=BACKUP_DIR,
set_releasever=self.set_releasever,
custom_releasever=self.custom_releasever,
varsdir=self.varsdir,
reposdir=self.reposdir,
)
if self.reposdir:
loggerinst.debug("Using repository files stored in %s." % self.reposdir)

for pkg in self.pkgs:
self._backedup_pkgs_paths.append(
utils.download_pkg(
pkg=pkg,
dest=BACKUP_DIR,
set_releasever=self.set_releasever,
custom_releasever=self.custom_releasever,
varsdir=self.varsdir,
reposdir=self.reposdir,
)
)
else:
loggerinst.warning("Can't access %s" % BACKUP_DIR)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest
import six

from convert2rhel import unit_tests
from convert2rhel import subscription, unit_tests
from convert2rhel.actions.pre_ponr_changes import backup_system
from convert2rhel.backup import files
from convert2rhel.backup.files import RestorableFile
Expand Down Expand Up @@ -390,11 +390,20 @@ def test_backup_repository_redhat(self, monkeypatch, tmpdir, backup_repository_a
redhat_repo = generate_repo(tmpdir, "redhat.repo")

monkeypatch.setattr(backup_system, "DEFAULT_YUM_REPOFILE_DIR", os.path.dirname(redhat_repo))

monkeypatch.setattr(subscription, "should_subscribe", mock.Mock(side_effect=lambda: False))
backup_repository = backup_repository_action
backup_repository.run()

assert "No .repo files backed up." == caplog.records[-1].message
assert "Skipping backup of redhat.repo as it is not needed." == caplog.records[-1].message

def test_backup_repository_other_files(self, monkeypatch, tmpdir, backup_repository_action, caplog):
"""Test if redhat.repo is not backed up."""
non_repo_file = generate_repo(tmpdir, "redhat.nonrepo")

monkeypatch.setattr(backup_system, "DEFAULT_YUM_REPOFILE_DIR", os.path.dirname(non_repo_file))
backup_repository = backup_repository_action
backup_repository.run()
assert "Skipping backup as redhat.nonrepo is not a repository file." == caplog.records[-1].message

def test_backup_repository_no_repofile_presence(self, tmpdir, monkeypatch, caplog, backup_repository_action):
"""Test empty path, nothing for backup."""
Expand All @@ -405,8 +414,7 @@ def test_backup_repository_no_repofile_presence(self, tmpdir, monkeypatch, caplo
backup_repository = backup_repository_action

backup_repository.run()

assert "No .repo files backed up." in caplog.text
assert ("Repository folder %s seems to be empty." % etc) in caplog.text


class TestBackupVariables:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ def test_duplicate_packages_error(monkeypatch, output, expected, duplicate_packa
)


def test_duplicate_packages_success(monkeypatch, duplicate_packages_action):
@pytest.mark.parametrize(
("output"),
((""),),
)
def test_duplicate_packages_success(monkeypatch, duplicate_packages_action, output):

monkeypatch.setattr(utils, "run_subprocess", RunSubprocessMocked(return_value=("", 0)))
monkeypatch.setattr(utils, "run_subprocess", RunSubprocessMocked(return_value=(output, 0)))
duplicate_packages_action.run()
unit_tests.assert_actions_result(
duplicate_packages_action,
Expand Down
33 changes: 0 additions & 33 deletions convert2rhel/unit_tests/backup/packages_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,39 +120,6 @@ def test_enable_eus_systems(self, monkeypatch, tmpdir, global_system_info):
assert packages.get_hardcoded_repofiles_dir.call_count == 1
assert utils.download_pkg.call_count == 1

@pytest.mark.parametrize(
(
"has_internet_access",
"expected",
),
(
(
True,
"Using repository files stored in %s",
),
(
False,
"Not using repository files stored in %s due to the absence of internet access.",
),
),
)
def test_enable_has_internet_connection(
self, has_internet_access, expected, monkeypatch, tmpdir, global_system_info, caplog
):
tmpdir = str(tmpdir)
monkeypatch.setattr(packages, "BACKUP_DIR", tmpdir)
monkeypatch.setattr(utils, "download_pkg", DownloadPkgMocked())
monkeypatch.setattr(packages, "system_info", global_system_info)

global_system_info.has_internet_access = has_internet_access

rp = RestorablePackage(pkgs=["test.rpm"], reposdir=tmpdir)
rp._backedup_pkgs_paths = ["test.rpm"]
rp.enable()

assert utils.download_pkg.call_count == 1
assert expected % tmpdir in caplog.records[-1].message

def test_package_already_enabled(self, monkeypatch, tmpdir):
monkeypatch.setattr(packages, "BACKUP_DIR", str(tmpdir))
monkeypatch.setattr(utils, "download_pkg", DownloadPkgMocked())
Expand Down

0 comments on commit 9c3c798

Please sign in to comment.