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 threading
|
2021-03-19 23:54:24 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import List, 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
|
|
|
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.emulator.data import IpPrefixes
|
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
|
|
|
|
2021-03-19 23:54:24 +00:00
|
|
|
_PATH: Path = Path(__file__).resolve().parent
|
|
|
|
_MOBILITY_FILE: Path = _PATH / "mobility.scen"
|
|
|
|
_WIRED: List = [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_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
|
2022-05-25 18:51:42 +01:00
|
|
|
options = CoreNode.create_options()
|
|
|
|
options.model = "mdr"
|
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
|
2022-05-25 18:51:42 +01:00
|
|
|
options = CoreNode.create_options()
|
|
|
|
options.model = "mdr"
|
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 = {
|
2021-03-19 23:54:24 +00:00
|
|
|
"file": str(_MOBILITY_FILE),
|
2018-06-07 20:57:32 +01:00
|
|
|
"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)
|