fix(vpc): persist local neighbors and avoid healthy bridge resets
parent
7c6dd642d6
commit
5783ae618f
@ -0,0 +1,211 @@
|
||||
"""Execute shared neighbor ownership checks and the durable Fabric parser."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from test_vpc_nft_dataplane import _plan
|
||||
from virsh_panel.fabric import _BOOT_HELPER, _VPC_STATE_LOADER
|
||||
from virsh_panel.shell_contracts import LOCAL_NEIGHBOR_SH
|
||||
from virsh_panel.vpc_dataplane import _routes, _state
|
||||
|
||||
|
||||
MAC = '02:00:00:00:00:10'
|
||||
PAIR = '10.44.0.10|' + MAC
|
||||
BRIDGE = 'vcv-0123456789a'
|
||||
|
||||
|
||||
def _run(body, *, env=None, shell='/bin/sh'):
|
||||
return subprocess.run([shell, '-s'], input='set -eu\n' + body,
|
||||
text=True, capture_output=True, env=env)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('raw,expected', [
|
||||
('', ''),
|
||||
(f'10.44.0.10 lladdr {MAC} PERMANENT\n', PAIR + '|PERMANENT\n'),
|
||||
(f'10.44.0.10 dev {BRIDGE} lladdr {MAC.upper()} STALE\n', PAIR + '|STALE\n'),
|
||||
('10.44.0.10 INCOMPLETE\n', '10.44.0.10|-|INCOMPLETE\n'),
|
||||
('10.44.0.10 FAILED\n', '10.44.0.10|-|FAILED\n'),
|
||||
('224.0.0.1 lladdr 01:00:5e:00:00:01 NOARP\n', ''),
|
||||
(f'10.44.0.10 lladdr {MAC} PERMANENT router\n', None),
|
||||
(f'10.44.0.10 lladdr {MAC} PERMANENT extern_learn\n', None),
|
||||
('10.44.0.10 proxy\n', None),
|
||||
('10.44.0.10 lladdr 03:00:00:00:00:10 PERMANENT\n', None),
|
||||
('10.44.0.10 lladdr 00:00:00:00:00:00 PERMANENT\n', None),
|
||||
('10.44.0.10 PERMANENT\n', None),
|
||||
(f'10.44.0.10 lladdr {MAC} STALE REACHABLE\n', None),
|
||||
(f'10.44.0.10 lladdr {MAC} STALE\n10.44.0.10 FAILED\n', None),
|
||||
(f'10.44.0.999 lladdr {MAC} STALE\n', None),
|
||||
(f'010.44.0.10 lladdr {MAC} STALE\n', None),
|
||||
])
|
||||
def test_neighbor_records_are_strict_and_empty_is_valid(raw, expected):
|
||||
script = LOCAL_NEIGHBOR_SH + '\n' + '''
|
||||
ip_fixture() { [ "$*" = "-o -4 neigh show dev ''' + BRIDGE + '''" ] || return 90
|
||||
printf '%s' ''' + shlex.quote(raw) + '''
|
||||
}
|
||||
IP=ip_fixture
|
||||
vc_local_neighbor_records ''' + BRIDGE
|
||||
result = _run(script)
|
||||
assert (result.returncode == 0) == (expected is not None), result.stderr
|
||||
if expected is not None:
|
||||
assert result.stdout == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('initial,success', [
|
||||
([], True),
|
||||
([PAIR + '|STALE'], True),
|
||||
(['10.44.0.10|-|FAILED'], True),
|
||||
(['10.44.0.10|-|INCOMPLETE'], True),
|
||||
([PAIR + '|PERMANENT'], True),
|
||||
(['10.44.0.10|02:00:00:00:00:99|PERMANENT'], False),
|
||||
([PAIR + '|PERMANENT', '10.44.0.99|02:00:00:00:00:99|STALE'], False),
|
||||
])
|
||||
@pytest.mark.parametrize('shell', ['/bin/sh', '/bin/bash'])
|
||||
def test_restore_missing_local_next_hop_without_overwriting_foreign(tmp_path, initial, success, shell):
|
||||
state = tmp_path / 'neighbors.json'
|
||||
state.write_text(json.dumps(initial))
|
||||
calls = tmp_path / 'calls'
|
||||
calls.write_text('')
|
||||
tool = tmp_path / 'ip'
|
||||
tool.write_text('#!' + sys.executable + '\n' + r'''
|
||||
import json, os, sys
|
||||
from pathlib import Path
|
||||
p=Path(os.environ['NEIGHBORS']); rows=json.loads(p.read_text()); args=sys.argv[1:]
|
||||
if args == ['-o','-4','neigh','show','dev',os.environ['BRIDGE']]:
|
||||
for row in rows:
|
||||
ip,mac,nud=row.split('|')
|
||||
print(ip + ('' if mac=='-' else ' lladdr '+mac) + ' ' + nud)
|
||||
elif len(args)==10 and args[:3]==['-4','neigh','replace']:
|
||||
assert args[4]=='lladdr' and args[6:]==['nud','permanent','dev',os.environ['BRIDGE']]
|
||||
with Path(os.environ['CALLS']).open('a') as out: out.write(json.dumps(args)+'\n')
|
||||
rows=[row for row in rows if row.split('|')[0]!=args[3]]
|
||||
rows.append(args[3]+'|'+args[5]+'|PERMANENT'); p.write_text(json.dumps(rows))
|
||||
else: raise SystemExit(97)
|
||||
''')
|
||||
tool.chmod(0o700)
|
||||
env = dict(os.environ, NEIGHBORS=str(state), CALLS=str(calls), BRIDGE=BRIDGE)
|
||||
result = _run('IP=' + shlex.quote(str(tool)) + '\n' + LOCAL_NEIGHBOR_SH
|
||||
+ '\nvc_local_neighbor_ensure ' + BRIDGE + ' ' + shlex.quote(PAIR),
|
||||
env=env, shell=shell)
|
||||
assert (result.returncode == 0) == success, result.stderr
|
||||
if success:
|
||||
assert json.loads(state.read_text()) == [PAIR + '|PERMANENT']
|
||||
else:
|
||||
assert json.loads(state.read_text()) == initial
|
||||
assert calls.read_text() == ''
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mode', ['current', 'legacy', 'wrong_mac', 'multicast_mac',
|
||||
'duplicate', 'remote_ip', 'service_ip', 'outside_cidr', 'extra_field', 'partial'])
|
||||
@pytest.mark.parametrize('shell', ['/bin/sh', '/bin/bash'])
|
||||
def test_fabric_loader_binds_neighbors_only_to_local_member_direct_routes(tmp_path, mode, shell):
|
||||
plan = _plan()
|
||||
lines = [line for line in _state(plan, _routes(plan)).splitlines()
|
||||
if not line.startswith('local_neighbor|')]
|
||||
entry = 'local_neighbor|10.44.0.10|' + MAC
|
||||
if mode == 'wrong_mac':
|
||||
entry = entry.replace(MAC, '02:xx:00:00:00:10')
|
||||
elif mode == 'multicast_mac':
|
||||
entry = entry.replace(MAC, '03:00:00:00:00:10')
|
||||
elif mode == 'remote_ip':
|
||||
entry = entry.replace('10.44.0.10', '10.44.0.11')
|
||||
elif mode in ('service_ip', 'outside_cidr'):
|
||||
ip = '10.44.0.99' if mode == 'service_ip' else '10.45.0.10'
|
||||
entry = entry.replace('10.44.0.10', ip)
|
||||
lines.append(f'route|{ip}/32|{plan.resources.bridge}|{plan.resources.route_table}|10.44.0.10|100')
|
||||
elif mode == 'extra_field':
|
||||
entry += '|'
|
||||
elif mode == 'partial':
|
||||
lines.append(f'route|10.44.0.20/32|{plan.resources.bridge}|{plan.resources.route_table}|-|100')
|
||||
if mode != 'legacy':
|
||||
lines.append(entry)
|
||||
if mode == 'duplicate':
|
||||
lines.append(entry)
|
||||
states = tmp_path / 'states'
|
||||
states.mkdir(mode=0o700)
|
||||
path = states / (plan.resources.token + '.state')
|
||||
path.write_text('\n'.join(lines) + '\n')
|
||||
path.chmod(0o600)
|
||||
loader = (_VPC_STATE_LOADER
|
||||
.replace('__VC_FABRIC_HOST_ID__', plan.host_id)
|
||||
.replace('__VC_FABRIC_ID__', plan.vpc.fabric_id)
|
||||
.replace('__VC_FABRIC_NETWORK__', '100.64.0.0/10')
|
||||
.replace('__VC_VPC_STATE_OBSERVE__', 'yes'))
|
||||
script = loader + '\n' + '\n'.join([
|
||||
'VPC_STATE_DIR=' + shlex.quote(str(states)),
|
||||
'VPC_PEER_FILE=' + shlex.quote(str(tmp_path / 'peers')),
|
||||
'VPC_ROUTE_FILE=' + shlex.quote(str(tmp_path / 'routes')),
|
||||
'VPC_TENANT_FILE=' + shlex.quote(str(tmp_path / 'tenants')),
|
||||
'FIREWALL_MODE=native_nftables',
|
||||
]) + r'''
|
||||
fixture_stat() {
|
||||
case "$2" in
|
||||
'%u:%a') if [ -d "$3" ]; then printf '0:700\n'; else printf '0:600\n'; fi ;;
|
||||
'%s') wc -c < "$3" ;;
|
||||
*) return 9 ;;
|
||||
esac
|
||||
}
|
||||
STAT=fixture_stat
|
||||
vpc_state_load
|
||||
printf '%s\n' "$VPC_STATE_STATUS"
|
||||
'''
|
||||
result = _run(script, shell=shell)
|
||||
assert (result.returncode == 0) == (mode in ('current', 'legacy')), result.stderr
|
||||
if mode in ('current', 'legacy'):
|
||||
assert result.stdout == 'owned\n'
|
||||
|
||||
|
||||
def test_fabric_boot_proves_neighbors_before_quarantine_and_restores_before_open():
|
||||
emergency = _BOOT_HELPER[_BOOT_HELPER.index('vc_vpc_emergency_down() {'):]
|
||||
assert emergency.index('vc_local_neighbor_preflight') < emergency.index('"$NMCLI" device set')
|
||||
assert emergency.index('vc_local_neighbor_preflight') < emergency.index('vc_vpc_bridge_force_down')
|
||||
recovery = _BOOT_HELPER[_BOOT_HELPER.index('recover_vpc_dataplanes() {'):]
|
||||
restore = recovery.index('vc_local_neighbor_ensure "$vpc_bridge"')
|
||||
assert recovery.index('vpc_expected_marker_unique') < restore
|
||||
assert recovery.index('vc_ip_rule_ensure') < restore
|
||||
assert restore < recovery.index('vc_vpc_nm_preserve_recover "$vpc_bridge"')
|
||||
assert restore < recovery.index('vpc_local_neighbors_actual=$(vc_local_neighbor_records')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('shell', ['/bin/sh', '/bin/bash'])
|
||||
@pytest.mark.parametrize('scenario,raw', [
|
||||
('foreign_ip', '10.44.0.99 lladdr 02:00:00:00:00:99 STALE'),
|
||||
('foreign_mac', '10.44.0.10 lladdr 02:00:00:00:00:99 PERMANENT'),
|
||||
('legacy_failed', '10.44.0.10 FAILED'),
|
||||
('legacy_permanent', f'10.44.0.10 lladdr {MAC} PERMANENT'),
|
||||
])
|
||||
def test_emergency_owned_bridge_stays_isolated_after_neighbor_drift(tmp_path, shell, scenario, raw):
|
||||
from test_fabric_nm_bridge_preserve import _fixture, TOKEN, BRIDGE as owned_bridge
|
||||
|
||||
body, env = _fixture(tmp_path, 'healthy')
|
||||
tool = tmp_path / 'bin' / 'ip'
|
||||
tool.write_text(tool.read_text().replace(
|
||||
"elif args==['-o','-4','neigh','show','dev',b]:pass",
|
||||
"elif args==['-o','-4','neigh','show','dev',b]:print(" + repr(raw) + ")"))
|
||||
if not scenario.startswith('legacy_'):
|
||||
state = tmp_path / 'state' / (TOKEN + '.state')
|
||||
state.write_text(state.read_text() + 'local_neighbor|' + PAIR + '\n')
|
||||
journal = tmp_path / 'evidence.txn'
|
||||
journal.write_text('keep incomplete operation evidence\n')
|
||||
# A failed preflight must not permit the later recovery stage, but the
|
||||
# owner-proven bridge must still be quarantined. DOWN may clear neighbors;
|
||||
# the helper performs no explicit neighbor replacement/deletion/flush.
|
||||
body += 'vc_vpc_emergency_down\nprintf recovered\n'
|
||||
result = _run(body, env=env, shell=shell)
|
||||
assert result.returncode != 0, result.stderr
|
||||
assert 'recovered' not in result.stdout
|
||||
runtime = json.loads((tmp_path / 'runtime.json').read_text())
|
||||
assert not runtime['up']
|
||||
assert runtime['ports'] == ['vnet4'] and runtime['index'] == 19
|
||||
calls = [json.loads(line) for line in (tmp_path / 'calls').read_text().splitlines()]
|
||||
assert ['ip', 'link', 'set', 'dev', owned_bridge, 'down'] in calls
|
||||
assert not any(call[0] == 'ip' and 'neigh' in call and
|
||||
any(verb in call for verb in ('replace', 'delete', 'del', 'flush'))
|
||||
for call in calls)
|
||||
assert journal.read_text() == 'keep incomplete operation evidence\n'
|
||||
@ -0,0 +1,379 @@
|
||||
"""Successful apply receipts avoid disrupting an unchanged VPC bridge."""
|
||||
from __future__ import annotations
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from dataclasses import replace
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from test_vpc_dataplane import _active_observation_markers, _plan
|
||||
from virsh_panel import vpc_dataplane as dp
|
||||
|
||||
SHELLS = [p for p in ('/bin/sh', '/bin/dash', '/bin/bash') if Path(p).exists()]
|
||||
RECEIPT = ','.join(c * 64 for c in 'abc')
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def empty_receipts():
|
||||
with dp._APPLY_RECEIPT_LOCK:
|
||||
dp._APPLY_RECEIPTS.clear()
|
||||
yield
|
||||
with dp._APPLY_RECEIPT_LOCK:
|
||||
dp._APPLY_RECEIPTS.clear()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def plan():
|
||||
p = _plan()
|
||||
return replace(p, fabric_peer_keys=frozenset(x.public_key for x in p.peers))
|
||||
|
||||
|
||||
class Adapter(dp.VpcPrivateHostAdapter):
|
||||
def __init__(self, plan, **profile):
|
||||
super().__init__(None, {
|
||||
'ssh_target': 'owned', 'ssh_config_file': '/generation-a/config',
|
||||
'host': '192.0.2.1', 'port': 22, 'fingerprint': 'host-key-a', **profile,
|
||||
})
|
||||
self.plan = plan
|
||||
self.calls = []
|
||||
self.observe_error = None
|
||||
self.apply_error = None
|
||||
self.observation = '\n'.join(_active_observation_markers(plan) + [
|
||||
'__VC_VPC_FIREWALL_MODE__=native_nftables',
|
||||
'__VC_VPC_FIREWALL_STATE__=owned',
|
||||
'__VC_VPC_STATE_FIREWALL_MODE__=native_nftables',
|
||||
'__VC_VPC_WG_PEER_SET__=exact',
|
||||
'__VC_VPC_HELPER_CONTRACT__=owned',
|
||||
'__VC_VPC_BRIDGE_ADMIN__=up',
|
||||
'__VC_VPC_NFT_FILE_EXACT__=yes',
|
||||
'__VC_VPC_STATE_FILE_EXACT__=yes',
|
||||
'__VC_VPC_PERSISTENCE_EXACT__=yes',
|
||||
f'__VC_VPC_NFT_RECEIPT__={RECEIPT}',
|
||||
])
|
||||
self.applied = '\n'.join([
|
||||
'__VC_VPC_APPLIED__=yes',
|
||||
f'__VC_VPC_OWNER__={dp.DATAPLANE_OWNER_MARKER}',
|
||||
f'__VC_VPC_REVISION__={plan.desired_revision}',
|
||||
f'__VC_VPC_DIGEST__={plan.desired_digest}',
|
||||
f'__VC_VPC_NFT_RECEIPT__={RECEIPT}',
|
||||
])
|
||||
|
||||
def _deployment_contract(self):
|
||||
return ('networkmanager_nmcli', '/usr/sbin/ifup', '/usr/sbin/ifdown',
|
||||
'/usr/sbin/ifquery', 'native_nftables')
|
||||
|
||||
def _run(self, script):
|
||||
if '__VC_VPC_APPLIED__=yes' in script:
|
||||
self.calls.append('apply')
|
||||
if self.apply_error:
|
||||
raise self.apply_error
|
||||
return self.applied
|
||||
if '__VC_VPC_REMOVED__=yes' in script:
|
||||
self.calls.append('remove')
|
||||
return '__VC_VPC_REMOVED__=yes'
|
||||
self.calls.append('observe')
|
||||
if self.observe_error:
|
||||
raise self.observe_error
|
||||
return self.observation
|
||||
|
||||
|
||||
def test_success_is_reused_across_adapter_instances_without_activating_member(plan):
|
||||
first = Adapter(plan)
|
||||
assert first.apply(plan)['state'] == 'applying'
|
||||
assert first.calls == ['apply']
|
||||
second = Adapter(plan)
|
||||
result = second.apply(plan)
|
||||
assert second.calls == ['observe']
|
||||
assert result == {'state': 'applying', 'readback': 'pending', 'rollback': 'none',
|
||||
'owner': dp.DATAPLANE_OWNER_MARKER}
|
||||
public = second.observe(plan)
|
||||
assert public['state'] != 'active'
|
||||
assert public['config_verified'] is False
|
||||
assert public['member_nics'][0]['xml_verified'] is False
|
||||
assert second.calls == ['observe', 'observe']
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('old', 'new'), [
|
||||
('LOCAL_NEIGHBOR_STATE__=owned', 'LOCAL_NEIGHBOR_STATE__=missing'),
|
||||
('BRIDGE_ADMIN__=up', 'BRIDGE_ADMIN__=down'),
|
||||
('HELPER_CONTRACT__=owned', 'HELPER_CONTRACT__=missing'),
|
||||
('NFT_FILE_EXACT__=yes', 'NFT_FILE_EXACT__=no'),
|
||||
('STATE_FILE_EXACT__=yes', 'STATE_FILE_EXACT__=no'),
|
||||
('PERSISTENCE_EXACT__=yes', 'PERSISTENCE_EXACT__=no'),
|
||||
('FIREWALL_STATE__=owned', 'FIREWALL_STATE__=mismatch'),
|
||||
('PERSISTENCE__=yes', 'PERSISTENCE__=no'),
|
||||
('BRIDGE_BOUNDARY__=yes', 'BRIDGE_BOUNDARY__=no'),
|
||||
('FABRIC_NFT__=yes', 'FABRIC_NFT__=no'),
|
||||
('WG_PEER_SET__=exact', 'WG_PEER_SET__=extra'),
|
||||
('RP_FILTER__=2', 'RP_FILTER__=1'),
|
||||
('ROUTE_EXTRA__=no', 'ROUTE_EXTRA__=yes'),
|
||||
('RULE_EXTRA__=no', 'RULE_EXTRA__=yes'),
|
||||
('PROXY_STATE__=owned', 'PROXY_STATE__=missing'),
|
||||
(f'NFT_RECEIPT__={RECEIPT}', 'NFT_RECEIPT__=' + ','.join(c * 64 for c in 'abd')),
|
||||
('NFT_RECEIPT__=', 'UNKNOWN_RECEIPT__='),
|
||||
('FIREWALL_MODE__=', 'UNKNOWN_FIREWALL_MODE__='),
|
||||
])
|
||||
def test_every_host_gate_or_full_nft_drift_requires_protected_repair(plan, old, new):
|
||||
adapter = Adapter(plan)
|
||||
adapter.apply(plan)
|
||||
assert old in adapter.observation
|
||||
adapter.observation = adapter.observation.replace(old, new)
|
||||
adapter.apply(plan)
|
||||
assert adapter.calls == ['apply', 'observe', 'apply']
|
||||
|
||||
|
||||
def test_explicit_route_linkdown_never_skips(plan):
|
||||
adapter = Adapter(plan)
|
||||
adapter.apply(plan)
|
||||
adapter.observation = re.sub(
|
||||
r'(__VC_VPC_ROUTE__=[^\n]+)\|owned', r'\1|linkdown|owned',
|
||||
adapter.observation)
|
||||
adapter.apply(plan)
|
||||
assert adapter.calls == ['apply', 'observe', 'apply']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('error', [TimeoutError('read timed out'),
|
||||
dp.VpcDataplaneError('read failed', 502, 'read')])
|
||||
def test_read_exception_invalidates_receipt_without_mutation(plan, error):
|
||||
adapter = Adapter(plan)
|
||||
adapter.apply(plan)
|
||||
adapter.observe_error = error
|
||||
with pytest.raises(type(error)):
|
||||
adapter.apply(plan)
|
||||
assert adapter.calls == ['apply', 'observe']
|
||||
assert not dp._APPLY_RECEIPTS
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', [
|
||||
lambda s: s.replace('APPLIED__=yes', 'APPLIED__=no'),
|
||||
lambda s: s.replace('OWNER__=virt-cloud-vpc-dataplane-v1', 'OWNER__=foreign'),
|
||||
lambda s: s.replace('NFT_RECEIPT__=', 'MISSING_RECEIPT__='),
|
||||
lambda s: s.replace(RECEIPT, 'malformed'),
|
||||
lambda s: s + f'\n__VC_VPC_NFT_RECEIPT__={RECEIPT}',
|
||||
lambda s: s + '\n__VC_VPC_APPLIED__=yes',
|
||||
lambda s: s.replace('REVISION__=', 'MISSING_REVISION__='),
|
||||
lambda s: s.replace('DIGEST__=', 'MISSING_DIGEST__='),
|
||||
])
|
||||
def test_ambiguous_or_incomplete_apply_never_seeds_skip(plan, change):
|
||||
adapter = Adapter(plan)
|
||||
adapter.applied = change(adapter.applied)
|
||||
try:
|
||||
adapter.apply(plan)
|
||||
except dp.VpcDataplaneError:
|
||||
pass
|
||||
assert not dp._APPLY_RECEIPTS
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('key', 'value'), [
|
||||
('ssh_config_file', '/generation-b/config'), ('host', '192.0.2.2'),
|
||||
('port', 2222), ('fingerprint', 'other-key'), ('ssh_target', 'other-alias'),
|
||||
])
|
||||
def test_new_host_identity_requires_successful_apply(plan, key, value):
|
||||
Adapter(plan).apply(plan)
|
||||
second = Adapter(plan, **{key: value})
|
||||
second.apply(plan)
|
||||
assert second.calls == ['apply']
|
||||
|
||||
|
||||
def test_changed_plan_or_source_contract_does_not_use_old_receipt(plan, monkeypatch):
|
||||
adapter = Adapter(plan)
|
||||
adapter.apply(plan)
|
||||
original = dp.render_apply_script
|
||||
monkeypatch.setattr(dp, 'render_apply_script', lambda *a, **kw: original(*a, **kw) + '\n# new-source-contract\n')
|
||||
adapter.apply(plan)
|
||||
assert adapter.calls == ['apply', 'apply']
|
||||
|
||||
|
||||
def test_repair_failure_discards_old_success(plan):
|
||||
adapter = Adapter(plan)
|
||||
adapter.apply(plan)
|
||||
adapter.observation = adapter.observation.replace('BRIDGE_ADMIN__=up', 'BRIDGE_ADMIN__=down')
|
||||
adapter.apply_error = TimeoutError('uncertain write')
|
||||
with pytest.raises(TimeoutError):
|
||||
adapter.apply(plan)
|
||||
assert not dp._APPLY_RECEIPTS
|
||||
|
||||
|
||||
def test_remove_discards_success_receipt(plan):
|
||||
adapter = Adapter(plan)
|
||||
adapter.apply(plan)
|
||||
empty = replace(plan, members=tuple(m for m in plan.members if m.host_id != plan.host_id))
|
||||
adapter.remove(empty)
|
||||
assert not dp._APPLY_RECEIPTS
|
||||
|
||||
|
||||
def test_cache_is_bounded_under_concurrent_adapters(monkeypatch):
|
||||
monkeypatch.setattr(dp, '_APPLY_RECEIPT_LIMIT', 8)
|
||||
with ThreadPoolExecutor(max_workers=8) as executor:
|
||||
list(executor.map(lambda i: Adapter._save_receipt((str(i),), str(i), RECEIPT), range(100)))
|
||||
assert len(dp._APPLY_RECEIPTS) == 8
|
||||
Adapter._save_receipt(('latest',), 'latest', RECEIPT)
|
||||
assert len(dp._APPLY_RECEIPTS) == 8
|
||||
assert next(reversed(dp._APPLY_RECEIPTS)) == ('latest',)
|
||||
|
||||
|
||||
NFT_MOCK = r'''
|
||||
import os, sys
|
||||
args=sys.argv[1:]
|
||||
assert args[:3] == ['-s','-n','-y'], args
|
||||
scenario=os.environ.get('SCENARIO','healthy')
|
||||
if args[3:5] == ['list','tables']:
|
||||
if scenario=='inventory_error': sys.exit(96)
|
||||
if scenario=='malformed_inventory': print('permission denied'); sys.exit()
|
||||
if scenario=='missing': sys.exit()
|
||||
print('table '+args[5]+' owned')
|
||||
if scenario=='duplicate': print('table '+args[5]+' owned')
|
||||
else:
|
||||
assert args[3:5]==['list','table'] and args[6]=='owned', args
|
||||
if scenario=='read_error': sys.exit(97)
|
||||
if scenario=='empty': sys.exit()
|
||||
action='accept' if scenario=='verdict_drift' else 'drop'
|
||||
print('table '+args[5]+' owned { chain forward { '+action+' comment "same-owner-digest"; } }')
|
||||
'''
|
||||
|
||||
|
||||
def executable(path, body):
|
||||
path.write_text('#!' + sys.executable + '\n' + body)
|
||||
path.chmod(0o700)
|
||||
return str(path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('shell', SHELLS)
|
||||
@pytest.mark.parametrize('scenario', ['healthy', 'verdict_drift', 'inventory_error',
|
||||
'malformed_inventory', 'read_error', 'missing', 'duplicate', 'empty'])
|
||||
def test_actual_nft_receipt_shell_rejects_read_errors_and_kept_comment_drift(tmp_path, shell, scenario):
|
||||
nft = executable(tmp_path / 'nft', NFT_MOCK)
|
||||
sha = executable(tmp_path / 'sha256sum', 'import hashlib,sys\nprint(hashlib.sha256(sys.stdin.buffer.read()).hexdigest()+" -")\n')
|
||||
script = dp.VPC_APPLY_RECEIPT_SH + '\nNFT=$1; SHA256SUM=$2; TABLE=owned\nvc_vpc_nft_receipt\n'
|
||||
result = subprocess.run([shell, '-eu', '-s', nft, sha], input=script,
|
||||
text=True, capture_output=True,
|
||||
env=os.environ | {'SCENARIO': scenario})
|
||||
if scenario in {'healthy', 'verdict_drift'}:
|
||||
expected = ','.join(hashlib.sha256(
|
||||
('table '+family+' owned { chain forward { '+
|
||||
('accept' if scenario=='verdict_drift' else 'drop')+
|
||||
' comment "same-owner-digest"; } }\n').encode()).hexdigest()
|
||||
for family in ('inet', 'bridge', 'arp'))
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout.strip() == expected
|
||||
elif scenario == 'missing':
|
||||
assert result.returncode == 0 and result.stdout.strip() == 'missing'
|
||||
else:
|
||||
assert result.returncode != 0
|
||||
assert not result.stdout
|
||||
|
||||
|
||||
@pytest.mark.parametrize('shell', SHELLS)
|
||||
@pytest.mark.parametrize(('line', 'expected'), [
|
||||
('20: vcv-test: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP', 'up'),
|
||||
('20: vcv-test: <BROADCAST,MULTICAST,UP> mtu 1500 state DOWN', 'up'),
|
||||
('20: vcv-test: <BROADCAST,MULTICAST> mtu 1500 state DOWN', 'down'),
|
||||
('', None), ('malformed', None),
|
||||
])
|
||||
def test_bridge_admin_uses_flag_not_carrier_state(tmp_path, shell, line, expected):
|
||||
ip = executable(tmp_path / 'ip', 'import os\nprint(os.environ["LINK"])\n')
|
||||
result = subprocess.run([shell, '-eu', '-s', ip],
|
||||
input=dp.VPC_APPLY_RECEIPT_SH + '\nIP=$1; BRIDGE=vcv-test\nvc_vpc_bridge_admin_state\n',
|
||||
text=True, capture_output=True, env=os.environ | {'LINK': line})
|
||||
if expected is None:
|
||||
assert result.returncode != 0
|
||||
else:
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout.strip() == expected
|
||||
|
||||
|
||||
def test_receipt_is_anchored_in_protected_apply_and_persistence_exact(plan):
|
||||
script = dp.render_apply_script(plan)
|
||||
receipt = script.index('vc_apply_nft_receipt=$(vc_vpc_nft_receipt)')
|
||||
assert script.rfind('vc_vpc_nft_matches_snapshot "$TXN/nft.desired" || exit 74', 0, receipt) > 0
|
||||
assert script.index('vc_vpc_nft_matches_snapshot "$TXN/nft.desired" || exit 74', receipt) < script.index('__VC_VPC_NFT_RECEIPT__=$vc_apply_nft_receipt')
|
||||
assert script.index('__VC_VPC_NFT_RECEIPT__=$vc_apply_nft_receipt') < script.index('COMMITTED=yes', receipt)
|
||||
persisted = dp.render_vpc_nft_rules(plan).rstrip() + '\n'
|
||||
assert hashlib.sha256(persisted.encode()).hexdigest() in dp.render_observe_script(plan)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key', ['host', 'port', 'ssh_target', 'ssh_config_file', 'fingerprint'])
|
||||
def test_missing_host_identity_never_enters_shared_cache(plan, key):
|
||||
adapter = Adapter(plan)
|
||||
del adapter.profile[key]
|
||||
adapter.apply(plan)
|
||||
adapter.apply(plan)
|
||||
assert adapter.calls == ['apply', 'apply']
|
||||
assert not dp._APPLY_RECEIPTS
|
||||
|
||||
|
||||
@pytest.mark.parametrize('receipt', ['bad', RECEIPT + '\n__VC_VPC_NFT_RECEIPT__=' + RECEIPT])
|
||||
def test_malformed_observer_receipt_propagates_without_repair(plan, receipt):
|
||||
adapter = Adapter(plan)
|
||||
adapter.apply(plan)
|
||||
adapter.observation = adapter.observation.replace(RECEIPT, receipt)
|
||||
with pytest.raises(dp.VpcDataplaneError, match='收据无效'):
|
||||
adapter.apply(plan)
|
||||
assert adapter.calls == ['apply', 'observe']
|
||||
assert not dp._APPLY_RECEIPTS
|
||||
|
||||
|
||||
def test_explicit_nft_absence_repairs_without_losing_public_observation(plan):
|
||||
adapter = Adapter(plan)
|
||||
adapter.apply(plan)
|
||||
adapter.observation = adapter.observation.replace(RECEIPT, 'missing').replace(
|
||||
'ANTI_SPOOF__=yes', 'ANTI_SPOOF__=no')
|
||||
assert adapter.observe(plan)['state'] != 'active'
|
||||
adapter.apply(plan)
|
||||
assert adapter.calls == ['apply', 'observe', 'observe', 'apply']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('shell', SHELLS)
|
||||
@pytest.mark.parametrize('mutation', ['none', 'nft', 'state', 'ifup', 'missing_state', 'helper'])
|
||||
def test_rendered_host_receipt_requires_exact_persistent_bytes(tmp_path, shell, mutation, plan):
|
||||
nft_file = tmp_path / 'vpc.nft'
|
||||
state = tmp_path / 'vpc.state'
|
||||
fragment = tmp_path / 'vpc.ifup'
|
||||
nft_file.write_text(dp.render_vpc_nft_rules(plan).rstrip() + '\n')
|
||||
state.write_text(dp._state(plan, dp._routes(plan)))
|
||||
fragment.write_text(
|
||||
f'# {dp.DATAPLANE_OWNER_MARKER} vpc={plan.vpc.vpc_id} '
|
||||
f'revision={plan.desired_revision} digest={plan.desired_digest}\n'
|
||||
f'iface {plan.resources.bridge} inet manual\n'
|
||||
' bridge_ports none\n bridge-stp off\n')
|
||||
if mutation in {'nft', 'state', 'ifup'}:
|
||||
path = {'nft': nft_file, 'state': state, 'ifup': fragment}[mutation]
|
||||
path.write_text(path.read_text() + '# retained-owner-but-drift\n')
|
||||
if mutation == 'missing_state':
|
||||
state.unlink()
|
||||
sha = executable(tmp_path / 'sha256sum',
|
||||
'import hashlib,sys,pathlib\n'
|
||||
'raw=pathlib.Path(sys.argv[1]).read_bytes() if len(sys.argv)>1 else sys.stdin.buffer.read()\n'
|
||||
'print(hashlib.sha256(raw).hexdigest()+" -")\n')
|
||||
stat = executable(tmp_path / 'stat', 'print("0:0:600")\n')
|
||||
nft = executable(tmp_path / 'nft', NFT_MOCK)
|
||||
ip = executable(tmp_path / 'ip', 'print("20: vcv-test: <UP,LOWER_UP> mtu 1500 state UP")\n')
|
||||
rendered = dp.render_observe_script(plan)
|
||||
fragment_script = rendered.split('# vc-apply-receipt-observe-begin', 1)[1].split(
|
||||
'# vc-apply-receipt-observe-end', 1)[0]
|
||||
settings = {
|
||||
'IP': ip, 'NFT': nft, 'SHA256SUM': sha, 'STAT': stat,
|
||||
'BRIDGE': 'vcv-test', 'TABLE': 'owned', 'NFT_FILE': str(nft_file),
|
||||
'STATE': str(state), 'IF_FRAGMENT': str(fragment),
|
||||
'nft_file_ok': 'yes', 'bridge_shape': 'yes', 'nm_owned': 'no',
|
||||
'ifup_owned': 'yes', 'nm_present': 'no',
|
||||
}
|
||||
script = dp.VPC_APPLY_RECEIPT_SH + '\n' + '\n'.join(
|
||||
k + '=' + shlex.quote(v) for k, v in settings.items()) + '\n'
|
||||
script += 'vc_vpc_neighbor_helper_ready() { return ' + ('1' if mutation == 'helper' else '0') + '; }\n'
|
||||
result = subprocess.run([shell, '-eu'], input=script + fragment_script,
|
||||
text=True, capture_output=True)
|
||||
assert result.returncode == 0, result.stderr
|
||||
markers = dp._markers(result.stdout)
|
||||
assert markers['bridge_admin'] == 'up'
|
||||
assert markers['nft_file_exact'] == ('no' if mutation == 'nft' else 'yes')
|
||||
assert markers['state_file_exact'] == ('no' if mutation in {'state', 'missing_state'} else 'yes')
|
||||
assert markers['persistence_exact'] == ('no' if mutation == 'ifup' else 'yes')
|
||||
assert markers['helper_contract'] == ('missing' if mutation == 'helper' else 'owned')
|
||||
@ -0,0 +1,282 @@
|
||||
"""Run production local-neighbor transactions against a stateful ip boundary."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from test_vpc_nft_dataplane import _plan
|
||||
from virsh_panel.shell_contracts import LOCAL_NEIGHBOR_SH
|
||||
from virsh_panel.vpc_dataplane import (
|
||||
VPC_LOCAL_NEIGHBOR_TRANSACTION_SH,
|
||||
_routes,
|
||||
_state,
|
||||
render_apply_script,
|
||||
render_observe_script,
|
||||
render_remove_script,
|
||||
)
|
||||
|
||||
SHELLS = [path for path in ("/bin/sh", "/bin/dash", "/bin/bash") if Path(path).exists()]
|
||||
FIRST = "10.44.0.10|02:00:00:00:00:10"
|
||||
SECOND = "10.44.0.11|02:00:00:00:00:11"
|
||||
|
||||
IP_MOCK = r'''
|
||||
import json, os, pathlib, sys
|
||||
args=sys.argv[1:]
|
||||
path=pathlib.Path(os.environ['NEIGHBORS'])
|
||||
state=json.loads(path.read_text())
|
||||
if args == ['-o','-4','neigh','show','dev','vcv-test']:
|
||||
if os.environ.get('SCENARIO') == 'read_error': sys.exit(97)
|
||||
for ip,(mac,nud) in sorted(state['vcv-test'].items()):
|
||||
print(ip + (' lladdr '+mac if mac != '-' else '') + ' '+nud)
|
||||
sys.exit(0)
|
||||
if args == ['link','show','dev','vcv-test']: sys.exit(0)
|
||||
if args == ['link','set','dev','vcv-test','down']:
|
||||
with open(os.environ['ACTIONS'],'a') as log: log.write('down\n')
|
||||
state['vcv-test']={}
|
||||
else:
|
||||
if args[:1] == ['-4']: args=args[1:]
|
||||
if args[:2] not in (['neigh','replace'], ['neigh','del']): sys.exit(98)
|
||||
action,ip=args[1:3]
|
||||
assert args[-2:]==['dev','vcv-test']
|
||||
with open(os.environ['ACTIONS'],'a') as log: log.write(action+':'+ip+'\n')
|
||||
if action == 'del':
|
||||
if os.environ.get('SCENARIO') == 'delete_error': sys.exit(97)
|
||||
if ip not in state['vcv-test']: sys.exit(2)
|
||||
del state['vcv-test'][ip]
|
||||
else:
|
||||
assert args[3]=='lladdr' and args[5]=='nud'
|
||||
if os.environ.get('SCENARIO') == 'write_error' and ip.endswith('.11'): sys.exit(97)
|
||||
nud=args[6].upper()
|
||||
if os.environ.get('SCENARIO') == 'readback_drift': nud='STALE'
|
||||
state['vcv-test'][ip]=[args[4],nud]
|
||||
path.write_text(json.dumps(state))
|
||||
'''
|
||||
|
||||
|
||||
def boundary(tmp_path, current=None, *, before=FIRST, desired=FIRST, scenario=""):
|
||||
txn = tmp_path / "txn"
|
||||
txn.mkdir()
|
||||
(txn / "state.before").write_text("".join(
|
||||
f"local_neighbor|{pair}\n" for pair in before.splitlines() if pair))
|
||||
(txn / "neighbors.desired").write_text(desired + "\n" if desired else "")
|
||||
state = tmp_path / "neighbors.json"
|
||||
state.write_text(json.dumps({"vcv-test": current or {}, "other-bridge": {
|
||||
"10.44.0.10": ["02:ff:00:00:00:01", "PERMANENT"]}}))
|
||||
command = tmp_path / "ip"
|
||||
command.write_text("#!" + sys.executable + "\n" + IP_MOCK)
|
||||
command.chmod(0o700)
|
||||
actions = tmp_path / "actions"
|
||||
actions.write_text("")
|
||||
env = dict(os.environ, TXN=str(txn), NEIGHBORS=str(state), ACTIONS=str(actions),
|
||||
SCENARIO=scenario, IP=str(command))
|
||||
prefix = ("set -eu\nBRIDGE=vcv-test; CMP=cmp; BRIDGE_TOPOLOGY_CAPTURED=yes; "
|
||||
"LOCAL_NEIGHBORS_CHANGED=no\n" + LOCAL_NEIGHBOR_SH + "\n" +
|
||||
VPC_LOCAL_NEIGHBOR_TRANSACTION_SH + "\n")
|
||||
return env, prefix, state, actions
|
||||
|
||||
|
||||
def execute(shell, env, prefix, commands):
|
||||
return subprocess.run([shell, "-s"], input=prefix + commands, text=True,
|
||||
capture_output=True, env=env, timeout=15, check=False)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("prior_state", ["PERMANENT", "STALE", "INCOMPLETE", "FAILED", "absent"])
|
||||
def test_apply_survives_down_and_repeated_convergence(tmp_path, shell, prior_state):
|
||||
mac = "-" if prior_state in {"INCOMPLETE", "FAILED"} else FIRST.split("|")[1]
|
||||
current = {} if prior_state == "absent" else {"10.44.0.10": [mac, prior_state]}
|
||||
env, prefix, state, _ = boundary(tmp_path, current)
|
||||
result = execute(shell, env, prefix, '''
|
||||
vc_vpc_neighbors_baseline
|
||||
"$IP" link set dev "$BRIDGE" down
|
||||
vc_vpc_neighbors_reconcile
|
||||
# A later periodic apply must recreate permanent entries flushed by DOWN.
|
||||
vc_vpc_neighbors_baseline
|
||||
"$IP" link set dev "$BRIDGE" down
|
||||
vc_vpc_neighbors_reconcile
|
||||
''')
|
||||
assert result.returncode == 0, result.stderr
|
||||
actual = json.loads(state.read_text())
|
||||
assert actual["vcv-test"] == {"10.44.0.10": [FIRST.split("|")[1], "PERMANENT"]}
|
||||
assert actual["other-bridge"] == {"10.44.0.10": ["02:ff:00:00:00:01", "PERMANENT"]}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("current", [
|
||||
{"10.44.0.10": ["02:ff:00:00:00:10", "PERMANENT"]},
|
||||
{"10.44.0.99": ["02:00:00:00:00:99", "STALE"]},
|
||||
{"10.44.0.99": ["-", "FAILED"]},
|
||||
])
|
||||
def test_foreign_binding_rejected_before_mutation(tmp_path, shell, current):
|
||||
env, prefix, state, actions = boundary(tmp_path, current)
|
||||
original = state.read_bytes()
|
||||
result = execute(shell, env, prefix, "vc_vpc_neighbors_baseline\nvc_vpc_neighbors_reconcile\n")
|
||||
assert result.returncode != 0
|
||||
assert state.read_bytes() == original
|
||||
assert actions.read_text() == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("prior_state", ["PERMANENT", "REACHABLE", "NOARP"])
|
||||
def test_partial_apply_rollback_restores_only_sampled_bindings(tmp_path, shell, prior_state):
|
||||
env, prefix, state, _ = boundary(tmp_path,
|
||||
{"10.44.0.10": [FIRST.split("|")[1], prior_state]},
|
||||
desired=FIRST + "\n" + SECOND, scenario="write_error")
|
||||
result = execute(shell, env, prefix, '''
|
||||
vc_vpc_neighbors_baseline
|
||||
"$IP" link set dev "$BRIDGE" down
|
||||
if vc_vpc_neighbors_reconcile; then exit 99; fi
|
||||
vc_vpc_neighbors_rollback_guard
|
||||
"$IP" link set dev "$BRIDGE" down
|
||||
vc_vpc_neighbors_restore
|
||||
''')
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert json.loads(state.read_text())["vcv-test"] == {
|
||||
"10.44.0.10": [FIRST.split("|")[1], prior_state if prior_state in {"PERMANENT", "NOARP"} else "STALE"]}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("scenario", ["read_error", "readback_drift"])
|
||||
def test_read_or_readback_failure_never_succeeds(tmp_path, shell, scenario):
|
||||
env, prefix, _, _ = boundary(tmp_path, scenario=scenario)
|
||||
result = execute(shell, env, prefix, "vc_vpc_neighbors_baseline\nvc_vpc_neighbors_reconcile\n")
|
||||
assert result.returncode != 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("prior_state", ["PERMANENT", "STALE"])
|
||||
def test_legacy_state_adopts_only_authenticated_same_binding(tmp_path, shell, prior_state):
|
||||
env, prefix, state, _ = boundary(tmp_path,
|
||||
{"10.44.0.10": [FIRST.split("|")[1], prior_state]}, before="")
|
||||
result = execute(shell, env, prefix, "vc_vpc_neighbors_baseline\nvc_vpc_neighbors_reconcile\n")
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert json.loads(state.read_text())["vcv-test"]["10.44.0.10"][1] == "PERMANENT"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
def test_member_removal_deletes_old_binding_and_keeps_remaining(tmp_path, shell):
|
||||
env, prefix, state, actions = boundary(tmp_path, {
|
||||
"10.44.0.10": [FIRST.split("|")[1], "PERMANENT"],
|
||||
"10.44.0.11": [SECOND.split("|")[1], "PERMANENT"]},
|
||||
before=FIRST + "\n" + SECOND, desired=SECOND)
|
||||
result = execute(shell, env, prefix, "vc_vpc_neighbors_baseline\nvc_vpc_neighbors_reconcile\n")
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert json.loads(state.read_text())["vcv-test"] == {"10.44.0.11": [SECOND.split("|")[1], "PERMANENT"]}
|
||||
assert actions.read_text().splitlines() == ["del:10.44.0.10", "replace:10.44.0.11"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
def test_empty_expected_does_not_accept_foreign_neighbor(tmp_path, shell):
|
||||
env, prefix, _, actions = boundary(tmp_path,
|
||||
{"10.44.0.10": [FIRST.split("|")[1], "PERMANENT"]}, before="", desired="")
|
||||
result = execute(shell, env, prefix, "vc_vpc_neighbors_baseline\n")
|
||||
assert result.returncode != 0
|
||||
assert actions.read_text() == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
def test_rollback_refuses_binding_changed_after_snapshot(tmp_path, shell):
|
||||
env, prefix, state, actions = boundary(tmp_path,
|
||||
{"10.44.0.10": [FIRST.split("|")[1], "PERMANENT"]})
|
||||
result = execute(shell, env, prefix, "vc_vpc_neighbors_baseline\n")
|
||||
assert result.returncode == 0, result.stderr
|
||||
current = json.loads(state.read_text())
|
||||
current["vcv-test"]["10.44.0.10"][0] = "02:ff:00:00:00:10"
|
||||
state.write_text(json.dumps(current))
|
||||
original = state.read_bytes()
|
||||
result = execute(shell, env, prefix, "vc_vpc_neighbors_restore\n")
|
||||
assert result.returncode != 0
|
||||
assert state.read_bytes() == original
|
||||
assert actions.read_text() == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("case", ["new", "old", "wrong_digest", "wrong_mode", "symlink", "missing"])
|
||||
def test_helper_upgrade_gate_runs_before_network_mutation(tmp_path, shell, case):
|
||||
env, prefix, state, actions = boundary(tmp_path)
|
||||
digest = "ab" * 32
|
||||
helper = tmp_path / "virt-cloud-wg-fabric"
|
||||
header = ("#!/bin/sh\n# virt-cloud-wg-fabric-v1\n# fabric-plan-digest=" + digest +
|
||||
'\nPLAN_DIGEST="' + digest + '"\n# libvirt-startup-independent-v1\n')
|
||||
if case != "old": header += "# vpc-local-neighbor-contract-v1\n"
|
||||
if case == "wrong_digest": header = header.replace(digest, "cd" * 32)
|
||||
if case != "missing":
|
||||
helper.write_text(header)
|
||||
helper.chmod(0o700)
|
||||
if case == "symlink":
|
||||
target = helper.with_suffix(".real")
|
||||
helper.rename(target)
|
||||
helper.symlink_to(target)
|
||||
prefix = prefix.replace("vc_ln_helper=/usr/local/libexec/virt-cloud-wg-fabric",
|
||||
"vc_ln_helper=" + shlex.quote(str(helper)))
|
||||
prefix += "\nEXPECTED_DIGEST=" + digest + "\nSTAT=fake_stat\n"
|
||||
prefix += "fake_stat() { printf '%s\\n' " + ("0:0:755" if case == "wrong_mode" else "0:0:700") + "; }\n"
|
||||
script = render_apply_script(_plan())
|
||||
guard = "vc_vpc_neighbor_helper_ready || exit 71"
|
||||
assert script.index(guard) < script.index('mkdir -m 700 "$TXN"')
|
||||
assert script.index(guard) < script.index("MUTATION_STARTED=yes")
|
||||
result = execute(shell, env, prefix, guard + '\n"$IP" link set dev "$BRIDGE" down\nprintf ready\n')
|
||||
assert result.returncode == (0 if case == "new" else 71), result.stderr
|
||||
assert result.stdout == ("ready" if case == "new" else "")
|
||||
assert actions.read_text() == ("down\n" if case == "new" else "")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("case", ["exact", "missing", "dynamic", "wrong_mac", "legacy", "reader_error"])
|
||||
def test_rendered_observer_requires_durable_and_permanent_binding(tmp_path, shell, case):
|
||||
plan = _plan()
|
||||
member = plan.local_members[0]
|
||||
pair = f"{member.private_ip}|{member.private_mac}"
|
||||
current = {} if case == "missing" else {member.private_ip: [
|
||||
"02:ff:00:00:00:10" if case == "wrong_mac" else member.private_mac,
|
||||
"STALE" if case == "dynamic" else "PERMANENT"]}
|
||||
env, prefix, _, _ = boundary(tmp_path, current, before="" if case == "legacy" else pair,
|
||||
desired=pair, scenario="read_error" if case == "reader_error" else "")
|
||||
env["STATE"] = str(tmp_path / "txn" / "state.before")
|
||||
script = render_observe_script(plan)
|
||||
fragment = script.split("# vc-local-neighbor-observe-begin\n", 1)[1].split(
|
||||
"# vc-local-neighbor-observe-end", 1)[0]
|
||||
result = execute(shell, env, prefix, fragment)
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout.strip() == "__VC_VPC_LOCAL_NEIGHBOR_STATE__=" + ("owned" if case == "exact" else "missing")
|
||||
|
||||
|
||||
def test_local_neighbor_state_contains_only_local_vm_next_hops():
|
||||
plan = _plan()
|
||||
state = _state(plan, _routes(plan))
|
||||
assert [line for line in state.splitlines() if line.startswith("local_neighbor|")] == [
|
||||
f"local_neighbor|{m.private_ip}|{m.private_mac}" for m in sorted(plan.local_members, key=lambda m: m.private_ip)]
|
||||
assert all(f"local_neighbor|{m.private_ip}|" not in state for m in plan.remote_members)
|
||||
script = render_apply_script(plan)
|
||||
assert script.index("vc_vpc_neighbors_baseline || exit 71") < script.index("MUTATION_STARTED=yes\n RUNTIME_QUARANTINED=yes")
|
||||
assert script.index("vc_vpc_neighbors_reconcile || exit 72") < script.index("ROUTES_CHANGED=yes\n")
|
||||
assert script.index("vc_ln_final_actual=") < script.index("printf '%s\\n' ready")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("case", ["healthy", "foreign", "delete_error"])
|
||||
def test_rendered_remove_cleanup_only_deletes_exact_old_members(tmp_path, shell, case):
|
||||
env, prefix, state, actions = boundary(tmp_path, {
|
||||
"10.44.0.10": ["02:ff:00:00:00:10" if case == "foreign" else FIRST.split("|")[1], "PERMANENT"]},
|
||||
scenario=case)
|
||||
env["REMOVE_NEIGHBOR_EXPECTED"] = FIRST
|
||||
plan = _plan()
|
||||
script = render_remove_script(replace(plan, members=tuple(m for m in plan.members if m.host_id != plan.host_id)))
|
||||
fragment = script.split("# vc-local-neighbor-remove-cleanup-begin\n", 1)[1].split(
|
||||
"# vc-local-neighbor-remove-cleanup-end", 1)[0]
|
||||
result = execute(shell, env, prefix, fragment)
|
||||
if case == "healthy":
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert json.loads(state.read_text())["vcv-test"] == {}
|
||||
assert actions.read_text() == "del:10.44.0.10\n"
|
||||
else:
|
||||
assert result.returncode != 0
|
||||
assert json.loads(state.read_text())["vcv-test"]
|
||||
if case == "foreign": assert actions.read_text() == ""
|
||||
Loading…
Reference in New Issue