From 1404ca19c5750ef482b4db508bb36b79df91a2bc Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Mon, 23 Apr 2018 15:46:08 -0700 Subject: [PATCH] added some test cases based on future api interactions, node add/delete/update link add/delete/update --- daemon/core/future/coreemu.py | 9 +- daemon/core/netns/vnode.py | 5 +- daemon/tests/test_future.py | 293 ++++++++++++++++++++++++++++++++++ 3 files changed, 300 insertions(+), 7 deletions(-) create mode 100644 daemon/tests/test_future.py diff --git a/daemon/core/future/coreemu.py b/daemon/core/future/coreemu.py index 704106a6..c1e08a26 100644 --- a/daemon/core/future/coreemu.py +++ b/daemon/core/future/coreemu.py @@ -303,8 +303,6 @@ class FutureSession(Session): :param core.data.LinkData link_data: data to create a link with :return: nothing """ - logger.info("link_data: %s", link_data) - # interface data interface_one_data, interface_two_data = get_interfaces(link_data) @@ -430,6 +428,8 @@ class FutureSession(Session): interface_two = common_interface_two break + logger.info("deleting link for interfaces interface_one(%s) interface_two(%s)", + interface_one, interface_two) if all([interface_one, interface_two]) and any([interface_one.net, interface_two.net]): if interface_one.net != interface_two.net and all([interface_one.up, interface_two.up]): raise ValueError("no common network found") @@ -438,8 +438,8 @@ class FutureSession(Session): interface_two.detachnet() if net_one.numnetif() == 0: self.delete_object(net_one.objid) - node_one.delnetif(interface_one_data.id) - node_two.delnetif(interface_two_data.id) + node_one.delnetif(interface_one.netindex) + node_two.delnetif(interface_two.netindex) finally: if node_one: node_one.lock.release() @@ -559,6 +559,7 @@ class FutureSession(Session): name = "%s%s" % (node_class.__name__, node_id) # create node + logger.info("creating node(%s) id(%s) name(%s) start(%s)", node_class, node_id, name, start) node = self.add_object(cls=node_class, objid=node_id, name=name, start=start) # set node attributes diff --git a/daemon/core/netns/vnode.py b/daemon/core/netns/vnode.py index 49d4e79c..a874c692 100644 --- a/daemon/core/netns/vnode.py +++ b/daemon/core/netns/vnode.py @@ -110,7 +110,7 @@ class SimpleLxcNode(PyCoreNode): self.check_cmd([constants.IP_BIN, "link", "set", "lo", "up"]) # set hostname for node - logger.info("setting hostname: %s" % self.name) + logger.info("setting hostname: %s", self.name) self.check_cmd(["hostname", self.name]) # mark node as up @@ -214,7 +214,7 @@ class SimpleLxcNode(PyCoreNode): :raises CoreCommandError: when a non-zero exit status occurs """ source = os.path.abspath(source) - logger.info("mounting %s at %s" % (source, target)) + logger.info("mounting %s at %s", source, target) cmd = 'mkdir -p "%s" && %s -n --bind "%s" "%s"' % (target, constants.MOUNT_BIN, source, target) status, output = self.client.shcmd_result(cmd) if status: @@ -567,7 +567,6 @@ class LxcNode(SimpleLxcNode): finally: self.rmnodedir() - # TODO: should change how this exception is just swallowed up def privatedir(self, path): """ Create a private directory. diff --git a/daemon/tests/test_future.py b/daemon/tests/test_future.py new file mode 100644 index 00000000..807133f7 --- /dev/null +++ b/daemon/tests/test_future.py @@ -0,0 +1,293 @@ +import os +import time + +import pytest + +from core.data import NodeData, LinkData +from core.enumerations import NodeTypes, EventTypes +from core.future.coreemu import CoreEmu, FutureIpv4Prefix +from core.misc import utils + + +@pytest.fixture +def future_session(): + # use coreemu and create a session + coreemu = CoreEmu() + session = coreemu.create_session(master=True) + session.set_state(EventTypes.CONFIGURATION_STATE.value) + + # return created session + yield session + + # shutdown coreemu + coreemu.shutdown() + + +MODELS = [ + "router", + "host", + "PC", + "mdr", +] + +NET_TYPES = [ + NodeTypes.SWITCH, + NodeTypes.HUB, + NodeTypes.WIRELESS_LAN +] + + +class TestFuture: + @pytest.mark.parametrize("model", MODELS) + def test_node_add(self, future_session, model): + # given + node_data = NodeData(node_type=NodeTypes.DEFAULT.value, model=model) + + # when + node_id = future_session.node_add(node_data) + + # give time for node services to boot + time.sleep(1) + + # then + node = future_session.get_object(node_id) + assert node + assert os.path.exists(node.nodedir) + assert node.alive() + assert node.up + assert node.check_cmd(["ip", "addr", "show", "lo"]) + node.validate() + + def test_node_update(self, future_session): + # given + node_data = NodeData(node_type=NodeTypes.DEFAULT.value) + node_id = future_session.node_add(node_data) + position_value = 100 + update_data = NodeData( + id=node_id, + x_position=position_value, + y_position=position_value + ) + + # when + future_session.node_update(update_data) + + # then + node = future_session.get_object(node_id) + assert node.position.x == position_value + assert node.position.y == position_value + + def test_node_delete(self, future_session): + # given + node_data = NodeData(node_type=NodeTypes.DEFAULT.value) + node_id = future_session.node_add(node_data) + + # when + future_session.node_delete(node_id) + + # then + with pytest.raises(KeyError): + future_session.get_object(node_id) + + @pytest.mark.parametrize("net_type", NET_TYPES) + def test_net(self, future_session, net_type): + # given + node_data = NodeData(node_type=net_type.value) + + # when + node_id = future_session.node_add(node_data) + + # then + node = future_session.get_object(node_id) + assert node + assert node.up + assert utils.check_cmd(["brctl", "show", node.brname]) + + def test_ptp(self, future_session): + # given + prefix = FutureIpv4Prefix("10.83.0.0/16") + node_data = NodeData(node_type=NodeTypes.DEFAULT.value) + node_one_id = future_session.node_add(node_data) + node_two_id = future_session.node_add(node_data) + + node_one = future_session.get_object(node_one_id) + inteface_one_index = node_one.newifindex() + address_one = prefix.addr(node_one_id) + + node_two = future_session.get_object(node_two_id) + inteface_two_index = node_two.newifindex() + address_two = prefix.addr(node_two_id) + + link_data = LinkData( + node1_id=node_one_id, + node2_id=node_two_id, + interface1_id=inteface_one_index, + interface1_ip4=str(address_one), + interface1_ip4_mask=prefix.prefixlen, + interface2_id=inteface_two_index, + interface2_ip4=str(address_two), + interface2_ip4_mask=prefix.prefixlen, + ) + + # when + future_session.link_add(link_data) + + # then + assert node_one.netif(inteface_one_index) + assert node_two.netif(inteface_two_index) + + def test_node_to_net(self, future_session): + # given + prefix = FutureIpv4Prefix("10.83.0.0/16") + + node_data = NodeData(node_type=NodeTypes.DEFAULT.value) + node_one = future_session.node_add(node_data) + node_data = NodeData(node_type=NodeTypes.SWITCH.value) + node_two = future_session.node_add(node_data) + + node = future_session.get_object(node_one) + inteface_index = node.newifindex() + address = prefix.addr(node_one) + + link_data = LinkData( + node1_id=node_one, + node2_id=node_two, + interface1_id=inteface_index, + interface1_ip4=str(address), + interface1_ip4_mask=prefix.prefixlen, + ) + + # when + future_session.link_add(link_data) + + # then + node_two = future_session.get_object(node_two) + assert node_two.all_link_data(0) + assert node.netif(inteface_index) + + def test_net_to_node(self, future_session): + # given + prefix = FutureIpv4Prefix("10.83.0.0/16") + + node_data = NodeData(node_type=NodeTypes.SWITCH.value) + node_one = future_session.node_add(node_data) + node_data = NodeData(node_type=NodeTypes.DEFAULT.value) + node_two = future_session.node_add(node_data) + + node = future_session.get_object(node_two) + inteface_index = node.newifindex() + address = prefix.addr(node_two) + + link_data = LinkData( + node1_id=node_one, + node2_id=node_two, + interface2_id=inteface_index, + interface2_ip4=str(address), + interface2_ip4_mask=prefix.prefixlen, + ) + + # when + future_session.link_add(link_data) + + # then + node_one = future_session.get_object(node_one) + assert node_one.all_link_data(0) + assert node.netif(inteface_index) + + def test_net_to_net(self, future_session): + # given + node_data = NodeData(node_type=NodeTypes.SWITCH.value) + node_one = future_session.node_add(node_data) + node_data = NodeData(node_type=NodeTypes.SWITCH.value) + node_two = future_session.node_add(node_data) + + link_data = LinkData( + node1_id=node_one, + node2_id=node_two, + ) + + # when + future_session.link_add(link_data) + + # then + node_one = future_session.get_object(node_one) + assert node_one.all_link_data(0) + + def test_link_update(self, future_session): + # given + prefix = FutureIpv4Prefix("10.83.0.0/16") + node_data = NodeData(node_type=NodeTypes.DEFAULT.value) + node_one = future_session.node_add(node_data) + node_data = NodeData(node_type=NodeTypes.SWITCH.value) + node_two = future_session.node_add(node_data) + node = future_session.get_object(node_one) + inteface_index = node.newifindex() + address = prefix.addr(node_one) + link_data = LinkData( + node1_id=node_one, + node2_id=node_two, + interface1_id=inteface_index, + interface1_ip4=str(address), + interface1_ip4_mask=prefix.prefixlen, + ) + future_session.link_add(link_data) + update_data = LinkData( + node1_id=node_one, + node2_id=node_two, + interface1_id=inteface_index, + delay=50, + bandwidth=5000000, + per=25, + dup=25 + ) + interface = node.netif(inteface_index) + output = utils.check_cmd(["tc", "qdisc", "show", "dev", interface.localname]) + assert "delay" not in output + assert "rate" not in output + assert "loss" not in output + assert "duplicate" not in output + + # when + future_session.link_update(update_data) + + # then + output = utils.check_cmd(["tc", "qdisc", "show", "dev", interface.localname]) + assert "delay" in output + assert "rate" in output + assert "loss" in output + assert "duplicate" in output + + def test_link_delete(self, future_session): + # given + prefix = FutureIpv4Prefix("10.83.0.0/16") + node_data = NodeData(node_type=NodeTypes.DEFAULT.value) + node_one_id = future_session.node_add(node_data) + node_two_id = future_session.node_add(node_data) + node_one = future_session.get_object(node_one_id) + inteface_one_index = node_one.newifindex() + address_one = prefix.addr(node_one_id) + node_two = future_session.get_object(node_two_id) + inteface_two_index = node_two.newifindex() + address_two = prefix.addr(node_two_id) + link_data = LinkData( + node1_id=node_one_id, + node2_id=node_two_id, + interface1_id=inteface_one_index, + interface1_ip4=str(address_one), + interface1_ip4_mask=prefix.prefixlen, + interface2_id=inteface_two_index, + interface2_ip4=str(address_two), + interface2_ip4_mask=prefix.prefixlen, + ) + future_session.link_add(link_data) + assert node_one.netif(inteface_one_index) + assert node_two.netif(inteface_two_index) + assert future_session.get_node_count() == 3 + + # when + future_session.link_delete(link_data) + + # then + assert not node_one.netif(inteface_one_index) + assert not node_two.netif(inteface_two_index) + assert future_session.get_node_count() == 2