Skip to content

Commit

Permalink
RHEL6 unittests enablement (#552)
Browse files Browse the repository at this point in the history
* Syntax fix for python2.6
* Update Makefile to run tests6
* Enable CentOS Linux 6 unit tests in CI
* Move coverage settings to .coveragerc
* Fix fixture for config file unit_tests

In the pytest version we use (3.2.5) for CentOS 6, we don't have the
fixture `tmp_path` available, but instead, we have tmpdir.
  • Loading branch information
r0x0d authored Aug 11, 2022
1 parent b38aecf commit f0a5ebf
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 82 deletions.
15 changes: 15 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[run]
branch = True
source =
convert2rhel
omit =
convert2rhel/unit_tests/*

[paths]
source =
convert2rhel/

[report]
fail_under = 80
skip_covered = True
show_missing = True
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: coverage-centos${{ matrix.centos_ver }}
strategy:
matrix:
centos_ver: [7, 8]
centos_ver: [6, 7, 8]

runs-on: ubuntu-20.04
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
centos_ver: [7, 8]
centos_ver: [6, 7, 8]

runs-on: ubuntu-20.04
steps:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ lint: images
lint-errors: images
@$(DOCKER) run --rm -v $(shell pwd):/data:Z $(IMAGE)/centos8 bash -c "scripts/run_lint.sh --errors-only"

tests: tests7 tests8
tests: tests6 tests7 tests8

tests6: images
@echo 'CentOS Linux 6 tests'
@$(DOCKER) run --rm --user=$(id -ur):$(id -gr) -v $(shell pwd):/data:Z $(IMAGE)/centos6 pytest --show-capture=$(SHOW_CAPTURE) $(PYTEST_ARGS)
@$(DOCKER) run --rm --user=$(id -ur):$(id -gr) -v $(shell pwd):/data:Z $(IMAGE)/centos6 pytest --capture=$(SHOW_CAPTURE) $(PYTEST_ARGS)

tests7: images
@echo 'CentOS Linux 7 tests'
Expand Down
6 changes: 3 additions & 3 deletions convert2rhel/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ def sanitized_iterator():
pos = c.index("=")

if c[:pos] in options_to_sanitize:
yield "{}={}".format(c[:pos], ((len(c) - pos - 1) * "*"))
yield "{0}={1}".format(c[:pos], ((len(c) - pos - 1) * "*"))
elif " " in c[pos:]:
# if value contains a space we need to add quotes
yield '{}="{}"'.format(c[:pos], c[pos + 1 :])
yield '{0}="{1}"'.format(c[:pos], c[pos + 1 :])
else:
if c in options_to_sanitize:
yield c
Expand All @@ -186,7 +186,7 @@ def sanitized_iterator():
else:
if " " in c:
# if value contains a space we need to add quotes
yield '"{}"'.format(c)
yield '"{0}"'.format(c)
else:
yield c

Expand Down
16 changes: 16 additions & 0 deletions convert2rhel/unit_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from functools import wraps

import pytest

from convert2rhel.utils import run_subprocess


Expand Down Expand Up @@ -107,6 +109,20 @@ def safe_repr(obj, short=False):
return result[:_MAX_LENGTH] + " [truncated]..."


def get_pytest_mark(request, mark_name):
"""
Get a pytest mark from a request.
The pytest API to retrieve a mark changed between RHEL6 and RHEL7. This function is
a compatibility shim to retrieve the value.
"""
if pytest.__version__.split(".") <= ["3", "6", "0"]:
mark = request.node.get_marker(mark_name)
else:
mark = request.node.get_closest_marker(mark_name)

return mark


class ExtendedTestCase(unittest.TestCase):
"""
Extends Nose test case with more helpers.
Expand Down
4 changes: 2 additions & 2 deletions convert2rhel/unit_tests/backup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ def test_restorable_file_restore_oserror(tmpdir, caplog, monkeypatch):
@pytest.mark.parametrize(
("pkgs_to_remove", "ret_code", "backup_pkg", "critical", "expected"),
(
(["pkg1"], 1, False, True, "Error: Couldn't remove {}."),
(["pkg1"], 1, False, False, "Couldn't remove {}."),
(["pkg1"], 1, False, True, "Error: Couldn't remove {0}."),
(["pkg1"], 1, False, False, "Couldn't remove {0}."),
),
)
def test_remove_pkgs_failed_to_remove(
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/unit_tests/breadcrumbs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def test_sanitize_cli_options():
options_to_sanitize = {"--password", "-p", "--activationkey", "-k"}
options_to_sanitize = frozenset(("--password", "-p", "--activationkey", "-k"))

io = [
(
Expand Down
1 change: 0 additions & 1 deletion convert2rhel/unit_tests/cert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


six.add_move(six.MovedModule("mock", "mock", "unittest.mock"))
from six.moves import mock

from convert2rhel import cert, utils
from convert2rhel.systeminfo import system_info
Expand Down
66 changes: 47 additions & 19 deletions convert2rhel/unit_tests/checks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@
"/lib/modules/5.8.0-7642-generic/kernel/lib/c.ko.xz\n"
)

HOST_MODULES_STUB_GOOD = {
"kernel/lib/a.ko.xz",
"kernel/lib/b.ko.xz",
"kernel/lib/c.ko.xz",
}
HOST_MODULES_STUB_BAD = {
"kernel/lib/d.ko.xz",
"kernel/lib/e.ko.xz",
"kernel/lib/f.ko.xz",
}
HOST_MODULES_STUB_GOOD = frozenset(
(
"kernel/lib/a.ko.xz",
"kernel/lib/b.ko.xz",
"kernel/lib/c.ko.xz",
)
)
HOST_MODULES_STUB_BAD = frozenset(
(
"kernel/lib/d.ko.xz",
"kernel/lib/e.ko.xz",
"kernel/lib/f.ko.xz",
)
)

REPOQUERY_F_STUB_GOOD = (
"kernel-core-0:4.18.0-240.10.1.el8_3.x86_64\n"
Expand Down Expand Up @@ -231,9 +235,30 @@ def test_convert2rhel_latest_log_check_exit(caplog, convert2rhel_latest_version_
@pytest.mark.parametrize(
("convert2rhel_latest_version_test",),
(
[{"local_version": "0.18", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "6", "enset": "1"}],
[{"local_version": "0.18", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "7", "enset": "1"}],
[{"local_version": "0.18", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "8", "enset": "1"}],
[
{
"local_version": "0.18",
"package_version": "convert2rhel-0:0.22-1.el7.noarch",
"pmajor": "6",
"enset": "1",
}
],
[
{
"local_version": "0.18",
"package_version": "convert2rhel-0:0.22-1.el7.noarch",
"pmajor": "7",
"enset": "1",
}
],
[
{
"local_version": "0.18",
"package_version": "convert2rhel-0:0.22-1.el7.noarch",
"pmajor": "8",
"enset": "1",
}
],
),
indirect=True,
)
Expand Down Expand Up @@ -363,7 +388,9 @@ def test_ensure_compatibility_of_kmods_excluded(
monkeypatch.setattr(
checks,
"get_loaded_kmods",
mock.Mock(return_value=HOST_MODULES_STUB_GOOD | {unsupported_pkg}),
mock.Mock(
return_value=HOST_MODULES_STUB_GOOD | frozenset((unsupported_pkg,)),
),
)
get_unsupported_kmods_mocked = mock.Mock(wraps=checks.get_unsupported_kmods)
run_subprocess_mock = mock.Mock(
Expand All @@ -388,6 +415,7 @@ def test_ensure_compatibility_of_kmods_excluded(
checks.ensure_compatibility_of_kmods()
else:
checks.ensure_compatibility_of_kmods()

get_unsupported_kmods_mocked.assert_called_with(
# host kmods
set(
Expand Down Expand Up @@ -439,7 +467,7 @@ def test_get_loaded_kmods(monkeypatch):
"run_subprocess",
value=run_subprocess_mocked,
)
assert get_loaded_kmods() == {"kernel/lib/c.ko.xz", "kernel/lib/a.ko.xz", "kernel/lib/b.ko.xz"}
assert get_loaded_kmods() == frozenset(("kernel/lib/c.ko.xz", "kernel/lib/a.ko.xz", "kernel/lib/b.ko.xz"))


@pytest.mark.parametrize(
Expand Down Expand Up @@ -974,7 +1002,7 @@ def test_check_package_updates_skip_on_not_latest_ol(pretend_os, caplog):
@pytest.mark.parametrize(
("packages", "exception", "expected"),
(
(["package-1", "package-2"], True, "The system has {} packages not updated"),
(["package-1", "package-2"], True, "The system has {0} packages not updated"),
([], False, "System is up-to-date."),
),
)
Expand Down Expand Up @@ -1169,9 +1197,9 @@ def test_is_loaded_kernel_latest_eus_system_no_connection(pretend_os, monkeypatc
"1",
"Detected 'CONVERT2RHEL_UNSUPPORTED_SKIP_KERNEL_CURRENCY_CHECK' environment variable",
),
("", 0, 8, "kernel-core", "0", "Could not find any {} from repositories"),
("", 0, 7, "kernel", "0", "Could not find any {} from repositories"),
("", 0, 6, "kernel", "0", "Could not find any {} from repositories"),
("", 0, 8, "kernel-core", "0", "Could not find any {0} from repositories"),
("", 0, 7, "kernel", "0", "Could not find any {0} from repositories"),
("", 0, 6, "kernel", "0", "Could not find any {0} from repositories"),
),
)
def test_is_loaded_kernel_latest_unsupported_skip(
Expand Down
50 changes: 43 additions & 7 deletions convert2rhel/unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,37 @@
from convert2rhel.logger import setup_logger_handler
from convert2rhel.systeminfo import system_info
from convert2rhel.toolopts import tool_opts
from convert2rhel.unit_tests import get_pytest_mark


if sys.version_info[:2] <= (2, 7):
import mock # pylint: disable=import-error
else:
from unittest import mock # pylint: disable=no-name-in-module

try:
from pytest_catchlog import CompatLogCaptureFixture

class SubCompatLogCaptureFixture(CompatLogCaptureFixture):
@property
def messages(self):
return [r.getMessage() for r in self.records]

@pytest.fixture
def caplog(request):
"""Access and control log capturing.
Captured logs are available through the following properties/methods::
* caplog.messages -> list of format-interpolated log messages
* caplog.text -> string containing formatted log output
* caplog.records -> list of logging.LogRecord instances
* caplog.record_tuples -> list of (logger_name, level, message) tuples
* caplog.clear() -> clear captured records and formatted log output string
"""
return SubCompatLogCaptureFixture(request.node)

except ImportError:
pass


@pytest.fixture(scope="session")
def is_py26():
Expand Down Expand Up @@ -117,12 +141,12 @@ def system_cert_with_target_path(monkeypatch, tmpdir, request):
.. seealso::
https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#using-markers-to-pass-data-to-fixtures
"""
cert_file_returns = get_pytest_mark(request, "cert_filename")

mark = request.node.get_closest_marker("cert_filename")
if not mark:
if not cert_file_returns:
temporary_filename = "filename"
else:
temporary_filename = mark.args[0]
temporary_filename = cert_file_returns.args[0]

tmp_file = tmpdir / temporary_filename

Expand Down Expand Up @@ -228,30 +252,42 @@ def pretend_os(request, pkg_root, monkeypatch):
all_systems = pytest.mark.parametrize(
"pretend_os",
(
("6.10.1111", "CentOS Linux"),
("6.10.1111", "Oracle Linux Server"),
("7.9.1111", "CentOS Linux"),
("7.9.1111", "Oracle Linux Server"),
("8.4.1111", "CentOS Linux"),
("8.4.1111", "Oracle Linux Server"),
),
indirect=True,
)
centos8 = pytest.mark.parametrize(
centos6 = pytest.mark.parametrize(
"pretend_os",
(("8.4.1111", "CentOS Linux"),),
(("6.10.1111", "CentOS Linux"),),
indirect=True,
)
centos7 = pytest.mark.parametrize(
"pretend_os",
(("7.9.1111", "CentOS Linux"),),
indirect=True,
)
oracle8 = pytest.mark.parametrize(
centos8 = pytest.mark.parametrize(
"pretend_os",
(("8.4.1111", "Oracle Linux Server"),),
(("8.4.1111", "CentOS Linux"),),
indirect=True,
)
oracle6 = pytest.mark.parametrize(
"pretend_os",
(("6.10.1111", "Oracle Linux Server"),),
indirect=True,
)
oracle7 = pytest.mark.parametrize(
"pretend_os",
(("7.9.1111", "Oracle Linux Server"),),
indirect=True,
)
oracle8 = pytest.mark.parametrize(
"pretend_os",
(("8.4.1111", "Oracle Linux Server"),),
indirect=True,
)
3 changes: 2 additions & 1 deletion convert2rhel/unit_tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ def test_initialize_logger(exception_type, exception, monkeypatch, capsys):

if exception:
main.initialize_logger("convert2rhel.log", "/tmp")
assert "Warning: Unable to archive previous log:" in capsys.readouterr().out
out, _ = capsys.readouterr()
assert "Warning: Unable to archive previous log:" in out
else:
main.initialize_logger("convert2rhel.log", "/tmp")
setup_logger_handler_mock.assert_called_once()
Expand Down
Loading

0 comments on commit f0a5ebf

Please sign in to comment.