mirror of
https://github.com/azaion/loader.git
synced 2026-04-23 01:26:32 +00:00
8f7deb3fca
Made-with: Cursor
67 lines
1.8 KiB
Python
67 lines
1.8 KiB
Python
import os
|
|
import subprocess
|
|
import time
|
|
|
|
|
|
COMPOSE_FILE = os.path.join(os.path.dirname(__file__), "..", "docker-compose.test.yml")
|
|
|
|
|
|
def _compose_exec(cmd: str):
|
|
subprocess.run(
|
|
["docker", "compose", "-f", COMPOSE_FILE, "exec", "system-under-test",
|
|
"bash", "-c", cmd],
|
|
capture_output=True, timeout=15,
|
|
)
|
|
|
|
|
|
def _wait_for_settled(base_url, client, timeout=30):
|
|
deadline = time.monotonic() + timeout
|
|
while time.monotonic() < deadline:
|
|
resp = client.get(f"{base_url}/unlock/status")
|
|
state = resp.json()["state"]
|
|
if state in ("idle", "error", "ready"):
|
|
return state
|
|
time.sleep(0.5)
|
|
return None
|
|
|
|
|
|
def test_unlock_status_idle(base_url, api_client):
|
|
# Act
|
|
response = api_client.get(f"{base_url}/unlock/status")
|
|
|
|
# Assert
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["state"] == "idle"
|
|
assert data["error"] is None
|
|
|
|
|
|
def test_unlock_missing_archive(base_url, api_client):
|
|
# Arrange
|
|
payload = {"email": "test@azaion.com", "password": "testpass"}
|
|
|
|
# Act
|
|
response = api_client.post(f"{base_url}/unlock", json=payload)
|
|
|
|
# Assert
|
|
assert response.status_code == 404
|
|
|
|
|
|
def test_unlock_concurrent_returns_current_state(base_url, api_client):
|
|
# Arrange
|
|
_compose_exec("dd if=/dev/urandom of=/tmp/test.enc bs=1024 count=1 2>/dev/null")
|
|
payload = {"email": "test@azaion.com", "password": "testpass"}
|
|
|
|
try:
|
|
# Act
|
|
first = api_client.post(f"{base_url}/unlock", json=payload)
|
|
second = api_client.post(f"{base_url}/unlock", json=payload)
|
|
|
|
# Assert
|
|
assert first.status_code == 200
|
|
assert second.status_code == 200
|
|
assert second.json()["state"] != "idle"
|
|
finally:
|
|
_compose_exec("rm -f /tmp/test.enc /tmp/test.tar")
|
|
_wait_for_settled(base_url, api_client)
|