Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RHELC-967] Verify breadcrumbs source_os #1432

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions convert2rhel/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,52 @@ def finish_collection(self, success=False):

def _set_activity(self):
"""Set the activity that convert2rhel is going to perform"""
self.activity = tool_opts.activity
try:
self.activity = tool_opts.activity
if not self.activity:
raise ValueError("Activity is not set in tool_opts")
except Exception as e:
logger.logging.warnning(f"Error setting activity: {e}")

def _set_pkg_object(self):
"""Set pkg_object which is used to get information about installed Convert2RHEL"""
# the index position is there because get_installed_pkg_objects return list, which is filtered and
# should contain just one item
self._pkg_object = pkghandler.get_installed_pkg_objects(name="convert2rhel")[0]
try:
pkg_objects = pkghandler.get_installed_pkg_objects(name="convert2rhel")
if not pkg_objects:
raise ValueError("No installed package objects found for convert2rhel")
self._pkg_object = pkg_objects[0]
except Exception as e:
logger.logging.warnning(f"Error setting pkg_object: {e}")

def _set_executed(self):
"""Set how was Convert2RHEL executed"""
self.executed = " ".join(utils.hide_secrets(args=sys.argv))
try:
self.executed = " ".join(utils.hide_secrets(args=sys.argv))
if not self.executed:
raise ValueError("Executed command is not set")
except Exception as e:
logger.logging.warnning(f"Error setting executed: {e}")

def _set_nevra(self):
"""Set NEVRA of installed Convert2RHEL"""
self.nevra = pkghandler.get_pkg_nevra(self._pkg_object, include_zero_epoch=True)
try:
self.nevra = pkghandler.get_pkg_nevra(self._pkg_object, include_zero_epoch=True)
if not self.nevra:
raise ValueError("NEVRA is not set")
except Exception as e:
logger.logging.warnning(f"Error setting nevra: {e}")

def _set_signature(self):
"""Set signature of installed Convert2RHEL"""
package = pkghandler.get_installed_pkg_information(str(self._pkg_object))[0]
self.signature = package.signature
try:
package_info = pkghandler.get_installed_pkg_information(str(self._pkg_object))
if not package_info:
raise ValueError("No package information found for convert2rhel")
self.signature = package_info[0].signature
if not self.signature:
raise ValueError("Signature is not set")
except Exception as e:
logger.logging.warnning(f"Error setting signature: {e}")

def _set_started(self):
"""Set start time of activity"""
Expand Down
139 changes: 131 additions & 8 deletions convert2rhel/unit_tests/breadcrumbs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,101 @@ def test_save_migration_results(tmpdir, monkeypatch, caplog):
assert write_obj_to_array_json_mock.call_count == 1


def test_set_pkg_object(_mock_pkg_obj, monkeypatch):
monkeypatch.setattr(pkghandler, "get_installed_pkg_objects", lambda name: [_mock_pkg_obj])
breadcrumbs.breadcrumbs._set_pkg_object()
assert breadcrumbs.breadcrumbs._pkg_object.name == "convert2rhel"
@pytest.mark.parametrize(
("attribute", "value", "expected", "exception"),
[
("tool_opts.activity", "Test Activity", "Test Activity", None),
("tool_opts.activity", None, None, ValueError("Activity is not set in tool_opts")),
(
"tool_opts.activity",
mock.Mock(side_effect=Exception("Test Exception")),
None,
Exception("Error setting activity: Test Exception"),
),
],
)
def test_set_activity(monkeypatch, attribute, value, expected, exception, caplog):
monkeypatch.setattr(*attribute.split("."), value)
if exception:
with pytest.raises(type(exception), match=str(exception)):
breadcrumbs.breadcrumbs._set_activity()
if isinstance(exception, ValueError):
assert str(exception) in caplog.text
else:
breadcrumbs.breadcrumbs._set_activity()
assert breadcrumbs.breadcrumbs.activity == expected


