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.
138 lines
6.3 KiB
Python
138 lines
6.3 KiB
Python
"""A rejected VPC admission must not turn its unrelated claims uncertain."""
|
|
|
|
import asyncio
|
|
|
|
import pytest
|
|
|
|
from virsh_panel.coordination import (
|
|
CoordinationError, DurableCoordinator, mark_remote_write_started,
|
|
)
|
|
from virsh_panel.vpc import VpcCoordinator
|
|
from test_vpc import FakeFabric, INSTANCE_A, INSTANCE_B, payload
|
|
from test_engine_coordination import (
|
|
local_backends, prepare_hosts, request, running_engine,
|
|
)
|
|
|
|
|
|
def vpc_payload():
|
|
return payload(hosts=[{'host_id': 'host-a', 'members': [
|
|
{'member_id': 'member-a', 'instance_uuid': INSTANCE_A,
|
|
'private_ip_cidr': '10.20.0.10/32', 'subnet_id': 'subnet-a'},
|
|
{'member_id': 'member-b', 'instance_uuid': INSTANCE_B,
|
|
'private_ip_cidr': '10.20.0.11/32', 'subnet_id': 'subnet-a'},
|
|
]}])
|
|
|
|
|
|
async def seeded_vpc(engine, store):
|
|
vpc = VpcCoordinator(store, FakeFabric(hosts=('host-a',)),
|
|
catalog_lock=engine._network_catalog_mutation_lock,
|
|
vm_lock=engine._target_lock)
|
|
await vpc.apply(vpc_payload())
|
|
assert engine.coordinator.snapshot() == {}
|
|
engine.vpc = vpc
|
|
return vpc
|
|
|
|
|
|
@pytest.mark.parametrize('blocking_state', ['active', 'uncertain'])
|
|
@pytest.mark.parametrize('blocked_resource', ['catalog', 'second_vm'])
|
|
def test_http_confirmed_preflight_rejection_does_not_poison_other_claims(
|
|
tmp_path, monkeypatch, blocking_state, blocked_resource):
|
|
async def exercise():
|
|
async with running_engine(tmp_path) as (engine, store, _services):
|
|
await prepare_hosts(engine, 'host-a')
|
|
vpc = await seeded_vpc(engine, store)
|
|
host_calls = []
|
|
|
|
async def forbidden_host(*_args, **_kwargs):
|
|
host_calls.append(True)
|
|
pytest.fail('blocked admission reached host dataplane')
|
|
monkeypatch.setattr(vpc, '_reconcile_dataplane_unlocked', forbidden_host)
|
|
blocker = DurableCoordinator(store)
|
|
resource = ('catalog:network' if blocked_resource == 'catalog'
|
|
else f'vm:host-a:{INSTANCE_B}')
|
|
old = blocker.claim(resources=(resource,), kind='resource')
|
|
if blocking_state == 'uncertain':
|
|
blocker.retain(old)
|
|
before = blocker.snapshot()
|
|
if blocked_resource == 'catalog':
|
|
target = f'/internal/v1/hosts/host-a/vms/{INSTANCE_A}/private-state/reconcile'
|
|
value = {'version': 1, 'link_state': 'preserve'}
|
|
else:
|
|
target = '/internal/v1/vpcs/vpc-hk-1/reconcile'
|
|
value = {}
|
|
status, body = await request(engine, 'POST', target, value,
|
|
operation_id='vpc-preflight-test')
|
|
assert status == (503 if blocking_state == 'uncertain' else 409), body
|
|
assert body['error_code'] == ('coordination_uncertain'
|
|
if blocking_state == 'uncertain' else 'coordination_busy')
|
|
assert host_calls == []
|
|
# Includes previously acquired first-VM, catalog and ASGI request.
|
|
assert blocker.snapshot() == before
|
|
asyncio.run(exercise())
|
|
|
|
|
|
@pytest.mark.parametrize('error_code', ['coordination_uncertain', 'coordination_unavailable'])
|
|
def test_failure_after_dataplane_entry_retains_execution(tmp_path, monkeypatch, error_code):
|
|
async def exercise():
|
|
async with running_engine(tmp_path) as (engine, store, _services):
|
|
vpc = await seeded_vpc(engine, store)
|
|
host_calls = []
|
|
|
|
async def ambiguous_host(*_args, **_kwargs):
|
|
host_calls.append(True)
|
|
raise CoordinationError(error_code, 503)
|
|
monkeypatch.setattr(vpc, '_reconcile_dataplane_unlocked', ambiguous_host)
|
|
status, body = await request(engine, 'POST',
|
|
'/internal/v1/vpcs/vpc-hk-1/reconcile', {},
|
|
operation_id='vpc-host-failure-test')
|
|
assert status == 503, body
|
|
assert host_calls == [True]
|
|
claims = list(engine.coordinator.snapshot().values())
|
|
assert len(claims) == 4
|
|
assert all(claim['state'] == 'uncertain' for claim in claims)
|
|
asyncio.run(exercise())
|
|
|
|
|
|
def test_unavailable_admission_store_is_not_declared_safe(tmp_path, monkeypatch):
|
|
async def exercise():
|
|
async with running_engine(tmp_path) as (engine, store, _services):
|
|
await prepare_hosts(engine, 'host-a')
|
|
vpc = await seeded_vpc(engine, store)
|
|
class UnknownAdmission:
|
|
async def __aenter__(self):
|
|
raise CoordinationError('coordination_unavailable', 503)
|
|
async def __aexit__(self, *_args):
|
|
pytest.fail('unacquired guard must not exit')
|
|
monkeypatch.setattr(vpc, '_catalog_mutation_lock', lambda: UnknownAdmission())
|
|
status, body = await request(engine, 'POST',
|
|
f'/internal/v1/hosts/host-a/vms/{INSTANCE_A}/private-state/reconcile',
|
|
{'version': 1}, operation_id='vpc-db-admission-test')
|
|
assert status == 503, body
|
|
claims = list(engine.coordinator.snapshot().values())
|
|
assert len(claims) == 1 and claims[0]['state'] == 'uncertain'
|
|
asyncio.run(exercise())
|
|
|
|
|
|
def test_preexisting_remote_write_evidence_keeps_request_uncertain(tmp_path, monkeypatch):
|
|
async def exercise():
|
|
async with running_engine(tmp_path) as (engine, store, _services):
|
|
await prepare_hosts(engine, 'host-a')
|
|
vpc = await seeded_vpc(engine, store)
|
|
blocker = DurableCoordinator(store)
|
|
old = blocker.claim(resources=('catalog:network',), kind='resource')
|
|
blocker.retain(old)
|
|
reconcile = vpc.reconcile_instance
|
|
|
|
async def after_prior_write(*args, **kwargs):
|
|
mark_remote_write_started()
|
|
return await reconcile(*args, **kwargs)
|
|
monkeypatch.setattr(vpc, 'reconcile_instance', after_prior_write)
|
|
status, body = await request(engine, 'POST',
|
|
f'/internal/v1/hosts/host-a/vms/{INSTANCE_A}/private-state/reconcile',
|
|
{'version': 1}, operation_id='vpc-prior-mutation-test')
|
|
assert status == 503, body
|
|
claims = engine.coordinator.snapshot()
|
|
assert len(claims) == 2 and old.token in claims
|
|
assert all(claim['state'] == 'uncertain' for claim in claims.values())
|
|
asyncio.run(exercise())
|