100 lines
2.3 KiB
Python
100 lines
2.3 KiB
Python
import base64
|
|
import json
|
|
import secrets
|
|
import subprocess
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
BASE = "http://" + "127.0.0.1:8080"
|
|
username = "failure_" + secrets.token_hex(4)
|
|
password = "StrongPassword123!"
|
|
|
|
|
|
def request(method, path, body=None, authorization=None):
|
|
headers = {}
|
|
if body is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
if authorization:
|
|
headers["Authorization"] = authorization
|
|
|
|
req = urllib.request.Request(
|
|
BASE + path,
|
|
data=json.dumps(body).encode() if body is not None else None,
|
|
headers=headers,
|
|
method=method,
|
|
)
|
|
|
|
try:
|
|
response = urllib.request.urlopen(req, timeout=20)
|
|
except urllib.error.HTTPError as exc:
|
|
response = exc
|
|
|
|
with response:
|
|
raw = response.read()
|
|
return response.status, json.loads(raw) if raw else None
|
|
|
|
|
|
status, _ = request("POST", "/api/v1/users", {
|
|
"username": username,
|
|
"display_name": "Failure Test",
|
|
"password": password,
|
|
})
|
|
assert status == 201, status
|
|
|
|
basic = base64.b64encode(
|
|
(username + ":" + password).encode()
|
|
).decode()
|
|
|
|
status, result = request(
|
|
"POST",
|
|
"/api/v1/auth/sessions",
|
|
authorization="Basic " + basic,
|
|
)
|
|
assert status == 201, status
|
|
|
|
bearer = "Bearer " + result["token"]
|
|
|
|
status, _ = request(
|
|
"GET", "/api/v1/users/me", authorization=bearer
|
|
)
|
|
assert status == 200, status
|
|
|
|
print("PASS: token works before database failure")
|
|
|
|
try:
|
|
subprocess.run(
|
|
["docker", "compose", "stop", "postgres"],
|
|
check=True,
|
|
)
|
|
|
|
time.sleep(2)
|
|
|
|
status, _ = request(
|
|
"GET", "/api/v1/users/me", authorization=bearer
|
|
)
|
|
|
|
assert status == 503, status
|
|
print("PASS: database failure does not grant access:", status)
|
|
|
|
status, _ = request("GET", "/health/ready")
|
|
assert status == 503, status
|
|
|
|
finally:
|
|
subprocess.run(
|
|
["docker", "compose", "start", "postgres"],
|
|
check=True,
|
|
)
|
|
|
|
for _ in range(30):
|
|
try:
|
|
status, _ = request("GET", "/api/v1/users/me", authorization=bearer)
|
|
if status == 200:
|
|
break
|
|
except (OSError, TimeoutError):
|
|
pass
|
|
time.sleep(1)
|
|
else:
|
|
raise AssertionError("PostgreSQL did not recover")
|
|
print("ALL DATABASE FAILURE CHECKS PASSED; original session recovered")
|