2017-04-27 21:34:23 +01:00
|
|
|
"""
|
2017-08-01 17:28:11 +01:00
|
|
|
Unit tests for testing basic CORE networks.
|
2017-04-27 21:34:23 +01:00
|
|
|
"""
|
2017-04-27 22:49:51 +01:00
|
|
|
|
2017-08-03 00:39:38 +01:00
|
|
|
import os
|
|
|
|
import threading
|
2020-05-30 05:41:58 +01:00
|
|
|
from typing import Type
|
2017-07-27 22:17:33 +01:00
|
|
|
|
2018-02-27 18:48:01 +00:00
|
|
|
import pytest
|
2019-09-11 05:33:35 +01:00
|
|
|
|
2020-06-16 20:50:24 +01:00
|
|
|
from core.emulator.data import IpPrefixes, NodeOptions
|
2020-05-21 06:14:03 +01:00
|
|
|
from core.emulator.enumerations import MessageFlags
|
2020-05-25 07:37:38 +01:00
|
|
|
from core.emulator.session import Session
|
2019-10-11 07:01:16 +01:00
|
|
|
from core.errors import CoreCommandError
|
2019-09-10 22:20:51 +01:00
|
|
|
from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
|
2020-05-30 05:41:58 +01:00
|
|
|
from core.nodes.base import CoreNode, NodeBase
|
2020-05-21 06:14:03 +01:00
|
|
|
from core.nodes.network import HubNode, PtpNet, SwitchNode, WlanNode
|
2017-04-27 21:34:23 +01:00
|
|
|
|
2017-08-03 00:39:38 +01:00
|
|
|
_PATH = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
_MOBILITY_FILE = os.path.join(_PATH, "mobility.scen")
|
2020-05-21 06:14:03 +01:00
|
|
|
_WIRED = [PtpNet, HubNode, SwitchNode]
|
2017-04-27 21:34:23 +01:00
|
|
|
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2020-06-09 16:48:18 +01:00
|
|
|
def ping(from_node: CoreNode, to_node: CoreNode, ip_prefixes: IpPrefixes):
|
2020-06-10 02:40:50 +01:00
|
|
|
address = ip_prefixes.ip4_address(to_node.id)
|
2019-10-11 07:01:16 +01:00
|
|
|
try:
|
2019-10-24 07:15:19 +01:00
|
|
|
from_node.cmd(f"ping -c 1 {address}")
|
2019-10-11 07:01:16 +01:00
|
|
|
status = 0
|
|
|
|
except CoreCommandError as e:
|
|
|
|
status = e.returncode
|
|
|
|
return status
|
2018-04-26 00:33:58 +01:00
|
|
|
|
|
|
|
|
2017-07-27 22:59:40 +01:00
|
|
|
class TestCore:
|
2018-04-26 00:33:58 +01:00
|
|
|
@pytest.mark.parametrize("net_type", _WIRED)
|
2020-05-30 05:41:58 +01:00
|
|
|
def test_wired_ping(
|
|
|
|
self, session: Session, net_type: Type[NodeBase], ip_prefixes: IpPrefixes
|
|
|
|
):
|
2017-07-27 22:17:33 +01:00
|
|
|
"""
|
|
|
|
Test ptp node network.
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
:param session: session for test
|
|
|
|
:param core.enumerations.NodeTypes net_type: type of net node to create
|
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-07-27 22:17:33 +01:00
|
|
|
"""
|
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# create net node
|
2020-05-21 06:14:03 +01:00
|
|
|
net_node = session.add_node(net_type)
|
2017-07-27 22:17:33 +01:00
|
|
|
|
|
|
|
# create nodes
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2017-07-27 22:17:33 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes to net node
|
2020-06-13 00:52:41 +01:00
|
|
|
for node in [node1, node2]:
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = ip_prefixes.create_iface(node)
|
|
|
|
session.add_link(node.id, net_node.id, iface1_data=iface_data)
|
2017-07-27 22:17:33 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-07-27 22:17:33 +01:00
|
|
|
|
2020-06-13 00:52:41 +01:00
|
|
|
# ping node2 from node1 and assert success
|
|
|
|
status = ping(node1, node2, ip_prefixes)
|
2017-07-27 22:17:33 +01:00
|
|
|
assert not status
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_vnode_client(self, request, session: Session, ip_prefixes: IpPrefixes):
|
2017-07-27 19:57:01 +01:00
|
|
|
"""
|
|
|
|
Test vnode client methods.
|
|
|
|
|
2019-10-24 07:15:19 +01:00
|
|
|
:param request: pytest request
|
2018-04-26 00:33:58 +01:00
|
|
|
:param session: session for test
|
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-07-27 19:57:01 +01:00
|
|
|
"""
|
|
|
|
# create ptp
|
2020-05-21 06:14:03 +01:00
|
|
|
ptp_node = session.add_node(PtpNet)
|
2017-07-27 19:57:01 +01:00
|
|
|
|
|
|
|
# create nodes
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2017-07-27 19:57:01 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes to ptp net
|
2020-06-13 00:52:41 +01:00
|
|
|
for node in [node1, node2]:
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = ip_prefixes.create_iface(node)
|
|
|
|
session.add_link(node.id, ptp_node.id, iface1_data=iface_data)
|
2017-07-27 19:57:01 +01:00
|
|
|
|
|
|
|
# get node client for testing
|
2020-06-13 00:52:41 +01:00
|
|
|
client = node1.client
|
2017-07-27 19:57:01 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-07-27 19:57:01 +01:00
|
|
|
|
|
|
|
# check we are connected
|
|
|
|
assert client.connected()
|
|
|
|
|
2019-10-11 21:55:06 +01:00
|
|
|
# validate command
|
2019-10-24 07:15:19 +01:00
|
|
|
if not request.config.getoption("mock"):
|
|
|
|
assert client.check_cmd("echo hello") == "hello"
|
2017-07-27 19:57:01 +01:00
|
|
|
|
2020-06-16 17:30:16 +01:00
|
|
|
def test_iface(self, session: Session, ip_prefixes: IpPrefixes):
|
2017-07-27 00:52:17 +01:00
|
|
|
"""
|
2020-06-16 17:30:16 +01:00
|
|
|
Test interface methods.
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
:param session: session for test
|
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-07-27 00:52:17 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# create ptp
|
2020-05-21 06:14:03 +01:00
|
|
|
ptp_node = session.add_node(PtpNet)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# create nodes
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode)
|
|
|
|
node2 = session.add_node(CoreNode)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes to ptp net
|
2020-06-13 00:52:41 +01:00
|
|
|
for node in [node1, node2]:
|
2020-06-16 17:30:16 +01:00
|
|
|
iface = ip_prefixes.create_iface(node)
|
|
|
|
session.add_link(node.id, ptp_node.id, iface1_data=iface)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# check link data gets generated
|
2020-06-19 05:33:28 +01:00
|
|
|
assert ptp_node.links(MessageFlags.ADD)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# check common nets exist between linked nodes
|
2020-06-13 00:52:41 +01:00
|
|
|
assert node1.commonnets(node2)
|
|
|
|
assert node2.commonnets(node1)
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2020-06-16 17:30:16 +01:00
|
|
|
# check we can retrieve interface id
|
|
|
|
assert 0 in node1.ifaces
|
|
|
|
assert 0 in node2.ifaces
|
2017-07-27 00:52:17 +01:00
|
|
|
|
|
|
|
# check interface parameters
|
2020-06-16 17:30:16 +01:00
|
|
|
iface = node1.get_iface(0)
|
|
|
|
iface.setparam("test", 1)
|
|
|
|
assert iface.getparam("test") == 1
|
|
|
|
assert iface.getparams()
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2020-06-16 17:30:16 +01:00
|
|
|
# delete interface and test that if no longer exists
|
|
|
|
node1.delete_iface(0)
|
|
|
|
assert 0 not in node1.ifaces
|
2017-07-27 00:52:17 +01:00
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_wlan_ping(self, session: Session, ip_prefixes: IpPrefixes):
|
2017-04-27 21:34:23 +01:00
|
|
|
"""
|
|
|
|
Test basic wlan network.
|
|
|
|
|
2018-05-01 18:40:25 +01:00
|
|
|
:param core.emulator.coreemu.EmuSession session: session for test
|
2018-04-26 00:33:58 +01:00
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-04-27 21:34:23 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# create wlan
|
2020-05-21 06:14:03 +01:00
|
|
|
wlan_node = session.add_node(WlanNode)
|
2018-06-13 19:59:50 +01:00
|
|
|
session.mobility.set_model(wlan_node, BasicRangeModel)
|
2017-04-27 21:34:23 +01:00
|
|
|
|
|
|
|
# create nodes
|
2019-10-22 23:13:28 +01:00
|
|
|
options = NodeOptions(model="mdr")
|
|
|
|
options.set_position(0, 0)
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode, options=options)
|
|
|
|
node2 = session.add_node(CoreNode, options=options)
|
2017-04-27 21:34:23 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes
|
2020-06-13 00:52:41 +01:00
|
|
|
for node in [node1, node2]:
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_id = ip_prefixes.create_iface(node)
|
|
|
|
session.add_link(node.id, wlan_node.id, iface1_data=iface_id)
|
2017-04-27 21:34:23 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-04-27 21:34:23 +01:00
|
|
|
|
2020-06-13 00:52:41 +01:00
|
|
|
# ping node2 from node1 and assert success
|
|
|
|
status = ping(node1, node2, ip_prefixes)
|
2017-04-27 21:34:23 +01:00
|
|
|
assert not status
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_mobility(self, session: Session, ip_prefixes: IpPrefixes):
|
2017-08-03 00:39:38 +01:00
|
|
|
"""
|
|
|
|
Test basic wlan network.
|
|
|
|
|
2018-05-01 18:40:25 +01:00
|
|
|
:param core.emulator.coreemu.EmuSession session: session for test
|
2018-04-26 00:33:58 +01:00
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
2017-08-03 00:39:38 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# create wlan
|
2020-05-21 06:14:03 +01:00
|
|
|
wlan_node = session.add_node(WlanNode)
|
2018-06-13 19:59:50 +01:00
|
|
|
session.mobility.set_model(wlan_node, BasicRangeModel)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# create nodes
|
2019-10-22 23:13:28 +01:00
|
|
|
options = NodeOptions(model="mdr")
|
|
|
|
options.set_position(0, 0)
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = session.add_node(CoreNode, options=options)
|
|
|
|
node2 = session.add_node(CoreNode, options=options)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
# link nodes
|
2020-06-13 00:52:41 +01:00
|
|
|
for node in [node1, node2]:
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_id = ip_prefixes.create_iface(node)
|
|
|
|
session.add_link(node.id, wlan_node.id, iface1_data=iface_id)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# configure mobility script for session
|
2018-06-07 20:57:32 +01:00
|
|
|
config = {
|
|
|
|
"file": _MOBILITY_FILE,
|
|
|
|
"refresh_ms": "50",
|
|
|
|
"loop": "1",
|
|
|
|
"autostart": "0.0",
|
|
|
|
"map": "",
|
|
|
|
"script_start": "",
|
|
|
|
"script_pause": "",
|
|
|
|
"script_stop": "",
|
|
|
|
}
|
2018-06-13 19:59:50 +01:00
|
|
|
session.mobility.set_model(wlan_node, Ns2ScriptedMobility, config)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# add handler for receiving node updates
|
|
|
|
event = threading.Event()
|
|
|
|
|
|
|
|
def node_update(_):
|
|
|
|
event.set()
|
2017-08-03 17:48:26 +01:00
|
|
|
|
2018-04-26 00:33:58 +01:00
|
|
|
session.node_handlers.append(node_update)
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# instantiate session
|
2018-04-26 00:33:58 +01:00
|
|
|
session.instantiate()
|
2017-08-03 00:39:38 +01:00
|
|
|
|
|
|
|
# validate we receive a node message for updating its location
|
|
|
|
assert event.wait(5)
|