forked from longtao.wu/droboticflow
193 lines
6.9 KiB
Python
193 lines
6.9 KiB
Python
import json
|
|
import os
|
|
import subprocess
|
|
import threading
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SCRIPT = ROOT / "scripts" / "import_poc_acceptance_result.py"
|
|
|
|
|
|
def test_import_poc_acceptance_result_posts_platform_payload(tmp_path):
|
|
received = {}
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def do_POST(self):
|
|
length = int(self.headers.get("Content-Length", "0"))
|
|
received["path"] = self.path
|
|
received["body"] = json.loads(self.rfile.read(length).decode("utf-8"))
|
|
self.send_response(201)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps({"run": {"id": "run-platform-acceptance"}}).encode("utf-8"))
|
|
|
|
def log_message(self, *_args):
|
|
return
|
|
|
|
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
|
thread = threading.Thread(target=server.serve_forever, daemon=True)
|
|
thread.start()
|
|
try:
|
|
result_file = tmp_path / "result.txt"
|
|
result_file.write_text(
|
|
"DROBOTICFLOW_POC_OPERATOR_ACCEPTANCE="
|
|
+ json.dumps(
|
|
{
|
|
"runId": "poc-operator-acceptance-test",
|
|
"status": "passed",
|
|
"acceptanceScope": "dependency-isolation",
|
|
"fullExternalAcceptance": False,
|
|
"operatorCount": 28,
|
|
"operatorPassedCount": 26,
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
result = subprocess.run(
|
|
[
|
|
"python3",
|
|
str(SCRIPT),
|
|
"--api-base-url",
|
|
f"http://127.0.0.1:{server.server_port}",
|
|
"--result-file",
|
|
str(result_file),
|
|
"--environment",
|
|
"poc",
|
|
"--namespace",
|
|
"droboticflow",
|
|
"--job-name",
|
|
"acceptance-job",
|
|
"--runner-image",
|
|
"registry/runner:tag",
|
|
"--runner-image-id",
|
|
"registry/runner@sha256:" + "b" * 64,
|
|
],
|
|
cwd=ROOT,
|
|
env={**os.environ, "NO_PROXY": "127.0.0.1,localhost", "no_proxy": "127.0.0.1,localhost"},
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
finally:
|
|
server.shutdown()
|
|
thread.join(timeout=5)
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
assert json.loads(result.stdout) == {"imported": True, "runId": "run-platform-acceptance"}
|
|
assert received["path"] == "/api/acceptance/poc-operator-result"
|
|
assert received["body"]["result"]["runId"] == "poc-operator-acceptance-test"
|
|
assert received["body"]["source"] == {
|
|
"environment": "poc",
|
|
"namespace": "droboticflow",
|
|
"jobName": "acceptance-job",
|
|
"image": "registry/runner:tag",
|
|
"imageId": "registry/runner@sha256:" + "b" * 64,
|
|
}
|
|
|
|
|
|
def test_import_poc_acceptance_result_requires_api_base_url(tmp_path):
|
|
result_file = tmp_path / "result.json"
|
|
result_file.write_text("{}", encoding="utf-8")
|
|
|
|
result = subprocess.run(
|
|
["python3", str(SCRIPT), "--result-file", str(result_file)],
|
|
cwd=ROOT,
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
|
|
assert result.returncode != 0
|
|
assert "--api-base-url or API_BASE_URL is required" in result.stderr
|
|
|
|
|
|
def test_import_poc_acceptance_result_logs_in_when_session_credentials_are_configured(tmp_path):
|
|
received = {}
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def do_POST(self):
|
|
length = int(self.headers.get("Content-Length", "0"))
|
|
if self.path == "/api/auth/login":
|
|
received["login"] = json.loads(self.rfile.read(length).decode("utf-8"))
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("Set-Cookie", "droboticflow_session=session-1; Path=/; HttpOnly")
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps({"email": received["login"]["email"]}).encode("utf-8"))
|
|
return
|
|
if self.path == "/api/acceptance/poc-operator-result":
|
|
received["acceptanceCookie"] = self.headers.get("Cookie", "")
|
|
received["acceptanceBody"] = json.loads(self.rfile.read(length).decode("utf-8"))
|
|
self.send_response(201)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps({"run": {"id": "run-with-session"}}).encode("utf-8"))
|
|
return
|
|
self.send_response(404)
|
|
self.end_headers()
|
|
|
|
def log_message(self, *_args):
|
|
return
|
|
|
|
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
|
thread = threading.Thread(target=server.serve_forever, daemon=True)
|
|
thread.start()
|
|
try:
|
|
result_file = tmp_path / "result.json"
|
|
result_file.write_text(json.dumps({"runId": "poc-session-import", "status": "failed"}), encoding="utf-8")
|
|
result = subprocess.run(
|
|
[
|
|
"python3",
|
|
str(SCRIPT),
|
|
"--api-base-url",
|
|
f"http://127.0.0.1:{server.server_port}",
|
|
"--result-file",
|
|
str(result_file),
|
|
"--acceptance-email",
|
|
"admin@example.test",
|
|
"--acceptance-password",
|
|
"password",
|
|
],
|
|
cwd=ROOT,
|
|
env={**os.environ, "NO_PROXY": "127.0.0.1,localhost", "no_proxy": "127.0.0.1,localhost"},
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
finally:
|
|
server.shutdown()
|
|
thread.join(timeout=5)
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
assert json.loads(result.stdout.splitlines()[-1]) == {"imported": True, "runId": "run-with-session"}
|
|
assert received["login"] == {"email": "admin@example.test", "password": "password"}
|
|
assert "droboticflow_session=session-1" in received["acceptanceCookie"]
|
|
assert received["acceptanceBody"]["result"]["runId"] == "poc-session-import"
|
|
|
|
|
|
def test_import_poc_acceptance_result_rejects_unpaired_session_credentials(tmp_path):
|
|
result_file = tmp_path / "result.json"
|
|
result_file.write_text("{}", encoding="utf-8")
|
|
|
|
result = subprocess.run(
|
|
[
|
|
"python3",
|
|
str(SCRIPT),
|
|
"--api-base-url",
|
|
"http://127.0.0.1:9",
|
|
"--result-file",
|
|
str(result_file),
|
|
"--acceptance-email",
|
|
"admin@example.test",
|
|
],
|
|
cwd=ROOT,
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
|
|
assert result.returncode != 0
|
|
assert "DROBOTICFLOW_ACCEPTANCE_EMAIL and DROBOTICFLOW_ACCEPTANCE_PASSWORD must be set together" in result.stderr
|