Repository navigation
Expand file tree
/
Copy pathtest_cli.py
More file actions
298 lines (249 loc) · 13.4 KB
/
Copy pathtest_cli.py
File metadata and controls
298 lines (249 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Tests that run tronitor.py as a subprocess."""
import json
import subprocess
import sys
from pathlib import Path
import pytest
from conftest import (
HC_UUID_DATABASE,
HC_UUID_FILESYSTEM,
RunCli,
bash_fixture,
coverage_startup_code,
hc_fixture_checks,
run_tronitor,
serve_checks,
serve_monitors,
subprocess_env,
)
from fake_server import FakeServer
import tronitor
run = run_tronitor
def test_version_prints_the_date_and_exits_0() -> None:
"""--version prints the version and exits 0."""
result = run("--version")
assert result.returncode == 0
assert result.stdout.strip() == f"tronitor {tronitor.__version__}"
def run_without_tomllib(
version: tuple[int, int, int], argv: list[str], *, tomli: bool
) -> subprocess.CompletedProcess[str]:
"""Run tronitor.py in a Python that pretends to be version, with tomllib hidden, and tomli too unless tomli is True.
A Python older than 3.11 has no tomllib, so hiding it is what such a Python looks like to the
script. The fake sys.version_info has the real one's fields, so the standard library still works.
"""
hidden = ["tomllib"] if tomli else ["tomllib", "tomli"]
script = str(Path(tronitor.__file__))
code = "; ".join(
[
*coverage_startup_code(),
"import collections, runpy, sys",
*(["import tomli"] if tomli else []),
*(f"sys.modules[{name!r}] = None" for name in hidden),
"VersionInfo = collections.namedtuple('VersionInfo', 'major minor micro releaselevel serial')",
f"sys.version_info = VersionInfo{version + ('final', 0)!r}",
f"sys.argv = {[script, *argv]!r}",
f"runpy.run_path({script!r}, run_name='__main__')",
]
)
return subprocess.run(
[sys.executable, "-c", code],
check=False,
capture_output=True,
text=True,
encoding="utf-8",
env=subprocess_env(),
)
def test_old_python_stops_with_one_line() -> None:
"""On a Python older than 3.10, the script stops with one line on stderr and exit 2, not a traceback."""
result = run_without_tomllib((3, 9, 2), ["--version"], tomli=False)
assert result.returncode == 2
assert result.stderr == f"tronitor: Python 3.10 or newer is needed, but {sys.executable} is Python 3.9.2" + "\n"
assert result.stdout == ""
def test_python_3_10_without_tomli_says_how_to_get_it() -> None:
"""On Python 3.10 without tomli, the one line says how to install tomli, and the exit code is 2."""
result = run_without_tomllib((3, 10, 12), ["--version"], tomli=False)
assert result.returncode == 2
assert result.stderr == (
f"tronitor: {sys.executable} is Python 3.10.12 without the tomli package; install tomli "
"(sudo apt install python3-tomli, or python3 -m pip install --user tomli) or use Python 3.11 or newer" + "\n"
)
assert result.stdout == ""
def test_python_3_10_reads_the_config_with_tomli(tmp_path: Path) -> None:
"""On Python 3.10 with tomli, the script reads TOML through tomli: --migrate validates the file it writes."""
pytest.importorskip("tomli")
script = bash_fixture(tmp_path, hcApiKey="hc-secret-key")
result = run_without_tomllib((3, 10, 12), ["--migrate", str(script)], tomli=True)
assert result.returncode == 0, result.stderr
assert f"Wrote {tmp_path / 'tronitor.toml'}" in result.stdout
assert tronitor.load_config(tmp_path / "tronitor.toml", {}, []).healthchecks.api_key == "hc-secret-key"
def test_no_arguments_prints_usage_and_exits_2() -> None:
"""No arguments prints the usage to stderr and exits 2."""
result = run()
assert result.returncode == 2
assert "usage:" in result.stderr
def test_cli_list_against_fake_server(fake_server: FakeServer, hc_config_file: Path, run_cli: RunCli) -> None:
"""-l prints the header and one row per check."""
serve_checks(fake_server)
result = run_cli("-m", "hc", "-l")
assert result.returncode == 0, result.stderr
assert "The following Healthchecks monitors were found:" in result.stdout
assert f"Filesystem Backup UUID: {HC_UUID_FILESYSTEM} Slug: filesystem-backup Status: Up" in result.stdout
assert f"Database Backup UUID: {HC_UUID_DATABASE} Slug: database-backup Status: Paused" in result.stdout
assert result.stderr == ""
def test_cli_ignore_paused_pings_against_fake_server(
fake_server: FakeServer, hc_config_file: Path, run_cli: RunCli
) -> None:
"""--ignore-paused-pings all sets the setting on each check, from a separate program."""
serve_checks(fake_server)
for uuid, check in zip((HC_UUID_FILESYSTEM, HC_UUID_DATABASE), hc_fixture_checks(), strict=True):
fake_server.add("POST", f"/api/v3/checks/{uuid}", 200, {**check, "manual_resume": True})
result = run_cli("-m", "hc", "--ignore-paused-pings", "all")
assert result.returncode == 0, result.stderr
for name, uuid in (("Filesystem Backup", HC_UUID_FILESYSTEM), ("Database Backup", HC_UUID_DATABASE)):
assert f"Setting {name} (UUID {uuid}) to ignore pings while paused... done" in result.stdout
assert result.stderr == ""
def test_cli_pause_all_without_tty_needs_no_input(
fake_server: FakeServer, hc_config_file: Path, run_cli: RunCli, tmp_path: Path
) -> None:
"""Pausing never prompts, so it works from cron with nothing on stdin."""
serve_checks(fake_server)
for uuid in (HC_UUID_FILESYSTEM, HC_UUID_DATABASE):
fake_server.add("POST", f"/api/v3/checks/{uuid}/pause", 200, {"status": "paused"})
result = run_cli("-m", "healthchecks", "-p", "all")
assert result.returncode == 0, result.stderr
assert (tmp_path / "locks" / "healthchecks.lock").is_file()
assert result.stdout.count("... done") == 2
def test_cli_delete_without_yes_refuses_and_exits_2(
fake_server: FakeServer, hc_config_file: Path, run_cli: RunCli
) -> None:
"""Without a terminal and without --yes, delete refuses and sends no DELETE."""
serve_checks(fake_server)
result = run_cli("-m", "hc", "-d", "all")
assert result.returncode == 2
assert "tronitor: Refusing to delete without a terminal; pass --yes to confirm." in result.stderr
assert not [r for r in fake_server.requests if r.method == "DELETE"]
def test_cli_failure_reaches_stderr(fake_server: FakeServer, hc_config_file: Path, run_cli: RunCli) -> None:
"""A failed pause is reported on stderr, so cron sees it even when stdout is thrown away."""
serve_checks(fake_server)
fake_server.add("POST", f"/api/v3/checks/{HC_UUID_FILESYSTEM}/pause", 500, {"error": "server error"})
fake_server.add("POST", f"/api/v3/checks/{HC_UUID_DATABASE}/pause", 200, {"status": "paused"})
result = run_cli("-m", "hc", "-p", "all")
assert result.returncode == 3
failed = f"tronitor: Pausing Filesystem Backup (UUID {HC_UUID_FILESYSTEM})... failed: HTTP 500: server error"
assert failed in result.stderr
assert "failed" not in result.stdout
assert f"Pausing Database Backup (UUID {HC_UUID_DATABASE})... done" in result.stdout
def test_cli_find_f_without_tty_behaves_like_n(fake_server: FakeServer, hc_config_file: Path, run_cli: RunCli) -> None:
"""-f without a terminal lists the paused checks and does not prompt or unpause."""
serve_checks(fake_server)
result = run_cli("-m", "hc", "-f", stdin="y\n")
assert result.returncode == 0, result.stderr
assert "The following Healthchecks monitors are paused:" in result.stdout
assert "Database Backup" in result.stdout
assert "Filesystem Backup" not in result.stdout
assert "Unpause them?" not in result.stdout
assert not [r for r in fake_server.requests if r.method == "POST"]
def test_cli_help_mentions_providers_and_all() -> None:
"""--help explains the provider names and "all" and points at the docs."""
result = run("--help")
assert result.returncode == 0
assert "healthchecks (hc)" in result.stdout
assert "uptimerobot (ur)" in result.stdout
assert '"all"' in result.stdout
assert result.stdout.rstrip().endswith(
"Documentation: https://github.com/tronyx/tronitor/blob/master/docs/usage.md"
)
def test_cli_colour_is_off_when_piped(fake_server: FakeServer, hc_config_file: Path) -> None:
"""Piped output has no colour codes even without NO_COLOR."""
serve_checks(fake_server)
result = run("-m", "hc", "-l", env={"TRONITOR_CONFIG": str(hc_config_file), "NO_PROXY": "127.0.0.1"})
assert result.returncode == 0, result.stderr
assert "Status: Paused" in result.stdout
assert "\x1b[" not in result.stdout
def test_cli_env_only_no_config_file(fake_server: FakeServer, tmp_path: Path) -> None:
"""With the URL, key and lock directory in the environment, no config file is needed."""
serve_checks(fake_server)
fake_server.add("POST", f"/api/v3/checks/{HC_UUID_DATABASE}/pause", 200, {"status": "paused"})
env = {
"TRONITOR_HEALTHCHECKS_URL": fake_server.url,
"TRONITOR_HEALTHCHECKS_API_KEY": "hc-test-key",
"TRONITOR_LOCK_DIR": str(tmp_path / "env-locks"),
"HOME": str(tmp_path),
"APPDATA": str(tmp_path),
"NO_COLOR": "1",
"NO_PROXY": "127.0.0.1",
}
result = run("-m", "hc", "-p", "Database Backup", env=env)
assert result.returncode == 0, result.stderr
assert (tmp_path / "env-locks" / f"{HC_UUID_DATABASE}.lock").is_file()
assert fake_server.requests[0].headers["X-Api-Key"] == "hc-test-key"
def test_cli_secret_never_printed_with_verbose(fake_server: FakeServer, hc_config_file: Path, run_cli: RunCli) -> None:
"""--verbose logs the requests but never the API key, even when a request fails."""
serve_checks(fake_server)
fake_server.add("GET", f"/api/v3/checks/{HC_UUID_FILESYSTEM}", 401, {"error": "The API key is invalid."})
listed = run_cli("-m", "hc", "-l", "--verbose")
failed = run_cli("-m", "hc", "-i", "Filesystem Backup", "--verbose")
assert listed.returncode == 0
assert failed.returncode == 1
assert "GET " in listed.stderr
for result in (listed, failed):
assert "hc-test-key" not in result.stdout + result.stderr
def test_cli_json_with_a_non_latin_name_does_not_crash(fake_server: FakeServer, run_cli: RunCli) -> None:
"""A name outside the console's code page is printed as UTF-8 rather than crashing."""
checks = hc_fixture_checks()
checks[0]["name"] = "Sauvegarde 日本"
fake_server.add("GET", "/api/v3/checks/", 200, {"checks": checks})
result = run_cli("-m", "hc", "-l", "--json")
assert result.returncode == 0, result.stderr
assert json.loads(result.stdout)[0]["name"] == "Sauvegarde 日本"
def test_cli_missing_provider_is_a_usage_error() -> None:
"""An action without -m is rejected with exit 2, with a message that names the providers."""
result = run("-l")
assert result.returncode == 2
assert result.stderr == "tronitor: choose a provider with -m: healthchecks (hc) or uptimerobot (ur)\n"
def test_cli_migrate(tmp_path: Path) -> None:
"""--migrate, without -m, writes tronitor.toml next to the Bash script and exits 0."""
webhook = "https://discord.com/api/webhooks/1/secret-token"
script = bash_fixture(tmp_path, hcApiKey="hc-secret-key", webhookUrl=webhook)
result = run("--migrate", str(script))
assert result.returncode == 0, result.stderr
out = tmp_path / "tronitor.toml"
assert f"Wrote {out}" in result.stdout
assert "hc-secret-key" not in result.stdout + result.stderr
assert "secret-token" not in result.stdout + result.stderr
config = tronitor.load_config(out, {}, [])
assert config.healthchecks == tronitor.HealthchecksConfig("https://healthchecks.io", "hc-secret-key")
assert config.notify.webhook_url == webhook
def test_cli_two_actions_are_a_usage_error() -> None:
"""Two actions at once are rejected with exit 2."""
result = run("-m", "hc", "-l", "-f")
assert result.returncode == 2
assert "not allowed with" in result.stderr
def test_cli_ur_list(fake_server: FakeServer, ur_config_file: Path, run_cli_ur: RunCli) -> None:
"""-m ur -l prints one row per UptimeRobot monitor."""
serve_monitors(fake_server)
result = run_cli_ur("-m", "ur", "-l")
assert result.returncode == 0, result.stderr
assert "The following UptimeRobot monitors were found:" in result.stdout
assert "Plex ID: 1001 Status: Up" in result.stdout
assert "Sonarr ID: 1002 Status: Paused" in result.stdout
assert "Radarr ID: 1003 Status: Seems down" in result.stdout
assert "ur-test-key" not in result.stdout + result.stderr
def test_cli_unpause_all_resumes_and_removes_the_locks(
fake_server: FakeServer, hc_config_file: Path, run_cli: RunCli, tmp_path: Path
) -> None:
"""-u all resumes the paused check, removes healthchecks.lock and each check's lock, and exits 0."""
serve_checks(fake_server)
fake_server.add("POST", f"/api/v3/checks/{HC_UUID_DATABASE}/resume", 200, {"status": "new"})
locks = tmp_path / "locks"
locks.mkdir()
names = ["healthchecks.lock", f"{HC_UUID_FILESYSTEM}.lock", f"{HC_UUID_DATABASE}.lock", "other.lock"]
for name in names:
(locks / name).touch()
result = run_cli("-m", "hc", "-u", "all")
assert result.returncode == 0, result.stderr
assert "Filesystem Backup is already running" in result.stdout
assert f"Unpausing Database Backup (UUID {HC_UUID_DATABASE})... done" in result.stdout
assert [r.path for r in fake_server.requests if r.method == "POST"] == [f"/api/v3/checks/{HC_UUID_DATABASE}/resume"]
assert [p.name for p in locks.iterdir()] == ["other.lock"]
assert result.stderr == ""