You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/engine/tests/test_cloud_template_startup.py

165 lines
7.4 KiB
Python

"""Private per-image scripts: persistence, edit conflicts and API isolation."""
from concurrent.futures import ThreadPoolExecutor
import pytest
from test_cloud_template_runtime import MemoryStore, record_for
from test_engine_boundary import ADMIN, SUPER, USER, boundary, call
from virsh_panel.cloud_template_runtime import CATALOG_KIND, CloudTemplateRuntime
from virsh_panel.cloud_templates import TemplateError
from virsh_panel.startup_script import MAX_STARTUP_SCRIPT_BYTES, normalize_startup_script
@pytest.fixture
def catalog(tmp_path):
store = MemoryStore()
runtime = CloudTemplateRuntime(store, tmp_path)
record = record_for(b"per-image-startup")
runtime.catalog.add(record)
try:
yield runtime, store, record
finally:
runtime.close()
def test_script_survives_runtime_restart_and_rename_but_is_not_public(catalog, tmp_path):
runtime, store, record = catalog
template_id = record["id"]
assert runtime.get_startup_script(template_id) == {
"startup_script": "", "revision": 0, "created_at": 1,
}
assert runtime.get(template_id)["startup_script"] == ""
script = "#!/bin/bash\necho 'private startup content'\n"
assert runtime.set_startup_script(
template_id, script.replace("\n", "\r\n"),
expected_revision=0, created_at=1,
) == {"startup_script": script, "revision": 1, "created_at": 1}
# Independent rename/script submissions must preserve both settings.
with ThreadPoolExecutor(max_workers=2) as executor:
rename = executor.submit(runtime.rename, template_id, "Renamed image")
write = executor.submit(
runtime.set_startup_script, template_id, script + "true\n",
expected_revision=1, created_at=1)
public = rename.result()
write.result()
assert "startup_script" not in public
restarted = CloudTemplateRuntime(store, tmp_path)
try:
restored = restarted.get(template_id)
assert restored["name"] == "Renamed image"
assert restored["startup_script"] == script + "true\n"
assert restored["startup_script_revision"] == 2
public = restarted.list()[0]
assert "startup_script" not in public
assert "startup_script_revision" not in public
job = restarted.public_job({"template": restored, "status": "completed"})
assert job["template"] == public
finally:
restarted.close()
def test_conflicting_editor_cannot_replace_or_clear_newer_script(catalog):
runtime, _store, record = catalog
template_id = record["id"]
runtime.set_startup_script(template_id, "echo first\n", expected_revision=0, created_at=1)
for script in ("echo stale\n", ""):
with pytest.raises(TemplateError) as error:
runtime.set_startup_script(template_id, script, expected_revision=0, created_at=1)
assert error.value.status == 409
assert runtime.get_startup_script(template_id)["startup_script"] == "echo first\n"
unchanged = runtime.set_startup_script(
template_id, "echo first\n", expected_revision=1, created_at=1)
assert unchanged["revision"] == 1
cleared = runtime.set_startup_script(template_id, "", expected_revision=1, created_at=1)
assert cleared == {"startup_script": "", "revision": 2, "created_at": 1}
def test_delete_reimport_never_restores_previous_generation_script(catalog):
runtime, store, record = catalog
template_id = record["id"]
runtime.set_startup_script(template_id, "echo old\n", expected_revision=0, created_at=1)
runtime.delete(template_id)
assert store.get(CATALOG_KIND, template_id) is None
runtime.catalog.add({**record, "created_at": 2})
assert runtime.get_startup_script(template_id) == {
"startup_script": "", "revision": 0, "created_at": 2,
}
with pytest.raises(TemplateError) as error:
runtime.set_startup_script(template_id, "echo stale\n", expected_revision=0, created_at=1)
assert error.value.status == 409
@pytest.mark.parametrize("value", [None, 0, [], {}, "echo\0bad", "\ud800",
"a" * (MAX_STARTUP_SCRIPT_BYTES + 1)])
def test_invalid_script_is_rejected_without_mutating_catalog(catalog, value):
runtime, store, record = catalog
before = store.get(CATALOG_KIND, record["id"])
with pytest.raises(TemplateError) as error:
runtime.set_startup_script(record["id"], value, expected_revision=0, created_at=1)
assert error.value.status == 422
assert store.get(CATALOG_KIND, record["id"]) == before
def test_script_limit_is_utf8_bytes_and_crlf_normalization_precedes_limit():
exact = "中" * 10922 + "ab"
assert len(exact.encode("utf-8")) == MAX_STARTUP_SCRIPT_BYTES
assert normalize_startup_script(exact) == exact
with pytest.raises(ValueError):
normalize_startup_script(exact + "c")
assert normalize_startup_script("\r\n" * MAX_STARTUP_SCRIPT_BYTES) == "\n" * MAX_STARTUP_SCRIPT_BYTES
def test_private_script_api_permissions_validation_and_no_host_dispatch(boundary):
client, engine, _store, _logs, services = boundary
record = record_for(b"startup-api")
engine.registry.cloud_templates.catalog.add(record)
path = f"/internal/v1/vm-templates/{record['id']}/startup-script"
body = {"script": "echo configured\r\n", "expected_revision": 0, "created_at": 1}
assert call(client, "GET", path, USER).status_code == 403
assert call(client, "PATCH", path, USER, body).status_code == 403
assert call(client, "PATCH", path, ADMIN, body).status_code == 403
assert call(client, "PATCH", path, {**SUPER, "can_manage": False}, body).status_code == 403
assert call(client, "GET", path, ADMIN).json() == {
"startup_script": "", "revision": 0, "created_at": 1,
}
updated = call(client, "PATCH", path, SUPER, body)
assert updated.status_code == 200, updated.text
assert updated.json() == {
"startup_script": "echo configured\n", "revision": 1, "created_at": 1,
}
assert call(client, "GET", path, SUPER).json() == updated.json()
assert call(client, "PATCH", path, SUPER, body).status_code == 409
listed = call(client, "GET", "/internal/v1/vm-templates", USER).json()
assert "startup_script" not in listed["templates"][0]
assert "startup_script_revision" not in listed["templates"][0]
assert services == {}
assert engine.coordinator.snapshot() == {}
@pytest.mark.parametrize("body", [
{}, {"script": "a"}, [], None,
{"script": [], "expected_revision": 0, "created_at": 1},
{"script": "a", "expected_revision": False, "created_at": 1},
{"script": "a", "expected_revision": "0", "created_at": 1},
{"script": "a", "expected_revision": -1, "created_at": 1},
{"script": "a", "expected_revision": 0, "created_at": True},
{"script": "a", "expected_revision": 0, "created_at": "1"},
{"script": "a", "expected_revision": 0, "created_at": 0},
{"script": "a", "expected_revision": 0, "created_at": 1, "host_id": "injected"},
])
def test_script_api_rejects_malformed_bodies(boundary, body):
client, engine, _store, _logs, _services = boundary
record = record_for(b"startup-invalid-api")
runtime = engine.registry.cloud_templates
runtime.catalog.add(record)
path = f"/internal/v1/vm-templates/{record['id']}/startup-script"
response = call(client, "PATCH", path, SUPER, body)
assert response.status_code == 422, response.text
assert runtime.get_startup_script(record["id"])["revision"] == 0
assert engine.coordinator.snapshot() == {}