2018-04-26 00:33:58 +01:00
|
|
|
import pytest
|
2019-09-11 05:33:35 +01:00
|
|
|
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.emulator.data import InterfaceData
|
2020-05-25 07:37:38 +01:00
|
|
|
from core.emulator.session import Session
|
2019-09-28 07:29:15 +01:00
|
|
|
from core.errors import CoreError
|
2020-05-20 22:44:34 +01:00
|
|
|
from core.nodes.base import CoreNode
|
2020-05-21 06:14:03 +01:00
|
|
|
from core.nodes.network import HubNode, SwitchNode, WlanNode
|
2018-04-26 00:33:58 +01:00
|
|
|
|
2019-09-10 23:10:24 +01:00
|
|
|
MODELS = ["router", "host", "PC", "mdr"]
|
2020-05-21 06:14:03 +01:00
|
|
|
NET_TYPES = [SwitchNode, HubNode, WlanNode]
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestNodes:
|
|
|
|
@pytest.mark.parametrize("model", MODELS)
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_node_add(self, session: Session, model: str):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2022-05-25 18:51:42 +01:00
|
|
|
options = CoreNode.create_options()
|
|
|
|
options.model = model
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode, options=options)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
|
|
|
assert node
|
|
|
|
assert node.alive()
|
|
|
|
assert node.up
|
|
|
|
|
2021-08-28 00:58:44 +01:00
|
|
|
def test_node_set_pos(self, session: Session):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2021-08-28 00:58:44 +01:00
|
|
|
x, y = 100.0, 50.0
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2021-08-28 00:58:44 +01:00
|
|
|
session.set_node_pos(node, x, y)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
2021-08-28 00:58:44 +01:00
|
|
|
assert node.position.x == x
|
|
|
|
assert node.position.y == y
|
|
|
|
|
|
|
|
def test_node_set_geo(self, session: Session):
|
|
|
|
# given
|
|
|
|
node = session.add_node(CoreNode)
|
|
|
|
lon, lat, alt = 0.0, 0.0, 0.0
|
|
|
|
|
|
|
|
# when
|
|
|
|
session.set_node_geo(node, lon, lat, alt)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert node.position.lon == lon
|
|
|
|
assert node.position.lat == lat
|
|
|
|
assert node.position.alt == alt
|
2018-04-26 00:33:58 +01:00
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_node_delete(self, session: Session):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2019-04-27 06:07:51 +01:00
|
|
|
session.delete_node(node.id)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
2019-09-11 21:12:42 +01:00
|
|
|
with pytest.raises(CoreError):
|
2020-05-20 22:44:34 +01:00
|
|
|
session.get_node(node.id, CoreNode)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
2022-03-17 22:28:38 +00:00
|
|
|
def test_node_add_iface(self, session: Session):
|
|
|
|
# given
|
|
|
|
node = session.add_node(CoreNode)
|
|
|
|
|
|
|
|
# when
|
|
|
|
iface = node.create_iface()
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert iface.id in node.ifaces
|
|
|
|
|
|
|
|
def test_node_get_iface(self, session: Session):
|
|
|
|
# given
|
|
|
|
node = session.add_node(CoreNode)
|
|
|
|
iface = node.create_iface()
|
|
|
|
assert iface.id in node.ifaces
|
|
|
|
|
|
|
|
# when
|
|
|
|
iface2 = node.get_iface(iface.id)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert iface == iface2
|
|
|
|
|
|
|
|
def test_node_delete_iface(self, session: Session):
|
|
|
|
# given
|
|
|
|
node = session.add_node(CoreNode)
|
|
|
|
iface = node.create_iface()
|
|
|
|
assert iface.id in node.ifaces
|
|
|
|
|
|
|
|
# when
|
|
|
|
node.delete_iface(iface.id)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert iface.id not in node.ifaces
|
|
|
|
|
2020-06-19 23:41:41 +01:00
|
|
|
@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):
|
2020-01-09 22:23:08 +00:00
|
|
|
# given
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData()
|
2022-03-17 22:28:38 +00:00
|
|
|
iface = node.create_iface(iface_data)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# when
|
2022-03-17 22:28:38 +00:00
|
|
|
iface.set_mac(mac)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# then
|
2020-06-19 23:41:41 +01:00
|
|
|
assert str(iface.mac) == expected
|
2020-01-09 22:23:08 +00:00
|
|
|
|
2020-06-19 23:41:41 +01:00
|
|
|
@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):
|
2020-01-09 22:23:08 +00:00
|
|
|
# given
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData()
|
2022-03-17 22:28:38 +00:00
|
|
|
iface = node.create_iface(iface_data)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# when
|
|
|
|
with pytest.raises(CoreError):
|
2022-03-17 22:28:38 +00:00
|
|
|
iface.set_mac(mac)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
2020-06-19 23:32:17 +01:00
|
|
|
@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):
|
2020-01-09 22:23:08 +00:00
|
|
|
# given
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData()
|
2022-03-17 22:28:38 +00:00
|
|
|
iface = node.create_iface(iface_data)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# when
|
2022-03-17 22:28:38 +00:00
|
|
|
iface.add_ip(ip)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# then
|
2020-06-19 23:32:17 +01:00
|
|
|
if is_ip6:
|
|
|
|
assert str(iface.get_ip6()) == expected
|
|
|
|
else:
|
|
|
|
assert str(iface.get_ip4()) == expected
|
2020-01-09 22:23:08 +00:00
|
|
|
|
2020-06-19 18:54:58 +01:00
|
|
|
def test_node_add_ip_exception(self, session):
|
2020-01-09 22:23:08 +00:00
|
|
|
# given
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData()
|
2022-03-17 22:28:38 +00:00
|
|
|
iface = node.create_iface(iface_data)
|
2020-06-19 18:54:58 +01:00
|
|
|
ip = "256.168.0.1/24"
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# when
|
|
|
|
with pytest.raises(CoreError):
|
2022-03-17 22:28:38 +00:00
|
|
|
iface.add_ip(ip)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
@pytest.mark.parametrize("net_type", NET_TYPES)
|
|
|
|
def test_net(self, session, net_type):
|
|
|
|
# given
|
|
|
|
|
|
|
|
# when
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(net_type)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
|
|
|
assert node
|
|
|
|
assert node.up
|