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

201 lines
6.9 KiB
Python

"""Validation of a host's public IPv4 allocation range."""
from __future__ import annotations
import ipaddress
import pytest
from virsh_panel.remote_host import (
MAX_PUBLIC_NETWORK_POOLS,
RemoteHostError,
validate_public_network_pool,
validate_public_network_pools,
validate_public_pool,
)
_DEFAULT = object()
def validate(cidr="1.2.3.0/24", gateway="1.2.3.1", dns=_DEFAULT, reserved=_DEFAULT,
vm_prefix=None):
return validate_public_pool(
cidr,
gateway,
["1.1.1.1", "8.8.8.8"] if dns is _DEFAULT else dns,
[] if reserved is _DEFAULT else reserved,
"1.2.3.2",
vm_prefix,
)
def test_unconfigured_public_pool_has_empty_defaults():
assert validate_public_pool("", "", [], [], "host.example") == {
"public_pool_cidr": "",
"public_gateway": "",
"public_dns_servers": [],
"public_reserved_ips": [],
"public_vm_prefix": None,
}
with pytest.raises(RemoteHostError):
validate_public_pool("", "", [], [], "host.example", 24)
def test_public_pool_normalizes_and_deduplicates_addresses():
assert validate(
dns=["1.1.1.1", "1.1.1.1", "8.8.8.8"],
reserved=["1.2.3.10", "1.2.3.10", "1.2.3.20"],
) == {
"public_pool_cidr": "1.2.3.0/24",
"public_gateway": "1.2.3.1",
"public_dns_servers": ["1.1.1.1", "8.8.8.8"],
"public_reserved_ips": ["1.2.3.10", "1.2.3.20"],
"public_vm_prefix": None,
}
@pytest.mark.parametrize("cidr", ["123.123.123.112/28", "123.123.123.113/28"])
def test_provider_28_is_normalized_without_widening_to_24(cidr):
configured = validate(
cidr, "123.123.123.113", ["1.1.1.1"],
["123.123.123.114", "123.123.123.126"],
)
assert configured["public_pool_cidr"] == "123.123.123.112/28"
assert configured["public_gateway"] == "123.123.123.113"
assert configured["public_reserved_ips"] == [
"123.123.123.114", "123.123.123.126",
]
def test_guest_mask_can_be_24_while_allocation_remains_28():
configured = validate("123.123.123.112/28", "123.123.123.113",
["1.1.1.1"], [], vm_prefix=24)
assert configured["public_pool_cidr"] == "123.123.123.112/28"
assert configured["public_vm_prefix"] == 24
@pytest.mark.parametrize("prefix", [23, 29, 31, True, "24"])
def test_guest_mask_rejects_values_outside_pool_bounds(prefix):
with pytest.raises(RemoteHostError):
validate("123.123.123.112/28", "123.123.123.113",
["1.1.1.1"], [], vm_prefix=prefix)
@pytest.mark.parametrize(
("kwargs", "error_code"),
[
({"cidr": "1.2.3.18/24"}, "host_public_pool_invalid"),
({"gateway": ""}, "host_public_gateway_invalid"),
({"cidr": "1.2.3.0/24", "gateway": "1.2.4.1"},
"host_public_gateway_invalid"),
({"dns": []}, "host_public_dns_invalid"),
({"dns": ["10.0.0.1"]}, "host_public_dns_invalid"),
({"reserved": ["1.2.4.2"]}, "host_public_reserved_ip_invalid"),
({"cidr": "123.123.123.112/28",
"gateway": "123.123.123.113",
"dns": ["1.1.1.1"],
"vm_prefix": 29}, "host_vm_prefix_invalid"),
],
)
def test_public_pool_errors_have_stable_codes(kwargs, error_code):
with pytest.raises(RemoteHostError) as rejected:
validate(**kwargs)
assert rejected.value.error_code == error_code
@pytest.mark.parametrize(
("cidr", "gateway", "dns", "reserved"),
[
("1.2.3.0/24", "", ["1.1.1.1"], []),
("", "1.2.3.1", ["1.1.1.1"], []),
("1.2.3.0/24", "1.2.3.1", [], []),
("", "", [], ["1.2.3.10"]),
("1.2.3.1/24", "1.2.3.1", ["1.1.1.1"], []),
("1.2.2.0/23", "1.2.3.1", ["1.1.1.1"], []),
("1.2.3.0/31", "1.2.3.1", ["1.1.1.1"], []),
("123.123.123.112/28", "123.123.123.111", ["1.1.1.1"], []),
("123.123.123.112/28", "123.123.123.127", ["1.1.1.1"], []),
("123.123.123.112/28", "123.123.123.113", ["1.1.1.1"], ["123.123.123.128"]),
("10.0.0.0/24", "10.0.0.1", ["1.1.1.1"], []),
("203.0.113.0/24", "203.0.113.1", ["1.1.1.1"], []),
("2001:db8::/24", "2001:db8::1", ["1.1.1.1"], []),
("1.2.3.0/24", "1.2.4.1", ["1.1.1.1"], []),
("1.2.3.0/24", "1.2.3.0", ["1.1.1.1"], []),
("1.2.3.0/24", "1.2.3.255", ["1.1.1.1"], []),
("1.2.3.0/24", "1.2.3.1", ["10.0.0.1"], []),
("1.2.3.0/24", "1.2.3.1",
["1.1.1.1", "8.8.8.8", "9.9.9.9", "208.67.222.222", "208.67.220.220"], []),
("1.2.3.0/24", "1.2.3.1", ["1.1.1.1"], ["1.2.4.2"]),
("1.2.3.0/24", "1.2.3.1", ["1.1.1.1"], ["1.2.3.0"]),
("1.2.3.0/24", "1.2.3.1", ["1.1.1.1"], ["1.2.3.255"]),
],
)
def test_public_pool_rejects_invalid_configuration(cidr, gateway, dns, reserved):
with pytest.raises(RemoteHostError):
validate(cidr, gateway, dns, reserved)
@pytest.mark.parametrize("value", [None, "1.1.1.1", ["1.1.1.1", None]])
def test_dns_must_be_list_of_ip_strings(value):
with pytest.raises(RemoteHostError):
validate(dns=value)
def native_pool(key="a" * 32, cidr="8.8.8.0/29", gateway="8.8.8.1",
revision=1, state="active"):
return {
"pool_key": key,
"revision": revision,
"state": state,
"network_selector": "bridge:br0",
"cidr": cidr,
"gateway": gateway,
"dns_servers": ["1.1.1.1"],
"reserved_ips": [str(next(iter(ipaddress.ip_network(cidr).hosts())))],
"guest_prefix_length": ipaddress.ip_network(cidr).prefixlen,
}
def test_revisioned_public_pool_normalizes_and_preserves_stable_key():
value = native_pool(cidr="8.8.8.8/29", gateway="8.8.8.9", revision=7)
value["reserved_ips"] = ["8.8.8.10", "8.8.8.10"]
assert validate_public_network_pool(value, "8.8.4.4") == {
**value,
"cidr": "8.8.8.8/29",
"reserved_ips": ["8.8.8.10"],
}
def test_legacy_pool_key_is_read_only_compatibility_identity():
value = native_pool(key="legacy-default")
with pytest.raises(RemoteHostError):
validate_public_network_pool(value, "8.8.4.4")
assert validate_public_network_pool(
value, "8.8.4.4", allow_legacy_key=True,
)["pool_key"] == "legacy-default"
def test_public_pool_catalog_rejects_duplicate_and_overlapping_networks():
first = native_pool()
with pytest.raises(RemoteHostError):
validate_public_network_pools([first, {**first}], "8.8.4.4")
with pytest.raises(RemoteHostError):
validate_public_network_pools([
first,
native_pool("b" * 32, "8.8.8.0/30", "8.8.8.1"),
], "8.8.4.4")
def test_public_pool_catalog_has_a_bounded_size():
values = []
for number in range(MAX_PUBLIC_NETWORK_POOLS + 1):
third = number // 32
fourth = (number % 32) * 8
values.append(native_pool(
f"{number:032x}", f"9.9.{third}.{fourth}/29",
f"9.9.{third}.{fourth + 1}",
))
with pytest.raises(RemoteHostError):
validate_public_network_pools(values, "8.8.4.4")