added unit tests to help check new utils functions and usages within nodes for adding addresses and macs
This commit is contained in:
parent
834ceea55d
commit
7028d3e78f
3 changed files with 93 additions and 1 deletions
|
@ -800,7 +800,7 @@ class CoreNode(CoreNodeBase):
|
||||||
|
|
||||||
with self.lock:
|
with self.lock:
|
||||||
# TODO: emane specific code
|
# TODO: emane specific code
|
||||||
if net.is_emane is True:
|
if net is not None and net.is_emane is True:
|
||||||
ifindex = self.newtuntap(ifindex, ifname)
|
ifindex = self.newtuntap(ifindex, ifname)
|
||||||
# TUN/TAP is not ready for addressing yet; the device may
|
# TUN/TAP is not ready for addressing yet; the device may
|
||||||
# take some time to appear, and installing it into a
|
# take some time to appear, and installing it into a
|
||||||
|
|
|
@ -47,6 +47,54 @@ class TestNodes:
|
||||||
with pytest.raises(CoreError):
|
with pytest.raises(CoreError):
|
||||||
session.get_node(node.id)
|
session.get_node(node.id)
|
||||||
|
|
||||||
|
def test_node_sethwaddr(self, session):
|
||||||
|
# given
|
||||||
|
node = session.add_node()
|
||||||
|
index = node.newnetif()
|
||||||
|
interface = node.netif(index)
|
||||||
|
mac = "aa:aa:aa:ff:ff:ff"
|
||||||
|
|
||||||
|
# when
|
||||||
|
node.sethwaddr(index, mac)
|
||||||
|
|
||||||
|
# then
|
||||||
|
assert interface.hwaddr == mac
|
||||||
|
|
||||||
|
def test_node_sethwaddr_exception(self, session):
|
||||||
|
# given
|
||||||
|
node = session.add_node()
|
||||||
|
index = node.newnetif()
|
||||||
|
node.netif(index)
|
||||||
|
mac = "aa:aa:aa:ff:ff:fff"
|
||||||
|
|
||||||
|
# when
|
||||||
|
with pytest.raises(CoreError):
|
||||||
|
node.sethwaddr(index, mac)
|
||||||
|
|
||||||
|
def test_node_addaddr(self, session):
|
||||||
|
# given
|
||||||
|
node = session.add_node()
|
||||||
|
index = node.newnetif()
|
||||||
|
interface = node.netif(index)
|
||||||
|
addr = "192.168.0.1/24"
|
||||||
|
|
||||||
|
# when
|
||||||
|
node.addaddr(index, addr)
|
||||||
|
|
||||||
|
# then
|
||||||
|
assert interface.addrlist[0] == addr
|
||||||
|
|
||||||
|
def test_node_addaddr_exception(self, session):
|
||||||
|
# given
|
||||||
|
node = session.add_node()
|
||||||
|
index = node.newnetif()
|
||||||
|
node.netif(index)
|
||||||
|
addr = "256.168.0.1/24"
|
||||||
|
|
||||||
|
# when
|
||||||
|
with pytest.raises(CoreError):
|
||||||
|
node.addaddr(index, addr)
|
||||||
|
|
||||||
@pytest.mark.parametrize("net_type", NET_TYPES)
|
@pytest.mark.parametrize("net_type", NET_TYPES)
|
||||||
def test_net(self, session, net_type):
|
def test_net(self, session, net_type):
|
||||||
# given
|
# given
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
|
import netaddr
|
||||||
|
import pytest
|
||||||
|
|
||||||
from core import utils
|
from core import utils
|
||||||
|
from core.errors import CoreError
|
||||||
|
|
||||||
|
|
||||||
class TestUtils:
|
class TestUtils:
|
||||||
|
@ -20,3 +24,43 @@ class TestUtils:
|
||||||
assert len(one_arg) == 1
|
assert len(one_arg) == 1
|
||||||
assert len(two_args) == 2
|
assert len(two_args) == 2
|
||||||
assert len(unicode_args) == 3
|
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, expected):
|
||||||
|
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):
|
||||||
|
with pytest.raises(CoreError):
|
||||||
|
utils.validate_ip("")
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"data,expected",
|
||||||
|
[
|
||||||
|
("AA-AA-AA-FF-FF-FF", "aa:aa:aa:ff:ff:ff"),
|
||||||
|
("AA:AA:AA:FF:FF:FF", "aa:aa:aa:ff:ff:ff"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_validate_mac(self, data, expected):
|
||||||
|
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):
|
||||||
|
with pytest.raises(CoreError):
|
||||||
|
utils.validate_mac(data)
|
||||||
|
|
||||||
|
def test_random_mac(self):
|
||||||
|
value = utils.random_mac()
|
||||||
|
assert netaddr.EUI(value) is not None
|
||||||
|
|
Loading…
Add table
Reference in a new issue