|
17 | 17 | # You should have received a copy of the GNU General Public License |
18 | 18 | # along with Checkbox. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
|
| 20 | +import argparse |
20 | 21 | import subprocess |
21 | 22 |
|
22 | 23 | from checkbox_support.snap_utils.system import on_ubuntucore |
@@ -53,57 +54,103 @@ def watchdog_service_check(): |
53 | 54 | raise SystemExit("Error: {}".format(err)) |
54 | 55 |
|
55 | 56 |
|
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: |
60 | 58 | 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" |
62 | 61 |
|
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: |
65 | 64 | print( |
66 | 65 | "systemd watchdog should be enabled but reset timeout " |
67 | 66 | "(RuntimeWatchdogUSec) is set to: " |
68 | 67 | "{}".format(runtime_watchdog_usec) |
69 | 68 | ) |
70 | 69 | 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." |
74 | 73 | ) |
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 |
84 | 78 | ) |
85 | | - print("watchdog.service is not active") |
| 79 | + ) |
86 | 80 | else: |
87 | | - if systemd_wdt_configured: |
| 81 | + if is_systemd_wdt_configured: |
88 | 82 | print( |
89 | 83 | "systemd watchdog should not be enabled but reset timeout " |
90 | 84 | "(RuntimeWatchdogUSec) is set to: " |
91 | 85 | "{}".format(runtime_watchdog_usec) |
92 | 86 | ) |
93 | 87 | 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 |
97 | 96 | ) |
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") |
107 | 154 |
|
108 | 155 |
|
109 | 156 | if __name__ == "__main__": |
|
0 commit comments