From e56a959a07fe42809f435b7d6569373b4dbbfa2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Zdraveck=C3=BD?= Date: Tue, 16 Jan 2024 10:13:12 +0100 Subject: [PATCH] Merge upgrade and preupgrade script, add sync script for rhc devel worker --- misc/sync_scripts.py | 73 +++ requirements.txt | 1 + ..._schema_1.0.json => leapp_schema_1.0.json} | 0 schemas/upgrade_schema_1.0.json | 69 --- scripts/leapp_preupgrade.py | 428 ------------------ scripts/{leapp_upgrade.py => leapp_script.py} | 72 +-- tests/{preupgrade => }/__init__.py | 0 tests/preupgrade/test_check_pkg_installed.py | 13 - tests/preupgrade/test_collect_report_level.py | 57 --- tests/preupgrade/test_get_rhel_version.py | 20 - tests/preupgrade/test_install_rhui_pkgs.py | 38 -- tests/preupgrade/test_main.py | 66 --- tests/preupgrade/test_non_eligible_release.py | 12 - tests/preupgrade/test_parse_results.py | 43 -- tests/preupgrade/test_remove_reports.py | 23 - tests/preupgrade/test_run.py | 10 - tests/preupgrade/test_run_subprocess.py | 97 ---- tests/preupgrade/test_schema.py | 34 -- tests/preupgrade/test_setup_leapp.py | 60 --- tests/preupgrade/test_should_use_rhsm.py | 50 -- .../test_update_insights_inventory.py | 24 - .../{upgrade => }/test_check_pkg_installed.py | 4 +- .../test_collect_report_level.py | 2 +- tests/{upgrade => }/test_get_rhel_version.py | 2 +- tests/{upgrade => }/test_install_rhui_pkgs.py | 6 +- tests/test_main.py | 193 ++++++++ .../test_non_eligible_release.py | 4 +- tests/{upgrade => }/test_parse_results.py | 6 +- tests/{upgrade => }/test_reboot.py | 4 +- tests/{upgrade => }/test_remove_reports.py | 2 +- tests/test_run.py | 10 + tests/{upgrade => }/test_run_subprocess.py | 2 +- tests/{upgrade => }/test_schema.py | 6 +- tests/{upgrade => }/test_setup_leapp.py | 10 +- tests/{upgrade => }/test_should_use_rhsm.py | 8 +- tests/test_update_insights_inventory.py | 24 + tests/upgrade/__init__.py | 0 tests/upgrade/test_main.py | 119 ----- tests/upgrade/test_run.py | 10 - .../upgrade/test_update_insights_inventory.py | 18 - 40 files changed, 369 insertions(+), 1251 deletions(-) create mode 100644 misc/sync_scripts.py rename schemas/{preupgrade_schema_1.0.json => leapp_schema_1.0.json} (100%) delete mode 100644 schemas/upgrade_schema_1.0.json delete mode 100644 scripts/leapp_preupgrade.py rename scripts/{leapp_upgrade.py => leapp_script.py} (86%) rename tests/{preupgrade => }/__init__.py (100%) delete mode 100644 tests/preupgrade/test_check_pkg_installed.py delete mode 100644 tests/preupgrade/test_collect_report_level.py delete mode 100644 tests/preupgrade/test_get_rhel_version.py delete mode 100644 tests/preupgrade/test_install_rhui_pkgs.py delete mode 100644 tests/preupgrade/test_main.py delete mode 100644 tests/preupgrade/test_non_eligible_release.py delete mode 100644 tests/preupgrade/test_parse_results.py delete mode 100644 tests/preupgrade/test_remove_reports.py delete mode 100644 tests/preupgrade/test_run.py delete mode 100644 tests/preupgrade/test_run_subprocess.py delete mode 100644 tests/preupgrade/test_schema.py delete mode 100644 tests/preupgrade/test_setup_leapp.py delete mode 100644 tests/preupgrade/test_should_use_rhsm.py delete mode 100644 tests/preupgrade/test_update_insights_inventory.py rename tests/{upgrade => }/test_check_pkg_installed.py (76%) rename tests/{upgrade => }/test_collect_report_level.py (97%) rename tests/{upgrade => }/test_get_rhel_version.py (91%) rename tests/{upgrade => }/test_install_rhui_pkgs.py (90%) create mode 100644 tests/test_main.py rename tests/{upgrade => }/test_non_eligible_release.py (72%) rename tests/{upgrade => }/test_parse_results.py (92%) rename tests/{upgrade => }/test_reboot.py (57%) rename tests/{upgrade => }/test_remove_reports.py (94%) create mode 100644 tests/test_run.py rename tests/{upgrade => }/test_run_subprocess.py (98%) rename tests/{upgrade => }/test_schema.py (84%) rename tests/{upgrade => }/test_setup_leapp.py (86%) rename tests/{upgrade => }/test_should_use_rhsm.py (85%) create mode 100644 tests/test_update_insights_inventory.py delete mode 100644 tests/upgrade/__init__.py delete mode 100644 tests/upgrade/test_main.py delete mode 100644 tests/upgrade/test_run.py delete mode 100644 tests/upgrade/test_update_insights_inventory.py diff --git a/misc/sync_scripts.py b/misc/sync_scripts.py new file mode 100644 index 0000000..193a239 --- /dev/null +++ b/misc/sync_scripts.py @@ -0,0 +1,73 @@ +import os +import ruamel.yaml + +# Scripts located in this project +SCRIPT_PATH = "scripts/leapp_script.py" + +# Yaml playbooks in rhc-worker-script +PRE_UPGRADE_YAML_PATH = os.path.join( + "..", "rhc-worker-script/development/nginx/data/leapp_preupgrade.yml" +) +UPGRADE_YAML_PATH = os.path.join( + "..", "rhc-worker-script/development/nginx/data/leapp_upgrade.yml" +) + +DEFAULT_YAML_ENVELOPE = """ +- name: LEAPP + vars: + insights_signature: | + ascii_armored gpg signature + insights_signature_exclude: /vars/insights_signature,/vars/content_vars + interpreter: /usr/bin/python + content: | + placeholder + content_vars: + # variables that will be handed to the script as environment vars + # will be prefixed with RHC_WORKER_* + LEAPP_SCRIPT_TYPE: type +""" + + +def _get_updated_yaml_content(yaml_path, script_path): + if not os.path.exists(yaml_path): + yaml = ruamel.yaml.YAML() + config = yaml.load(DEFAULT_YAML_ENVELOPE) + mapping = 2 + offset = 0 + else: + config, mapping, offset = ruamel.yaml.util.load_yaml_guess_indent( + open(yaml_path) + ) + print(mapping, offset) + + with open(script_path) as script: + content = script.read() + + script_type = "PREUPGRADE" if "preupgrade" in yaml_path else "UPGRADE" + config[0]["name"] = "LEAPP %s" % script_type.title() + config[0]["vars"]["content"] = content + config[0]["vars"]["content_vars"]["LEAPP_SCRIPT_TYPE"] = script_type + return config, mapping, offset + + +def _write_content(config, path, mapping=None, offset=None): + yaml = ruamel.yaml.YAML() + if mapping and offset: + yaml.indent(mapping=mapping, sequence=mapping, offset=offset) + with open(path, "w") as handler: + yaml.dump(config, handler) + + +def main(): + config, mapping, offset = _get_updated_yaml_content( + PRE_UPGRADE_YAML_PATH, SCRIPT_PATH + ) + print("Writing new content to %s" % PRE_UPGRADE_YAML_PATH) + _write_content(config, PRE_UPGRADE_YAML_PATH, mapping, offset) + config, mapping, offset = _get_updated_yaml_content(UPGRADE_YAML_PATH, SCRIPT_PATH) + print("Writing new content to %s" % UPGRADE_YAML_PATH) + _write_content(config, UPGRADE_YAML_PATH, mapping, offset) + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index afbf629..533dabf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ jsonschema==3.2.0 mock==3.0.5 pytest==4.6.11 pytest-cov==2.12.1 +ruamel.yaml==0.16.13 diff --git a/schemas/preupgrade_schema_1.0.json b/schemas/leapp_schema_1.0.json similarity index 100% rename from schemas/preupgrade_schema_1.0.json rename to schemas/leapp_schema_1.0.json diff --git a/schemas/upgrade_schema_1.0.json b/schemas/upgrade_schema_1.0.json deleted file mode 100644 index 8096710..0000000 --- a/schemas/upgrade_schema_1.0.json +++ /dev/null @@ -1,69 +0,0 @@ -{ - "title": "Leapp pre-upgrade script schema", - "description": "Script is expected to set up Leapp and run pre-upgrade analysis. This schema defines the output format that is expected by Red Hat Insights Task UI.", - "type": "object", - "properties": { - "alert": { - "type": "boolean" - }, - "error": { - "type": "boolean" - }, - "status": { - "$ref": "#/$defs/status_codes" - }, - "report": { - "type": "string" - }, - "message": { - "type": "string" - }, - "report_json": { - "oneOf": [ - { - "type": "null" - }, - { - "type": "object", - "properties": { - "entries": { - "type": "object" - }, - "tasks_format_version": { - "type": "string" - }, - "tasks_format_id": { - "type": "string" - } - }, - "required": [ - "entries", - "tasks_format_version", - "tasks_format_id" - ] - } - ] - } - }, - "required": [ - "alert", - "error", - "status", - "message", - "report", - "report_json" - ], - "additionalProperties": false, - "$defs": { - "status_codes": { - "description": "The severity of the results and messages", - "type": "string", - "enum": [ - "SUCCESS", - "INFO", - "WARNING", - "ERROR" - ] - } - } - } diff --git a/scripts/leapp_preupgrade.py b/scripts/leapp_preupgrade.py deleted file mode 100644 index f498bd0..0000000 --- a/scripts/leapp_preupgrade.py +++ /dev/null @@ -1,428 +0,0 @@ -import json -import os -import subprocess - - -JSON_REPORT_PATH = "/var/log/leapp/leapp-report.json" -TXT_REPORT_PATH = "/var/log/leapp/leapp-report.txt" - -# Based on https://github.com/oamg/leapp/blob/master/report-schema-v110.json#L211 -STATUS_CODE = { - "high": 3, - "medium": 2, - "low": 1, - "info": 0, -} -STATUS_CODE_NAME_MAP = { - "high": "ERROR", - "medium": "WARNING", - "low": "WARNING", - "info": "INFO", -} - - -# 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 leapp""" - - def __init__(self, message, report): - super(ProcessError, self).__init__(report) - self.message = message - self.report = report - - -class OutputCollector(object): - """Wrapper class for script expected stdout""" - - # pylint: disable=too-many-instance-attributes - # pylint: disable=too-many-arguments - # Nine and six is reasonable in this case. - - def __init__( - self, status="", message="", report="", entries=None, alert=False, error=False - ): - self.status = status - self.alert = alert # true if error true or if pre-upgrade inhibited - - # NOTE: currently false everywhere - # here for consistency with conversions scripts - # expected to change after tasks implement new statuses - self.error = error - - self.message = message - self.report = report - self.tasks_format_version = "1.0" - self.tasks_format_id = "oamg-format" - self.entries = entries - self.report_json = None - - def to_dict(self): - # If we have entries, then we change report_json to be a dictionary - # with the needed values, otherwise, we leave it as `None` to be - # transformed to `null` in json. - if self.entries: - self.report_json = { - "tasks_format_version": self.tasks_format_version, - "tasks_format_id": self.tasks_format_id, - "entries": self.entries, - } - - return { - "status": self.status, - "alert": self.alert, - "error": self.error, - "message": self.message, - "report": self.report, - "report_json": self.report_json, - } - - -def get_rhel_version(): - """Currently we execute the task only for RHEL 7 or 8""" - print("Checking OS distribution and version ID ...") - try: - distribution_id = None - version_id = None - with open("/etc/os-release", "r") as os_release_file: - for line in os_release_file: - if line.startswith("ID="): - distribution_id = line.split("=")[1].strip().strip('"') - elif line.startswith("VERSION_ID="): - version_id = line.split("=")[1].strip().strip('"') - except IOError: - print("Couldn't read /etc/os-release") - return distribution_id, version_id - - -def is_non_eligible_releases(release): - print("Exit if not RHEL 7 or RHEL 8 ...") - major_version, _ = release.split(".") if release is not None else (None, None) - return release is None or major_version not in ["7", "8"] - - -# Code taken from -# https://github.com/oamg/convert2rhel/blob/v1.4.1/convert2rhel/utils.py#L345 -# and modified to adapt the needs of the tools that are being executed in this -# script. -def run_subprocess(cmd, print_cmd=True, env=None, wait=True): - """ - Call the passed command and optionally log the called command - (print_cmd=True) and environment variables in form of dictionary(env=None). - Switching off printing the command can be useful in case it contains a - password in plain text. - - The cmd is specified as a list starting with the command and followed by a - list of arguments. Example: ["yum", "install", ""] - """ - if isinstance(cmd, str): - raise TypeError("cmd should be a list, not a str") - - if print_cmd: - print("Calling command '%s'" % " ".join(cmd)) - - process = subprocess.Popen( - cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, env=env - ) - - output = "" - - if not wait: - return output, None - - for line in iter(process.stdout.readline, b""): - line = line.decode("utf8") - output += line - - # Call wait() to wait for the process to terminate so that we can - # get the return code. - process.wait() - - return output, process.returncode - - -def _check_if_package_installed(pkg_name): - _, return_code = run_subprocess(["/usr/bin/rpm", "-q", pkg_name]) - return return_code == 0 - - -def _get_leapp_command_and_packages(version): - if version.startswith("7"): - leapp_install_command = [ - "/usr/bin/yum", - "install", - "leapp-upgrade", - "-y", - "--enablerepo=rhel-7-server-extras-rpms", - ] - rhui_packages = [ - {"src_pkg": "rh-amazon-rhui-client", "leapp_pkg": "leapp-rhui-aws"}, - { - "src_pkg": "rh-amazon-rhui-client-sap-bundle", - "leapp_pkg": "leapp-rhui-aws-sap-e4s", - }, - {"src_pkg": "rhui-azure-rhel7", "leapp_pkg": "leapp-rhui-azure"}, - { - "src_pkg": "rhui-azure-rhel7-base-sap-apps", - "leapp_pkg": "leapp-rhui-azure-sap", - }, - { - "src_pkg": "rhui-azure-rhel7-base-sap-ha", - "leapp_pkg": "leapp-rhui-azure-sap", - }, - { - "src_pkg": "google-rhui-client-rhel7", - "leapp_pkg": "leapp-rhui-google", - }, - { - "src_pkg": "google-rhui-client-rhel79-sap", - "leapp_pkg": "leapp-rhui-google-sap", - }, - ] - if version.startswith("8"): - leapp_install_command = ["/usr/bin/dnf", "install", "leapp-upgrade", "-y"] - rhui_packages = [ - {"src_pkg": "rh-amazon-rhui-client", "leapp_pkg": "leapp-rhui-aws"}, - { - "src_pkg": "rh-amazon-rhui-client-sap-bundle-e4s", - "leapp_pkg": "leapp-rhui-aws-sap-e4s", - }, - {"src_pkg": "rhui-azure-rhel8", "leapp_pkg": "leapp-rhui-azure"}, - { - "src_pkg": "rhui-azure-rhel8-eus", - "leapp_pkg": "leapp-rhui-azure-eus", - }, - { - "src_pkg": "rhui-azure-rhel8-sap-ha", - "leapp_pkg": "leapp-rhui-azure-sap", - }, - { - "src_pkg": "rhui-azure-rhel8-sapapps", - "leapp_pkg": "leapp-rhui-azure-sap", - }, - { - "src_pkg": "google-rhui-client-rhel8", - "leapp_pkg": "leapp-rhui-google", - }, - { - "src_pkg": "google-rhui-client-rhel8-sap", - "leapp_pkg": "leapp-rhui-google-sap", - }, - ] - return leapp_install_command, rhui_packages - - -def setup_leapp(version): - leapp_install_command, rhel_rhui_packages = _get_leapp_command_and_packages(version) - 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: - if _check_if_package_installed(pkg["src_pkg"]): - pkg["installed"] = True - return [pkg for pkg in rhel_rhui_packages if pkg.get("installed", False)] - - -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 = _check_if_package_installed("subscription-manager") - if rhsm_installed_check: - rhsm_repo_check, _ = run_subprocess( - ["/usr/sbin/subscription-manager", "repos", "--list-enabled"] - ) - rhsm_repo_check_fail = ( - "This system has no repositories available through subscriptions." - in rhsm_repo_check - or "Repositories disabled by configuration." in rhsm_repo_check - ) - - if rhui_installed and not rhsm_repo_check_fail: - print("RHUI packages detected, adding --no-rhsm flag to preupgrade command") - command.append("--no-rhsm") - return True - return False - - -def install_leapp_pkg_corresponding_to_installed_rhui(rhui_pkgs): - print("Installing leapp package corresponding to installed rhui packages") - for pkg in rhui_pkgs: - install_pkg = pkg["leapp_pkg"] - install_output, returncode = run_subprocess( - ["/usr/bin/yum", "install", "-y", install_pkg] - ) - if returncode: - raise ProcessError( - message="Installation of %s (coresponding pkg to '%s') failed", - report="Installation of %s (coresponding pkg to '%s') failed with exit code %s and output: %s." - % (install_pkg, pkg, returncode, install_output.rstrip("\n")), - ) - - -def remove_previous_reports(): - print("Removing previous preupgrade reports at /var/log/leapp/leapp-report.* ...") - - if os.path.exists(JSON_REPORT_PATH): - os.remove(JSON_REPORT_PATH) - - if os.path.exists(TXT_REPORT_PATH): - os.remove(TXT_REPORT_PATH) - - -def execute_preupgrade(command): - print("Executing preupgrade ...") - _, _ = run_subprocess(command) - - # NOTE: we do not care about returncode because non-null always means actor error (or leapp error) - # if returncode: - # print( - # "The process leapp exited with code '%s' and output: %s\n" - # % (returncode, output) - # ) - # raise ProcessError(message="Leapp exited with code '%s'." % returncode) - - -def _find_highest_report_level(entries): - """ - Gather status codes from entries. - """ - print("Collecting and combining report status.") - action_level_combined = [] - for value in entries: - action_level_combined.append(value["severity"]) - - valid_action_levels = [ - level for level in action_level_combined if level in STATUS_CODE - ] - valid_action_levels.sort(key=lambda status: STATUS_CODE[status], reverse=True) - return STATUS_CODE_NAME_MAP[valid_action_levels[0]] - - -def parse_results(output): - print("Processing preupgrade results ...") - - report_json = "Not found" - message = "Can't open json report at " + JSON_REPORT_PATH - alert = True - status = "ERROR" - - print("Reading JSON report") - if os.path.exists(JSON_REPORT_PATH): - with open(JSON_REPORT_PATH, mode="r") as handler: - report_json = json.load(handler) - - report_entries = report_json.get("entries", []) - error_count = len( - [entry for entry in report_entries if "error" in entry.get("groups")] - ) - inhibitor_count = len( - [entry for entry in report_entries if "inhibitor" in entry.get("groups")] - ) - message = ( - "Your system has %s error%s and %s inhibitor%s out of %s potential problem%s." - % ( - error_count, - "" if error_count == 1 else "s", - inhibitor_count, - "" if inhibitor_count == 1 else "s", - len(report_entries), - "" if len(report_entries) == 1 else "s", - ) - ) - alert = inhibitor_count > 0 or error_count > 0 - status = ( - _find_highest_report_level(report_entries) - if len(report_entries) > 0 - else "SUCCESS" - ) - - output.status = status - output.report_json = report_json - output.alert = alert - output.message = message - - print("Reading TXT report") - report_txt = "Not found" - if os.path.exists(TXT_REPORT_PATH): - with open(JSON_REPORT_PATH, mode="r") as handler: - report_txt = handler.read() - - output.report = report_txt - - -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(): - try: - # Exit if not RHEL 7 or 8 - dist, version = get_rhel_version() - if dist != "rhel" or is_non_eligible_releases(version): - raise ProcessError( - message="In-place upgrades are supported only on RHEL distributions.", - report='Exiting because distribution="%s" and version="%s"' - % (dist, version), - ) - - output = OutputCollector() - preupgrade_command = ["/usr/bin/leapp", "preupgrade", "--report-schema=1.2.0"] - rhui_pkgs = setup_leapp(version) - - # Check for RHUI PKGs - use_no_rhsm = should_use_no_rhsm_check(len(rhui_pkgs) > 1, preupgrade_command) - if use_no_rhsm: - install_leapp_pkg_corresponding_to_installed_rhui(rhui_pkgs) - - remove_previous_reports() - execute_preupgrade(preupgrade_command) - parse_results(output) - update_insights_inventory() - print("Pre-upgrade successfully executed.") - except ProcessError as exception: - print(exception.report) - output = OutputCollector( - status="ERROR", - alert=True, - error=False, - message=exception.message, - report=exception.report, - ) - except Exception as exception: - print(str(exception)) - output = OutputCollector( - status="ERROR", - alert=True, - error=False, - message="An unexpected error occurred. Expand the row for more details.", - report=str(exception), - ) - finally: - print("### JSON START ###") - print(json.dumps(output.to_dict(), indent=4)) - print("### JSON END ###") - - -if __name__ == "__main__": - main() diff --git a/scripts/leapp_upgrade.py b/scripts/leapp_script.py similarity index 86% rename from scripts/leapp_upgrade.py rename to scripts/leapp_script.py index b9e80f7..0925b99 100644 --- a/scripts/leapp_upgrade.py +++ b/scripts/leapp_script.py @@ -2,7 +2,11 @@ import os import subprocess - +# SCRIPT_TYPE is either 'PREUPGRADE' or 'UPGRADE' +# Value is set in signed yaml envelope in content_vars (RHC_WORKER_LEAPP_SCRIPT_TYPE) +SCRIPT_TYPE = os.environ.get("RHC_WORKER_LEAPP_SCRIPT_TYPE", "None") +IS_UPGRADE = SCRIPT_TYPE == "UPGRADE" +IS_PREUPGRADE = SCRIPT_TYPE == "PREUPGRADE" JSON_REPORT_PATH = "/var/log/leapp/leapp-report.json" TXT_REPORT_PATH = "/var/log/leapp/leapp-report.txt" REBOOT_GUIDANCE_MESSAGE = "A reboot is required to continue. Please reboot your system." @@ -97,11 +101,10 @@ def get_rhel_version(): def is_non_eligible_releases(release): - print("Exit if not RHEL 7.9 or 8.4") - eligible_releases = ["7.9", "8.4"] - major_version, minor = release.split(".") if release is not None else (None, None) - version_str = major_version + "." + minor - return release is None or version_str not in eligible_releases + """Check if the release is eligible for upgrade or preupgrade.""" + print("Exit if not RHEL 7 or RHEL 8 ...") + major_version, _ = release.split(".") if release is not None else (None, None) + return release is None or major_version not in ["7", "8"] # Code taken from @@ -251,7 +254,11 @@ def should_use_no_rhsm_check(rhui_installed, command): ) if rhui_installed and not rhsm_repo_check_fail: - print("RHUI packages detected, adding --no-rhsm flag to preupgrade command") + print( + "RHUI packages detected, adding --no-rhsm flag to {} command".format( + SCRIPT_TYPE.title() + ) + ) command.append("--no-rhsm") return True return False @@ -273,7 +280,7 @@ def install_leapp_pkg_corresponding_to_installed_rhui(rhui_pkgs): def remove_previous_reports(): - print("Removing previous preupgrade reports at /var/log/leapp/leapp-report.* ...") + print("Removing previous leapp reports at /var/log/leapp/leapp-report.* ...") if os.path.exists(JSON_REPORT_PATH): os.remove(JSON_REPORT_PATH) @@ -282,20 +289,12 @@ def remove_previous_reports(): os.remove(TXT_REPORT_PATH) -def execute_upgrade(command): - print("Executing upgrade ...") +def execute_operation(command): + print("Executing {} ...".format(SCRIPT_TYPE.title())) output, _ = run_subprocess(command) return output - # NOTE: we do not care about returncode because non-null always means actor error (or leapp error) - # if returncode: - # print( - # "The process leapp exited with code '%s' and output: %s\n" - # % (returncode, output) - # ) - # raise ProcessError(message="Leapp exited with code '%s'." % returncode) - def _find_highest_report_level(entries): """ @@ -314,7 +313,7 @@ def _find_highest_report_level(entries): def parse_results(output, reboot_required=False): - print("Processing upgrade results ...") + print("Processing {} results ...".format(SCRIPT_TYPE.title())) report_json = "Not found" message = "Can't open json report at " + JSON_REPORT_PATH @@ -368,17 +367,16 @@ def parse_results(output, reboot_required=False): output.report = report_txt -def update_insights_inventory(): +def update_insights_inventory(output): """Call insights-client to update insights inventory.""" print("Updating system status in Red Hat Insights.") - output, returncode = run_subprocess(["/usr/bin/insights-client"]) + _, 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 registration failed with exit code %s." % returncode) + output.message += " Failed to update Insights Inventory." + output.alert = True + return print("System registered with insights-client successfully.") @@ -390,7 +388,15 @@ def reboot_system(): def main(): try: - # Exit if not RHEL 7.9 or 8.4 + # Exit if invalid value for SCRIPT_TYPE + if SCRIPT_TYPE not in ["PREUPGRADE", "UPGRADE"]: + raise ProcessError( + message="Allowed values for RHC_WORKER_LEAPP_SCRIPT_TYPE are 'PREUPGRADE' and 'UPGRADE'.", + report="Exiting because RHC_WORKER_LEAPP_SCRIPT_TYPE='%s'" + % SCRIPT_TYPE, + ) + + # Exit if not eligible release dist, version = get_rhel_version() if dist != "rhel" or is_non_eligible_releases(version): raise ProcessError( @@ -400,20 +406,22 @@ def main(): ) output = OutputCollector() + preupgrade_command = ["/usr/bin/leapp", "preupgrade", "--report-schema=1.2.0"] upgrade_command = ["/usr/bin/leapp", "upgrade", "--report-schema=1.2.0"] + operation_command = preupgrade_command if IS_PREUPGRADE else upgrade_command rhui_pkgs = setup_leapp(version) # Check for RHUI PKGs - use_no_rhsm = should_use_no_rhsm_check(len(rhui_pkgs) > 1, upgrade_command) + use_no_rhsm = should_use_no_rhsm_check(len(rhui_pkgs) > 1, operation_command) if use_no_rhsm: install_leapp_pkg_corresponding_to_installed_rhui(rhui_pkgs) remove_previous_reports() - leapp_upgrade_output = execute_upgrade(upgrade_command) - reboot_required = REBOOT_GUIDANCE_MESSAGE in leapp_upgrade_output + leapp_output = execute_operation(operation_command) + reboot_required = REBOOT_GUIDANCE_MESSAGE in leapp_output parse_results(output, reboot_required) - update_insights_inventory() - print("Leapp upgrade command successfully executed.") + update_insights_inventory(output) + print("Operation {} finished successfully.".format(SCRIPT_TYPE.title())) if reboot_required: reboot_system() except ProcessError as exception: diff --git a/tests/preupgrade/__init__.py b/tests/__init__.py similarity index 100% rename from tests/preupgrade/__init__.py rename to tests/__init__.py diff --git a/tests/preupgrade/test_check_pkg_installed.py b/tests/preupgrade/test_check_pkg_installed.py deleted file mode 100644 index 2383681..0000000 --- a/tests/preupgrade/test_check_pkg_installed.py +++ /dev/null @@ -1,13 +0,0 @@ -from mock import patch - -from scripts.leapp_preupgrade import _check_if_package_installed - - -@patch("scripts.leapp_preupgrade.run_subprocess") -def test_check_if_package_installed(mock_run_subprocess): - pkg_name = "example-package" - mock_run_subprocess.return_value = ("", 0) - result = _check_if_package_installed(pkg_name) - expected_command = ["/usr/bin/rpm", "-q", pkg_name] - mock_run_subprocess.assert_called_once_with(expected_command) - assert result diff --git a/tests/preupgrade/test_collect_report_level.py b/tests/preupgrade/test_collect_report_level.py deleted file mode 100644 index bac5bc8..0000000 --- a/tests/preupgrade/test_collect_report_level.py +++ /dev/null @@ -1,57 +0,0 @@ -import pytest -from scripts.leapp_preupgrade import ( - _find_highest_report_level, -) - - -@pytest.mark.parametrize( - ("entries", "expected"), - ( - ( - [ - { - "severity": "high", - }, - { - "severity": "info", - }, - ], - "ERROR", - ), - ( - [ - { - "severity": "info", - } - ], - "INFO", - ), - ( - [ - { - "severity": "medium", - }, - { - "severity": "info", - }, - ], - "WARNING", - ), - ), -) -def test_find_highest_report_level_expected(entries, expected): - """Should be sorted descending from the highest status to the lower one.""" - result = _find_highest_report_level(entries) - assert result == expected - - -def test_find_highest_report_level_unknown_status(): - """Should ignore unknown statuses in report""" - expected_output = "WARNING" - - action_results_test = [ - {"severity": "medium"}, - {"severity": "foo"}, - ] - result = _find_highest_report_level(action_results_test) - assert result == expected_output diff --git a/tests/preupgrade/test_get_rhel_version.py b/tests/preupgrade/test_get_rhel_version.py deleted file mode 100644 index 0143ba3..0000000 --- a/tests/preupgrade/test_get_rhel_version.py +++ /dev/null @@ -1,20 +0,0 @@ -from mock import patch, mock_open -from scripts.leapp_preupgrade import get_rhel_version - - -@patch("__builtin__.open", mock_open(read_data='ID="rhel"\nVERSION_ID="7.9"\n')) -def test_get_rhel_version_with_existing_file(): - distribution_id, version_id = get_rhel_version() - - assert distribution_id == "rhel" - assert version_id == "7.9" - - -@patch("__builtin__.open") -def test_get_rhel_version_with_missing_file(mock_open_file): - mock_open_file.side_effect = IOError("Couldn't read /etc/os-release") - - distribution_id, version_id = get_rhel_version() - - assert distribution_id is None - assert version_id is None diff --git a/tests/preupgrade/test_install_rhui_pkgs.py b/tests/preupgrade/test_install_rhui_pkgs.py deleted file mode 100644 index f91ae35..0000000 --- a/tests/preupgrade/test_install_rhui_pkgs.py +++ /dev/null @@ -1,38 +0,0 @@ -import pytest -from mock import patch, call -from scripts.leapp_preupgrade import ( - ProcessError, - install_leapp_pkg_corresponding_to_installed_rhui, -) - - -@patch("scripts.leapp_preupgrade.run_subprocess") -def test_install_leapp_pkg_to_installed_rhui(mock_run_subprocess): - rhui_pkgs = [{"leapp_pkg": "pkg1"}, {"leapp_pkg": "pkg2"}] - - mock_run_subprocess.return_value = ("Installation successful", 0) - - install_leapp_pkg_corresponding_to_installed_rhui(rhui_pkgs) - - for pkg in rhui_pkgs: - expected_command = ["/usr/bin/yum", "install", "-y", pkg["leapp_pkg"]] - assert call(expected_command) in mock_run_subprocess.call_args_list - - -@patch("scripts.leapp_preupgrade.run_subprocess") -def test_install_leapp_pkg_to_installed_rhui_error( - mock_run_subprocess, -): - rhui_pkgs = [{"leapp_pkg": "pkg1"}, {"leapp_pkg": "pkg2"}] - mock_run_subprocess.return_value = ("Installation failed", 1) - - with pytest.raises(ProcessError) as exception: - install_leapp_pkg_corresponding_to_installed_rhui(rhui_pkgs) - - expected_command = ["/usr/bin/yum", "install", "-y", "pkg1"] - mock_run_subprocess.assert_called_once_with(expected_command) - - assert ( - str(exception.value) - == "Installation of pkg1 (coresponding pkg to '{'leapp_pkg': 'pkg1'}') failed with exit code 1 and output: Installation failed." - ) diff --git a/tests/preupgrade/test_main.py b/tests/preupgrade/test_main.py deleted file mode 100644 index 4be2e56..0000000 --- a/tests/preupgrade/test_main.py +++ /dev/null @@ -1,66 +0,0 @@ -from mock import patch -from scripts.leapp_preupgrade import main, OutputCollector - - -@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.update_insights_inventory") -@patch("scripts.leapp_preupgrade.OutputCollector") -def test_main_non_eligible_release( - mock_output_collector, - mock_update_insights_inventory, - mock_setup_leapp, - mock_is_non_eligible_releases, - mock_get_rhel_version, -): - mock_get_rhel_version.return_value = ("rhel", "6.9") - mock_is_non_eligible_releases.return_value = True - mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) - - main() - - mock_get_rhel_version.assert_called_once() - mock_is_non_eligible_releases.assert_called_once() - mock_output_collector.assert_called_once() - mock_setup_leapp.assert_not_called() - mock_update_insights_inventory.assert_not_called() - - -@patch("scripts.leapp_preupgrade.parse_results") -@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.should_use_no_rhsm_check") -@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.update_insights_inventory") -@patch("scripts.leapp_preupgrade.OutputCollector") -def test_main_eligible_release( - mock_output_collector, - mock_update_insights_inventory, - mock_execute_preupgrade, - mock_remove_previous_reports, - mock_should_use_no_rhsm_check, - mock_install_rhui, - mock_setup_leapp, - mock_is_non_eligible_releases, - mock_get_rhel_version, - mock_parse_results, -): - mock_get_rhel_version.return_value = ("rhel", "7.9") - mock_is_non_eligible_releases.return_value = False - mock_setup_leapp.return_value = [{"leapp_pkg": "to_install"}] - mock_should_use_no_rhsm_check.return_value = True - mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) - - main() - - mock_setup_leapp.assert_called_once() - mock_should_use_no_rhsm_check.assert_called_once() - mock_install_rhui.assert_called_once() - mock_remove_previous_reports.assert_called_once() - mock_execute_preupgrade.assert_called_once() - mock_parse_results.assert_called_once() - mock_update_insights_inventory.assert_called_once() diff --git a/tests/preupgrade/test_non_eligible_release.py b/tests/preupgrade/test_non_eligible_release.py deleted file mode 100644 index 2c66a1d..0000000 --- a/tests/preupgrade/test_non_eligible_release.py +++ /dev/null @@ -1,12 +0,0 @@ -from scripts.leapp_preupgrade import is_non_eligible_releases - - -def test_is_non_eligible_releases(): - eligible_releases = ["7.9", "8.4"] - non_eligible_releases = ["6.10", "9.0", "10.0"] - - for release in eligible_releases: - assert not is_non_eligible_releases(release) - - for release in non_eligible_releases: - assert is_non_eligible_releases(release) diff --git a/tests/preupgrade/test_parse_results.py b/tests/preupgrade/test_parse_results.py deleted file mode 100644 index d21173c..0000000 --- a/tests/preupgrade/test_parse_results.py +++ /dev/null @@ -1,43 +0,0 @@ -from mock import mock_open, patch - -from scripts.leapp_preupgrade import ( - parse_results, - OutputCollector, -) - - -@patch("os.path.exists", return_value=True) -@patch("scripts.leapp_preupgrade._find_highest_report_level", return_value="ERROR") -def test_gather_report_files_exist(mock_find_level, mock_exists): - test_txt_content = "Test data" - test_json_content = '{"entries": [{"groups": ["error"]}]}' - output = OutputCollector() - with patch("__builtin__.open") as mock_open_reports: - return_values = [test_json_content, test_txt_content] - mock_open_reports.side_effect = lambda file, mode: mock_open( - read_data=return_values.pop(0) - )(file, mode) - parse_results(output) - - assert mock_find_level.call_count == 1 # entries do not exists -> [] - assert output.status == "ERROR" - assert mock_exists.call_count == 2 - assert output.report == test_txt_content - assert output.report_json.get("entries") is not None - assert ( - output.message - == "Your system has 1 error and 0 inhibitors out of 1 potential problem." - ) - - -@patch("os.path.exists", return_value=False) -def test_gather_report_files_not_exist(mock_exists): - output = OutputCollector() - with patch("__builtin__.open") as mock_open_reports: - parse_results(output) - mock_open_reports.assert_not_called() - - assert mock_exists.call_count == 2 - assert output.report != "" - assert output.report_json != "" - assert output.message != "" diff --git a/tests/preupgrade/test_remove_reports.py b/tests/preupgrade/test_remove_reports.py deleted file mode 100644 index 615698d..0000000 --- a/tests/preupgrade/test_remove_reports.py +++ /dev/null @@ -1,23 +0,0 @@ -from mock import patch -from scripts.leapp_preupgrade import ( - remove_previous_reports, -) - - -@patch("os.path.exists", side_effect=[True, True]) -@patch("os.remove") -def test_remove_previous_reports_with_files(mock_remove, _): - json_report_path = "/var/log/leapp/leapp-report.json" - text_report_path = "/var/log/leapp/leapp-report.txt" - - remove_previous_reports() - - mock_remove.assert_any_call(json_report_path) - mock_remove.assert_any_call(text_report_path) - - -@patch("os.path.exists", side_effect=[False, False]) -@patch("os.remove") -def test_remove_previous_reports_without_files(mock_remove, _): - remove_previous_reports() - mock_remove.assert_not_called() diff --git a/tests/preupgrade/test_run.py b/tests/preupgrade/test_run.py deleted file mode 100644 index 3afe73c..0000000 --- a/tests/preupgrade/test_run.py +++ /dev/null @@ -1,10 +0,0 @@ -from mock import patch - -from scripts.leapp_preupgrade import execute_preupgrade - - -@patch("scripts.leapp_preupgrade.run_subprocess", return_value=(b"", 0)) -def test_run_leapp_preupgrade(mock_popen): - execute_preupgrade(["fake command"]) - - mock_popen.assert_called_once_with(["fake command"]) diff --git a/tests/preupgrade/test_run_subprocess.py b/tests/preupgrade/test_run_subprocess.py deleted file mode 100644 index 82184c3..0000000 --- a/tests/preupgrade/test_run_subprocess.py +++ /dev/null @@ -1,97 +0,0 @@ -# Taken from https://github.com/oamg/convert2rhel-worker-scripts/blob/main/tests/preconversion_assessment/test_misc.py -import subprocess -import pytest -from mock import patch - -from scripts.leapp_preupgrade import run_subprocess - - -class SubprocessMock(object): - def __init__(self, output, returncode): - self.call_count = 0 - self.output = output - self.returncode = returncode - - def __call__(self, args, stdout, stderr, bufsize): - return self - - @property - def stdout(self): - return self - - def readline(self): - try: - next_line = self.output[self.call_count] - except IndexError: - return b"" - - self.call_count += 1 - return next_line - - def wait(self): - pass - - -def test_run_subprocess_cmd_as_list(): - cmd = "echo 'test'" - - with pytest.raises(TypeError, match="cmd should be a list, not a str"): - run_subprocess(cmd) - - -@pytest.mark.parametrize( - ("subprocess_out", "expected"), - ( - (("output".encode("utf-8"), 0), ("output", 0)), - (("output".encode("utf-8"), 1), ("output", 1)), - ), -) -def test_run_subprocess(subprocess_out, expected): - with patch( - "subprocess.Popen", - return_value=SubprocessMock(subprocess_out[0], subprocess_out[1]), - ) as mocked_subprocess: - output, returncode = run_subprocess(["test", "hi"], print_cmd=True) - - assert (output, returncode) == expected - mocked_subprocess.assert_called_once_with( - ["test", "hi"], - bufsize=1, - env=None, - stderr=subprocess.STDOUT, - stdout=subprocess.PIPE, - ) - - -def test_run_subprocess_custom_env(): - with patch( - "subprocess.Popen", return_value=SubprocessMock("output".encode("utf-8"), 0) - ) as mocked_subprocess: - output, returncode = run_subprocess( - ["test", "hi"], print_cmd=False, env={"PATH": "/fake_path"} - ) - - assert (output, returncode) == ("output", 0) - mocked_subprocess.assert_called_once_with( - ["test", "hi"], - env={"PATH": "/fake_path"}, - bufsize=1, - stderr=subprocess.STDOUT, - stdout=subprocess.PIPE, - ) - - -def test_run_subprocess_custom_not_wait(): - with patch( - "subprocess.Popen", return_value=SubprocessMock("output".encode("utf-8"), 0) - ) as mocked_subprocess: - output, returncode = run_subprocess(["test", "hi"], wait=False) - - assert (output, returncode) == ("", None) - mocked_subprocess.assert_called_once_with( - ["test", "hi"], - env=None, - bufsize=1, - stderr=subprocess.STDOUT, - stdout=subprocess.PIPE, - ) diff --git a/tests/preupgrade/test_schema.py b/tests/preupgrade/test_schema.py deleted file mode 100644 index 20d47d2..0000000 --- a/tests/preupgrade/test_schema.py +++ /dev/null @@ -1,34 +0,0 @@ -import json -import jsonschema -from scripts.leapp_preupgrade import OutputCollector, STATUS_CODE_NAME_MAP - - -def test_output_schema(): - """Test that pre-upgrade report is wrapped in expected json""" - for status in STATUS_CODE_NAME_MAP.values(): - output_collector = OutputCollector(status=status) - empty_output = output_collector.to_dict() - - with open("schemas/preupgrade_schema_1.0.json", "r") as schema: - schema_json = json.load(schema) - # If some difference between generated json and its schema invoke exception - jsonschema.validate(instance=empty_output, schema=schema_json) - assert not empty_output["alert"] - assert not empty_output["error"] - - -def test_output_schema_entries_report(): - """ - Test that pre-upgrade report is wrapped in expected json with entries - key. - """ - output_collector = OutputCollector(status="WARNING") - # Not close to a real json returned by the assessment report, but we don't - # have to check that here. - output_collector.entries = {"hi": "world"} - full_output = output_collector.to_dict() - - with open("schemas/preupgrade_schema_1.0.json", "r") as schema: - schema_json = json.load(schema) - # If some difference between generated json and its schema invoke exception - jsonschema.validate(instance=full_output, schema=schema_json) diff --git a/tests/preupgrade/test_setup_leapp.py b/tests/preupgrade/test_setup_leapp.py deleted file mode 100644 index bc6c6a7..0000000 --- a/tests/preupgrade/test_setup_leapp.py +++ /dev/null @@ -1,60 +0,0 @@ -import pytest -from mock import patch -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, installed): - mock_run_subprocess.return_value = ("Installation successful", 0) - mock_check_if_package_installed.return_value = installed - - rhel7_command = [ - "/usr/bin/yum", - "install", - "leapp-upgrade", - "-y", - "--enablerepo=rhel-7-server-extras-rpms", - ] - rhel8_command = ["/usr/bin/dnf", "install", "leapp-upgrade", "-y"] - - for version, command in [("7", rhel7_command), ("8", rhel8_command)]: - result = setup_leapp(version) - 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 = False - - rhel7_command = [ - "/usr/bin/yum", - "install", - "leapp-upgrade", - "-y", - "--enablerepo=rhel-7-server-extras-rpms", - ] - rhel8_command = ["/usr/bin/dnf", "install", "leapp-upgrade", "-y"] - - for version, command in [("7", rhel7_command), ("8", rhel8_command)]: - with pytest.raises(ProcessError) as e_info: - setup_leapp(version) - - mock_run_subprocess.assert_called_with(command) - assert ( - str(e_info.value) - == "Installation of leapp failed with code '1' and output: Installation failed." - ) diff --git a/tests/preupgrade/test_should_use_rhsm.py b/tests/preupgrade/test_should_use_rhsm.py deleted file mode 100644 index 7ca3250..0000000 --- a/tests/preupgrade/test_should_use_rhsm.py +++ /dev/null @@ -1,50 +0,0 @@ -from mock import patch -from scripts.leapp_preupgrade import should_use_no_rhsm_check - - -@patch("scripts.leapp_preupgrade.run_subprocess") -def test_should_use_no_rhsm_rhsm_and_rhui_installed( - mock_run_subprocess, -): - mock_run_subprocess.side_effect = [ - ("/path/to/subscription-manager\n", 0), - ("output_of_subscription_manager_repos_command", 0), - ] - - rhui_installed = True - command = ["preupgrade"] - result = should_use_no_rhsm_check(rhui_installed, command) - - mock_run_subprocess.call_count = 2 - assert result - - -@patch("scripts.leapp_preupgrade.run_subprocess") -def test_should_use_no_rhsm_rhsm_installed_rhui_not( - mock_run_subprocess, -): - mock_run_subprocess.side_effect = [ - ("/path/to/subscription-manager\n", 0), - ("output_of_subscription_manager_repos_command", 0), - ] - - rhui_installed = False - command = ["preupgrade"] - result = should_use_no_rhsm_check(rhui_installed, command) - - mock_run_subprocess.call_count = 2 - assert not result - - -@patch("scripts.leapp_preupgrade.run_subprocess") -def test_should_use_no_rhsm_rhsm_not_installed(mock_run_subprocess): - mock_run_subprocess.side_effect = [ - ("error_message", 1), - ] - - rhui_installed = True - command = ["preupgrade"] - result = should_use_no_rhsm_check(rhui_installed, command) - - mock_run_subprocess.assert_called_once() - assert not result diff --git a/tests/preupgrade/test_update_insights_inventory.py b/tests/preupgrade/test_update_insights_inventory.py deleted file mode 100644 index cb1d645..0000000 --- a/tests/preupgrade/test_update_insights_inventory.py +++ /dev/null @@ -1,24 +0,0 @@ -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"]) diff --git a/tests/upgrade/test_check_pkg_installed.py b/tests/test_check_pkg_installed.py similarity index 76% rename from tests/upgrade/test_check_pkg_installed.py rename to tests/test_check_pkg_installed.py index 1f5580c..0197f8c 100644 --- a/tests/upgrade/test_check_pkg_installed.py +++ b/tests/test_check_pkg_installed.py @@ -1,9 +1,9 @@ from mock import patch -from scripts.leapp_upgrade import _check_if_package_installed +from scripts.leapp_script import _check_if_package_installed -@patch("scripts.leapp_upgrade.run_subprocess") +@patch("scripts.leapp_script.run_subprocess") def test_check_if_package_installed(mock_run_subprocess): pkg_name = "example-package" mock_run_subprocess.return_value = ("", 0) diff --git a/tests/upgrade/test_collect_report_level.py b/tests/test_collect_report_level.py similarity index 97% rename from tests/upgrade/test_collect_report_level.py rename to tests/test_collect_report_level.py index a0d6ac7..0c81c3c 100644 --- a/tests/upgrade/test_collect_report_level.py +++ b/tests/test_collect_report_level.py @@ -1,5 +1,5 @@ import pytest -from scripts.leapp_upgrade import ( +from scripts.leapp_script import ( _find_highest_report_level, ) diff --git a/tests/upgrade/test_get_rhel_version.py b/tests/test_get_rhel_version.py similarity index 91% rename from tests/upgrade/test_get_rhel_version.py rename to tests/test_get_rhel_version.py index 8abf1ab..2ead088 100644 --- a/tests/upgrade/test_get_rhel_version.py +++ b/tests/test_get_rhel_version.py @@ -1,5 +1,5 @@ from mock import patch, mock_open -from scripts.leapp_upgrade import get_rhel_version +from scripts.leapp_script import get_rhel_version @patch("__builtin__.open", mock_open(read_data='ID="rhel"\nVERSION_ID="7.9"\n')) diff --git a/tests/upgrade/test_install_rhui_pkgs.py b/tests/test_install_rhui_pkgs.py similarity index 90% rename from tests/upgrade/test_install_rhui_pkgs.py rename to tests/test_install_rhui_pkgs.py index b83a7a3..c00c870 100644 --- a/tests/upgrade/test_install_rhui_pkgs.py +++ b/tests/test_install_rhui_pkgs.py @@ -1,12 +1,12 @@ import pytest from mock import patch, call -from scripts.leapp_upgrade import ( +from scripts.leapp_script import ( ProcessError, install_leapp_pkg_corresponding_to_installed_rhui, ) -@patch("scripts.leapp_upgrade.run_subprocess") +@patch("scripts.leapp_script.run_subprocess") def test_install_leapp_pkg_to_installed_rhui(mock_run_subprocess): rhui_pkgs = [{"leapp_pkg": "pkg1"}, {"leapp_pkg": "pkg2"}] @@ -19,7 +19,7 @@ def test_install_leapp_pkg_to_installed_rhui(mock_run_subprocess): assert call(expected_command) in mock_run_subprocess.call_args_list -@patch("scripts.leapp_upgrade.run_subprocess") +@patch("scripts.leapp_script.run_subprocess") def test_install_leapp_pkg_to_installed_rhui_error( mock_run_subprocess, ): diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..2acaa6b --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,193 @@ +from mock import patch +from scripts.leapp_script import ( + main, + OutputCollector, + REBOOT_GUIDANCE_MESSAGE, +) + + +@patch("scripts.leapp_script.SCRIPT_TYPE", "PREUPGRADE") +@patch("scripts.leapp_script.IS_PREUPGRADE", True) +@patch("scripts.leapp_script.get_rhel_version") +@patch("scripts.leapp_script.is_non_eligible_releases") +@patch("scripts.leapp_script.setup_leapp") +@patch("scripts.leapp_script.update_insights_inventory") +@patch("scripts.leapp_script.OutputCollector") +def test_main_non_eligible_release_preupgrade( + mock_output_collector, + mock_update_insights_inventory, + mock_setup_leapp, + mock_is_non_eligible_releases, + mock_get_rhel_version, +): + mock_get_rhel_version.return_value = ("rhel", "6.9") + mock_is_non_eligible_releases.return_value = True + mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) + + main() + + mock_get_rhel_version.assert_called_once() + mock_is_non_eligible_releases.assert_called_once() + mock_output_collector.assert_called_once() + mock_setup_leapp.assert_not_called() + mock_update_insights_inventory.assert_not_called() + + +@patch("scripts.leapp_script.SCRIPT_TYPE", "PREUPGRADE") +@patch("scripts.leapp_script.IS_PREUPGRADE", True) +@patch("scripts.leapp_script.parse_results") +@patch("scripts.leapp_script.get_rhel_version") +@patch("scripts.leapp_script.is_non_eligible_releases") +@patch("scripts.leapp_script.setup_leapp") +@patch("scripts.leapp_script.should_use_no_rhsm_check") +@patch("scripts.leapp_script.install_leapp_pkg_corresponding_to_installed_rhui") +@patch("scripts.leapp_script.remove_previous_reports") +@patch("scripts.leapp_script.execute_operation") +@patch("scripts.leapp_script.update_insights_inventory") +@patch("scripts.leapp_script.OutputCollector") +def test_main_eligible_release_preupgrade( + mock_output_collector, + mock_update_insights_inventory, + mock_execute_operation, + mock_remove_previous_reports, + mock_should_use_no_rhsm_check, + mock_install_rhui, + mock_setup_leapp, + mock_is_non_eligible_releases, + mock_get_rhel_version, + mock_parse_results, +): + mock_get_rhel_version.return_value = ("rhel", "7.9") + mock_is_non_eligible_releases.return_value = False + mock_setup_leapp.return_value = [{"leapp_pkg": "to_install"}] + mock_should_use_no_rhsm_check.return_value = True + mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) + + main() + + mock_setup_leapp.assert_called_once() + mock_should_use_no_rhsm_check.assert_called_once() + mock_install_rhui.assert_called_once() + mock_remove_previous_reports.assert_called_once() + mock_execute_operation.assert_called_once() + mock_parse_results.assert_called_once() + mock_update_insights_inventory.assert_called_once() + + +@patch("scripts.leapp_script.SCRIPT_TYPE", "UPGRADE") +@patch("scripts.leapp_script.IS_UPGRADE", True) +@patch("scripts.leapp_script.get_rhel_version") +@patch("scripts.leapp_script.is_non_eligible_releases") +@patch("scripts.leapp_script.setup_leapp") +@patch("scripts.leapp_script.update_insights_inventory") +@patch("scripts.leapp_script.OutputCollector") +def test_main_non_eligible_release_upgrade( + mock_output_collector, + mock_update_insights_inventory, + mock_setup_leapp, + mock_is_non_eligible_releases, + mock_get_rhel_version, +): + mock_get_rhel_version.return_value = ("rhel", "6.9") + mock_is_non_eligible_releases.return_value = True + mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) + + main() + + mock_get_rhel_version.assert_called_once() + mock_is_non_eligible_releases.assert_called_once() + mock_output_collector.assert_called_once() + mock_setup_leapp.assert_not_called() + mock_update_insights_inventory.assert_not_called() + + +@patch("scripts.leapp_script.SCRIPT_TYPE", "UPGRADE") +@patch("scripts.leapp_script.IS_UPGRADE", True) +@patch("scripts.leapp_script.parse_results") +@patch("scripts.leapp_script.reboot_system") +@patch("scripts.leapp_script.get_rhel_version") +@patch("scripts.leapp_script.is_non_eligible_releases") +@patch("scripts.leapp_script.setup_leapp") +@patch("scripts.leapp_script.should_use_no_rhsm_check") +@patch("scripts.leapp_script.install_leapp_pkg_corresponding_to_installed_rhui") +@patch("scripts.leapp_script.remove_previous_reports") +@patch("scripts.leapp_script.execute_operation") +@patch("scripts.leapp_script.update_insights_inventory") +@patch("scripts.leapp_script.OutputCollector") +def test_main_eligible_release_upgrade( + mock_output_collector, + mock_update_insights_inventory, + mock_execute_operation, + mock_remove_previous_reports, + mock_should_use_no_rhsm_check, + mock_install_rhui, + mock_setup_leapp, + mock_is_non_eligible_releases, + mock_get_rhel_version, + mock_reboot_system, + mock_parse_results, +): + mock_get_rhel_version.return_value = ("rhel", "7.9") + mock_is_non_eligible_releases.return_value = False + mock_setup_leapp.return_value = [{"leapp_pkg": "to_install"}] + mock_should_use_no_rhsm_check.return_value = True + mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) + mock_execute_operation.return_value = ( + "LOREM IPSUM\nTEST" + REBOOT_GUIDANCE_MESSAGE + "TEST\nDOLOR SIT AMET" + ) + + main() + + mock_setup_leapp.assert_called_once() + mock_should_use_no_rhsm_check.assert_called_once() + mock_install_rhui.assert_called_once() + mock_remove_previous_reports.assert_called_once() + mock_execute_operation.assert_called_once() + mock_parse_results.assert_called_once() + mock_update_insights_inventory.assert_called_once() + mock_reboot_system.assert_called_once() + + +@patch("scripts.leapp_script.SCRIPT_TYPE", "UPGRADE") +@patch("scripts.leapp_script.IS_UPGRADE", True) +@patch("scripts.leapp_script.parse_results") +@patch("scripts.leapp_script.reboot_system") +@patch("scripts.leapp_script.get_rhel_version") +@patch("scripts.leapp_script.is_non_eligible_releases") +@patch("scripts.leapp_script.setup_leapp") +@patch("scripts.leapp_script.should_use_no_rhsm_check") +@patch("scripts.leapp_script.install_leapp_pkg_corresponding_to_installed_rhui") +@patch("scripts.leapp_script.remove_previous_reports") +@patch("scripts.leapp_script.execute_operation") +@patch("scripts.leapp_script.update_insights_inventory") +@patch("scripts.leapp_script.OutputCollector") +def test_main_upgrade_not_sucessfull( + mock_output_collector, + mock_update_insights_inventory, + mock_execute_operation, + mock_remove_previous_reports, + mock_should_use_no_rhsm_check, + mock_install_rhui, + mock_setup_leapp, + mock_is_non_eligible_releases, + mock_get_rhel_version, + mock_reboot_system, + mock_parse_results, +): + mock_get_rhel_version.return_value = ("rhel", "7.9") + mock_is_non_eligible_releases.return_value = False + mock_setup_leapp.return_value = [{"leapp_pkg": "to_install"}] + mock_should_use_no_rhsm_check.return_value = True + mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) + mock_execute_operation.return_value = "LOREM IPSUM\n" + "\nDOLOR SIT AMET" + + main() + + mock_setup_leapp.assert_called_once() + mock_should_use_no_rhsm_check.assert_called_once() + mock_install_rhui.assert_called_once() + mock_remove_previous_reports.assert_called_once() + mock_execute_operation.assert_called_once() + mock_parse_results.assert_called_once() + mock_update_insights_inventory.assert_called_once() + mock_reboot_system.assert_not_called() diff --git a/tests/upgrade/test_non_eligible_release.py b/tests/test_non_eligible_release.py similarity index 72% rename from tests/upgrade/test_non_eligible_release.py rename to tests/test_non_eligible_release.py index 078caa4..54dfefa 100644 --- a/tests/upgrade/test_non_eligible_release.py +++ b/tests/test_non_eligible_release.py @@ -1,7 +1,7 @@ -from scripts.leapp_upgrade import is_non_eligible_releases +from scripts.leapp_script import is_non_eligible_releases -def test_is_non_eligible_releases(): +def test_is_non_eligible_releases_upgrade(): eligible_releases = ["7.9", "8.4"] non_eligible_releases = ["6.10", "9.0", "10.0"] diff --git a/tests/upgrade/test_parse_results.py b/tests/test_parse_results.py similarity index 92% rename from tests/upgrade/test_parse_results.py rename to tests/test_parse_results.py index d9c790a..8cc3e63 100644 --- a/tests/upgrade/test_parse_results.py +++ b/tests/test_parse_results.py @@ -1,13 +1,13 @@ from mock import mock_open, patch -from scripts.leapp_upgrade import ( +from scripts.leapp_script import ( parse_results, OutputCollector, ) @patch("os.path.exists", return_value=True) -@patch("scripts.leapp_upgrade._find_highest_report_level", return_value="ERROR") +@patch("scripts.leapp_script._find_highest_report_level", return_value="ERROR") def test_gather_report_files_exist(mock_find_level, mock_exists): test_txt_content = "Test data" test_json_content = '{"entries": [{"groups": ["error"]}]}' @@ -31,7 +31,7 @@ def test_gather_report_files_exist(mock_find_level, mock_exists): @patch("os.path.exists", return_value=True) -@patch("scripts.leapp_upgrade._find_highest_report_level", return_value="ERROR") +@patch("scripts.leapp_script._find_highest_report_level", return_value="ERROR") def test_gather_report_files_exist_with_reboot(mock_find_level, mock_exists): test_txt_content = "Test data" test_json_content = '{"test": "hi"}' diff --git a/tests/upgrade/test_reboot.py b/tests/test_reboot.py similarity index 57% rename from tests/upgrade/test_reboot.py rename to tests/test_reboot.py index 0c3dc07..4cf6fee 100644 --- a/tests/upgrade/test_reboot.py +++ b/tests/test_reboot.py @@ -1,9 +1,9 @@ from mock import patch -from scripts.leapp_upgrade import reboot_system +from scripts.leapp_script import reboot_system -@patch("scripts.leapp_upgrade.run_subprocess", return_value=(b"", 0)) +@patch("scripts.leapp_script.run_subprocess", return_value=(b"", 0)) def test_reboot(mock_popen): reboot_system() mock_popen.assert_called_once_with(["/usr/sbin/shutdown", "-r", "1"], wait=False) diff --git a/tests/upgrade/test_remove_reports.py b/tests/test_remove_reports.py similarity index 94% rename from tests/upgrade/test_remove_reports.py rename to tests/test_remove_reports.py index 908fae2..dcce050 100644 --- a/tests/upgrade/test_remove_reports.py +++ b/tests/test_remove_reports.py @@ -1,5 +1,5 @@ from mock import patch -from scripts.leapp_upgrade import ( +from scripts.leapp_script import ( remove_previous_reports, ) diff --git a/tests/test_run.py b/tests/test_run.py new file mode 100644 index 0000000..a18167f --- /dev/null +++ b/tests/test_run.py @@ -0,0 +1,10 @@ +from mock import patch + +from scripts.leapp_script import execute_operation + + +@patch("scripts.leapp_script.run_subprocess", return_value=(b"", 0)) +def test_run_leapp_upgrade(mock_popen): + execute_operation(["fake command"]) + + mock_popen.assert_called_once_with(["fake command"]) diff --git a/tests/upgrade/test_run_subprocess.py b/tests/test_run_subprocess.py similarity index 98% rename from tests/upgrade/test_run_subprocess.py rename to tests/test_run_subprocess.py index e284bdd..35a8b70 100644 --- a/tests/upgrade/test_run_subprocess.py +++ b/tests/test_run_subprocess.py @@ -3,7 +3,7 @@ import pytest from mock import patch -from scripts.leapp_upgrade import run_subprocess +from scripts.leapp_script import run_subprocess class SubprocessMock(object): diff --git a/tests/upgrade/test_schema.py b/tests/test_schema.py similarity index 84% rename from tests/upgrade/test_schema.py rename to tests/test_schema.py index cfa2117..e40833b 100644 --- a/tests/upgrade/test_schema.py +++ b/tests/test_schema.py @@ -1,6 +1,6 @@ import json import jsonschema -from scripts.leapp_upgrade import OutputCollector, STATUS_CODE_NAME_MAP +from scripts.leapp_script import OutputCollector, STATUS_CODE_NAME_MAP def test_output_schema(): @@ -9,7 +9,7 @@ def test_output_schema(): output_collector = OutputCollector(status=status) empty_output = output_collector.to_dict() - with open("schemas/upgrade_schema_1.0.json", "r") as schema: + with open("schemas/leapp_schema_1.0.json", "r") as schema: schema_json = json.load(schema) # If some difference between generated json and its schema invoke exception jsonschema.validate(instance=empty_output, schema=schema_json) @@ -28,7 +28,7 @@ def test_output_schema_entries_report(): output_collector.entries = {"hi": "world"} full_output = output_collector.to_dict() - with open("schemas/upgrade_schema_1.0.json", "r") as schema: + with open("schemas/leapp_schema_1.0.json", "r") as schema: schema_json = json.load(schema) # If some difference between generated json and its schema invoke exception jsonschema.validate(instance=full_output, schema=schema_json) diff --git a/tests/upgrade/test_setup_leapp.py b/tests/test_setup_leapp.py similarity index 86% rename from tests/upgrade/test_setup_leapp.py rename to tests/test_setup_leapp.py index 57683cc..3ad43bf 100644 --- a/tests/upgrade/test_setup_leapp.py +++ b/tests/test_setup_leapp.py @@ -1,6 +1,6 @@ import pytest from mock import patch -from scripts.leapp_upgrade import setup_leapp, ProcessError +from scripts.leapp_script import setup_leapp, ProcessError @pytest.mark.parametrize( ("installed"), @@ -9,8 +9,8 @@ (False), ), ) -@patch("scripts.leapp_upgrade.run_subprocess") -@patch("scripts.leapp_upgrade._check_if_package_installed") +@patch("scripts.leapp_script.run_subprocess") +@patch("scripts.leapp_script._check_if_package_installed") 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 = installed @@ -33,8 +33,8 @@ def test_setup_leapp_success(mock_check_if_package_installed, mock_run_subproces assert all(pkg.get("installed", False) for pkg in result) -@patch("scripts.leapp_upgrade.run_subprocess") -@patch("scripts.leapp_upgrade._check_if_package_installed") +@patch("scripts.leapp_script.run_subprocess") +@patch("scripts.leapp_script._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 = False diff --git a/tests/upgrade/test_should_use_rhsm.py b/tests/test_should_use_rhsm.py similarity index 85% rename from tests/upgrade/test_should_use_rhsm.py rename to tests/test_should_use_rhsm.py index 2d3d9f4..0914c65 100644 --- a/tests/upgrade/test_should_use_rhsm.py +++ b/tests/test_should_use_rhsm.py @@ -1,8 +1,8 @@ from mock import patch -from scripts.leapp_upgrade import should_use_no_rhsm_check +from scripts.leapp_script import should_use_no_rhsm_check -@patch("scripts.leapp_upgrade.run_subprocess") +@patch("scripts.leapp_script.run_subprocess") def test_should_use_no_rhsm_rhsm_and_rhui_installed( mock_run_subprocess, ): @@ -19,7 +19,7 @@ def test_should_use_no_rhsm_rhsm_and_rhui_installed( assert result -@patch("scripts.leapp_upgrade.run_subprocess") +@patch("scripts.leapp_script.run_subprocess") def test_should_use_no_rhsm_rhsm_installed_rhui_not( mock_run_subprocess, ): @@ -36,7 +36,7 @@ def test_should_use_no_rhsm_rhsm_installed_rhui_not( assert not result -@patch("scripts.leapp_upgrade.run_subprocess") +@patch("scripts.leapp_script.run_subprocess") def test_should_use_no_rhsm_rhsm_not_installed(mock_run_subprocess): mock_run_subprocess.side_effect = [ ("error_message", 1), diff --git a/tests/test_update_insights_inventory.py b/tests/test_update_insights_inventory.py new file mode 100644 index 0000000..47a0df5 --- /dev/null +++ b/tests/test_update_insights_inventory.py @@ -0,0 +1,24 @@ +from mock import patch +from scripts.leapp_script import update_insights_inventory, OutputCollector + + +def test_update_insights_inventory_successfully(): + output = OutputCollector() + with patch( + "scripts.leapp_script.run_subprocess", return_value=(b"", 0) + ) as mock_popen: + update_insights_inventory(output) + assert "Failed to update Insights Inventory." not in output.message + + mock_popen.assert_called_once_with(cmd=["/usr/bin/insights-client"]) + + +def test_update_insights_inventory_non_success(): + output = OutputCollector() + with patch( + "scripts.leapp_script.run_subprocess", return_value=(b"output", 1) + ) as mock_popen: + update_insights_inventory(output) + assert "Failed to update Insights Inventory." in output.message + + mock_popen.assert_called_once_with(cmd=["/usr/bin/insights-client"]) diff --git a/tests/upgrade/__init__.py b/tests/upgrade/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/upgrade/test_main.py b/tests/upgrade/test_main.py deleted file mode 100644 index 4969393..0000000 --- a/tests/upgrade/test_main.py +++ /dev/null @@ -1,119 +0,0 @@ -from mock import patch -from scripts.leapp_upgrade import ( - main, - OutputCollector, - REBOOT_GUIDANCE_MESSAGE, -) - - -@patch("scripts.leapp_upgrade.get_rhel_version") -@patch("scripts.leapp_upgrade.is_non_eligible_releases") -@patch("scripts.leapp_upgrade.setup_leapp") -@patch("scripts.leapp_upgrade.update_insights_inventory") -@patch("scripts.leapp_upgrade.OutputCollector") -def test_main_non_eligible_release( - mock_output_collector, - mock_update_insights_inventory, - mock_setup_leapp, - mock_is_non_eligible_releases, - mock_get_rhel_version, -): - mock_get_rhel_version.return_value = ("rhel", "6.9") - mock_is_non_eligible_releases.return_value = True - mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) - - main() - - mock_get_rhel_version.assert_called_once() - mock_is_non_eligible_releases.assert_called_once() - mock_output_collector.assert_called_once() - mock_setup_leapp.assert_not_called() - mock_update_insights_inventory.assert_not_called() - - -@patch("scripts.leapp_upgrade.parse_results") -@patch("scripts.leapp_upgrade.reboot_system") -@patch("scripts.leapp_upgrade.get_rhel_version") -@patch("scripts.leapp_upgrade.is_non_eligible_releases") -@patch("scripts.leapp_upgrade.setup_leapp") -@patch("scripts.leapp_upgrade.should_use_no_rhsm_check") -@patch("scripts.leapp_upgrade.install_leapp_pkg_corresponding_to_installed_rhui") -@patch("scripts.leapp_upgrade.remove_previous_reports") -@patch("scripts.leapp_upgrade.execute_upgrade") -@patch("scripts.leapp_upgrade.update_insights_inventory") -@patch("scripts.leapp_upgrade.OutputCollector") -def test_main_eligible_release( - mock_output_collector, - mock_update_insights_inventory, - mock_execute_upgrade, - mock_remove_previous_reports, - mock_should_use_no_rhsm_check, - mock_install_rhui, - mock_setup_leapp, - mock_is_non_eligible_releases, - mock_get_rhel_version, - mock_reboot_system, - mock_parse_results, -): - mock_get_rhel_version.return_value = ("rhel", "7.9") - mock_is_non_eligible_releases.return_value = False - mock_setup_leapp.return_value = [{"leapp_pkg": "to_install"}] - mock_should_use_no_rhsm_check.return_value = True - mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) - mock_execute_upgrade.return_value = ( - "LOREM IPSUM\nTEST" + REBOOT_GUIDANCE_MESSAGE + "TEST\nDOLOR SIT AMET" - ) - - main() - - mock_setup_leapp.assert_called_once() - mock_should_use_no_rhsm_check.assert_called_once() - mock_install_rhui.assert_called_once() - mock_remove_previous_reports.assert_called_once() - mock_execute_upgrade.assert_called_once() - mock_parse_results.assert_called_once() - mock_update_insights_inventory.assert_called_once() - mock_reboot_system.assert_called_once() - - -@patch("scripts.leapp_upgrade.parse_results") -@patch("scripts.leapp_upgrade.reboot_system") -@patch("scripts.leapp_upgrade.get_rhel_version") -@patch("scripts.leapp_upgrade.is_non_eligible_releases") -@patch("scripts.leapp_upgrade.setup_leapp") -@patch("scripts.leapp_upgrade.should_use_no_rhsm_check") -@patch("scripts.leapp_upgrade.install_leapp_pkg_corresponding_to_installed_rhui") -@patch("scripts.leapp_upgrade.remove_previous_reports") -@patch("scripts.leapp_upgrade.execute_upgrade") -@patch("scripts.leapp_upgrade.update_insights_inventory") -@patch("scripts.leapp_upgrade.OutputCollector") -def test_main_upgrade_not_sucessfull( - mock_output_collector, - mock_update_insights_inventory, - mock_execute_upgrade, - mock_remove_previous_reports, - mock_should_use_no_rhsm_check, - mock_install_rhui, - mock_setup_leapp, - mock_is_non_eligible_releases, - mock_get_rhel_version, - mock_reboot_system, - mock_parse_results, -): - mock_get_rhel_version.return_value = ("rhel", "7.9") - mock_is_non_eligible_releases.return_value = False - mock_setup_leapp.return_value = [{"leapp_pkg": "to_install"}] - mock_should_use_no_rhsm_check.return_value = True - mock_output_collector.return_value = OutputCollector(entries=["non-empty"]) - mock_execute_upgrade.return_value = "LOREM IPSUM\n" + "\nDOLOR SIT AMET" - - main() - - mock_setup_leapp.assert_called_once() - mock_should_use_no_rhsm_check.assert_called_once() - mock_install_rhui.assert_called_once() - mock_remove_previous_reports.assert_called_once() - mock_execute_upgrade.assert_called_once() - mock_parse_results.assert_called_once() - mock_update_insights_inventory.assert_called_once() - mock_reboot_system.assert_not_called() diff --git a/tests/upgrade/test_run.py b/tests/upgrade/test_run.py deleted file mode 100644 index f1a0c6b..0000000 --- a/tests/upgrade/test_run.py +++ /dev/null @@ -1,10 +0,0 @@ -from mock import patch - -from scripts.leapp_upgrade import execute_upgrade - - -@patch("scripts.leapp_upgrade.run_subprocess", return_value=(b"", 0)) -def test_run_leapp_upgrade(mock_popen): - execute_upgrade(["fake command"]) - - mock_popen.assert_called_once_with(["fake command"]) diff --git a/tests/upgrade/test_update_insights_inventory.py b/tests/upgrade/test_update_insights_inventory.py deleted file mode 100644 index 7a75371..0000000 --- a/tests/upgrade/test_update_insights_inventory.py +++ /dev/null @@ -1,18 +0,0 @@ -import pytest -from mock import patch -from scripts.leapp_upgrade import ProcessError, update_insights_inventory - - -@patch("scripts.leapp_upgrade.run_subprocess", return_value=(b"", 0)) -def test_update_insights_inventory(mock_popen): - update_insights_inventory() - mock_popen.assert_called_once_with(["/usr/bin/insights-client"]) - - -@patch("scripts.leapp_upgrade.run_subprocess", return_value=(b"", 1)) -def test_update_insights_inventory_error(mock_popen): - with pytest.raises(ProcessError) as exception: - update_insights_inventory() - mock_popen.assert_called_once_with(["/usr/bin/insights-client"]) - - assert str(exception.value) == "insights-client execution exited with code '1'."