tests: extract shared mock grafana server from jellyfin test
This commit is contained in:
@@ -4,22 +4,18 @@
|
||||
...
|
||||
}:
|
||||
let
|
||||
mockServer = pkgs.writeText "mock-server.py" ''
|
||||
import http.server, json, os, sys
|
||||
from urllib.parse import urlparse
|
||||
mockGrafana = ./mock-grafana-server.py;
|
||||
|
||||
MODE = sys.argv[1]
|
||||
PORT = int(sys.argv[2])
|
||||
DATA_FILE = sys.argv[3]
|
||||
mockJellyfin = pkgs.writeText "mock-jellyfin-server.py" ''
|
||||
import http.server, json, sys
|
||||
|
||||
PORT = int(sys.argv[1])
|
||||
DATA_FILE = sys.argv[2]
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
def log_message(self, fmt, *args):
|
||||
pass
|
||||
|
||||
def _read_body(self):
|
||||
length = int(self.headers.get("Content-Length", 0))
|
||||
return json.loads(self.rfile.read(length)) if length else {}
|
||||
|
||||
def _json(self, code, body):
|
||||
data = json.dumps(body).encode()
|
||||
self.send_response(code)
|
||||
@@ -28,7 +24,7 @@ let
|
||||
self.wfile.write(data)
|
||||
|
||||
def do_GET(self):
|
||||
if MODE == "jellyfin" and self.path.startswith("/Sessions"):
|
||||
if self.path.startswith("/Sessions"):
|
||||
try:
|
||||
with open(DATA_FILE) as f:
|
||||
sessions = json.load(f)
|
||||
@@ -39,43 +35,6 @@ let
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
|
||||
def do_POST(self):
|
||||
if MODE == "grafana" and self.path == "/api/annotations":
|
||||
body = self._read_body()
|
||||
try:
|
||||
with open(DATA_FILE) as f:
|
||||
annotations = json.load(f)
|
||||
except Exception:
|
||||
annotations = []
|
||||
aid = len(annotations) + 1
|
||||
body["id"] = aid
|
||||
annotations.append(body)
|
||||
with open(DATA_FILE, "w") as f:
|
||||
json.dump(annotations, f)
|
||||
self._json(200, {"id": aid, "message": "Annotation added"})
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
|
||||
def do_PATCH(self):
|
||||
if MODE == "grafana" and self.path.startswith("/api/annotations/"):
|
||||
aid = int(self.path.rsplit("/", 1)[-1])
|
||||
body = self._read_body()
|
||||
try:
|
||||
with open(DATA_FILE) as f:
|
||||
annotations = json.load(f)
|
||||
except Exception:
|
||||
annotations = []
|
||||
for a in annotations:
|
||||
if a["id"] == aid:
|
||||
a.update(body)
|
||||
with open(DATA_FILE, "w") as f:
|
||||
json.dump(annotations, f)
|
||||
self._json(200, {"message": "Annotation patched"})
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
|
||||
http.server.HTTPServer(("127.0.0.1", PORT), Handler).serve_forever()
|
||||
'';
|
||||
|
||||
@@ -102,7 +61,8 @@ pkgs.testers.runNixOSTest {
|
||||
STATE_FILE = "/tmp/annotations-state.json"
|
||||
CREDS_DIR = "/tmp/test-creds"
|
||||
PYTHON = "${python}/bin/python3"
|
||||
MOCK = "${mockServer}"
|
||||
MOCK_GRAFANA = "${mockGrafana}"
|
||||
MOCK_JELLYFIN = "${mockJellyfin}"
|
||||
SCRIPT = "${script}"
|
||||
|
||||
def read_annotations():
|
||||
@@ -119,10 +79,10 @@ pkgs.testers.runNixOSTest {
|
||||
|
||||
with subtest("Start mock Jellyfin and Grafana servers"):
|
||||
machine.succeed(
|
||||
f"systemd-run --unit=mock-jellyfin {PYTHON} {MOCK} jellyfin {JELLYFIN_PORT} {SESSIONS_FILE}"
|
||||
f"systemd-run --unit=mock-jellyfin {PYTHON} {MOCK_JELLYFIN} {JELLYFIN_PORT} {SESSIONS_FILE}"
|
||||
)
|
||||
machine.succeed(
|
||||
f"systemd-run --unit=mock-grafana {PYTHON} {MOCK} grafana {GRAFANA_PORT} {ANNOTS_FILE}"
|
||||
f"systemd-run --unit=mock-grafana {PYTHON} {MOCK_GRAFANA} {GRAFANA_PORT} {ANNOTS_FILE}"
|
||||
)
|
||||
machine.wait_until_succeeds(
|
||||
f"curl -sf http://127.0.0.1:{JELLYFIN_PORT}/Sessions", timeout=10
|
||||
|
||||
58
tests/mock-grafana-server.py
Normal file
58
tests/mock-grafana-server.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import http.server, json, sys
|
||||
|
||||
PORT = int(sys.argv[1])
|
||||
DATA_FILE = sys.argv[2]
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
def log_message(self, fmt, *args):
|
||||
pass
|
||||
|
||||
def _read_body(self):
|
||||
length = int(self.headers.get("Content-Length", 0))
|
||||
return json.loads(self.rfile.read(length)) if length else {}
|
||||
|
||||
def _json(self, code, body):
|
||||
data = json.dumps(body).encode()
|
||||
self.send_response(code)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
|
||||
def do_POST(self):
|
||||
if self.path == "/api/annotations":
|
||||
body = self._read_body()
|
||||
try:
|
||||
with open(DATA_FILE) as f:
|
||||
annotations = json.load(f)
|
||||
except Exception:
|
||||
annotations = []
|
||||
aid = len(annotations) + 1
|
||||
body["id"] = aid
|
||||
annotations.append(body)
|
||||
with open(DATA_FILE, "w") as f:
|
||||
json.dump(annotations, f)
|
||||
self._json(200, {"id": aid, "message": "Annotation added"})
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
|
||||
def do_PATCH(self):
|
||||
if self.path.startswith("/api/annotations/"):
|
||||
aid = int(self.path.rsplit("/", 1)[-1])
|
||||
body = self._read_body()
|
||||
try:
|
||||
with open(DATA_FILE) as f:
|
||||
annotations = json.load(f)
|
||||
except Exception:
|
||||
annotations = []
|
||||
for a in annotations:
|
||||
if a["id"] == aid:
|
||||
a.update(body)
|
||||
with open(DATA_FILE, "w") as f:
|
||||
json.dump(annotations, f)
|
||||
self._json(200, {"message": "Annotation patched"})
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
|
||||
http.server.HTTPServer(("127.0.0.1", PORT), Handler).serve_forever()
|
||||
Reference in New Issue
Block a user