2018-04-26 00:33:58 +01:00
|
|
|
import pytest
|
2019-09-11 05:33:35 +01:00
|
|
|
|
2020-06-16 20:50:24 +01:00
|
|
|
from core.emulator.data import InterfaceData, NodeOptions
|
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
|
2019-10-22 23:31:50 +01:00
|
|
|
options = NodeOptions(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
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_node_update(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
|
|
|
position_value = 100
|
|
|
|
update_options = NodeOptions()
|
|
|
|
update_options.set_position(x=position_value, y=position_value)
|
|
|
|
|
|
|
|
# when
|
2019-10-23 04:55:06 +01:00
|
|
|
session.edit_node(node.id, update_options)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
|
|
|
assert node.position.x == position_value
|
|
|
|
assert node.position.y == position_value
|
|
|
|
|
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
|
|
|
|
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-09 20:42:15 +01:00
|
|
|
switch = session.add_node(SwitchNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData()
|
|
|
|
iface = node.new_iface(switch, iface_data)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# when
|
2020-06-17 07:25:26 +01:00
|
|
|
node.set_mac(iface.node_id, 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-09 20:42:15 +01:00
|
|
|
switch = session.add_node(SwitchNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData()
|
|
|
|
iface = node.new_iface(switch, iface_data)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# when
|
|
|
|
with pytest.raises(CoreError):
|
2020-06-17 07:25:26 +01:00
|
|
|
node.set_mac(iface.node_id, 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-09 20:42:15 +01:00
|
|
|
switch = session.add_node(SwitchNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData()
|
|
|
|
iface = node.new_iface(switch, iface_data)
|
2020-01-09 22:23:08 +00:00
|
|
|
|
|
|
|
# when
|
2020-06-19 18:54:58 +01:00
|
|
|
node.add_ip(iface.node_id, 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-09 20:42:15 +01:00
|
|
|
switch = session.add_node(SwitchNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData()
|
|
|
|
iface = node.new_iface(switch, 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):
|
2020-06-19 18:54:58 +01:00
|
|
|
node.add_ip(iface.node_id, 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
|