daemon: removed utils.validate_mac and shifted tests to test_nodes

This commit is contained in:
Blake Harnden 2020-06-19 15:41:41 -07:00
parent 0d4a360e89
commit adfce52632
3 changed files with 14 additions and 40 deletions

View file

@ -33,7 +33,7 @@ from typing import (
import netaddr
from core.errors import CoreCommandError, CoreError
from core.errors import CoreCommandError
if TYPE_CHECKING:
from core.emulator.session import Session
@ -430,17 +430,3 @@ def random_mac() -> str:
value |= 0x00163E << 24
mac = netaddr.EUI(value, dialect=netaddr.mac_unix_expanded)
return str(mac)
def validate_mac(value: str) -> str:
"""
Validate mac and return unix formatted version.
:param value: address to validate
:return: unix formatted mac
"""
try:
mac = netaddr.EUI(value, dialect=netaddr.mac_unix_expanded)
return str(mac)
except netaddr.AddrFormatError as e:
raise CoreError(f"invalid mac address {value}: {e}")

View file

@ -49,27 +49,35 @@ class TestNodes:
with pytest.raises(CoreError):
session.get_node(node.id, CoreNode)
def test_node_set_mac(self, session: Session):
@pytest.mark.parametrize(
"mac,expected",
[
("AA-AA-AA-FF-FF-FF", "aa:aa:aa:ff:ff:ff"),
("00:00:00:FF:FF:FF", "00:00:00:ff:ff:ff"),
],
)
def test_node_set_mac(self, session: Session, mac: str, expected: str):
# given
node = session.add_node(CoreNode)
switch = session.add_node(SwitchNode)
iface_data = InterfaceData()
iface = node.new_iface(switch, iface_data)
mac = "aa:aa:aa:ff:ff:ff"
# when
node.set_mac(iface.node_id, mac)
# then
assert str(iface.mac) == mac
assert str(iface.mac) == expected
def test_node_set_mac_exception(self, session: Session):
@pytest.mark.parametrize(
"mac", ["AAA:AA:AA:FF:FF:FF", "AA:AA:AA:FF:FF", "AA/AA/AA/FF/FF/FF"]
)
def test_node_set_mac_exception(self, session: Session, mac: str):
# given
node = session.add_node(CoreNode)
switch = session.add_node(SwitchNode)
iface_data = InterfaceData()
iface = node.new_iface(switch, iface_data)
mac = "aa:aa:aa:ff:ff:fff"
# when
with pytest.raises(CoreError):

View file

@ -1,8 +1,6 @@
import netaddr
import pytest
from core import utils
from core.errors import CoreError
class TestUtils:
@ -25,24 +23,6 @@ class TestUtils:
assert len(two_args) == 2
assert len(unicode_args) == 3
@pytest.mark.parametrize(
"data,expected",
[
("AA-AA-AA-FF-FF-FF", "aa:aa:aa:ff:ff:ff"),
("00:00:00:FF:FF:FF", "00:00:00:ff:ff:ff"),
],
)
def test_validate_mac(self, data: str, expected: str):
value = utils.validate_mac(data)
assert value == expected
@pytest.mark.parametrize(
"data", ["AAA:AA:AA:FF:FF:FF", "AA:AA:AA:FF:FF", "AA/AA/AA/FF/FF/FF"]
)
def test_validate_mac_exception(self, data: str):
with pytest.raises(CoreError):
utils.validate_mac(data)
def test_random_mac(self):
value = utils.random_mac()
assert netaddr.EUI(value) is not None