|
|
|
|
@ -5,12 +5,15 @@ import base64
|
|
|
|
|
import asyncio
|
|
|
|
|
import hashlib
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import struct
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
@ -2144,6 +2147,459 @@ def test_streamed_iso_digest_must_match_before_import_is_queued(boundary):
|
|
|
|
|
assert engine.registry.components["hk"].store.list("rescue_import_job") == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cloud_upload_fixture(size=1024 * 1024 + 257):
|
|
|
|
|
header = bytearray(104)
|
|
|
|
|
header[:4] = b"QFI\xfb"
|
|
|
|
|
header[4:8] = struct.pack(">I", 3)
|
|
|
|
|
header[20:24] = struct.pack(">I", 16)
|
|
|
|
|
header[24:32] = struct.pack(">Q", 20 * 1024**3)
|
|
|
|
|
header[100:104] = struct.pack(">I", 104)
|
|
|
|
|
return bytes(header) + b"\0" * (size - len(header))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_cloud_upload_job(client, job_id):
|
|
|
|
|
path = f"/internal/v1/vm-templates/import-jobs/{job_id}"
|
|
|
|
|
for _ in range(200):
|
|
|
|
|
response = call(client, "GET", path, SUPER)
|
|
|
|
|
assert response.status_code == 200, response.text
|
|
|
|
|
job = response.json()
|
|
|
|
|
if job["status"] in {"completed", "failed"}:
|
|
|
|
|
return job
|
|
|
|
|
time.sleep(0.01)
|
|
|
|
|
raise AssertionError("uploaded cloud template import did not finish")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cloud_engine_scope(method, target, body, actor, *, headers=None):
|
|
|
|
|
path, separator, query = target.partition("?")
|
|
|
|
|
request_headers = signed_headers(SECRET, method, target, body, actor)
|
|
|
|
|
request_headers.update(headers or {})
|
|
|
|
|
return {
|
|
|
|
|
"type": "http",
|
|
|
|
|
"asgi": {"version": "3.0"},
|
|
|
|
|
"http_version": "1.1",
|
|
|
|
|
"method": method,
|
|
|
|
|
"scheme": "http",
|
|
|
|
|
"path": path,
|
|
|
|
|
"raw_path": path.encode("ascii"),
|
|
|
|
|
"query_string": query.encode("ascii") if separator else b"",
|
|
|
|
|
"headers": [
|
|
|
|
|
(name.lower().encode("ascii"), value.encode("latin-1"))
|
|
|
|
|
for name, value in request_headers.items()
|
|
|
|
|
],
|
|
|
|
|
"client": ("testclient", 50000),
|
|
|
|
|
"server": ("127.0.0.1", 80),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def cloud_engine_request(engine, method, target, body, actor, *,
|
|
|
|
|
headers=None, receive=None):
|
|
|
|
|
scope = cloud_engine_scope(
|
|
|
|
|
method, target, body, actor, headers=headers)
|
|
|
|
|
delivered = False
|
|
|
|
|
|
|
|
|
|
async def receive_body():
|
|
|
|
|
nonlocal delivered
|
|
|
|
|
if delivered:
|
|
|
|
|
return {"type": "http.disconnect"}
|
|
|
|
|
delivered = True
|
|
|
|
|
return {"type": "http.request", "body": body, "more_body": False}
|
|
|
|
|
|
|
|
|
|
messages = []
|
|
|
|
|
|
|
|
|
|
async def send(message):
|
|
|
|
|
messages.append(message)
|
|
|
|
|
|
|
|
|
|
await engine(scope, receive or receive_body, send)
|
|
|
|
|
status = next(message["status"] for message in messages
|
|
|
|
|
if message["type"] == "http.response.start")
|
|
|
|
|
raw = b"".join(message.get("body", b"") for message in messages
|
|
|
|
|
if message["type"] == "http.response.body")
|
|
|
|
|
return status, json.loads(raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_global_cloud_template_upload_streams_past_json_limit(boundary):
|
|
|
|
|
client, engine, *_ = boundary
|
|
|
|
|
query = urlencode({
|
|
|
|
|
"name": "Debian 12 fixed",
|
|
|
|
|
"os_family": "Debian",
|
|
|
|
|
"os_version": "12",
|
|
|
|
|
})
|
|
|
|
|
path = "/internal/v1/vm-templates/upload?" + query
|
|
|
|
|
payload = cloud_upload_fixture()
|
|
|
|
|
with patch("virsh_panel.cloud_templates._verify_qcow2_with_qemu"):
|
|
|
|
|
response = call(client, "POST", path, SUPER, raw=payload, headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": hashlib.sha256(payload).hexdigest(),
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
})
|
|
|
|
|
assert response.status_code == 202, response.text
|
|
|
|
|
job = wait_for_cloud_upload_job(client, response.json()["job_id"])
|
|
|
|
|
assert job["status"] == "completed", job
|
|
|
|
|
assert job["template"]["name"] == "Debian 12 fixed"
|
|
|
|
|
assert job["template"]["os_family"] == "Debian"
|
|
|
|
|
assert job["template"]["os_version"] == "12"
|
|
|
|
|
assert job["template"]["virtual_size"] == 20 * 1024**3
|
|
|
|
|
with engine.registry.cloud_templates.source(
|
|
|
|
|
job["template"]["id"]) as (_record, source):
|
|
|
|
|
assert source.stat().st_size == len(payload)
|
|
|
|
|
assert hashlib.sha256(source.read_bytes()).digest() == hashlib.sha256(
|
|
|
|
|
payload).digest()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_global_cloud_template_upload_digest_must_match_before_queue(boundary):
|
|
|
|
|
client, engine, *_ = boundary
|
|
|
|
|
path = ("/internal/v1/vm-templates/upload?"
|
|
|
|
|
"name=Debian+12&os_family=Debian&os_version=12")
|
|
|
|
|
signed_body = cloud_upload_fixture()
|
|
|
|
|
wrong_body = signed_body[:-1] + b"x"
|
|
|
|
|
signed = signed_headers(SECRET, "POST", path, signed_body, SUPER)
|
|
|
|
|
signed["X-Cloud-Content-SHA256"] = hashlib.sha256(
|
|
|
|
|
signed_body).hexdigest()
|
|
|
|
|
signed["Content-Type"] = "application/octet-stream"
|
|
|
|
|
response = client.post(path, content=wrong_body, headers=signed)
|
|
|
|
|
assert response.status_code == 401, response.text
|
|
|
|
|
assert engine.registry.cloud_templates.jobs == {}
|
|
|
|
|
assert engine.registry.cloud_templates.pending == set()
|
|
|
|
|
root = engine.registry.cloud_templates.files.directory()
|
|
|
|
|
assert not list(root.glob(".cloud-image-upload-*.qcow2"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
("family", "version"),
|
|
|
|
|
[
|
|
|
|
|
("debian", "12"),
|
|
|
|
|
("Debian", "14"),
|
|
|
|
|
("CentOS", "10"),
|
|
|
|
|
("Ubuntu", "24"),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_global_cloud_template_upload_rejects_noncanonical_metadata(
|
|
|
|
|
boundary, family, version):
|
|
|
|
|
client, engine, *_ = boundary
|
|
|
|
|
path = "/internal/v1/vm-templates/upload?" + urlencode({
|
|
|
|
|
"name": "Invalid metadata",
|
|
|
|
|
"os_family": family,
|
|
|
|
|
"os_version": version,
|
|
|
|
|
})
|
|
|
|
|
payload = cloud_upload_fixture()
|
|
|
|
|
response = call(client, "POST", path, SUPER, raw=payload, headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": hashlib.sha256(payload).hexdigest(),
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
})
|
|
|
|
|
assert response.status_code == 422, response.text
|
|
|
|
|
assert engine.registry.cloud_templates.jobs == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_global_cloud_template_upload_requires_super_admin_and_octet_stream(
|
|
|
|
|
boundary):
|
|
|
|
|
client, engine, *_ = boundary
|
|
|
|
|
path = ("/internal/v1/vm-templates/upload?"
|
|
|
|
|
"name=Debian+12&os_family=Debian&os_version=12")
|
|
|
|
|
payload = cloud_upload_fixture()
|
|
|
|
|
digest = hashlib.sha256(payload).hexdigest()
|
|
|
|
|
forbidden = call(client, "POST", path, ADMIN, raw=payload, headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": digest,
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
})
|
|
|
|
|
assert forbidden.status_code == 403, forbidden.text
|
|
|
|
|
wrong_type = call(client, "POST", path, SUPER, raw=payload, headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": digest,
|
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
|
})
|
|
|
|
|
assert wrong_type.status_code == 415, wrong_type.text
|
|
|
|
|
assert engine.registry.cloud_templates.jobs == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_global_cloud_template_upload_enforces_minimum_size(boundary):
|
|
|
|
|
client, engine, *_ = boundary
|
|
|
|
|
path = ("/internal/v1/vm-templates/upload?"
|
|
|
|
|
"name=Too+Small&os_family=Debian&os_version=12")
|
|
|
|
|
payload = cloud_upload_fixture(104)
|
|
|
|
|
response = call(client, "POST", path, SUPER, raw=payload, headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": hashlib.sha256(payload).hexdigest(),
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
})
|
|
|
|
|
assert response.status_code == 422, response.text
|
|
|
|
|
assert engine.registry.cloud_templates.jobs == {}
|
|
|
|
|
assert engine.registry.cloud_templates.pending == set()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_global_cloud_template_upload_timeout_releases_reservation(
|
|
|
|
|
boundary, monkeypatch):
|
|
|
|
|
_client, engine, *_ = boundary
|
|
|
|
|
query = b"name=Timeout&os_family=Debian&os_version=12"
|
|
|
|
|
scope = {
|
|
|
|
|
"query_string": query,
|
|
|
|
|
"headers": [
|
|
|
|
|
(b"content-type", b"application/octet-stream"),
|
|
|
|
|
(b"content-length", str(1024 * 1024).encode()),
|
|
|
|
|
],
|
|
|
|
|
"engine_upload_validation": {"complete": False, "valid": False},
|
|
|
|
|
"engine_upload_digest": "0" * 64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async def stalled_receive():
|
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
return {"type": "http.request", "body": b"", "more_body": False}
|
|
|
|
|
|
|
|
|
|
async def send(_message):
|
|
|
|
|
raise AssertionError("timeout must not start a response")
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr("virsh_panel.internal.CLOUD_UPLOAD_IDLE_SECONDS", 0.01)
|
|
|
|
|
with pytest.raises(TemplateError, match="上传超时") as timeout:
|
|
|
|
|
asyncio.run(engine._upload_cloud_template(scope, stalled_receive, send))
|
|
|
|
|
assert timeout.value.status == 408
|
|
|
|
|
assert engine.registry.cloud_templates.pending == set()
|
|
|
|
|
assert engine.registry.cloud_templates.upload_reservations == {}
|
|
|
|
|
root = engine.registry.cloud_templates.files.directory()
|
|
|
|
|
assert not list(root.glob(".cloud-image-upload-*.qcow2"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stalled_cloud_upload_does_not_block_other_engine_writes(
|
|
|
|
|
boundary, monkeypatch):
|
|
|
|
|
_client, engine, *_ = boundary
|
|
|
|
|
payload = cloud_upload_fixture()
|
|
|
|
|
upload_path = "/internal/v1/vm-templates/upload?" + urlencode({
|
|
|
|
|
"name": "Stalled upload",
|
|
|
|
|
"os_family": "Debian",
|
|
|
|
|
"os_version": "12",
|
|
|
|
|
})
|
|
|
|
|
upload_started = asyncio.Event()
|
|
|
|
|
release_upload = asyncio.Event()
|
|
|
|
|
|
|
|
|
|
class FastRemoteHostManager:
|
|
|
|
|
def __init__(self, *_args, **_kwargs):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def scan(host, port):
|
|
|
|
|
return {"host": host, "port": port, "reachable": True}
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def close():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"virsh_panel.remote_host.RemoteHostManager", FastRemoteHostManager)
|
|
|
|
|
|
|
|
|
|
async def stalled_receive():
|
|
|
|
|
upload_started.set()
|
|
|
|
|
await release_upload.wait()
|
|
|
|
|
return {"type": "http.disconnect"}
|
|
|
|
|
|
|
|
|
|
async def scenario():
|
|
|
|
|
upload = asyncio.create_task(cloud_engine_request(
|
|
|
|
|
engine,
|
|
|
|
|
"POST",
|
|
|
|
|
upload_path,
|
|
|
|
|
payload,
|
|
|
|
|
SUPER,
|
|
|
|
|
headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": hashlib.sha256(payload).hexdigest(),
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
"Content-Length": str(len(payload)),
|
|
|
|
|
},
|
|
|
|
|
receive=stalled_receive,
|
|
|
|
|
))
|
|
|
|
|
await asyncio.wait_for(upload_started.wait(), timeout=1)
|
|
|
|
|
assert engine.registry.cloud_templates.pending
|
|
|
|
|
|
|
|
|
|
scan_body = json.dumps({
|
|
|
|
|
"host": "host.example.test", "port": 22,
|
|
|
|
|
}, separators=(",", ":")).encode()
|
|
|
|
|
status, result = await asyncio.wait_for(cloud_engine_request(
|
|
|
|
|
engine,
|
|
|
|
|
"POST",
|
|
|
|
|
"/internal/v1/hosts/scan",
|
|
|
|
|
scan_body,
|
|
|
|
|
SUPER,
|
|
|
|
|
headers={"Content-Type": "application/json"},
|
|
|
|
|
), timeout=0.5)
|
|
|
|
|
assert status == 200
|
|
|
|
|
assert result["host"] == "host.example.test"
|
|
|
|
|
|
|
|
|
|
release_upload.set()
|
|
|
|
|
upload_status, _result = await asyncio.wait_for(upload, timeout=1)
|
|
|
|
|
assert upload_status == 400
|
|
|
|
|
|
|
|
|
|
asyncio.run(scenario())
|
|
|
|
|
assert engine.registry.cloud_templates.pending == set()
|
|
|
|
|
assert engine.registry.cloud_templates.upload_reservations == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cloud_upload_fsync_does_not_block_event_loop_or_engine_writes(
|
|
|
|
|
boundary, monkeypatch):
|
|
|
|
|
_client, engine, *_ = boundary
|
|
|
|
|
payload = cloud_upload_fixture()
|
|
|
|
|
upload_path = "/internal/v1/vm-templates/upload?" + urlencode({
|
|
|
|
|
"name": "Slow fsync upload",
|
|
|
|
|
"os_family": "Debian",
|
|
|
|
|
"os_version": "12",
|
|
|
|
|
})
|
|
|
|
|
fsync_started = threading.Event()
|
|
|
|
|
release_fsync = threading.Event()
|
|
|
|
|
original_fsync = os.fsync
|
|
|
|
|
|
|
|
|
|
def slow_fsync(descriptor):
|
|
|
|
|
fsync_started.set()
|
|
|
|
|
assert release_fsync.wait(timeout=2), "test did not release fsync"
|
|
|
|
|
return original_fsync(descriptor)
|
|
|
|
|
|
|
|
|
|
class FastRemoteHostManager:
|
|
|
|
|
def __init__(self, *_args, **_kwargs):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def scan(host, port):
|
|
|
|
|
return {"host": host, "port": port, "reachable": True}
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def close():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr("virsh_panel.internal.os.fsync", slow_fsync)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"virsh_panel.remote_host.RemoteHostManager", FastRemoteHostManager)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"virsh_panel.cloud_templates._verify_qcow2_with_qemu",
|
|
|
|
|
lambda *_args: None)
|
|
|
|
|
|
|
|
|
|
async def scenario():
|
|
|
|
|
safety_release = threading.Timer(1.5, release_fsync.set)
|
|
|
|
|
safety_release.start()
|
|
|
|
|
started_at = time.monotonic()
|
|
|
|
|
try:
|
|
|
|
|
upload = asyncio.create_task(cloud_engine_request(
|
|
|
|
|
engine,
|
|
|
|
|
"POST",
|
|
|
|
|
upload_path,
|
|
|
|
|
payload,
|
|
|
|
|
SUPER,
|
|
|
|
|
headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": hashlib.sha256(payload).hexdigest(),
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
"Content-Length": str(len(payload)),
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
while not fsync_started.is_set():
|
|
|
|
|
await asyncio.sleep(0.001)
|
|
|
|
|
|
|
|
|
|
scan_body = json.dumps({
|
|
|
|
|
"host": "host.example.test", "port": 22,
|
|
|
|
|
}, separators=(",", ":")).encode()
|
|
|
|
|
status, _result = await asyncio.wait_for(cloud_engine_request(
|
|
|
|
|
engine,
|
|
|
|
|
"POST",
|
|
|
|
|
"/internal/v1/hosts/scan",
|
|
|
|
|
scan_body,
|
|
|
|
|
SUPER,
|
|
|
|
|
headers={"Content-Type": "application/json"},
|
|
|
|
|
), timeout=0.5)
|
|
|
|
|
assert status == 200
|
|
|
|
|
assert time.monotonic() - started_at < 0.5
|
|
|
|
|
assert not release_fsync.is_set()
|
|
|
|
|
release_fsync.set()
|
|
|
|
|
upload_status, _result = await asyncio.wait_for(upload, timeout=2)
|
|
|
|
|
assert upload_status == 202
|
|
|
|
|
finally:
|
|
|
|
|
release_fsync.set()
|
|
|
|
|
safety_release.cancel()
|
|
|
|
|
|
|
|
|
|
asyncio.run(scenario())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cloud_upload_rechecks_update_state_before_queue(
|
|
|
|
|
boundary, monkeypatch):
|
|
|
|
|
_client, engine, *_ = boundary
|
|
|
|
|
updater = FakeUpdater()
|
|
|
|
|
engine.updater = updater
|
|
|
|
|
payload = cloud_upload_fixture()
|
|
|
|
|
upload_path = "/internal/v1/vm-templates/upload?" + urlencode({
|
|
|
|
|
"name": "Update race upload",
|
|
|
|
|
"os_family": "Debian",
|
|
|
|
|
"os_version": "12",
|
|
|
|
|
})
|
|
|
|
|
upload_started = asyncio.Event()
|
|
|
|
|
release_upload = asyncio.Event()
|
|
|
|
|
delivered = False
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"virsh_panel.cloud_templates._verify_qcow2_with_qemu",
|
|
|
|
|
lambda *_args: None)
|
|
|
|
|
|
|
|
|
|
async def delayed_receive():
|
|
|
|
|
nonlocal delivered
|
|
|
|
|
if delivered:
|
|
|
|
|
return {"type": "http.disconnect"}
|
|
|
|
|
delivered = True
|
|
|
|
|
upload_started.set()
|
|
|
|
|
await release_upload.wait()
|
|
|
|
|
return {"type": "http.request", "body": payload, "more_body": False}
|
|
|
|
|
|
|
|
|
|
async def scenario():
|
|
|
|
|
upload = asyncio.create_task(cloud_engine_request(
|
|
|
|
|
engine,
|
|
|
|
|
"POST",
|
|
|
|
|
upload_path,
|
|
|
|
|
payload,
|
|
|
|
|
SUPER,
|
|
|
|
|
headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": hashlib.sha256(payload).hexdigest(),
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
"Content-Length": str(len(payload)),
|
|
|
|
|
},
|
|
|
|
|
receive=delayed_receive,
|
|
|
|
|
))
|
|
|
|
|
await asyncio.wait_for(upload_started.wait(), timeout=1)
|
|
|
|
|
assert engine.registry.cloud_templates.pending
|
|
|
|
|
updater.active = {
|
|
|
|
|
"job_id": "update-job-1234567890", "status": "running",
|
|
|
|
|
}
|
|
|
|
|
release_upload.set()
|
|
|
|
|
status, result = await asyncio.wait_for(upload, timeout=2)
|
|
|
|
|
assert status == 409
|
|
|
|
|
assert result["error"] == "面板正在更新,暂时不能执行虚拟化写操作"
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
asyncio.run(scenario())
|
|
|
|
|
finally:
|
|
|
|
|
updater.active = None
|
|
|
|
|
assert engine.registry.cloud_templates.pending == set()
|
|
|
|
|
assert engine.registry.cloud_templates.upload_reservations == {}
|
|
|
|
|
assert engine.registry.cloud_templates.jobs == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_upload_same_os_version_keeps_existing_template(boundary):
|
|
|
|
|
client, engine, *_ = boundary
|
|
|
|
|
|
|
|
|
|
def upload(name, payload):
|
|
|
|
|
path = "/internal/v1/vm-templates/upload?" + urlencode({
|
|
|
|
|
"name": name,
|
|
|
|
|
"os_family": "Debian",
|
|
|
|
|
"os_version": "12",
|
|
|
|
|
})
|
|
|
|
|
response = call(client, "POST", path, SUPER, raw=payload, headers={
|
|
|
|
|
"X-Cloud-Content-SHA256": hashlib.sha256(payload).hexdigest(),
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
})
|
|
|
|
|
assert response.status_code == 202, response.text
|
|
|
|
|
job = wait_for_cloud_upload_job(client, response.json()["job_id"])
|
|
|
|
|
assert job["status"] == "completed", job
|
|
|
|
|
return job["template"]
|
|
|
|
|
|
|
|
|
|
official_payload = cloud_upload_fixture()[:-1] + b"o"
|
|
|
|
|
fixed_payload = cloud_upload_fixture()[:-1] + b"f"
|
|
|
|
|
with patch("virsh_panel.cloud_templates._verify_qcow2_with_qemu"):
|
|
|
|
|
official = upload("Debian 12 official", official_payload)
|
|
|
|
|
fixed = upload("Debian 12 fixed", fixed_payload)
|
|
|
|
|
|
|
|
|
|
assert official["id"] != fixed["id"]
|
|
|
|
|
templates = call(
|
|
|
|
|
client, "GET", "/internal/v1/vm-templates", SUPER).json()["templates"]
|
|
|
|
|
assert {row["id"] for row in templates} == {official["id"], fixed["id"]}
|
|
|
|
|
with engine.registry.cloud_templates.source(
|
|
|
|
|
official["id"]) as (_record, source):
|
|
|
|
|
assert hashlib.sha256(source.read_bytes()).hexdigest() == official["sha256"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_main_and_log_schema_do_not_create_python_accounts_or_cross_database_tables():
|
|
|
|
|
connection = MagicMock()
|
|
|
|
|
cursor = connection.cursor.return_value.__enter__.return_value
|
|
|
|
|
|