From 575b4fd152bb6d03e152f75cb6751fc254cee8ac Mon Sep 17 00:00:00 2001 From: liaou3 Date: Wed, 17 Apr 2024 16:48:05 +0800 Subject: [PATCH] Fix blake8 --- providers/base/bin/watchdog_config_test.py | 4 +- providers/base/tests/watchdog_config_test.py | 126 +++++++++++++++++++ 2 files changed, 128 insertions(+), 2 deletions(-) create mode 100755 providers/base/tests/watchdog_config_test.py diff --git a/providers/base/bin/watchdog_config_test.py b/providers/base/bin/watchdog_config_test.py index 63c741985..b78b3bca2 100755 --- a/providers/base/bin/watchdog_config_test.py +++ b/providers/base/bin/watchdog_config_test.py @@ -95,8 +95,8 @@ def main(): else: if is_systemd_wdt_configured: print( - "systemd watchdog should not be enabled but reset timeout: " - "{}".format(runtime_watchdog_usec) + "systemd watchdog should not be enabled but " + "reset timeout: {}".format(runtime_watchdog_usec) ) watchdog_config_ready = False if watchdog_config_ready: diff --git a/providers/base/tests/watchdog_config_test.py b/providers/base/tests/watchdog_config_test.py new file mode 100755 index 000000000..66c6ac246 --- /dev/null +++ b/providers/base/tests/watchdog_config_test.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python3 +# Copyright 2021 Canonical Ltd. +# All rights reserved. +# +# Written by: +# Vic Liu +# +# Checkbox is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# Checkbox is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Checkbox. If not, see . + +""" +Watchdog implementation on both classic and core image no longer rely +on watchdogd service since 20.04, with this change watchdog/systemd-config +tests only systemd configuration on 20.04 and later series while keeping +the original test for prior releases +""" + +import subprocess +import argparse + +from checkbox_support.snap_utils.system import on_ubuntucore +from checkbox_support.snap_utils.system import get_series +from yaml import parse + + +def watchdog_argparse(): + parser = argparse.ArgumentParser() + parser.add_argument("-t", "--check_time", action="store_true") + parser.add_argument("-s", "--check_service", action="store_true") + + return parser.parse_args() + + +def get_systemd_wdt_usec(): + """ + Return value of systemd-watchdog RuntimeWatchdogUSec + """ + cmd = ["systemctl", "show", "-p", "RuntimeWatchdogUSec"] + try: + result = subprocess.check_output(cmd, universal_newlines=True) + except Exception as err: + raise SystemExit("Error: {}".format(err)) + + if result: + runtime_watchdog_usec = result.split("=")[1].strip() + return runtime_watchdog_usec + else: + raise SystemExit( + "Unexpected failure occurred when executing: {}".format(cmd) + ) + + +def watchdog_service_check(): + """ + Check if the watchdog service is configured correctly + """ + cmd = ["systemctl", "is-active", "watchdog.service", "--quiet"] + try: + return not subprocess.run(cmd).returncode + except Exception as err: + raise SystemExit("Error: {}".format(err)) + + +def main(): + args = watchdog_argparse() + + ubuntu_version = int(get_series().split(".")[0]) + watchdog_config_ready = True + + if args.check_time: + runtime_watchdog_usec = get_systemd_wdt_usec() + is_systemd_wdt_configured = runtime_watchdog_usec != "0" + + if (ubuntu_version >= 20) or (on_ubuntucore()): + if not is_systemd_wdt_configured: + print( + "systemd watchdog should be enabled but reset timeout: " + "{}".format(runtime_watchdog_usec) + ) + watchdog_config_ready = False + if watchdog_config_ready: + print( + "systemd watchdog enabled, reset timeout: {}".format( + runtime_watchdog_usec + ) + ) + else: + if is_systemd_wdt_configured: + print( + "systemd watchdog should not be enabled but reset timeout: " + "{}".format(runtime_watchdog_usec) + ) + watchdog_config_ready = False + if watchdog_config_ready: + print("systemd watchdog disabled") + + if args.check_service: + is_wdt_service_configured = watchdog_service_check() + + if (ubuntu_version >= 20) or (on_ubuntucore()): + if is_wdt_service_configured: + print("found unexpected active watchdog.service unit") + watchdog_config_ready = False + if watchdog_config_ready: + print("watchdog.service is not active") + else: + if not is_wdt_service_configured: + print("watchdog.service unit does not report as active") + watchdog_config_ready = False + if watchdog_config_ready: + print("watchdog.service active") + + raise SystemExit(not watchdog_config_ready) + + +if __name__ == "__main__": + main()