added mock for python testing, removed a couple unused methods, added some more quick tests for a variety of interface related methods

This commit is contained in:
Blake J. Harnden 2017-07-26 16:52:17 -07:00
parent 62e8ff8731
commit ebe3b9e3e3
7 changed files with 80 additions and 54 deletions

View file

@ -437,6 +437,7 @@ class CoreBroker(ConfigurableManager):
""" """
try: try:
net = self.session.get_object(node_id) net = self.session.get_object(node_id)
logger.info("adding net tunnel for: id(%s) %s", node_id, net)
except KeyError: except KeyError:
raise KeyError("network node %s not found" % node_id) raise KeyError("network node %s not found" % node_id)
@ -583,7 +584,9 @@ class CoreBroker(ConfigurableManager):
:param int nodenum: node id to add :param int nodenum: node id to add
:return: nothing :return: nothing
""" """
logger.info("adding net to broker: %s", nodenum)
self.network_nodes.add(nodenum) self.network_nodes.add(nodenum)
logger.info("broker network nodes: %s", self.network_nodes)
def addphys(self, nodenum): def addphys(self, nodenum):
""" """
@ -1010,10 +1013,10 @@ class CoreBroker(ConfigurableManager):
for server in servers: for server in servers:
if server.name == "localhost": if server.name == "localhost":
continue continue
try:
lhost, lport = server.sock.getsockname()
except IOError:
lhost, lport = None, None lhost, lport = None, None
if server.sock:
lhost, lport = server.sock.getsockname()
f.write("%s %s %s %s %s\n" % (server.name, server.host, server.port, lhost, lport)) f.write("%s %s %s %s %s\n" % (server.name, server.host, server.port, lhost, lport))
except IOError: except IOError:
logger.exception("error writing server list to the file: %s" % filename) logger.exception("error writing server list to the file: %s" % filename)

View file

@ -273,16 +273,6 @@ class PyCoreNode(PyCoreObj):
self.nodedir = None self.nodedir = None
self.tmpnodedir = False self.tmpnodedir = False
# TODO: getter method that should not be needed
def nodeid(self):
"""
Retrieve node id.
:return: node id
:rtype: int
"""
return self.objid
def addservice(self, service): def addservice(self, service):
""" """
Add a services to the service list. Add a services to the service list.
@ -465,35 +455,6 @@ class PyCoreNet(PyCoreObj):
with self._linked_lock: with self._linked_lock:
del self._linked[netif] del self._linked[netif]
# TODO: needs to be abstracted out, seems like it may be ok to remove
def netifparamstolink(self, netif):
"""
Helper for tolinkmsgs() to build TLVs having link parameters from interface parameters.
:param PyCoreNetIf netif: network interface to retrieve params from
:return: tlv data
"""
delay = netif.getparam("delay")
bw = netif.getparam("bw")
loss = netif.getparam("loss")
duplicate = netif.getparam("duplicate")
jitter = netif.getparam("jitter")
tlvdata = ""
if delay is not None:
tlvdata += coreapi.CoreLinkTlv.pack(LinkTlvs.DELAY.value, delay)
if bw is not None:
tlvdata += coreapi.CoreLinkTlv.pack(LinkTlvs.BANDWIDTH.value, bw)
if loss is not None:
tlvdata += coreapi.CoreLinkTlv.pack(LinkTlvs.PER.value, str(loss))
if duplicate is not None:
tlvdata += coreapi.CoreLinkTlv.pack(LinkTlvs.DUP.value, str(duplicate))
if jitter is not None:
tlvdata += coreapi.CoreLinkTlv.pack(LinkTlvs.JITTER.value, jitter)
return tlvdata
def all_link_data(self, flags): def all_link_data(self, flags):
""" """
Build link data objects for this network. Each link object describes a link Build link data objects for this network. Each link object describes a link

View file

@ -168,7 +168,7 @@ class CoreDocumentWriter0(Document):
n = self.createElement("Node") n = self.createElement("Node")
self.np.appendChild(n) self.np.appendChild(n)
n.setAttribute("name", node.name) n.setAttribute("name", node.name)
n.setAttribute("id", "%s" % node.nodeid()) n.setAttribute("id", "%s" % node.objid)
if node.type: if node.type:
n.setAttribute("type", node.type) n.setAttribute("type", node.type)
self.addinterfaces(n, node) self.addinterfaces(n, node)

