Configure full Docker setup and confirm consequential actions
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
spec = importlib.util.spec_from_file_location("setup", Path(__file__).with_name("setup-docker.py"))
|
||||
setup = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(setup)
|
||||
|
||||
|
||||
class DockerSetupTests(unittest.TestCase):
|
||||
def exercise(self, directory, existing=False, fail=None, confirm="yes"):
|
||||
calls = []
|
||||
token = "tk_" + "a" * 29
|
||||
logged = existing
|
||||
|
||||
def runner(command, **kwargs):
|
||||
nonlocal logged
|
||||
args = command[2:]
|
||||
calls.append(args)
|
||||
code, out = 0, ""
|
||||
if args[-4:] == ["auth", "list", "--format", "json"]:
|
||||
out = '[{"connections":[{"id":"fixture"}]}]' if logged else '[]'
|
||||
if args[-2:] == ["auth", "login"] and confirm == "yes":
|
||||
logged = True
|
||||
if "token" in args:
|
||||
out = f"token {token} created\n"
|
||||
if args[-2:] == ["status", "--json"]:
|
||||
out = '{"ntfy":{"health":"healthy","url":"http://192.168.1.20:8090"},"topic":"pipelines"}'
|
||||
if args[:1] == ["port"]:
|
||||
out = "127.0.0.1:8081\n"
|
||||
if fail and fail in args:
|
||||
code, out = 9, "private provider data/token"
|
||||
return subprocess.CompletedProcess(command, code, stdout=out, stderr="")
|
||||
|
||||
args = setup.arguments([] if existing else ["--ntfy-url", "http://192.168.1.20:8090",
|
||||
"--bind-address", "192.168.1.20", "--timezone", "Europe/London"])
|
||||
installer = setup.Setup(directory, runner=runner, prompt=lambda _: confirm)
|
||||
installer.run(args)
|
||||
return calls, token
|
||||
|
||||
def test_fresh_setup_wires_notifications_and_runs_container_login(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
calls, token = self.exercise(directory)
|
||||
self.assertIn(["run", "--rm", "--no-deps", "--entrypoint", "opencode", "pipelines", "auth", "login"], calls)
|
||||
self.assertTrue(any("--force-recreate" in c for c in calls))
|
||||
self.assertEqual(Path(directory, "secrets/ntfy-token.txt").read_text(), token + "\n")
|
||||
self.assertFalse(any(token in " ".join(c) for c in calls))
|
||||
self.assertLess(next(i for i,c in enumerate(calls) if "token" in c), next(i for i,c in enumerate(calls) if "build" in c))
|
||||
self.assertLess(next(i for i,c in enumerate(calls) if "login" in c), next(i for i,c in enumerate(calls) if "--force-recreate" in c))
|
||||
self.assertTrue(any("curl --config -" in " ".join(c) for c in calls))
|
||||
|
||||
def test_reinstall_preserves_configuration_tokens_and_provider_setup(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
self.exercise(directory)
|
||||
files = [Path(directory, ".env"), Path(directory, "secrets/ntfy-token.txt")]
|
||||
before = [p.read_bytes() for p in files]
|
||||
calls, _ = self.exercise(directory, existing=True)
|
||||
self.assertFalse(any("login" in c or "token" in c for c in calls))
|
||||
self.assertEqual(before, [p.read_bytes() for p in files])
|
||||
|
||||
def test_login_failure_or_cancel_does_not_mark_complete_or_start_server(self):
|
||||
for fail, confirm in [("login", "yes"), (None, "no")]:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
self.exercise(directory, fail=fail, confirm=confirm)
|
||||
self.assertNotIn("private provider", str(raised.exception))
|
||||
self.assertTrue(Path(directory, "secrets/ntfy-token.txt").stat().st_size)
|
||||
|
||||
def test_status_and_publication_failures_are_not_success(self):
|
||||
for failure in ["status", "sh"]:
|
||||
with tempfile.TemporaryDirectory() as directory, self.assertRaises(ValueError):
|
||||
self.exercise(directory, fail=failure)
|
||||
|
||||
def test_existing_env_is_not_replaced_by_flags(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory, ".env")
|
||||
path.write_text("existing settings")
|
||||
with self.assertRaises(ValueError):
|
||||
setup.Setup(directory).settings(setup.arguments(["--ntfy-url", "https://example.com"]))
|
||||
self.assertEqual(path.read_text(), "existing settings")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user