Skip to content

Commit

Permalink
testlib.site: Improve traceability for omd status
Browse files Browse the repository at this point in the history
CMK-19770

Change-Id: I4a60df27207d4c83257d785fe46468499b6f629a
  • Loading branch information
rene-slowenski-checkmk committed Oct 23, 2024
1 parent 3496463 commit cb92258
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions tests/testlib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,19 @@ def execute(
def run(
self,
args: list[str],
capture_output: bool = True,
check: bool = True,
encoding: str | None = "utf-8",
input: str | None = None, # pylint: disable=redefined-builtin
preserve_env: list[str] | None = None,
**kwargs: Any,
) -> subprocess.CompletedProcess:
return run(
args=args,
capture_output=capture_output,
check=check,
input=input,
encoding="utf-8",
encoding=encoding,
preserve_env=preserve_env,
sudo=True,
substitute_user=self.id,
Expand Down Expand Up @@ -581,23 +586,33 @@ def python_helper(self, name: str) -> PythonHelper:
return PythonHelper(self, helper_file)

def omd(self, mode: str, *args: str) -> int:
cmd = ["sudo", "omd", mode, self.id] + list(args)
cmd = ["omd", mode] + list(args)
logger.info("Executing: %s", subprocess.list2cmdline(cmd))
completed_process = subprocess.run(
completed_process = self.run(
cmd,
capture_output=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
check=False,
)

log_level = logging.DEBUG if completed_process.returncode == 0 else logging.WARNING
logger.log(log_level, "Exit code: %d", completed_process.returncode)
if completed_process.stdout:
logger.log(log_level, "Output:")
for line in completed_process.stdout.strip().split("\n"):
logger.log(log_level, "> %s", line)

if mode == "status":
logger.info(
"OMD status: %d (%s)",
completed_process.returncode,
{
0: "fully running",
1: "fully stopped",
2: "partially running",
}.get(completed_process.returncode, "unknown meaning"),
)

return completed_process.returncode

def path(self, rel_path: str | Path) -> str:
Expand Down Expand Up @@ -1043,31 +1058,13 @@ def ensure_running(self) -> None:
)

def is_running(self) -> bool:
return self._omd_status() == 0
return self.omd("status") == 0

def is_stopped(self) -> bool:
# 0 -> fully running
# 1 -> fully stopped
# 2 -> partially running
return self._omd_status() == 1

def _omd_status(self) -> int:
def _fmt_output(msg: str) -> str:
return ("\n> " + "\n> ".join(msg.splitlines()) + "\n") if msg else "-"

try:
self.run(["omd", "status", "--bare"])
logger.info("Exit code was: 0 (fully running)")
return 0
except subprocess.CalledProcessError as e:
status_text = {
0: "fully running",
1: "fully stopped",
2: "partially running",
}.get(e.returncode, "unknown meaning")
logger.info("Exit code was: %d (%s)", e.returncode, status_text)
logger.debug(str(e))
return e.returncode
return self.omd("status") == 1

def set_config(self, key: str, val: str, with_restart: bool = False) -> None:
if self.get_config(key) == val:
Expand Down

0 comments on commit cb92258

Please sign in to comment.