@pytest.mark.parametrize(
("method", "attribute", "value", "expected", "exception"),
[
("_set_pkg_object", "pkghandler.get_installed_pkg_objects", [_mock_pkg_obj], "convert2rhel", None),
(
"_set_pkg_object",
"pkghandler.get_installed_pkg_objects",
[],
None,
ValueError("No installed package objects found for convert2rhel"),
),
(
"_set_pkg_object",
"pkghandler.get_installed_pkg_objects",
mock.Mock(side_effect=Exception("Test Exception")),
None,
Exception("Error setting pkg_object: Test Exception"),
),
],
)
def test_set_pkg_object(monkeypatch, method, attribute, value, expected, exception, caplog):
monkeypatch.setattr(*attribute.split("."), value)
if exception:
with pytest.raises(type(exception), match=str(exception)):
getattr(breadcrumbs.breadcrumbs, method)()
if isinstance(exception, ValueError):
assert str(exception) in caplog.text

else:
getattr(breadcrumbs.breadcrumbs, method)()
assert breadcrumbs.breadcrumbs._pkg_object.name == expected


@pytest.mark.parametrize(
("method", "attribute", "value", "expected", "exception"),
[
(
"_set_nevra",
"pkghandler.get_pkg_nevra",
lambda obj, include_zero_epoch: "convert2rhel-1:2-3.x86_64",
"convert2rhel-1:2-3.x86_64",
None,
),
(
"_set_nevra",
"pkghandler.get_pkg_nevra",
lambda obj, include_zero_epoch: None,
None,
ValueError("NEVRA is not set"),
),
(
"_set_nevra",
"pkghandler.get_pkg_nevra",
mock.Mock(side_effect=Exception("Test Exception")),
None,
Exception("Error setting nevra: Test Exception"),
),
],
)
def test_set_nevra(monkeypatch, method, attribute, value, expected, exception, caplog, _mock_pkg_obj):
monkeypatch.setattr(breadcrumbs.breadcrumbs, "_pkg_object", _mock_pkg_obj)
monkeypatch.setattr(*attribute.split("."), value)
if exception:
with pytest.raises(type(exception), match=str(exception)):
getattr(breadcrumbs.breadcrumbs, method)()
if isinstance(exception, ValueError):
assert str(exception) in caplog.text
else:
getattr(breadcrumbs.breadcrumbs, method)()
assert breadcrumbs.breadcrumbs.nevra == expected


@pytest.mark.skipif(
Expand All @@ -295,12 +386,44 @@ def test_set_nevra_yum(monkeypatch, _mock_pkg_obj):
assert breadcrumbs.breadcrumbs.nevra == "1:convert2rhel-2-3.x86_64"


def test_set_signature(monkeypatch, _mock_pkg_obj, _mock_pkg_information):
@pytest.mark.parametrize(
("method", "attribute", "value", "expected", "exception"),
[
(
"_set_signature",
"pkghandler.get_installed_pkg_information",
lambda name: [_mock_pkg_information],
"73bde98381b46521",
None,
),
(
"_set_signature",
"pkghandler.get_installed_pkg_information",
lambda name: [_mock_pkg_obj],
None,
ValueError("Signature is not set"),
),
(
"_set_signature",
"pkghandler.get_installed_pkg_information",
mock.Mock(side_effect=Exception("Test Exception")),
None,
Exception("Error setting signature: Test Exception"),
),
],
)
def test_set_signature(monkeypatch, method, attribute, value, expected, exception, caplog, _mock_pkg_obj):
monkeypatch.setattr(pkgmanager, "TYPE", "yum")
monkeypatch.setattr(breadcrumbs.breadcrumbs, "_pkg_object", _mock_pkg_obj)
monkeypatch.setattr(pkghandler, "get_installed_pkg_information", lambda name: [_mock_pkg_information])
breadcrumbs.breadcrumbs._set_signature()
assert "73bde98381b46521" in breadcrumbs.breadcrumbs.signature
monkeypatch.setattr(*attribute.split("."), value)
if exception:
with pytest.raises(type(exception), match=str(exception)):
getattr(breadcrumbs.breadcrumbs, method)()
if isinstance(exception, ValueError):
assert str(exception) in caplog.text
else:
getattr(breadcrumbs.breadcrumbs, method)()
assert breadcrumbs.breadcrumbs.signature == expected


def test_set_started():
Expand Down
Loading