The webmail restarted under open tabs (K-W5) #36

Merged
jcoffey-dev merged 1 commits from feat/webmail-restart-test into main 2026-11-20 07:26:48 +00:00
2 changed files with 227 additions and 0 deletions

No files matched your search

+100
View File
@@ -0,0 +1,100 @@
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2026 Coffey Labs LLC
# SPDX-License-Identifier: Apache-2.0
#
# K-W5: the webmail restarted under open tabs. webmail-tabs.py runs in a
# pod in the cluster (namespace lab-sink, the CloudNativePG image's Python)
# and goes through the webmail Service: three tabs holding live-update
# streams, and a JMAP request every 200 ms. Meanwhile the webmail
# Deployment is restarted, one pod at a time.
#
# Passes when no request failed and every stream that ended, ended cleanly
# and came back: what a browser shows no error for.
#
# lab/chart/webmail-restart-test.sh
# WEBMAIL_IMAGE=inbuxa-lab/webmail:kw5 lab/chart/webmail-restart-test.sh
#
# WEBMAIL_IMAGE (a local image loaded into kind) installs that webmail
# first, with values-frontends.yaml; otherwise the installed one is tested.
set -euo pipefail
cd "$(dirname "$0")"
. ../versions.env
KCTX="kind-$KIND_CLUSTER"
K=(kubectl --context "$KCTX")
CHART="${CHART:-../../../inbuxa-charts/charts/inbuxa}"
STATE="${STATE:-$HOME/.cache/inbuxa-lab}"
fail() { echo "!! $*" >&2; exit 1; }
trap '"${K[@]}" -n lab-sink delete pod webmail-tabs --ignore-not-found --wait=false >/dev/null 2>&1 || true' EXIT
if [ -n "${WEBMAIL_IMAGE:-}" ]; then
echo "== installing the webmail $WEBMAIL_IMAGE"
helm --kube-context "$KCTX" -n inbuxa upgrade inbuxa "$CHART" --reuse-values \
--set webmail.image.repository="${WEBMAIL_IMAGE%:*}" --set webmail.image.tag="${WEBMAIL_IMAGE##*:}" \
--set webmail.image.digest= --set webmail.image.pullPolicy=Never --wait --timeout 10m >/dev/null
"${K[@]}" -n inbuxa rollout status deploy/inbuxa-webmail --timeout=5m >/dev/null
fi
echo "== the tabs, inside the cluster"
"${K[@]}" create namespace lab-sink --dry-run=client -o yaml | "${K[@]}" apply -f - >/dev/null
"${K[@]}" -n lab-sink create configmap webmail-tabs --from-file=webmail-tabs.py --dry-run=client -o yaml | "${K[@]}" apply -f - >/dev/null
"${K[@]}" -n lab-sink create secret generic webmail-tabs --from-file=password="$STATE/alice.password" \
--dry-run=client -o yaml | "${K[@]}" apply -f - >/dev/null
"${K[@]}" -n lab-sink delete pod webmail-tabs --ignore-not-found --wait >/dev/null
"${K[@]}" -n lab-sink apply -f - >/dev/null <<EOF
apiVersion: v1
kind: Pod
metadata: {name: webmail-tabs}
spec:
restartPolicy: Never
automountServiceAccountToken: false
securityContext: {runAsNonRoot: true, runAsUser: 2000, runAsGroup: 2000, fsGroup: 2000, seccompProfile: {type: RuntimeDefault}}
containers:
- name: tabs
image: $PG_IMAGE
imagePullPolicy: IfNotPresent
command: [python3, -u, /tabs/webmail-tabs.py, "http://inbuxa-webmail.inbuxa.svc:8080", "http://inbuxa-http.inbuxa.svc:8080"]
env:
- {name: ACCOUNT, value: [email protected]}
- {name: PASSWORD_FILE, value: /secret/password}
- {name: STOP_FILE, value: /tmp/stop}
securityContext: {allowPrivilegeEscalation: false, readOnlyRootFilesystem: true, capabilities: {drop: [ALL]}}
volumeMounts:
- {name: tabs, mountPath: /tabs}
- {name: secret, mountPath: /secret}
- {name: tmp, mountPath: /tmp}
volumes:
- {name: tabs, configMap: {name: webmail-tabs}}
- {name: secret, secret: {secretName: webmail-tabs, defaultMode: 0440}}
- {name: tmp, emptyDir: {}}
EOF
"${K[@]}" -n lab-sink wait --for=condition=Ready pod/webmail-tabs --timeout=120s >/dev/null
sleep 15
if [ "$("${K[@]}" -n lab-sink get pod webmail-tabs -o jsonpath='{.status.phase}')" != Running ]; then
"${K[@]}" -n lab-sink logs webmail-tabs | tail -20 >&2
fail "the tabs stopped before the restart"
fi
image="$("${K[@]}" -n inbuxa get deploy inbuxa-webmail -o jsonpath='{.spec.template.spec.containers[0].image}')"
echo "== restarting the webmail ($image) under them"
"${K[@]}" -n inbuxa rollout restart deploy/inbuxa-webmail >/dev/null
"${K[@]}" -n inbuxa rollout status deploy/inbuxa-webmail --timeout=5m >/dev/null
sleep 20
"${K[@]}" -n lab-sink exec webmail-tabs -- touch /tmp/stop
"${K[@]}" -n lab-sink wait --for=jsonpath='{.status.phase}'=Succeeded pod/webmail-tabs --timeout=120s >/dev/null \
|| fail "the tabs didn't finish"
out="$("${K[@]}" -n lab-sink logs webmail-tabs | tail -1)"
python3 - "$out" <<'PY'
import json, sys
r = json.loads(sys.argv[1])
print(f" requests: {r['requests']} made, {r['failed']} failed")
for i, t in enumerate(r["tabs"]):
print(f" tab {i}: {t['streams']} streams, {t['clean_ends']} ended cleanly, {t['cut_off']} cut off, "
f"longest wait for an event {t['longest_gap_s']} s")
for e in r["errors"]:
print(f" {e}")
bad = r["failed"] > 0 or any(t["cut_off"] > 0 or t["streams"] < 2 for t in r["tabs"])
print("== " + ("FAIL: a tab would have shown an error" if bad else "no request failed; every stream ended cleanly and came back"))
sys.exit(1 if bad else 0)
PY
+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2026 Coffey Labs LLC
# SPDX-License-Identifier: Apache-2.0
"""Open webmail tabs through a restart (K-W5), run inside the cluster so
every connection goes through the webmail Service, as a browser's would
through an ingress.
ACCOUNT=... PASSWORD_FILE=... STOP_FILE=... webmail-tabs.py WEBMAIL SERVER
Signs in once, then until STOP_FILE appears:
- TABS tabs (default 3), each holding /api/events open as EventSource
does: when a stream ends it reconnects after a second
- a JMAP Mailbox/get every 200 ms, as a busy tab's requests
and prints one JSON line: requests made and failed (with the errors), and
per tab how its streams ended (cleanly, or cut off mid-stream) and the
longest wait for a stream to deliver again. A clean end is what the
browser takes in its stride; a failed request is what shows an error.
"""
import http.client, json, os, sys, threading, time, urllib.parse
webmail, server = sys.argv[1:3]
account = os.environ["ACCOUNT"]
password = open(os.environ["PASSWORD_FILE"]).read().strip()
stop = os.environ["STOP_FILE"]
TABS = int(os.environ.get("TABS", "3"))
def request(base, method, path, headers=None, body=None, timeout=30):
u = urllib.parse.urlsplit(base)
conn = http.client.HTTPConnection(u.hostname, u.port, timeout=timeout)
try:
conn.request(method, path, body=body, headers=headers or {})
res = conn.getresponse()
return res, res.read()
finally:
conn.close()
def cookie(res, name):
for header, value in res.getheaders():
if header.lower() == "set-cookie" and value.startswith(name + "="):
return value.split(";", 1)[0].split("=", 1)[1]
return None
def sign_in():
res, _ = request(webmail, "GET", "/api/auth/oauth/start?username=" + urllib.parse.quote(account))
q = dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(res.getheader("location")).query))
bound = cookie(res, "ihm_session_signin")
res, data = request(server, "POST", "/api/auth", {"content-type": "application/json"}, json.dumps({
"type": "authCode", "accountName": account, "accountSecret": password, "clientId": q["client_id"],
"redirectUri": q["redirect_uri"], "scope": q.get("scope"), "codeChallenge": q["code_challenge"],
"codeChallengeMethod": q["code_challenge_method"], "state": q["state"]}))
a = json.loads(data)
code = a.get("client_code") or a.get("clientCode")
res, _ = request(webmail, "GET", "/api/auth/callback?" + urllib.parse.urlencode({"code": code, "state": q["state"]}),
{"cookie": f"ihm_session_signin={bound}"})
session = cookie(res, "ihm_session")
res, data = request(webmail, "GET", "/api/auth/session", {"cookie": f"ihm_session={session}"})
return session, json.loads(data)["primaryAccounts"]["urn:ietf:params:jmap:mail"]
session, mail_account = sign_in()
H = {"cookie": f"ihm_session={session}"}
lock = threading.Lock()
report = {"requests": 0, "failed": 0, "errors": [], "tabs": []}
def tab(i):
me = {"streams": 0, "clean_ends": 0, "cut_off": 0, "longest_gap_s": 0.0}
with lock:
report["tabs"].append(me)
last_event = time.time()
u = urllib.parse.urlsplit(webmail)
while not os.path.exists(stop):
try:
conn = http.client.HTTPConnection(u.hostname, u.port, timeout=45)
conn.request("GET", "/api/events?types=*&closeafter=no&ping=30", headers=dict(H, accept="text/event-stream"))
res = conn.getresponse()
if res.status != 200:
raise RuntimeError(f"HTTP {res.status}")
me["streams"] += 1
while not os.path.exists(stop):
line = res.readline()
if not line:
me["clean_ends"] += 1 # the stream ended properly
break
if line.startswith((b"event:", b":")):
now = time.time()
me["longest_gap_s"] = max(me["longest_gap_s"], round(now - last_event, 1))
last_event = now
conn.close()
except Exception as e: # reset, refused, an incomplete chunk: cut off
if os.path.exists(stop):
break
me["cut_off"] += 1
with lock:
report["errors"].append(f"tab {i} stream: {type(e).__name__}: {e}"[:160])
time.sleep(1) # EventSource's first retry, as push.ts does
threads = [threading.Thread(target=tab, args=(i,), daemon=True) for i in range(TABS)]
for t in threads:
t.start()
body = json.dumps({"using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
"methodCalls": [["Mailbox/get", {"accountId": mail_account, "properties": ["name"]}, "0"]]})
while not os.path.exists(stop):
with lock:
report["requests"] += 1
try:
res, data = request(webmail, "POST", "/api/jmap", dict(H, **{"content-type": "application/json",
"x-requested-with": "ihasmail"}), body)
if res.status != 200 or b"Mailbox/get" not in data:
raise RuntimeError(f"HTTP {res.status} {data[:80]!r}")
except Exception as e:
with lock:
report["failed"] += 1
report["errors"].append(f"request: {type(e).__name__}: {e}"[:160])
time.sleep(0.2)
for t in threads:
t.join(timeout=50)
report["errors"] = report["errors"][:15]
print(json.dumps(report))