View file

@ -1,9 +1,10 @@
enum34==1.1.6 enum34==1.1.6
grpcio==1.0.0 grpcio==1.0.0
grpcio-tools==1.0.0 grpcio-tools==1.0.0
mock==1.3.0
pycco==0.5.1 pycco==0.5.1
pytest==3.0.7
pytest-cov==2.5.1
pytest-runner==2.11.1
sphinx==1.4.8 sphinx==1.4.8
sphinx_rtd_theme==0.1.9 sphinx_rtd_theme==0.1.9
pytest==3.0.7
pytest-runner==2.11.1
pytest-cov==2.5.1

View file

@ -32,7 +32,8 @@ setup(name="core-python",
], ],
tests_require=[ tests_require=[
"pytest", "pytest",
"pytest-cov" "pytest-cov",
"mock"
], ],
description="Python components of CORE", description="Python components of CORE",
url="http://www.nrl.navy.mil/itd/ncs/products/core", url="http://www.nrl.navy.mil/itd/ncs/products/core",

9
daemon/test.py Normal file
View file

@ -0,0 +1,9 @@
import pytest
pytest.main([
"-v",
"--cov-report",
"xml",
"--cov=.",
"tests/test_core.py"
])

View file

@ -4,14 +4,66 @@ Unit tests for testing with a CORE switch.
import time import time
from mock import MagicMock
from conftest import EMANE_SERVICES from conftest import EMANE_SERVICES
from core.enumerations import MessageFlags
from core.mobility import BasicRangeModel from core.mobility import BasicRangeModel
from core.netns import nodes from core.netns import nodes
from core.phys.pnodes import PhysicalNode from core.phys.pnodes import PhysicalNode
class TestCore: class TestCore:
def skip_test_physical(self, core):
def test_netif(self, core):
"""
Test netif methods.
:param conftest.Core core: core fixture to test with
"""
# create ptp
ptp_node = core.session.add_object(cls=nodes.PtpNet)
# create nodes
core.create_node("n1")
core.create_node("n2")
# add interfaces
n1_interface = core.add_interface(ptp_node, "n1")
n2_interface = core.add_interface(ptp_node, "n2")
# get nodes
n1 = core.get_node("n1")
n2 = core.get_node("n2")
# instantiate session
core.session.instantiate()
# assert node directories created
core.assert_nodes()
# check link data gets generated
assert ptp_node.all_link_data(MessageFlags.ADD.value)
# check common nets exist between linked nodes
assert n1.commonnets(n2)
assert n2.commonnets(n1)
# check we can retrieve netif index
assert n1.getifindex(n1_interface) == 0
assert n2.getifindex(n2_interface) == 0
# check interface parameters
n1_interface.setparam("test", 1)
assert n1_interface.getparam("test") == 1
assert n1_interface.getparams()
# delete netif and test that if no longer exists
n1.delnetif(0)
assert not n1.netif(0)
def test_physical(self, core):
""" """
Test physical node network. Test physical node network.
@ -22,8 +74,11 @@ class TestCore:
switch_node = core.session.add_object(cls=nodes.SwitchNode) switch_node = core.session.add_object(cls=nodes.SwitchNode)
# create a physical node # create a physical node
physical_node = core.session.add_object(cls=PhysicalNode, name="p1") core.create_node(cls=PhysicalNode, name="p1")
core.nodes[physical_node.name] = physical_node
# mock method that will not work
physical_node = core.get_node("p1")
physical_node.newnetif = MagicMock(return_value=0)
# create regular node # create regular node
core.create_node("n1") core.create_node("n1")
@ -38,10 +93,6 @@ class TestCore:
# assert node directories created # assert node directories created
core.assert_nodes() core.assert_nodes()
# ping n2 from n1 and assert success
status = core.ping("n1", "p1")
assert not status
def test_ptp(self, core): def test_ptp(self, core):
""" """
Test ptp node network. Test ptp node network.