70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
"""真实 API 冒烟测试: 起服务 → 注册 → 建要求 → 提交 → 轮询报告"""
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
import requests
|
|
|
|
BASE = "http://127.0.0.1:8765"
|
|
|
|
|
|
def main():
|
|
import flask
|
|
if not flask:
|
|
pass
|
|
server = subprocess.Popen(
|
|
[sys.executable, "manage.py", "runserver", "127.0.0.1:8765", "--noreload"],
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
|
cwd=os.path.dirname(os.path.abspath(__file__)),
|
|
)
|
|
try:
|
|
for _ in range(30):
|
|
try:
|
|
requests.get(BASE + "/api/requirements/", timeout=1)
|
|
break
|
|
except requests.RequestException:
|
|
time.sleep(1)
|
|
s = requests.Session()
|
|
r = s.post(BASE + "/api/auth/token/", data={"username": "smoke_user", "password": ""})
|
|
if r.status_code != 200:
|
|
from django.contrib.auth.models import User
|
|
import django
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ojplatform.settings")
|
|
django.setup()
|
|
User.objects.create_user("smoke_user", password="pass12345")
|
|
r = s.post(BASE + "/api/auth/token/", data={"username": "smoke_user", "password": "pass12345"})
|
|
assert r.status_code == 200, r.text
|
|
token = r.json()["token"]
|
|
s.headers["Authorization"] = f"Token {token}"
|
|
|
|
r = s.post(BASE + "/api/requirements/create/", json={
|
|
"title": "冒烟作业",
|
|
"content": "实现 web 服务, 首页返回 ok",
|
|
})
|
|
assert r.status_code == 201, r.text
|
|
req_id = r.json()["id"]
|
|
print("requirement:", req_id, r.json()["spec"].get("stability", {}).get("enabled"))
|
|
|
|
with open("/tmp/opencode/smoke/proj.tar", "rb") as fh:
|
|
r = s.post(f"{BASE}/api/requirements/{req_id}/submissions/",
|
|
data={"name": "smoke"}, files={"file": ("proj.tar", fh, "application/x-tar")})
|
|
assert r.status_code == 201, r.text
|
|
sub_id = r.json()["id"]
|
|
|
|
deadline = time.time() + 300
|
|
while time.time() < deadline:
|
|
r = s.get(f"{BASE}/api/submissions/{sub_id}/report/")
|
|
if r.status_code == 200:
|
|
print("REPORT OK:", str(r.json())[:400])
|
|
return 0
|
|
time.sleep(3)
|
|
print("TIMEOUT")
|
|
return 1
|
|
finally:
|
|
server.terminate()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|