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/tests/test_public_ip_pool.py

113 lines
4.0 KiB
Python

"""Validation of a host's public IPv4 allocation range."""
from __future__ import annotations
import pytest
from virsh_panel.remote_host import RemoteHostError, 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(
("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)