#!/usr/bin/env python3 """HTTP tests for the julia-notebook (PlutoSliderServer) container. Checks that the server is up, lists the bundled notebooks, and actually serves the 'power' notebook -- the one that uses the packaged (Plots, Distributions, ...) Julia environment, so it runs offline from the shared JULIA_LOAD_PATH. Note: PlutoSliderServer on this stack (Julia 1.8, Pluto 0.19) can abort and be restarted by the wrapper, so the server is briefly unavailable in cycles. The client therefore retries until the server answers. Usage: BASE_URL=http://127.0.0.1:8890 python3 julia_notebook_http_tests.py """ import json import os import time import urllib.error import urllib.request BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8890") # How long to wait for a notebook to finish loading/running. TIMEOUT = int(os.environ.get("NOTEBOOK_TIMEOUT", "600")) TESTS = [] def test(name): def wrap(fn): TESTS.append((name, fn)) return fn return wrap def get(path, timeout=30, retry_for=None): """GET path, returning (status, body). Connection errors (the server being restarted) are retried for up to `retry_for` seconds (default: no retries beyond urllib's own). """ deadline = time.time() + (retry_for if retry_for is not None else 0) while True: req = urllib.request.Request(BASE_URL + path) try: with urllib.request.urlopen(req, timeout=timeout) as r: return r.status, r.read().decode("utf-8", "replace") except urllib.error.HTTPError as e: return e.code, e.read().decode("utf-8", "replace") except (urllib.error.URLError, ConnectionError, OSError): if time.time() >= deadline: return 0, "" time.sleep(2) def index_json(timeout=30, retry_for=None): status, body = get("/pluto_export.json", timeout=timeout, retry_for=retry_for) if status != 200: return None try: return json.loads(body) except ValueError: return None @test("t01 index page is served and lists notebooks") def t01(): deadline = time.time() + TIMEOUT while time.time() < deadline: status, body = get("/", retry_for=30) if status == 200 and "Notebooks" in body and "power" in body: return True time.sleep(5) return False @test("t02 pluto_export.json lists the bundled notebooks") def t02(): deadline = time.time() + TIMEOUT while time.time() < deadline: d = index_json(retry_for=30) if d is not None: names = set(d.get("notebooks", {})) return {"power.jl", "roc.jl", "gn-smoke-test.jl"} <= names time.sleep(5) return False @test("t03 the power notebook runs and is served") def t03(): # Wait for PlutoSliderServer to finish running the power notebook (it # uses only the packaged Julia environment, so it needs no network). deadline = time.time() + TIMEOUT while time.time() < deadline: d = index_json(retry_for=30) if d: nb = d.get("notebooks", {}).get("power.jl") if nb and nb.get("current_hash"): break time.sleep(5) else: return False # It is exposed as a static export, .html. deadline = time.time() + TIMEOUT while time.time() < deadline: status, body = get("/power.html", timeout=60, retry_for=30) if status == 200 and len(body) > 1000: return True time.sleep(5) return False def main(): passed = failed = 0 for name, fn in TESTS: try: ok = fn() except Exception as e: # noqa: BLE001 ok = False print(f"FAIL {name}: exception {e!r}") if ok: passed += 1 print(f"PASS {name}") else: failed += 1 print(f"FAIL {name}") print(f"== {passed} passed, {failed} failed ==") return 1 if failed else 0 if __name__ == "__main__": raise SystemExit(main())