Files
douyi_guaji_main/tests/test_log_upload.py
T

171 lines
6.8 KiB
Python

from __future__ import annotations
import json
import zipfile
from datetime import datetime, timedelta, timezone
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM
def test_build_diagnostic_log_archive_tails_logs_only(tmp_path, monkeypatch):
from douyin.core import path_config
from douyin.diagnostics import log_upload
logs_dir = tmp_path / "logs"
logs_dir.mkdir()
(logs_dir / "unified.log").write_text("a" * 40, encoding="utf-8")
(logs_dir / "actions.log").write_text("action\n", encoding="utf-8")
(tmp_path / "license.dat").write_text("secret", encoding="utf-8")
pic_dir = tmp_path / "pic"
state_dir = tmp_path / "state"
pic_dir.mkdir()
state_dir.mkdir()
monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir))
monkeypatch.setattr(path_config, "get_pic_dir", lambda: str(pic_dir))
monkeypatch.setattr(path_config, "get_state_dir", lambda: str(state_dir))
monkeypatch.setattr(path_config, "get_config_path", lambda: str(tmp_path / "missing-config.json"))
archive = log_upload.build_diagnostic_log_archive(
output_dir=tmp_path,
max_source_bytes=10,
max_archive_bytes=1024 * 1024,
)
with zipfile.ZipFile(archive) as zf:
names = set(zf.namelist())
assert "logs/unified.log" in names
assert "logs/actions.log" in names
assert "license.dat" not in names
assert zf.read("logs/unified.log") == b"a" * 10
def test_build_diagnostic_log_archive_includes_runtime_context_and_recent_images(tmp_path, monkeypatch):
from douyin.core import path_config
from douyin.diagnostics import log_upload
logs_dir = tmp_path / "logs"
logs_dir.mkdir()
pic_dir = tmp_path / "pic"
pic_dir.mkdir()
state_dir = tmp_path / "state"
state_dir.mkdir()
cfg_path = tmp_path / "config.json"
(logs_dir / "screenshot_calls.jsonl").write_text("call\n", encoding="utf-8")
(pic_dir / "screenshot.png").write_bytes(b"\x89PNG\r\n\x1a\n" + b"x" * 2048)
(pic_dir / "screenshot.png.tmp").write_bytes(b"broken screenshot output")
(pic_dir / "latest.png").write_bytes(b"\x89PNG\r\n\x1a\n" + b"y" * 2048)
(state_dir / "room_pos_map.json").write_text('{"1": "room"}', encoding="utf-8")
cfg_path.write_text(
json.dumps(
{
"device_id": "dev-1",
"license": {"card_key": "DG-SHOULD-NOT-LEAK"},
"api_token": "secret",
},
ensure_ascii=False,
),
encoding="utf-8",
)
monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir))
monkeypatch.setattr(path_config, "get_pic_dir", lambda: str(pic_dir))
monkeypatch.setattr(path_config, "get_state_dir", lambda: str(state_dir))
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg_path))
archive = log_upload.build_diagnostic_log_archive(output_dir=tmp_path, max_archive_bytes=1024 * 1024)
with zipfile.ZipFile(archive) as zf:
names = set(zf.namelist())
assert "logs/screenshot_calls.jsonl" in names
assert "pic/screenshot.png" in names
assert "pic/screenshot.png.tmp" in names
assert "pic/latest.png" in names
assert "state/room_pos_map.json" in names
assert "config/config.redacted.json" in names
assert "diagnostics/environment.json" in names
redacted = json.loads(zf.read("config/config.redacted.json").decode("utf-8"))
assert redacted["license"]["card_key"] == "<redacted>"
assert redacted["api_token"] == "<redacted>"
def test_license_server_log_upload_overwrites_same_card(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
card = service.create_cards(days=5, count=1, customer="log-user")[0]
activated = service.activate(card.code, machine_fingerprint="machine-api")
assert activated.license_doc is not None
client = TestClient(api.create_app(service=service))
first = client.post(
"/api/v1/logs/upload",
data={"license": json.dumps(activated.license_doc, ensure_ascii=False)},
files={"log_file": ("one.zip", b"first", "application/zip")},
)
second = client.post(
"/api/v1/logs/upload",
data={"license": json.dumps(activated.license_doc, ensure_ascii=False)},
files={"log_file": ("two.zip", b"second", "application/zip")},
)
entries = service.list_diagnostic_logs()
stored = service.diagnostic_log_path(entries[0].key)
assert first.status_code == 200
assert second.status_code == 200
assert len(entries) == 1
assert entries[0].filename == "two.zip"
assert stored is not None
assert stored.read_bytes() == b"second"
def test_upload_diagnostic_logs_posts_signed_license_and_file(tmp_path, monkeypatch):
from tools.license_dev import create_dev_license
from douyin.core import path_config
from douyin.license import license_manager
from douyin.diagnostics import log_upload
logs_dir = tmp_path / "logs"
logs_dir.mkdir()
(logs_dir / "unified.log").write_text("hello\n", encoding="utf-8")
license_path = tmp_path / "license.dat"
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(log_upload, "get_default_license_path", lambda: license_path)
monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir))
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=3),
private_key_pem=TEST_PRIVATE_KEY_PEM,
)
calls = {}
def fake_post(url, *, license_doc, file_path, timeout_seconds=20):
calls["url"] = url
calls["license_doc"] = license_doc
calls["file_path"] = file_path
calls["timeout_seconds"] = timeout_seconds
return {"ok": True, "message": "ok"}
result = log_upload.upload_diagnostic_logs(
upload_url="https://license.example.test/api/v1/logs/upload",
http_post_file=fake_post,
)
assert result.ok is True
assert calls["license_doc"]["payload"]["machine_fingerprint"] == "machine-a"
assert calls["file_path"].exists()
assert calls["file_path"].stat().st_size <= log_upload.DEFAULT_MAX_ARCHIVE_BYTES
assert calls["file_path"].stat().st_size <= 500 * 1024 * 1024
assert calls.get("timeout_seconds") == 120
def test_log_upload_defaults_allow_500mb_archives():
from douyin.diagnostics import log_upload
from license_server import api
assert log_upload.DEFAULT_MAX_ARCHIVE_BYTES == 500 * 1024 * 1024
assert api.DEFAULT_MAX_DIAGNOSTIC_LOG_UPLOAD_BYTES == 500 * 1024 * 1024