|
| 1 | +import json |
1 | 2 | import os |
2 | 3 | import signal |
3 | 4 | import subprocess |
| 5 | +import sys |
4 | 6 | import tempfile |
5 | 7 | import textwrap |
6 | 8 | import time |
@@ -146,6 +148,41 @@ def test_runner_reuses_compatible_build_until_source_changes(self): |
146 | 148 | self.assertNotEqual(stale.returncode, 0) |
147 | 149 |
|
148 | 150 |
|
| 151 | +@unittest.skipUnless(os.name == "posix", "POSIX shell integration") |
| 152 | +class InstallerOwnershipCheckTests(unittest.TestCase): |
| 153 | + def test_unix_installers_ask_health_whether_they_own_the_server(self): |
| 154 | + root = Path(__file__).resolve().parents[1] |
| 155 | + for name in ("install", "install-linux"): |
| 156 | + script = (root / "scripts" / name).read_text(encoding="utf-8") |
| 157 | + with self.subTest(script=name): |
| 158 | + self.assertIn('ownership_root="$(cd "$INSTALL_ROOT" && pwd -P)"', script) |
| 159 | + self.assertIn('--data-urlencode "runtime=$ownership_root"', script) |
| 160 | + self.assertIn('payload.get("runtime_matches") is not True', script) |
| 161 | + self.assertNotIn("page_path", script) |
| 162 | + |
| 163 | + def test_unix_ownership_check_rejects_servers_that_do_not_confirm_the_runtime(self): |
| 164 | + root = Path(__file__).resolve().parents[1] |
| 165 | + for name in ("install", "install-linux"): |
| 166 | + script = (root / "scripts" / name).read_text(encoding="utf-8") |
| 167 | + start = script.index('printf \'%s\' "$ownership_json" | python3 -c \'') + len('printf \'%s\' "$ownership_json" | python3 -c \'') |
| 168 | + check = script[start:script.index("\n'\n", start)] |
| 169 | + for answer, owned in ( |
| 170 | + ({"ok": True, "runtime_matches": True}, True), |
| 171 | + ({"ok": True, "runtime_matches": False}, False), |
| 172 | + ({"ok": True, "page_path": "/elsewhere/page.html"}, False), |
| 173 | + ("not json", False), |
| 174 | + ): |
| 175 | + with self.subTest(script=name, answer=answer): |
| 176 | + result = subprocess.run( |
| 177 | + [sys.executable, "-I", "-c", check], |
| 178 | + input=answer if isinstance(answer, str) else json.dumps(answer), |
| 179 | + capture_output=True, text=True, |
| 180 | + ) |
| 181 | + self.assertEqual(result.returncode == 0, owned, result.stderr) |
| 182 | + if not owned: |
| 183 | + self.assertIn("a different server owns port 8722", result.stderr) |
| 184 | + |
| 185 | + |
149 | 186 | @unittest.skipUnless(os.name == "posix", "POSIX shell integration") |
150 | 187 | class InstallerModeTests(unittest.TestCase): |
151 | 188 | def write_executable(self, path, source): |
@@ -236,7 +273,8 @@ def installer_environment(self, workspace, platform): |
236 | 273 | """ |
237 | 274 | #!/usr/bin/env bash |
238 | 275 | if [[ "${*: -1}" == */health ]]; then |
239 | | - printf '{"ok": true, "state_ready": true, "page_path": "%s/page.html"}\n' "$TEST_INSTALL_ROOT" |
| 276 | + owned=false; [[ "$*" == *"runtime=$(cd "$TEST_INSTALL_ROOT" 2>/dev/null && pwd -P)"* ]] && owned=true |
| 277 | + printf '{"ok": true, "state_ready": true, "runtime_matches": %s, "page_path": "%s/page.html"}\n' "$owned" "$TEST_INSTALL_ROOT" |
240 | 278 | else |
241 | 279 | printf '{"ok": true}\n' |
242 | 280 | fi |
@@ -401,7 +439,8 @@ def test_installers_draw_and_clear_a_spinner_on_a_terminal(self): |
401 | 439 | count=$(( $(cat {str(polls)!r} 2>/dev/null || echo 0) + 1 )) |
402 | 440 | echo "$count" > {str(polls)!r} |
403 | 441 | if (( count < 3 )); then exit 7; fi |
404 | | - printf '{{"ok": true, "state_ready": true, "page_path": "%s/page.html"}}\n' "$TEST_INSTALL_ROOT" |
| 442 | + owned=false; [[ "$*" == *"runtime=$(cd "$TEST_INSTALL_ROOT" 2>/dev/null && pwd -P)"* ]] && owned=true |
| 443 | + printf '{{"ok": true, "state_ready": true, "runtime_matches": %s, "page_path": "%s/page.html"}}\n' "$owned" "$TEST_INSTALL_ROOT" |
405 | 444 | else |
406 | 445 | printf '{{"ok": true}}\n' |
407 | 446 | fi |
@@ -522,7 +561,8 @@ def test_menu_bar_builds_while_the_server_indexes(self): |
522 | 561 | #!/usr/bin/env bash |
523 | 562 | if [[ "${{*: -1}}" == */health ]]; then |
524 | 563 | [[ -e {str(built)!r} ]] || exit 7 |
525 | | - printf '{{"ok": true, "state_ready": true, "page_path": "%s/page.html"}}\n' "$TEST_INSTALL_ROOT" |
| 564 | + owned=false; [[ "$*" == *"runtime=$(cd "$TEST_INSTALL_ROOT" 2>/dev/null && pwd -P)"* ]] && owned=true |
| 565 | + printf '{{"ok": true, "state_ready": true, "runtime_matches": %s, "page_path": "%s/page.html"}}\n' "$owned" "$TEST_INSTALL_ROOT" |
526 | 566 | else |
527 | 567 | printf '{{"ok": true}}\n' |
528 | 568 | fi |
@@ -574,7 +614,8 @@ def test_missing_toolchain_never_runs_swiftc_before_the_server_is_ready(self): |
574 | 614 | #!/usr/bin/env bash |
575 | 615 | if [[ "${{*: -1}}" == */health ]]; then |
576 | 616 | touch {str(ready)!r} |
577 | | - printf '{{"ok": true, "state_ready": true, "page_path": "%s/page.html"}}\n' "$TEST_INSTALL_ROOT" |
| 617 | + owned=false; [[ "$*" == *"runtime=$(cd "$TEST_INSTALL_ROOT" 2>/dev/null && pwd -P)"* ]] && owned=true |
| 618 | + printf '{{"ok": true, "state_ready": true, "runtime_matches": %s, "page_path": "%s/page.html"}}\n' "$owned" "$TEST_INSTALL_ROOT" |
578 | 619 | else |
579 | 620 | printf '{{"ok": true}}\n' |
580 | 621 | fi |
|
0 commit comments