added some test cases based on future api interactions, node add/delete/update link add/delete/update
This commit is contained in:
parent
8f0364c44b
commit
1404ca19c5
3 changed files with 300 additions and 7 deletions
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
293
daemon/tests/test_future.py
Normal file
293
daemon/tests/test_future.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue