Skip to content

Commit

Permalink
install-cmk: assign distinct returncodes for failures.
Browse files Browse the repository at this point in the history
+ Error in installation: returncode 22
    raise `RunTimeError` from `testlib.site::Site .install_cmk`.
+ Error in downloading artifact: returncode 11
    raise `FileNotFoundError` from `testlib.site::Site .install_cmk`.

CMK-18870

Change-Id: Iaeda202e8f9a15f20d321f9b093629863519e0a9
  • Loading branch information
dhsh-checkmk committed Sep 26, 2024
1 parent bbdb21c commit 17f79be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
26 changes: 19 additions & 7 deletions tests/scripts/install-cmk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

import logging
import os
import subprocess
import sys

import requests

# Make the tests.testlib available
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

Expand All @@ -20,6 +23,10 @@
logging.basicConfig(level=logging.INFO, format="%(asctime)-15s %(filename)s %(message)s")
logger = logging.getLogger()

CMK_OK = 0
CMK_DOWNLOAD_ERROR = 11
CMK_INSTALL_ERROR = 22


def main():
add_python_paths()
Expand All @@ -37,16 +44,21 @@ def main():

if version.is_installed():
logger.info("Already installed. Terminating.")
return 0
return CMK_OK

manager = ABCPackageManager.factory()
manager.install(version.version, version.edition)

if not version.is_installed():
logger.error("Failed not install version")
raise Exception(f"Failed to install {version.edition} {version.version}")
try:
manager.install(version.version, version.edition)
except subprocess.CalledProcessError as excp:
excp.add_note(f"Failed to install {version.edition} {version.version}!")
logger.exception(excp)
return CMK_INSTALL_ERROR
except requests.exceptions.HTTPError as excp:
excp.add_note(f"Failed to download {version.edition} {version.version}!")
logger.exception(excp)
return CMK_DOWNLOAD_ERROR

return 0
return CMK_OK


if __name__ == "__main__":
Expand Down
11 changes: 7 additions & 4 deletions tests/testlib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,13 @@ def install_cmk(self) -> None:
),
)
except subprocess.CalledProcessError as excp:
excp.add_note(
f"Version {self.version.version} could not be installed! "
'Use "tests/scripts/install-cmk.py" or install it manually.'
)
excp.add_note("Execute 'tests/scripts/install-cmk.py' manually to debug the issue.")
if excp.returncode == 22:
excp.add_note(f"Version {self.version.version} could not be installed!")
raise RuntimeError from excp
if excp.returncode == 11:
excp.add_note(f"Version {self.version.version} could not be downloaded!")
raise FileNotFoundError from excp
raise excp

def create(self) -> None:
Expand Down
11 changes: 6 additions & 5 deletions tests/testlib/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,12 @@ def _execute(self, cmd: list[str | Path]) -> None:
if os.geteuid() != 0:
cmd.insert(0, "sudo")

completed_process = subprocess.run(
cmd, shell=False, close_fds=True, encoding="utf-8", check=False
)
if completed_process.returncode >> 8 != 0:
raise Exception("Failed to install package")
try:
subprocess.run(cmd, shell=False, close_fds=True, encoding="utf-8", check=True)
except subprocess.CalledProcessError as excp:
if excp.returncode >> 8 != 0:
excp.add_note("Failed to install package!")
raise excp


def sha256_file(path: Path) -> str:
Expand Down

0 comments on commit 17f79be

Please sign in to comment.