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.
177 lines
6.8 KiB
Python
177 lines
6.8 KiB
Python
"""Check durable VPC peer state and complete Fabric overlay ranges."""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import replace
|
|
import shlex
|
|
import subprocess
|
|
|
|
import pytest
|
|
from test_vpc_nft_dataplane import _plan
|
|
import virsh_panel.vpc_dataplane as dataplane_module
|
|
from virsh_panel.shell_contracts import WG_ALLOWED_IPS_SH
|
|
|
|
|
|
|
|
def _loader_from_script(script):
|
|
start = script.index('VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"')
|
|
return script[start:script.index('vpc_state_load || exit 74', start)]
|
|
|
|
|
|
@pytest.mark.parametrize('peer_mode', ['none', 'valid', 'duplicate_ip', 'public_ip'])
|
|
def test_full_vpc_state_load_handles_peers_under_nounset(tmp_path, peer_mode):
|
|
plan = _plan()
|
|
text = dataplane_module._state(plan, dataplane_module._routes(plan))
|
|
if peer_mode == 'none':
|
|
text = '\n'.join(line for line in text.splitlines()
|
|
if not line.startswith('peer|')) + '\n'
|
|
elif peer_mode == 'duplicate_ip':
|
|
text += next(line for line in text.splitlines()
|
|
if line.startswith('peer|')) + '\n'
|
|
elif peer_mode == 'public_ip':
|
|
text = text.replace('10.44.0.11/32', '203.0.113.11/32')
|
|
state_dir = tmp_path / 'states'
|
|
state_dir.mkdir(mode=0o700)
|
|
state_file = state_dir / (plan.resources.token + '.state')
|
|
state_file.write_text(text)
|
|
state_file.chmod(0o600)
|
|
script = dataplane_module.render_apply_script(plan)
|
|
# The parser executes unchanged with set -u. This fixture adapter replaces
|
|
# GNU stat/root ownership only, so macOS can execute the same shell test.
|
|
harness = '\n'.join((
|
|
'set -eu',
|
|
_loader_from_script(script),
|
|
'VPC_STATE_DIR=' + shlex.quote(str(state_dir)),
|
|
'VPC_FIXTURE_STATE=' + shlex.quote(str(state_file)),
|
|
'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'''
|
|
vc_fixture_stat() {
|
|
[ "$1" = -c ] || return 9
|
|
case "$2" in
|
|
'%u:%a')
|
|
if [ "$3" = "$VPC_STATE_DIR" ]; then printf '0:700\n'
|
|
elif [ "$3" = "$VPC_FIXTURE_STATE" ]; then printf '0:600\n'
|
|
else return 9; fi ;;
|
|
'%s') [ "$3" = "$VPC_FIXTURE_STATE" ] || return 9
|
|
wc -c < "$3" | tr -d ' ' ;;
|
|
*) return 9 ;;
|
|
esac
|
|
}
|
|
STAT=vc_fixture_stat
|
|
vpc_state_load
|
|
printf 'state=%s\n' "$VPC_STATE_STATUS"
|
|
'''.strip(),
|
|
))
|
|
result = subprocess.run(['/bin/sh', '-c', harness], text=True,
|
|
capture_output=True, check=False)
|
|
if peer_mode in ('none', 'valid'):
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout == 'state=owned\n'
|
|
peers = (tmp_path / 'peers').read_text()
|
|
if peer_mode == 'none':
|
|
assert peers == ''
|
|
else:
|
|
assert peers == plan.peers[0].public_key + '|10.44.0.11/32\n'
|
|
else:
|
|
assert result.returncode != 0
|
|
assert 'state=owned' not in result.stdout
|
|
|
|
|
|
@pytest.mark.parametrize('transaction', ['apply', 'remove'])
|
|
@pytest.mark.parametrize(('actual', 'overlay'), [
|
|
('100.64.0.2/32,10.44.0.99/32', '100.64.0.2/32'),
|
|
('10.44.0.99/32,100.99.106.2/32', '100.99.106.2/32'),
|
|
('100.127.255.254/32,10.44.0.99/32', '100.127.255.254/32'),
|
|
('100.99.106.2/32', '100.99.106.2/32'),
|
|
('100.63.255.254/32,10.44.0.99/32', None),
|
|
('100.128.0.2/32,10.44.0.99/32', None),
|
|
('100.99.106.2/24,10.44.0.99/32', None),
|
|
('100.99.256.2/32,10.44.0.99/32', None),
|
|
('100.99.106.2/32,100.64.0.2/32', None),
|
|
('100.99.106.2/32,100.99.106.2/32', None),
|
|
('100.99.106.2/32,0.0.0.0/0', None),
|
|
('100.99.106.2/32,203.0.113.1/32', None),
|
|
('100.99.106.2/32,,10.44.0.99/32', None),
|
|
('100.99.106.2/32,', None),
|
|
('', None),
|
|
])
|
|
def test_vpc_apply_and_remove_merge_actual_full_fabric_overlay(
|
|
tmp_path, transaction, actual, overlay):
|
|
plan = _plan()
|
|
expected_overlay = overlay or '100.99.106.2/32'
|
|
plan = replace(plan, peers=(replace(
|
|
plan.peers[0], overlay_ip=expected_overlay.removesuffix('/32')),))
|
|
if transaction == 'apply':
|
|
script = dataplane_module.render_apply_script(plan)
|
|
start = script.index('while IFS=\'|\' read -r peer_host key allowed; do')
|
|
end = script.index('# The shared Fabric quarantine table', start)
|
|
else:
|
|
script = dataplane_module.render_remove_script(replace(
|
|
plan, members=tuple(m for m in plan.members
|
|
if m.host_id != plan.host_id)))
|
|
start = script.index('for key in $("$WG" show "$WG_IFACE" peers')
|
|
end = script.index('vc_vpc_fabric_nft_live_ready || exit 74', start)
|
|
merge = script[start:end]
|
|
peer_file = tmp_path / 'peers'
|
|
peer_file.write_text(plan.peers[0].public_key + '|10.44.0.11/32\n')
|
|
mutations = tmp_path / 'mutations'
|
|
mutations.write_text('')
|
|
harness = '\n'.join((
|
|
'set -eu',
|
|
_loader_from_script(script),
|
|
WG_ALLOWED_IPS_SH,
|
|
'VPC_PEER_FILE=' + shlex.quote(str(peer_file)),
|
|
'VC_ACTUAL=' + shlex.quote(actual),
|
|
'VC_KEY=' + shlex.quote(plan.peers[0].public_key),
|
|
'VC_MUTATIONS=' + shlex.quote(str(mutations)),
|
|
'WG_IFACE=wg-fabric0',
|
|
r'''
|
|
vc_fixture_wg() {
|
|
if [ "$*" = 'show wg-fabric0 peers' ]; then
|
|
printf '%s\n' "$VC_KEY"
|
|
elif [ "$*" = 'show wg-fabric0 allowed-ips' ]; then
|
|
printf '%s\t%s\n' "$VC_KEY" "$VC_ACTUAL"
|
|
elif [ "$1" = set ] && [ "$2" = wg-fabric0 ] &&
|
|
[ "$3" = peer ] && [ "$4" = "$VC_KEY" ] &&
|
|
[ "$5" = allowed-ips ]; then
|
|
printf '%s\n' "$6" >> "$VC_MUTATIONS"
|
|
else return 9; fi
|
|
}
|
|
WG=vc_fixture_wg
|
|
'''.strip(),
|
|
merge,
|
|
))
|
|
result = subprocess.run(['/bin/sh', '-c', harness], text=True,
|
|
capture_output=True, check=False)
|
|
if overlay is not None:
|
|
assert result.returncode == 0, result.stderr
|
|
assert mutations.read_text() == overlay + ',10.44.0.11/32\n'
|
|
else:
|
|
assert result.returncode == 74, result.stderr
|
|
assert mutations.read_text() == ''
|
|
|
|
|
|
@pytest.mark.parametrize('malformed', [
|
|
'100.99.106.2/32\n100.64.0.2/32',
|
|
'100.99.106.2/32 10.44.0.99/32',
|
|
'100.99.106.2/32;anything',
|
|
'100.99.106.2/32,*',
|
|
])
|
|
def test_overlay_parser_rejects_malformed_raw_input_without_shell_side_effects(
|
|
malformed):
|
|
script = dataplane_module.render_apply_script(_plan())
|
|
harness = '\n'.join((
|
|
'set -eu', _loader_from_script(script),
|
|
'IFS=:',
|
|
'if vpc_state_overlay_from_allowed ' + shlex.quote(malformed)
|
|
+ '; then exit 99; fi',
|
|
'[ "$IFS" = : ]',
|
|
'case "$-" in *f*) exit 98 ;; esac',
|
|
))
|
|
result = subprocess.run(['/bin/sh', '-c', harness], capture_output=True,
|
|
text=True, check=False)
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout == ''
|