From 184692cc03512dd686edd3e12169ba1e5ac9bda8 Mon Sep 17 00:00:00 2001 From: larsevj Date: Thu, 26 Sep 2024 17:56:57 +0200 Subject: [PATCH] Add more tests for update.py --- komodoenv/update.py | 5 ++-- tests/test_integration.py | 12 ++++++--- tests/test_update.py | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/komodoenv/update.py b/komodoenv/update.py index a97d89a..299ef98 100644 --- a/komodoenv/update.py +++ b/komodoenv/update.py @@ -28,12 +28,12 @@ def distro_id() -> str: return "rhel" - if "el7" in platform.release(): + if ".el7" in platform.release(): def distro_versions() -> Tuple[str, str, str]: return ("7", "0", "0") - elif "el8" in platform.release(): + elif ".el8" in platform.release(): def distro_versions() -> Tuple[str, str, str]: return ("8", "0", "0") @@ -297,7 +297,6 @@ def can_update(config: Dict[str, str]) -> bool: """Compares the version of komodoenv that built the release with the one in the one we want to update to. If the major versions are the same, we know the layout is identical, and can be safely updated with this script. - """ track_path = (Path(config["komodo-root"]) / config["tracked-release"]).resolve() if not (track_path / "root").is_dir(): diff --git a/tests/test_integration.py b/tests/test_integration.py index f81cb1e..d957ef0 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1,7 +1,6 @@ import sys -from subprocess import PIPE, CalledProcessError, Popen, check_output +from subprocess import PIPE, STDOUT, Popen, check_output -import pytest from komodoenv.__main__ import main as _main @@ -89,8 +88,13 @@ def revert(): request.addfinalizer(revert) # There's now an update - with pytest.raises(CalledProcessError): - check_output([str(tmp_path / "kenv/root/bin/komodoenv-update"), "--check"]) + output = check_output( + [str(tmp_path / "kenv/root/bin/komodoenv-update"), "--check"], stderr=STDOUT + ).decode("utf-8") + assert ( + "Warning: Your komodoenv is out of date. You will need to recreate komodo" + in output + ) # Update script = """\ diff --git a/tests/test_update.py b/tests/test_update.py index 710f923..993023e 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -1,12 +1,22 @@ +import importlib import shutil import sys import time from importlib.metadata import distribution from pathlib import Path from textwrap import dedent +from unittest.mock import mock_open, patch from komodoenv import update +CONFIG_CONTENT = """ +key1=value1 +key2 = value2 +# This is a comment +key3=value3 +malformed_line +""" + def test_rewrite_executable_python(): pip = dedent( @@ -165,3 +175,45 @@ def test_get_pkg_version_none(): ) assert ver is None + + +def test_read_config_with_valid_file(): + with patch( + "komodoenv.update.open", new_callable=mock_open, read_data=CONFIG_CONTENT + ): + config = update.read_config() + assert config["key1"] == "value1" + assert config["key2"] == "value2" + assert config["key3"] == "value3" + assert "malformed_line" not in config + assert config["komodo-root"] == "/prog/res/komodo" + + +def test_rhel_version_suffix_with_distro_not_installed(): + with patch.dict("sys.modules", {"distro": None}), patch( + "platform.release", return_value="3.10.0-1062.el7.x86_64" + ): + from komodoenv import update + + importlib.reload(update) + assert update.rhel_version_suffix() == "-rhel7" + with patch.dict("sys.modules", {"distro": None}), patch( + "platform.release", return_value="4.18.0-147.el8.x86_64" + ): + from komodoenv import update + + importlib.reload(update) + assert update.rhel_version_suffix() == "-rhel8" + + +def test_rhel_version_suffix_with_distro_not_installed_incompatible(capsys): + with patch.dict("sys.modules", {"distro": None}), patch( + "platform.release", return_value="5.6.14-300.fc32.x86_64" + ): + from komodoenv import update + + importlib.reload(update) + assert ( + "Warning: komodoenv is only compatible with RHEL7 or RHEL8" + in capsys.readouterr().err + )