2019-09-11 21:12:42 +01:00
|
|
|
"""
|
|
|
|
Unit tests for testing CORE EMANE networks.
|
|
|
|
"""
|
|
|
|
import os
|
2020-05-30 05:41:58 +01:00
|
|
|
from tempfile import TemporaryFile
|
2019-09-11 21:12:42 +01:00
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from core.emane.bypass import EmaneBypassModel
|
|
|
|
from core.emane.commeffect import EmaneCommEffectModel
|
2020-05-30 05:41:58 +01:00
|
|
|
from core.emane.emanemodel import EmaneModel
|
2019-09-11 21:12:42 +01:00
|
|
|
from core.emane.ieee80211abg import EmaneIeee80211abgModel
|
2020-05-20 22:44:34 +01:00
|
|
|
from core.emane.nodes import EmaneNet
|
2019-09-11 21:12:42 +01:00
|
|
|
from core.emane.rfpipe import EmaneRfPipeModel
|
|
|
|
from core.emane.tdma import EmaneTdmaModel
|
2020-05-30 05:41:58 +01:00
|
|
|
from core.emulator.emudata import IpPrefixes, NodeOptions
|
|
|
|
from core.emulator.session import Session
|
2019-10-11 07:01:16 +01:00
|
|
|
from core.errors import CoreCommandError, CoreError
|
2020-05-20 22:44:34 +01:00
|
|
|
from core.nodes.base import CoreNode
|
2019-09-11 21:12:42 +01:00
|
|
|
|
|
|
|
_EMANE_MODELS = [
|
|
|
|
EmaneIeee80211abgModel,
|
|
|
|
EmaneRfPipeModel,
|
|
|
|
EmaneBypassModel,
|
|
|
|
EmaneCommEffectModel,
|
|
|
|
EmaneTdmaModel,
|
|
|
|
]
|
|
|
|
_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
2020-06-09 16:48:18 +01:00
|
|
|
def ping(
|
|
|
|
from_node: CoreNode, to_node: CoreNode, ip_prefixes: IpPrefixes, count: int = 3
|
|
|
|
):
|
2019-09-11 21:16:09 +01:00
|
|
|
address = ip_prefixes.ip4_address(to_node)
|
2019-10-11 07:01:16 +01:00
|
|
|
try:
|
2019-10-19 07:28:09 +01:00
|
|
|
from_node.cmd(f"ping -c {count} {address}")
|
2019-10-11 07:01:16 +01:00
|
|
|
status = 0
|
|
|
|
except CoreCommandError as e:
|
|
|
|
status = e.returncode
|
|
|
|
return status
|
2019-09-11 21:16:09 +01:00
|
|
|
|
|
|
|
|
2019-09-11 21:12:42 +01:00
|
|
|
class TestEmane:
|
|
|
|
@pytest.mark.parametrize("model", _EMANE_MODELS)
|
2020-05-30 05:41:58 +01:00
|
|
|
def test_models(self, session: Session, model: EmaneModel, ip_prefixes: IpPrefixes):
|
2019-09-11 21:12:42 +01:00
|
|
|
"""
|
|
|
|
Test emane models within a basic network.
|
|
|
|
|
|
|
|
:param core.emulator.coreemu.EmuSession session: session for test
|
|
|
|
:param model: emane model to test
|
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
|
|
|
"""
|
|
|
|
|
|
|
|
# create emane node for networking the core nodes
|
2019-10-23 05:27:31 +01:00
|
|
|
session.set_location(47.57917, -122.13232, 2.00000, 1.0)
|
|
|
|
options = NodeOptions()
|
|
|
|
options.set_position(80, 50)
|
2020-05-21 06:14:03 +01:00
|
|
|
emane_network = session.add_node(EmaneNet, options=options)
|
2019-10-23 05:27:31 +01:00
|
|
|
session.emane.set_model(emane_network, model)
|
2019-09-11 21:12:42 +01:00
|
|
|
|
|
|
|
# configure tdma
|
|
|
|
if model == EmaneTdmaModel:
|
|
|
|
session.emane.set_model_config(
|
|
|
|
emane_network.id,
|
|
|
|
EmaneTdmaModel.name,
|
2019-09-13 00:13:24 +01:00
|
|
|
{"schedule": os.path.join(_DIR, "../../examples/tdma/schedule.xml")},
|
2019-09-11 21:12:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# create nodes
|
2019-10-22 23:13:28 +01:00
|
|
|
options = NodeOptions(model="mdr")
|
|
|
|
options.set_position(150, 150)
|
2020-05-21 06:14:03 +01:00
|
|
|
node_one = session.add_node(CoreNode, options=options)
|
2019-10-22 23:13:28 +01:00
|
|
|
options.set_position(300, 150)
|
2020-05-21 06:14:03 +01:00
|
|
|
node_two = session.add_node(CoreNode, options=options)
|
2019-09-11 21:12:42 +01:00
|
|
|
|
|
|
|
for i, node in enumerate([node_one, node_two]):
|
|
|
|
node.setposition(x=150 * (i + 1), y=150)
|
|
|
|
interface = ip_prefixes.create_interface(node)
|
|
|
|
session.add_link(node.id, emane_network.id, interface_one=interface)
|
|
|
|
|
|
|
|
# instantiate session
|
|
|
|
session.instantiate()
|
|
|
|
|
|
|
|
# ping n2 from n1 and assert success
|
|
|
|
status = ping(node_one, node_two, ip_prefixes, count=5)
|
|
|
|
assert not status
|
|
|
|
|
2020-05-30 05:41:58 +01:00
|
|
|
def test_xml_emane(
|
|
|
|
self, session: Session, tmpdir: TemporaryFile, ip_prefixes: IpPrefixes
|
|
|
|
):
|
2019-09-11 21:12:42 +01:00
|
|
|
"""
|
|
|
|
Test xml client methods for emane.
|
|
|
|
|
|
|
|
:param session: session for test
|
|
|
|
:param tmpdir: tmpdir to create data in
|
|
|
|
:param ip_prefixes: generates ip addresses for nodes
|
|
|
|
"""
|
|
|
|
# create emane node for networking the core nodes
|
2019-10-23 05:27:31 +01:00
|
|
|
session.set_location(47.57917, -122.13232, 2.00000, 1.0)
|
|
|
|
options = NodeOptions()
|
|
|
|
options.set_position(80, 50)
|
2020-05-21 06:14:03 +01:00
|
|
|
emane_network = session.add_node(EmaneNet, options=options)
|
2019-10-25 23:32:12 +01:00
|
|
|
config_key = "txpower"
|
|
|
|
config_value = "10"
|
|
|
|
session.emane.set_model(
|
|
|
|
emane_network, EmaneIeee80211abgModel, {config_key: config_value}
|
|
|
|
)
|
2019-09-11 21:12:42 +01:00
|
|
|
|
|
|
|
# create nodes
|
2019-10-22 23:13:28 +01:00
|
|
|
options = NodeOptions(model="mdr")
|
|
|
|
options.set_position(150, 150)
|
2020-05-21 06:14:03 +01:00
|
|
|
node_one = session.add_node(CoreNode, options=options)
|
2019-10-22 23:13:28 +01:00
|
|
|
options.set_position(300, 150)
|
2020-05-21 06:14:03 +01:00
|
|
|
node_two = session.add_node(CoreNode, options=options)
|
2019-09-11 21:12:42 +01:00
|
|
|
|
|
|
|
for i, node in enumerate([node_one, node_two]):
|
|
|
|
node.setposition(x=150 * (i + 1), y=150)
|
|
|
|
interface = ip_prefixes.create_interface(node)
|
|
|
|
session.add_link(node.id, emane_network.id, interface_one=interface)
|
|
|
|
|
|
|
|
# instantiate session
|
|
|
|
session.instantiate()
|
|
|
|
|
|
|
|
# get ids for nodes
|
|
|
|
emane_id = emane_network.id
|
|
|
|
n1_id = node_one.id
|
|
|
|
n2_id = node_two.id
|
|
|
|
|
|
|
|
# save xml
|
|
|
|
xml_file = tmpdir.join("session.xml")
|
|
|
|
file_path = xml_file.strpath
|
|
|
|
session.save_xml(file_path)
|
|
|
|
|
|
|
|
# verify xml file was created and can be parsed
|
|
|
|
assert xml_file.isfile()
|
|
|
|
assert ElementTree.parse(file_path)
|
|
|
|
|
|
|
|
# stop current session, clearing data
|
|
|
|
session.shutdown()
|
|
|
|
|
|
|
|
# verify nodes have been removed from session
|
|
|
|
with pytest.raises(CoreError):
|
2020-05-20 22:44:34 +01:00
|
|
|
assert not session.get_node(n1_id, CoreNode)
|
2019-09-11 21:12:42 +01:00
|
|
|
with pytest.raises(CoreError):
|
2020-05-20 22:44:34 +01:00
|
|
|
assert not session.get_node(n2_id, CoreNode)
|
2019-09-11 21:12:42 +01:00
|
|
|
|
|
|
|
# load saved xml
|
|
|
|
session.open_xml(file_path, start=True)
|
|
|
|
|
|
|
|
# retrieve configuration we set originally
|
|
|
|
value = str(
|
2019-10-25 23:32:12 +01:00
|
|
|
session.emane.get_config(config_key, emane_id, EmaneIeee80211abgModel.name)
|
2019-09-11 21:12:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# verify nodes and configuration were restored
|
2020-05-20 22:44:34 +01:00
|
|
|
assert session.get_node(n1_id, CoreNode)
|
|
|
|
assert session.get_node(n2_id, CoreNode)
|
|
|
|
assert session.get_node(emane_id, EmaneNet)
|
2019-10-25 23:32:12 +01:00
|
|
|
assert value == config_value
|