Skip to content

Commit

Permalink
Fix blake8
Browse files Browse the repository at this point in the history
  • Loading branch information
LiaoU3 committed Apr 17, 2024
1 parent f46c1b2 commit 575b4fd
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 2 deletions.
4 changes: 2 additions & 2 deletions providers/base/bin/watchdog_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
126 changes: 126 additions & 0 deletions providers/base/tests/watchdog_config_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env python3
# Copyright 2021 Canonical Ltd.
# All rights reserved.
#
# Written by:
# Vic Liu <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.

"""
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()

0 comments on commit 575b4fd

Please sign in to comment.