Skip to content

Commit

Permalink
Merge pull request #8 from oamg/fix/install-leapp-only-if-not-installed
Browse files Browse the repository at this point in the history
Don't call install if leapp already installed
  • Loading branch information
Monnte authored Jan 19, 2024
2 parents 7bd3516 + ba74df2 commit fc73bb3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
19 changes: 11 additions & 8 deletions scripts/leapp_preupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,18 @@ def _get_leapp_command_and_packages(version):


def setup_leapp(version):
print("Installing leapp ...")
leapp_install_command, rhel_rhui_packages = _get_leapp_command_and_packages(version)
output, returncode = run_subprocess(leapp_install_command)
if returncode:
raise ProcessError(
message="Installation of leapp failed",
report="Installation of leapp failed with code '%s' and output: %s."
% (returncode, output.rstrip("\n")),
)
if _check_if_package_installed('leapp-upgrade'):
print("'leapp-upgrade' already installed, skipping ...")
else:
print("Installing leapp ...")
output, returncode = run_subprocess(leapp_install_command)
if returncode:
raise ProcessError(
message="Installation of leapp failed",
report="Installation of leapp failed with code '%s' and output: %s."
% (returncode, output.rstrip("\n")),
)

print("Check installed rhui packages ...")
for pkg in rhel_rhui_packages:
Expand Down
19 changes: 11 additions & 8 deletions scripts/leapp_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,18 @@ def _get_leapp_command_and_packages(version):


def setup_leapp(version):
print("Installing leapp ...")
leapp_install_command, rhel_rhui_packages = _get_leapp_command_and_packages(version)
output, returncode = run_subprocess(leapp_install_command)
if returncode:
raise ProcessError(
message="Installation of leapp failed",
report="Installation of leapp failed with code '%s' and output: %s."
% (returncode, output.rstrip("\n")),
)
if _check_if_package_installed('leapp-upgrade'):
print("'leapp-upgrade' already installed, skipping ...")
else:
print("Installing leapp ...")
output, returncode = run_subprocess(leapp_install_command)
if returncode:
raise ProcessError(
message="Installation of leapp failed",
report="Installation of leapp failed with code '%s' and output: %s."
% (returncode, output.rstrip("\n")),
)

print("Check installed rhui packages ...")
for pkg in rhel_rhui_packages:
Expand Down
19 changes: 14 additions & 5 deletions tests/preupgrade/test_setup_leapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
from scripts.leapp_preupgrade import setup_leapp, ProcessError


@pytest.mark.parametrize(
("installed"),
(
(True),
(False),
),
)
@patch("scripts.leapp_preupgrade.run_subprocess")
@patch("scripts.leapp_preupgrade._check_if_package_installed")
def test_setup_leapp_success(mock_check_if_package_installed, mock_run_subprocess):
def test_setup_leapp_success(mock_check_if_package_installed, mock_run_subprocess, installed):
mock_run_subprocess.return_value = ("Installation successful", 0)
mock_check_if_package_installed.return_value = True
mock_check_if_package_installed.return_value = installed

rhel7_command = [
"/usr/bin/yum",
Expand All @@ -20,15 +27,18 @@ def test_setup_leapp_success(mock_check_if_package_installed, mock_run_subproces

for version, command in [("7", rhel7_command), ("8", rhel8_command)]:
result = setup_leapp(version)
mock_run_subprocess.assert_called_with(command)
if installed:
assert command not in mock_run_subprocess.call_args_list
else:
mock_run_subprocess.assert_called_with(command)
assert all(pkg.get("installed", False) for pkg in result)


@patch("scripts.leapp_preupgrade.run_subprocess")
@patch("scripts.leapp_preupgrade._check_if_package_installed")
def test_setup_leapp_failure(mock_check_if_package_installed, mock_run_subprocess):
mock_run_subprocess.return_value = ("Installation failed", 1)
mock_check_if_package_installed.return_value = True
mock_check_if_package_installed.return_value = False

rhel7_command = [
"/usr/bin/yum",
Expand All @@ -44,7 +54,6 @@ def test_setup_leapp_failure(mock_check_if_package_installed, mock_run_subproces
setup_leapp(version)

mock_run_subprocess.assert_called_with(command)
assert mock_check_if_package_installed.call_count == 0
assert (
str(e_info.value)
== "Installation of leapp failed with code '1' and output: Installation failed."
Expand Down
20 changes: 14 additions & 6 deletions tests/upgrade/test_setup_leapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
from mock import patch
from scripts.leapp_upgrade import setup_leapp, ProcessError


@pytest.mark.parametrize(
("installed"),
(
(True),
(False),
),
)
@patch("scripts.leapp_upgrade.run_subprocess")
@patch("scripts.leapp_upgrade._check_if_package_installed")
def test_setup_leapp_success(mock_check_if_package_installed, mock_run_subprocess):
def test_setup_leapp_success(mock_check_if_package_installed, mock_run_subprocess, installed):
mock_run_subprocess.return_value = ("Installation successful", 0)
mock_check_if_package_installed.return_value = True
mock_check_if_package_installed.return_value = installed

rhel7_command = [
"/usr/bin/yum",
Expand All @@ -20,15 +26,18 @@ def test_setup_leapp_success(mock_check_if_package_installed, mock_run_subproces

for version, command in [("7", rhel7_command), ("8", rhel8_command)]:
result = setup_leapp(version)
mock_run_subprocess.assert_called_with(command)
if installed:
assert command not in mock_run_subprocess.call_args_list
else:
mock_run_subprocess.assert_called_with(command)
assert all(pkg.get("installed", False) for pkg in result)


@patch("scripts.leapp_upgrade.run_subprocess")
@patch("scripts.leapp_upgrade._check_if_package_installed")
def test_setup_leapp_failure(mock_check_if_package_installed, mock_run_subprocess):
mock_run_subprocess.return_value = ("Installation failed", 1)
mock_check_if_package_installed.return_value = True
mock_check_if_package_installed.return_value = False

rhel7_command = [
"/usr/bin/yum",
Expand All @@ -44,7 +53,6 @@ def test_setup_leapp_failure(mock_check_if_package_installed, mock_run_subproces
setup_leapp(version)

mock_run_subprocess.assert_called_with(command)
assert mock_check_if_package_installed.call_count == 0
assert (
str(e_info.value)
== "Installation of leapp failed with code '1' and output: Installation failed."
Expand Down

0 comments on commit fc73bb3

Please sign in to comment.