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
|
#!/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, <name>.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())
|