From ebe3b9e3e39dd3f1a30a15d941476fbfd17bf6cf Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Wed, 26 Jul 2017 16:52:17 -0700 Subject: [PATCH] added mock for python testing, removed a couple unused methods, added some more quick tests for a variety of interface related methods --- daemon/core/broker.py | 9 +++-- daemon/core/coreobj.py | 39 --------------------- daemon/core/xml/xmlwriter0.py | 2 +- daemon/requirements.txt | 7 ++-- daemon/setup.py | 3 +- daemon/test.py | 9 +++++ daemon/tests/test_core.py | 65 +++++++++++++++++++++++++++++++---- 7 files changed, 80 insertions(+), 54 deletions(-) create mode 100644 daemon/test.py diff --git a/daemon/core/broker.py b/daemon/core/broker.py index 20605056..cdbbb124 100644 --- a/daemon/core/broker.py +++ b/daemon/core/broker.py @@ -437,6 +437,7 @@ class CoreBroker(ConfigurableManager): """ try: net = self.session.get_object(node_id) + logger.info("adding net tunnel for: id(%s) %s", node_id, net) except KeyError: raise KeyError("network node %s not found" % node_id) @@ -583,7 +584,9 @@ class CoreBroker(ConfigurableManager): :param int nodenum: node id to add :return: nothing """ + logger.info("adding net to broker: %s", nodenum) self.network_nodes.add(nodenum) + logger.info("broker network nodes: %s", self.network_nodes) def addphys(self, nodenum): """ @@ -1010,10 +1013,10 @@ class CoreBroker(ConfigurableManager): for server in servers: if server.name == "localhost": continue - try: + + lhost, lport = None, None + if server.sock: lhost, lport = server.sock.getsockname() - except IOError: - lhost, lport = None, None f.write("%s %s %s %s %s\n" % (server.name, server.host, server.port, lhost, lport)) except IOError: logger.exception("error writing server list to the file: %s" % filename) diff --git a/daemon/core/coreobj.py b/daemon/core/coreobj.py index f82cf194..4cee157c 100644 --- a/daemon/core/coreobj.py +++ b/daemon/core/coreobj.py @@ -273,16 +273,6 @@ class PyCoreNode(PyCoreObj): self.nodedir = None 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): """ Add a services to the service list. @@ -465,35 +455,6 @@ class PyCoreNet(PyCoreObj): with self._linked_lock: 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): """ Build link data objects for this network. Each link object describes a link diff --git a/daemon/core/xml/xmlwriter0.py b/daemon/core/xml/xmlwriter0.py index af9e2bd4..3b3c7b04 100644 --- a/daemon/core/xml/xmlwriter0.py +++ b/daemon/core/xml/xmlwriter0.py @@ -168,7 +168,7 @@ class CoreDocumentWriter0(Document): n = self.createElement("Node") self.np.appendChild(n) n.setAttribute("name", node.name) - n.setAttribute("id", "%s" % node.nodeid()) + n.setAttribute("id", "%s" % node.objid) if node.type: n.setAttribute("type", node.type) self.addinterfaces(n, node) diff --git a/daemon/requirements.txt b/daemon/requirements.txt index 9aa8fc3c..d626b47d 100644 --- a/daemon/requirements.txt +++ b/daemon/requirements.txt @@ -1,9 +1,10 @@ enum34==1.1.6 grpcio==1.0.0 grpcio-tools==1.0.0 +mock==1.3.0 pycco==0.5.1 +pytest==3.0.7 +pytest-cov==2.5.1 +pytest-runner==2.11.1 sphinx==1.4.8 sphinx_rtd_theme==0.1.9 -pytest==3.0.7 -pytest-runner==2.11.1 -pytest-cov==2.5.1 diff --git a/daemon/setup.py b/daemon/setup.py index fdaaf62b..2931f02c 100644 --- a/daemon/setup.py +++ b/daemon/setup.py @@ -32,7 +32,8 @@ setup(name="core-python", ], tests_require=[ "pytest", - "pytest-cov" + "pytest-cov", + "mock" ], description="Python components of CORE", url="http://www.nrl.navy.mil/itd/ncs/products/core", diff --git a/daemon/test.py b/daemon/test.py new file mode 100644 index 00000000..9274613a --- /dev/null +++ b/daemon/test.py @@ -0,0 +1,9 @@ +import pytest + +pytest.main([ + "-v", + "--cov-report", + "xml", + "--cov=.", + "tests/test_core.py" +]) diff --git a/daemon/tests/test_core.py b/daemon/tests/test_core.py index dce645d7..ab1d721e 100644 --- a/daemon/tests/test_core.py +++ b/daemon/tests/test_core.py @@ -4,14 +4,66 @@ Unit tests for testing with a CORE switch. import time +from mock import MagicMock + from conftest import EMANE_SERVICES +from core.enumerations import MessageFlags from core.mobility import BasicRangeModel from core.netns import nodes from core.phys.pnodes import PhysicalNode 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. @@ -22,8 +74,11 @@ class TestCore: switch_node = core.session.add_object(cls=nodes.SwitchNode) # create a physical node - physical_node = core.session.add_object(cls=PhysicalNode, name="p1") - core.nodes[physical_node.name] = physical_node + core.create_node(cls=PhysicalNode, name="p1") + + # mock method that will not work + physical_node = core.get_node("p1") + physical_node.newnetif = MagicMock(return_value=0) # create regular node core.create_node("n1") @@ -38,10 +93,6 @@ class TestCore: # assert node directories created core.assert_nodes() - # ping n2 from n1 and assert success - status = core.ping("n1", "p1") - assert not status - def test_ptp(self, core): """ Test ptp node network.