2020-05-25 07:37:38 +01:00
|
|
|
from typing import Tuple
|
|
|
|
|
2020-06-16 20:50:24 +01:00
|
|
|
from core.emulator.data import IpPrefixes, LinkOptions
|
2020-05-25 07:37:38 +01:00
|
|
|
from core.emulator.session import Session
|
2020-05-21 06:14:03 +01:00
|
|
|
from core.nodes.base import CoreNode
|
|
|
|
from core.nodes.network import SwitchNode
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def create_ptp_network(
|
|
|
|
session: Session, ip_prefixes: IpPrefixes
|
|
|
|
) -> Tuple[CoreNode, CoreNode]:
|
2018-04-26 00:33:58 +01:00
|
|
|
# create nodes
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# link nodes to net node
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_data = ip_prefixes.create_iface(node1)
|
|
|
|
iface2_data = ip_prefixes.create_iface(node2)
|
|
|
|
session.add_link(node1.id, node2.id, iface1_data, iface2_data)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# instantiate session
|
|
|
|
session.instantiate()
|
|
|
|
|
2020-06-13 00:52:41 +01:00
|
|
|
return node1, node2
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestLinks:
|
2020-06-12 03:01:38 +01:00
|
|
|
def test_add_ptp(self, session: Session, ip_prefixes: IpPrefixes):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_data = ip_prefixes.create_iface(node1)
|
|
|
|
iface2_data = ip_prefixes.create_iface(node2)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-16 17:30:16 +01:00
|
|
|
session.add_link(node1.id, node2.id, iface1_data, iface2_data)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-16 17:30:16 +01:00
|
|
|
assert node1.get_iface(iface1_data.id)
|
|
|
|
assert node2.get_iface(iface2_data.id)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
2020-06-12 03:01:38 +01:00
|
|
|
def test_add_node_to_net(self, session: Session, ip_prefixes: IpPrefixes):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(SwitchNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_data = ip_prefixes.create_iface(node1)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-16 17:30:16 +01:00
|
|
|
session.add_link(node1.id, node2.id, iface1_data=iface1_data)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-19 05:33:28 +01:00
|
|
|
assert node2.links()
|
2020-06-16 17:30:16 +01:00
|
|
|
assert node1.get_iface(iface1_data.id)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
2020-06-12 03:01:38 +01:00
|
|
|
def test_add_net_to_node(self, session: Session, ip_prefixes: IpPrefixes):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(SwitchNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface2_data = ip_prefixes.create_iface(node2)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-16 17:30:16 +01:00
|
|
|
session.add_link(node1.id, node2.id, iface2_data=iface2_data)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-19 05:33:28 +01:00
|
|
|
assert node1.links()
|
2020-06-16 17:30:16 +01:00
|
|
|
assert node2.get_iface(iface2_data.id)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
2020-06-12 03:08:50 +01:00
|
|
|
def test_add_net_to_net(self, session):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(SwitchNode)
|
|
|
|
node2 = session.add_node(SwitchNode)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-13 00:52:41 +01:00
|
|
|
session.add_link(node1.id, node2.id)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-19 05:33:28 +01:00
|
|
|
assert node1.links()
|
2018-04-26 00:33:58 +01:00
|
|
|
|
2020-06-12 03:01:38 +01:00
|
|
|
def test_update_node_to_net(self, session: Session, ip_prefixes: IpPrefixes):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2019-09-11 05:33:35 +01:00
|
|
|
delay = 50
|
|
|
|
bandwidth = 5000000
|
2020-06-12 20:49:53 +01:00
|
|
|
loss = 25
|
2019-09-11 05:33:35 +01:00
|
|
|
dup = 25
|
|
|
|
jitter = 10
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(SwitchNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_data = ip_prefixes.create_iface(node1)
|
|
|
|
session.add_link(node1.id, node2.id, iface1_data)
|
|
|
|
iface1 = node1.get_iface(iface1_data.id)
|
|
|
|
assert iface1.getparam("delay") != delay
|
|
|
|
assert iface1.getparam("bw") != bandwidth
|
|
|
|
assert iface1.getparam("loss") != loss
|
|
|
|
assert iface1.getparam("duplicate") != dup
|
|
|
|
assert iface1.getparam("jitter") != jitter
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-13 00:52:41 +01:00
|
|
|
options = LinkOptions(
|
2020-06-12 20:49:53 +01:00
|
|
|
delay=delay, bandwidth=bandwidth, loss=loss, dup=dup, jitter=jitter
|
2020-06-12 03:01:38 +01:00
|
|
|
)
|
2019-09-10 23:10:24 +01:00
|
|
|
session.update_link(
|
2020-06-16 17:30:16 +01:00
|
|
|
node1.id, node2.id, iface1_id=iface1_data.id, options=options
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-16 17:30:16 +01:00
|
|
|
assert iface1.getparam("delay") == delay
|
|
|
|
assert iface1.getparam("bw") == bandwidth
|
|
|
|
assert iface1.getparam("loss") == loss
|
|
|
|
assert iface1.getparam("duplicate") == dup
|
|
|
|
assert iface1.getparam("jitter") == jitter
|
2018-04-26 00:33:58 +01:00
|
|
|
|
2020-06-12 03:01:38 +01:00
|
|
|
def test_update_net_to_node(self, session: Session, ip_prefixes: IpPrefixes):
|
|
|
|
# given
|
|
|
|
delay = 50
|
|
|
|
bandwidth = 5000000
|
2020-06-12 20:49:53 +01:00
|
|
|
loss = 25
|
2020-06-12 03:01:38 +01:00
|
|
|
dup = 25
|
|
|
|
jitter = 10
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(SwitchNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface2_data = ip_prefixes.create_iface(node2)
|
|
|
|
session.add_link(node1.id, node2.id, iface2_data=iface2_data)
|
|
|
|
iface2 = node2.get_iface(iface2_data.id)
|
|
|
|
assert iface2.getparam("delay") != delay
|
|
|
|
assert iface2.getparam("bw") != bandwidth
|
|
|
|
assert iface2.getparam("loss") != loss
|
|
|
|
assert iface2.getparam("duplicate") != dup
|
|
|
|
assert iface2.getparam("jitter") != jitter
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-13 00:52:41 +01:00
|
|
|
options = LinkOptions(
|
2020-06-12 20:49:53 +01:00
|
|
|
delay=delay, bandwidth=bandwidth, loss=loss, dup=dup, jitter=jitter
|
2020-06-12 03:01:38 +01:00
|
|
|
)
|
|
|
|
session.update_link(
|
2020-06-16 17:30:16 +01:00
|
|
|
node1.id, node2.id, iface2_id=iface2_data.id, options=options
|
2020-06-12 03:01:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# then
|
2020-06-16 17:30:16 +01:00
|
|
|
assert iface2.getparam("delay") == delay
|
|
|
|
assert iface2.getparam("bw") == bandwidth
|
|
|
|
assert iface2.getparam("loss") == loss
|
|
|
|
assert iface2.getparam("duplicate") == dup
|
|
|
|
assert iface2.getparam("jitter") == jitter
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
def test_update_ptp(self, session: Session, ip_prefixes: IpPrefixes):
|
|
|
|
# given
|
|
|
|
delay = 50
|
|
|
|
bandwidth = 5000000
|
2020-06-12 20:49:53 +01:00
|
|
|
loss = 25
|
2020-06-12 03:01:38 +01:00
|
|
|
dup = 25
|
|
|
|
jitter = 10
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_data = ip_prefixes.create_iface(node1)
|
|
|
|
iface2_data = ip_prefixes.create_iface(node2)
|
|
|
|
session.add_link(node1.id, node2.id, iface1_data, iface2_data)
|
|
|
|
iface1 = node1.get_iface(iface1_data.id)
|
|
|
|
iface2 = node2.get_iface(iface2_data.id)
|
|
|
|
assert iface1.getparam("delay") != delay
|
|
|
|
assert iface1.getparam("bw") != bandwidth
|
|
|
|
assert iface1.getparam("loss") != loss
|
|
|
|
assert iface1.getparam("duplicate") != dup
|
|
|
|
assert iface1.getparam("jitter") != jitter
|
|
|
|
assert iface2.getparam("delay") != delay
|
|
|
|
assert iface2.getparam("bw") != bandwidth
|
|
|
|
assert iface2.getparam("loss") != loss
|
|
|
|
assert iface2.getparam("duplicate") != dup
|
|
|
|
assert iface2.getparam("jitter") != jitter
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-13 00:52:41 +01:00
|
|
|
options = LinkOptions(
|
2020-06-12 20:49:53 +01:00
|
|
|
delay=delay, bandwidth=bandwidth, loss=loss, dup=dup, jitter=jitter
|
2020-06-12 03:01:38 +01:00
|
|
|
)
|
2020-06-16 17:30:16 +01:00
|
|
|
session.update_link(node1.id, node2.id, iface1_data.id, iface2_data.id, options)
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-16 17:30:16 +01:00
|
|
|
assert iface1.getparam("delay") == delay
|
|
|
|
assert iface1.getparam("bw") == bandwidth
|
|
|
|
assert iface1.getparam("loss") == loss
|
|
|
|
assert iface1.getparam("duplicate") == dup
|
|
|
|
assert iface1.getparam("jitter") == jitter
|
|
|
|
assert iface2.getparam("delay") == delay
|
|
|
|
assert iface2.getparam("bw") == bandwidth
|
|
|
|
assert iface2.getparam("loss") == loss
|
|
|
|
assert iface2.getparam("duplicate") == dup
|
|
|
|
assert iface2.getparam("jitter") == jitter
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
def test_delete_ptp(self, session: Session, ip_prefixes: IpPrefixes):
|
2018-04-26 00:33:58 +01:00
|
|
|
# given
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_data = ip_prefixes.create_iface(node1)
|
|
|
|
iface2_data = ip_prefixes.create_iface(node2)
|
|
|
|
session.add_link(node1.id, node2.id, iface1_data, iface2_data)
|
|
|
|
assert node1.get_iface(iface1_data.id)
|
|
|
|
assert node2.get_iface(iface2_data.id)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-16 17:30:16 +01:00
|
|
|
session.delete_link(node1.id, node2.id, iface1_data.id, iface2_data.id)
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-16 17:30:16 +01:00
|
|
|
assert iface1_data.id not in node1.ifaces
|
|
|
|
assert iface2_data.id not in node2.ifaces
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
def test_delete_node_to_net(self, session: Session, ip_prefixes: IpPrefixes):
|
|
|
|
# given
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(SwitchNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_data = ip_prefixes.create_iface(node1)
|
|
|
|
session.add_link(node1.id, node2.id, iface1_data)
|
|
|
|
assert node1.get_iface(iface1_data.id)
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-16 17:30:16 +01:00
|
|
|
session.delete_link(node1.id, node2.id, iface1_id=iface1_data.id)
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-16 17:30:16 +01:00
|
|
|
assert iface1_data.id not in node1.ifaces
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
def test_delete_net_to_node(self, session: Session, ip_prefixes: IpPrefixes):
|
|
|
|
# given
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(SwitchNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface2_data = ip_prefixes.create_iface(node2)
|
|
|
|
session.add_link(node1.id, node2.id, iface2_data=iface2_data)
|
|
|
|
assert node2.get_iface(iface2_data.id)
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
# when
|
2020-06-16 17:30:16 +01:00
|
|
|
session.delete_link(node1.id, node2.id, iface2_id=iface2_data.id)
|
2020-06-12 03:01:38 +01:00
|
|
|
|
|
|
|
# then
|
2020-06-16 17:30:16 +01:00
|
|
|
assert iface2_data.id not in node2.ifaces
|