Skip to content

Commit c4c2f84

Browse files
revise/add watchdog related test scripts
revise watchdog_config_test.py add watchdog_test.py
1 parent 7b6dd5b commit c4c2f84

File tree

4 files changed

+1267
-33
lines changed

4 files changed

+1267
-33
lines changed

providers/base/bin/watchdog_config_test.py

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# You should have received a copy of the GNU General Public License
1818
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
1919

20+
import argparse
2021
import subprocess
2122

2223
from checkbox_support.snap_utils.system import on_ubuntucore
@@ -53,57 +54,103 @@ def watchdog_service_check():
5354
raise SystemExit("Error: {}".format(err))
5455

5556

56-
def main():
57-
runtime_watchdog_usec = get_systemd_wdt_usec()
58-
systemd_wdt_configured = runtime_watchdog_usec != "0"
59-
wdt_service_configured = watchdog_service_check()
57+
def check_timeout() -> bool:
6058
ubuntu_version = int(get_series().split(".")[0])
61-
watchdog_config_ready = True
59+
runtime_watchdog_usec = get_systemd_wdt_usec()
60+
is_systemd_wdt_configured = runtime_watchdog_usec != "0"
6261

63-
if (ubuntu_version >= 20) or (on_ubuntucore()):
64-
if not systemd_wdt_configured:
62+
if ubuntu_version >= 20 or on_ubuntucore():
63+
if not is_systemd_wdt_configured:
6564
print(
6665
"systemd watchdog should be enabled but reset timeout "
6766
"(RuntimeWatchdogUSec) is set to: "
6867
"{}".format(runtime_watchdog_usec)
6968
)
7069
print(
71-
"In order for the watchdog service to work, the "
72-
"RuntimeWatchdogUSec configuration option must be set before "
73-
"running this test."
70+
"In order for the systemd watchdog to work, "
71+
"the RuntimeWatchdogUSec configuration option must be set "
72+
"before running this test."
7473
)
75-
watchdog_config_ready = False
76-
if wdt_service_configured:
77-
print("found unexpected active watchdog.service unit")
78-
watchdog_config_ready = False
79-
if watchdog_config_ready:
80-
print(
81-
"systemd watchdog enabled, reset timeout: {}".format(
82-
runtime_watchdog_usec
83-
)
74+
raise SystemExit(1)
75+
print(
76+
"systemd watchdog enabled, reset timeout: {}".format(
77+
runtime_watchdog_usec
8478
)
85-
print("watchdog.service is not active")
79+
)
8680
else:
87-
if systemd_wdt_configured:
81+
if is_systemd_wdt_configured:
8882
print(
8983
"systemd watchdog should not be enabled but reset timeout "
9084
"(RuntimeWatchdogUSec) is set to: "
9185
"{}".format(runtime_watchdog_usec)
9286
)
9387
print(
94-
"In order for the watchdog service to work, the "
95-
"RuntimeWatchdogUSec configuration option must be set before "
96-
"running this test."
88+
"In order for the watchdog.service to work, "
89+
"the RuntimeWatchdogUSec configuration option must be 0 "
90+
"before running this test."
91+
)
92+
raise SystemExit(1)
93+
print(
94+
"systemd watchdog disabled, reset timeout: {}".format(
95+
runtime_watchdog_usec
9796
)
98-
watchdog_config_ready = False
99-
if not wdt_service_configured:
100-
print("watchdog.service unit does not report as active")
101-
watchdog_config_ready = False
102-
if watchdog_config_ready:
103-
print("systemd watchdog disabled")
104-
print("watchdog.service active")
105-
106-
raise SystemExit(not watchdog_config_ready)
97+
)
98+
99+
100+
def check_service() -> bool:
101+
ubuntu_version = int(get_series().split(".")[0])
102+
is_wdt_service_configured = watchdog_service_check()
103+
104+
if ubuntu_version >= 20 or on_ubuntucore():
105+
if is_wdt_service_configured:
106+
raise SystemExit("Found unexpected active watchdog.service unit")
107+
print("watchdog.service is not active")
108+
else:
109+
if not is_wdt_service_configured:
110+
raise SystemExit("watchdog.service unit does not report as active")
111+
print("watchdog.service is active")
112+
113+
114+
def watchdog_argparse() -> argparse.Namespace:
115+
"""
116+
Parse command line arguments and return the parsed arguments.
117+
118+
This function parses the command line arguments and returns the parsed
119+
arguments. The arguments are parsed using the `argparse` module. The
120+
function takes no parameters.
121+
122+
Returns:
123+
argparse.Namespace: The parsed command line arguments.
124+
"""
125+
parser = argparse.ArgumentParser(
126+
prog="Watchdog Testing Tool",
127+
description="This is a tool to help you perform the watchdog testing",
128+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
129+
)
130+
group = parser.add_mutually_exclusive_group(required=True)
131+
group.add_argument(
132+
"-t",
133+
"--check-time",
134+
action="store_true",
135+
help="Check if watchdog service timeout is configured correctly",
136+
)
137+
group.add_argument(
138+
"-s",
139+
"--check-service",
140+
action="store_true",
141+
help="Check if watchdog service is running",
142+
)
143+
return parser.parse_args()
144+
145+
146+
def main():
147+
args = watchdog_argparse()
148+
if args.check_time:
149+
check_timeout()
150+
elif args.check_service:
151+
check_service()
152+
else:
153+
raise SystemExit("Unexpected arguments")
107154

108155

109156
if __name__ == "__main__":

0 commit comments

Comments
 (0)