daemon: removed utils.validate_ip and shifted tests to test_nodes

This commit is contained in:
Blake Harnden 2020-06-19 15:32:17 -07:00
parent 1829a8e2f8
commit 0d4a360e89
3 changed files with 14 additions and 35 deletions

View file

@ -444,17 +444,3 @@ def validate_mac(value: str) -> str:
return str(mac)
except netaddr.AddrFormatError as e:
raise CoreError(f"invalid mac address {value}: {e}")
def validate_ip(value: str) -> str:
"""
Validate ip address with prefix and return formatted version.
:param value: address to validate
:return: formatted ip address
"""
try:
ip = netaddr.IPNetwork(value)
return str(ip)
except (ValueError, netaddr.AddrFormatError) as e:
raise CoreError(f"invalid ip address {value}: {e}")

View file

@ -75,19 +75,30 @@ class TestNodes:
with pytest.raises(CoreError):
node.set_mac(iface.node_id, mac)
def test_node_add_ip(self, session: Session):
@pytest.mark.parametrize(
"ip,expected,is_ip6",
[
("127", "127.0.0.0/32", False),
("10.0.0.1/24", "10.0.0.1/24", False),
("2001::", "2001::/128", True),
("2001::/64", "2001::/64", True),
],
)
def test_node_add_ip(self, session: Session, ip: str, expected: str, is_ip6: bool):
# given
node = session.add_node(CoreNode)
switch = session.add_node(SwitchNode)
iface_data = InterfaceData()
iface = node.new_iface(switch, iface_data)
ip = "192.168.0.1/24"
# when
node.add_ip(iface.node_id, ip)
# then
assert str(iface.get_ip4()) == ip
if is_ip6:
assert str(iface.get_ip6()) == expected
else:
assert str(iface.get_ip4()) == expected
def test_node_add_ip_exception(self, session):
# given

View file

@ -25,24 +25,6 @@ class TestUtils:
assert len(two_args) == 2
assert len(unicode_args) == 3
@pytest.mark.parametrize(
"data,expected",
[
("127", "127.0.0.0/32"),
("10.0.0.1/24", "10.0.0.1/24"),
("2001::", "2001::/128"),
("2001::/64", "2001::/64"),
],
)
def test_validate_ip(self, data: str, expected: str):
value = utils.validate_ip(data)
assert value == expected
@pytest.mark.parametrize("data", ["256", "1270.0.0.1", "127.0.0.0.1"])
def test_validate_ip_exception(self, data: str):
with pytest.raises(CoreError):
utils.validate_ip("")
@pytest.mark.parametrize(
"data,expected",
[