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_app_vm_concurrency.py

183 lines
6.9 KiB
Python

"""Standalone admission stays per VM when no Engine coordinator is installed."""
from concurrent.futures import ThreadPoolExecutor
import threading
import time
import pytest
from test_app import CREATE_BODY, HOST_BODY, VM_ID, configure_host, setup_admin, web # noqa: F401
OTHER_VM_ID = "00000000-0000-4000-8000-000000000002"
@pytest.mark.parametrize("second_vm", [VM_ID, OTHER_VM_ID])
def test_stalled_action_serializes_only_the_same_vm(web, second_vm):
client, _, token, service, _, _ = web
csrf = setup_admin(client, token)
configure_host(client, csrf)
headers = {"X-CSRF-Token": csrf}
started = threading.Event()
release = threading.Event()
second_entered = threading.Event()
calls = []
service.get_vm = lambda vm_id: {**service.vm, "uuid": vm_id}
def action(vm_id, _action):
calls.append(vm_id)
if len(calls) == 1:
started.set()
assert release.wait(5)
else:
second_entered.set()
return {**service.vm, "uuid": vm_id}
service.action = action
def send(vm_id):
return client.post(f"/api/v1/vms/{vm_id}/actions",
json={"action": "reset"}, headers=headers)
with ThreadPoolExecutor(max_workers=2) as executor:
first = executor.submit(send, VM_ID)
try:
assert started.wait(2)
second = executor.submit(send, second_vm)
if second_vm == VM_ID:
assert not second_entered.wait(0.15)
assert not second.done()
else:
result = second.result(timeout=0.75)
assert result.status_code == 200, result.text
assert result.json()["uuid"] == OTHER_VM_ID
finally:
release.set()
assert first.result(timeout=2).status_code == 200
assert second.result(timeout=2).status_code == 200
assert calls == [VM_ID, second_vm]
def test_stalled_request_blocks_legacy_host_replacement_and_update(web):
client, _, token, service, manager, _ = web
csrf = setup_admin(client, token)
configure_host(client, csrf)
headers = {"X-CSRF-Token": csrf}
started = threading.Event()
release = threading.Event()
manager.calls.clear()
def action(_vm_id, _action):
started.set()
assert release.wait(5)
return service.vm
service.action = action
with ThreadPoolExecutor(max_workers=2) as executor:
active = executor.submit(
client.post, f"/api/v1/vms/{VM_ID}/actions",
json={"action": "reset"}, headers=headers)
try:
assert started.wait(2)
host = executor.submit(client.put, "/api/v1/host",
json=HOST_BODY, headers=headers).result(timeout=0.75)
assert host.status_code == 409, host.text
update = executor.submit(
client.post, "/api/v1/update/apply",
json={"expected_current_commit": "a" * 40,
"expected_remote_commit": "b" * 40},
headers=headers).result(timeout=0.75)
assert update.status_code == 409, update.text
assert not manager.calls
finally:
release.set()
assert active.result(timeout=2).status_code == 200
# The active counter is diagnostic admission, not a permanent lock.
assert client.put("/api/v1/host", json=HOST_BODY, headers=headers).status_code == 200
def test_stalled_create_does_not_block_an_existing_vm(web):
client, _, token, service, _, _ = web
csrf = setup_admin(client, token)
configure_host(client, csrf)
headers = {"X-CSRF-Token": csrf}
started = threading.Event()
release = threading.Event()
def create(_spec):
started.set()
assert release.wait(5)
return {**service.vm, "uuid": OTHER_VM_ID}
service.create_vm = create
with ThreadPoolExecutor(max_workers=2) as executor:
creating = executor.submit(client.post, "/api/v1/vms",
json=CREATE_BODY, headers=headers)
try:
assert started.wait(2)
action = executor.submit(
client.post, f"/api/v1/vms/{VM_ID}/actions",
json={"action": "reset"}, headers=headers).result(timeout=0.75)
assert action.status_code == 200, action.text
assert action.json()["uuid"] == VM_ID
finally:
release.set()
assert creating.result(timeout=2).status_code == 201
def test_template_delete_blocks_rescue_admission_but_not_another_vm(web):
client, _, token, service, _, _ = web
csrf = setup_admin(client, token)
configure_host(client, csrf)
headers = {"X-CSRF-Token": csrf}
iso = bytearray(1024 * 1024)
for sector, kind in ((16, 1), (17, 0), (18, 255)):
offset = sector * 2048
iso[offset] = kind
iso[offset + 1:offset + 6] = b"CD001"
iso[offset + 6] = 1
iso[17 * 2048 + 7:17 * 2048 + 30] = b"EL TORITO SPECIFICATION"
iso[17 * 2048 + 71:17 * 2048 + 75] = (20).to_bytes(4, "little")
uploaded = client.post("/api/v1/rescue-templates?name=Rescue",
content=bytes(iso), headers=headers)
assert uploaded.status_code == 202, uploaded.text
for _ in range(100):
job = client.get(
f"/api/v1/rescue-templates/import-jobs/{uploaded.json()['job_id']}"
).json()
if job["status"] in {"completed", "failed"}:
break
time.sleep(0.01)
assert job["status"] == "completed"
template = client.get("/api/v1/rescue-templates").json()["templates"][0]
started = threading.Event()
release = threading.Event()
def delete(_volume):
started.set()
assert release.wait(5)
service.delete_rescue_iso = delete
service.get_vm = lambda vm_id: {**service.vm, "uuid": vm_id}
service.action = lambda vm_id, _action: {**service.vm, "uuid": vm_id}
with ThreadPoolExecutor(max_workers=3) as executor:
deleting = executor.submit(client.delete,
f"/api/v1/rescue-templates/{template['id']}", headers=headers)
try:
assert started.wait(2)
entering = executor.submit(
client.post, f"/api/v1/vms/{VM_ID}/rescue",
json={"template_id": template["id"], "confirmation_name": service.vm["name"]},
headers=headers)
action = executor.submit(
client.post, f"/api/v1/vms/{OTHER_VM_ID}/actions",
json={"action": "reset"}, headers=headers).result(timeout=0.75)
assert action.status_code == 200, action.text
assert action.json()["uuid"] == OTHER_VM_ID
assert not entering.done()
finally:
release.set()
assert deleting.result(timeout=2).status_code == 200
assert entering.result(timeout=2).status_code == 404
assert not any(call[0] == "enter_rescue_vm" for call in service.calls)