Skip to content

Commit

Permalink
Refactor insights-client call and RHSM check
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Waltlova <[email protected]>
  • Loading branch information
andywaltlova committed Nov 16, 2023
1 parent 6e53a72 commit edad91c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
27 changes: 18 additions & 9 deletions scripts/leapp_preupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Both classes taken from:
# https://github.com/oamg/convert2rhel-worker-scripts/blob/main/scripts/preconversion_assessment_script.py
class ProcessError(Exception):
"""Custom exception to report errors during setup and run of conver2rhel"""
"""Custom exception to report errors during setup and run of leapp"""

def __init__(self, message, report):
super(ProcessError, self).__init__(report)
Expand Down Expand Up @@ -233,8 +233,8 @@ def setup_leapp(version):
def should_use_no_rhsm_check(rhui_installed, command):
print("Checking if subscription manager and repositories are available ...")
rhsm_repo_check_fail = True
_, rhsm_installed_check = run_subprocess(["which", "subscription-manager"])
if rhsm_installed_check == 0:
rhsm_installed_check = _check_if_package_installed("subscription-manager")
if rhsm_installed_check:
rhsm_repo_check, _ = run_subprocess(
["/usr/sbin/subscription-manager", "repos", "--list-enabled"]
)
Expand Down Expand Up @@ -348,10 +348,19 @@ def parse_results(output):
output.report = report_txt


def call_insights_client():
print("Calling insight-client in background for immediate data collection.")
run_subprocess(["/usr/bin/insights-client"], wait=False)
# NOTE: we do not care about returncode or output because we are not waiting for process to finish
def update_insights_inventory():
"""Call insights-client to update insights inventory."""
print("Updating system status in Red Hat Insights.")
output, returncode = run_subprocess(cmd=["/usr/bin/insights-client"])

if returncode:
raise ProcessError(
message="Failed to update Insights Inventory by registering the system again. See output the following output: %s"
% output,
report="insights-client execution exited with code '%s'." % returncode,
)

print("System registered with insights-client successfully.")


def main():
Expand All @@ -376,8 +385,9 @@ def main():

remove_previous_reports()
execute_preupgrade(preupgrade_command)
print("Pre-upgrade successfully executed.")
parse_results(output)
update_insights_inventory()
print("Pre-upgrade successfully executed.")
except ProcessError as exception:
print(exception.report)
output = OutputCollector(
Expand All @@ -400,7 +410,6 @@ def main():
print("### JSON START ###")
print(json.dumps(output.to_dict(), indent=4))
print("### JSON END ###")
call_insights_client()


if __name__ == "__main__":
Expand Down
9 changes: 0 additions & 9 deletions tests/preupgrade/test_insights_client_call.py

This file was deleted.

12 changes: 6 additions & 6 deletions tests/preupgrade/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
@patch("scripts.leapp_preupgrade.get_rhel_version")
@patch("scripts.leapp_preupgrade.is_non_eligible_releases")
@patch("scripts.leapp_preupgrade.setup_leapp")
@patch("scripts.leapp_preupgrade.call_insights_client")
@patch("scripts.leapp_preupgrade.update_insights_inventory")
@patch("scripts.leapp_preupgrade.OutputCollector")
def test_main_non_eligible_release(
mock_output_collector,
mock_call_insights_client,
mock_update_insights_inventory,
mock_setup_leapp,
mock_is_non_eligible_releases,
mock_get_rhel_version,
Expand All @@ -24,7 +24,7 @@ def test_main_non_eligible_release(
mock_is_non_eligible_releases.assert_called_once()
mock_output_collector.assert_called_once()
mock_setup_leapp.assert_not_called()
mock_call_insights_client.assert_called_once()
mock_update_insights_inventory.assert_not_called()


@patch("scripts.leapp_preupgrade.parse_results")
Expand All @@ -35,11 +35,11 @@ def test_main_non_eligible_release(
@patch("scripts.leapp_preupgrade.install_leapp_pkg_corresponding_to_installed_rhui")
@patch("scripts.leapp_preupgrade.remove_previous_reports")
@patch("scripts.leapp_preupgrade.execute_preupgrade")
@patch("scripts.leapp_preupgrade.call_insights_client")
@patch("scripts.leapp_preupgrade.update_insights_inventory")
@patch("scripts.leapp_preupgrade.OutputCollector")
def test_main_eligible_release(
mock_output_collector,
mock_call_insights_client,
mock_update_insights_inventory,
mock_execute_preupgrade,
mock_remove_previous_reports,
mock_should_use_no_rhsm_check,
Expand All @@ -63,4 +63,4 @@ def test_main_eligible_release(
mock_remove_previous_reports.assert_called_once()
mock_execute_preupgrade.assert_called_once()
mock_parse_results.assert_called_once()
mock_call_insights_client.assert_called_once()
mock_update_insights_inventory.assert_called_once()
24 changes: 24 additions & 0 deletions tests/preupgrade/test_update_insights_inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from mock import patch
import pytest
from scripts.leapp_preupgrade import update_insights_inventory, ProcessError


def test_update_insights_inventory_successfully():
with patch(
"scripts.leapp_preupgrade.run_subprocess", return_value=(b"", 0)
) as mock_popen:
update_insights_inventory()

mock_popen.assert_called_once_with(cmd=["/usr/bin/insights-client"])


def test_update_insights_inventory_non_success():
with patch(
"scripts.leapp_preupgrade.run_subprocess", return_value=(b"output", 1)
) as mock_popen:
with pytest.raises(
ProcessError, match="insights-client execution exited with code '1'"
):
update_insights_inventory()

mock_popen.assert_called_once_with(cmd=["/usr/bin/insights-client"])

0 comments on commit edad91c

Please sign in to comment.