2017-05-03 23:29:15 +01:00
|
|
|
"""
|
|
|
|
Unit tests for testing with a CORE switch.
|
|
|
|
"""
|
2017-08-03 17:48:26 +01:00
|
|
|
|
2017-07-26 16:48:59 +01:00
|
|
|
import threading
|
|
|
|
|
2017-05-03 23:29:15 +01:00
|
|
|
from core.api import coreapi, dataconversion
|
|
|
|
from core.api.coreapi import CoreExecuteTlv
|
2018-04-27 18:12:01 +01:00
|
|
|
from core.enumerations import CORE_API_PORT, NodeTypes
|
2017-07-10 17:25:33 +01:00
|
|
|
from core.enumerations import EventTlvs
|
|
|
|
from core.enumerations import EventTypes
|
|
|
|
from core.enumerations import ExecuteTlvs
|
|
|
|
from core.enumerations import LinkTlvs
|
|
|
|
from core.enumerations import LinkTypes
|
|
|
|
from core.enumerations import MessageFlags
|
|
|
|
from core.enumerations import MessageTypes
|
2017-05-03 23:29:15 +01:00
|
|
|
from core.misc import ipaddress
|
|
|
|
|
|
|
|
|
2017-07-26 16:48:59 +01:00
|
|
|
def command_message(node, command):
|
|
|
|
"""
|
|
|
|
Create an execute command TLV message.
|
|
|
|
|
|
|
|
:param node: node to execute command for
|
|
|
|
:param command: command to execute
|
|
|
|
:return: packed execute message
|
|
|
|
"""
|
|
|
|
tlv_data = CoreExecuteTlv.pack(ExecuteTlvs.NODE.value, node.objid)
|
|
|
|
tlv_data += CoreExecuteTlv.pack(ExecuteTlvs.NUMBER.value, 1)
|
|
|
|
tlv_data += CoreExecuteTlv.pack(ExecuteTlvs.COMMAND.value, command)
|
|
|
|
return coreapi.CoreExecMessage.pack(MessageFlags.STRING.value | MessageFlags.TEXT.value, tlv_data)
|
|
|
|
|
|
|
|
|
|
|
|
def state_message(state):
|
|
|
|
"""
|
|
|
|
Create a event TLV message for a new state.
|
|
|
|
|
|
|
|
:param core.enumerations.EventTypes state: state to create message for
|
|
|
|
:return: packed event message
|
|
|
|
"""
|
|
|
|
tlv_data = coreapi.CoreEventTlv.pack(EventTlvs.TYPE.value, state.value)
|
|
|
|
return coreapi.CoreEventMessage.pack(0, tlv_data)
|
|
|
|
|
|
|
|
|
|
|
|
def switch_link_message(switch, node, address, prefix_len):
|
|
|
|
"""
|
|
|
|
Create a link TLV message for node to a switch, with the provided address and prefix length.
|
|
|
|
|
|
|
|
:param switch: switch for link
|
|
|
|
:param node: node for link
|
|
|
|
:param address: address node on link
|
|
|
|
:param prefix_len: prefix length of address
|
|
|
|
:return: packed link message
|
|
|
|
"""
|
|
|
|
tlv_data = coreapi.CoreLinkTlv.pack(LinkTlvs.N1_NUMBER.value, switch.objid)
|
|
|
|
tlv_data += coreapi.CoreLinkTlv.pack(LinkTlvs.N2_NUMBER.value, node.objid)
|
|
|
|
tlv_data += coreapi.CoreLinkTlv.pack(LinkTlvs.TYPE.value, LinkTypes.WIRED.value)
|
|
|
|
tlv_data += coreapi.CoreLinkTlv.pack(LinkTlvs.INTERFACE2_NUMBER.value, 0)
|
|
|
|
tlv_data += coreapi.CoreLinkTlv.pack(LinkTlvs.INTERFACE2_IP4.value, address)
|
|
|
|
tlv_data += coreapi.CoreLinkTlv.pack(LinkTlvs.INTERFACE2_IP4_MASK.value, prefix_len)
|
|
|
|
return coreapi.CoreLinkMessage.pack(MessageFlags.ADD.value, tlv_data)
|
|
|
|
|
|
|
|
|
|
|
|
def run_cmd(node, exec_cmd):
|
2017-05-03 23:29:15 +01:00
|
|
|
"""
|
|
|
|
Convenience method for sending commands to a node using the legacy API.
|
|
|
|
|
|
|
|
:param node: The node the command should be issued too
|
|
|
|
:param exec_cmd: A string with the command to be run
|
|
|
|
:return: Returns the result of the command
|
|
|
|
"""
|
|
|
|
# Set up the command api message
|
2017-07-26 16:48:59 +01:00
|
|
|
# tlv_data = CoreExecuteTlv.pack(ExecuteTlvs.NODE.value, node.objid)
|
|
|
|
# tlv_data += CoreExecuteTlv.pack(ExecuteTlvs.NUMBER.value, 1)
|
|
|
|
# tlv_data += CoreExecuteTlv.pack(ExecuteTlvs.COMMAND.value, exec_cmd)
|
|
|
|
# message = coreapi.CoreExecMessage.pack(MessageFlags.STRING.value | MessageFlags.TEXT.value, tlv_data)
|
|
|
|
message = command_message(node, exec_cmd)
|
2017-05-03 23:29:15 +01:00
|
|
|
node.session.broker.handlerawmsg(message)
|
|
|
|
|
|
|
|
# Now wait for the response
|
|
|
|
server = node.session.broker.servers["localhost"]
|
|
|
|
server.sock.settimeout(50.0)
|
|
|
|
|
|
|
|
# receive messages until we get our execute response
|
|
|
|
result = None
|
2017-07-26 16:48:59 +01:00
|
|
|
status = False
|
2017-05-03 23:29:15 +01:00
|
|
|
while True:
|
|
|
|
message_header = server.sock.recv(coreapi.CoreMessage.header_len)
|
|
|
|
message_type, message_flags, message_length = coreapi.CoreMessage.unpack_header(message_header)
|
|
|
|
message_data = server.sock.recv(message_length)
|
|
|
|
|
|
|
|
# If we get the right response return the results
|
2018-04-27 20:09:31 +01:00
|
|
|
print "received response message: %s" % message_type
|
2017-05-03 23:29:15 +01:00
|
|
|
if message_type == MessageTypes.EXECUTE.value:
|
|
|
|
message = coreapi.CoreExecMessage(message_flags, message_header, message_data)
|
|
|
|
result = message.get_tlv(ExecuteTlvs.RESULT.value)
|
2017-07-26 16:48:59 +01:00
|
|
|
status = message.get_tlv(ExecuteTlvs.STATUS.value)
|
2017-05-03 23:29:15 +01:00
|
|
|
break
|
|
|
|
|
2017-07-26 16:48:59 +01:00
|
|
|
return result, status
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestGui:
|
2018-06-15 22:03:27 +01:00
|
|
|
def test_broker(self, cored):
|
2017-05-03 23:29:15 +01:00
|
|
|
"""
|
|
|
|
Test session broker creation.
|
|
|
|
|
2018-05-01 18:40:25 +01:00
|
|
|
:param core.emulator.coreemu.EmuSession session: session for test
|
2018-04-27 18:12:01 +01:00
|
|
|
:param cored: cored daemon server to test with
|
2017-05-03 23:29:15 +01:00
|
|
|
"""
|
|
|
|
|
2017-07-26 16:48:59 +01:00
|
|
|
# set core daemon to run in the background
|
2017-08-02 20:25:57 +01:00
|
|
|
thread = threading.Thread(target=cored.server.serve_forever)
|
2017-07-26 16:48:59 +01:00
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
# ip prefix for nodes
|
2017-05-03 23:29:15 +01:00
|
|
|
prefix = ipaddress.Ipv4Prefix("10.83.0.0/16")
|
|
|
|
daemon = "localhost"
|
|
|
|
|
|
|
|
# add server
|
2018-06-15 22:03:27 +01:00
|
|
|
session = cored.server.coreemu.create_session()
|
2018-04-27 18:12:01 +01:00
|
|
|
session.broker.addserver(daemon, "127.0.0.1", CORE_API_PORT)
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
# setup server
|
2018-04-27 18:12:01 +01:00
|
|
|
session.broker.setupserver(daemon)
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
# do not want the recvloop running as we will deal ourselves
|
2018-04-27 18:12:01 +01:00
|
|
|
session.broker.dorecvloop = False
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
# have broker handle a configuration state change
|
2018-04-27 18:12:01 +01:00
|
|
|
session.set_state(EventTypes.CONFIGURATION_STATE)
|
2017-07-26 16:48:59 +01:00
|
|
|
event_message = state_message(EventTypes.CONFIGURATION_STATE)
|
2018-04-27 18:12:01 +01:00
|
|
|
session.broker.handlerawmsg(event_message)
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
# create a switch node
|
2018-04-27 18:12:01 +01:00
|
|
|
switch = session.add_node(_type=NodeTypes.SWITCH)
|
2017-05-03 23:29:15 +01:00
|
|
|
switch.setposition(x=80, y=50)
|
|
|
|
switch.server = daemon
|
|
|
|
|
|
|
|
# retrieve switch data representation, create a switch message for broker to handle
|
|
|
|
switch_data = switch.data(MessageFlags.ADD.value)
|
|
|
|
switch_message = dataconversion.convert_node(switch_data)
|
2018-04-27 18:12:01 +01:00
|
|
|
session.broker.handlerawmsg(switch_message)
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
# create node one
|
2018-04-27 18:12:01 +01:00
|
|
|
node_one = session.add_node()
|
2017-05-03 23:29:15 +01:00
|
|
|
node_one.server = daemon
|
|
|
|
|
|
|
|
# create node two
|
2018-04-27 18:12:01 +01:00
|
|
|
node_two = session.add_node()
|
2017-05-03 23:29:15 +01:00
|
|
|
node_two.server = daemon
|
|
|
|
|
|
|
|
# create node messages for the broker to handle
|
|
|
|
for node in [node_one, node_two]:
|
|
|
|
node_data = node.data(MessageFlags.ADD.value)
|
|
|
|
node_message = dataconversion.convert_node(node_data)
|
2018-04-27 18:12:01 +01:00
|
|
|
session.broker.handlerawmsg(node_message)
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
# create links to switch from nodes for broker to handle
|
|
|
|
for index, node in enumerate([node_one, node_two], start=1):
|
|
|
|
ip4_address = prefix.addr(index)
|
2017-07-26 16:48:59 +01:00
|
|
|
link_message = switch_link_message(switch, node, ip4_address, prefix.prefixlen)
|
2018-04-27 18:12:01 +01:00
|
|
|
session.broker.handlerawmsg(link_message)
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
# change session to instantiation state
|
2017-07-26 16:48:59 +01:00
|
|
|
event_message = state_message(EventTypes.INSTANTIATION_STATE)
|
2018-04-27 18:12:01 +01:00
|
|
|
session.broker.handlerawmsg(event_message)
|
2017-05-03 23:29:15 +01:00
|
|
|
|
|
|
|
# Get the ip or last node and ping it from the first
|
2017-07-26 16:48:59 +01:00
|
|
|
output, status = run_cmd(node_one, "ip -4 -o addr show dev eth0")
|
|
|
|
pingip = output.split()[3].split("/")[0]
|
|
|
|
output, status = run_cmd(node_two, "ping -c 5 " + pingip)
|
|
|
|
assert not status
|