Major overhaul of oftest command interface
Added tests/oft as top level executable
Support command line options for many config params
Use logging module for output
Got rid of oft_config.py; consolidate configuration in
oft (top level script) and pass around as a dictionary
Add oft_assert.py (the one useful piece of oft_config that
remained).
diff --git a/tests/basic.py b/tests/basic.py
index f7db544..268d313 100644
--- a/tests/basic.py
+++ b/tests/basic.py
@@ -1,6 +1,10 @@
"""
Basic test cases for the oftest OpenFlow test framework
+It is recommended that these definitions be kept in their own
+namespace as different groups of tests will likely define
+similar identifiers.
+
Current Assumptions:
The oftest framework source is in ../src/python/oftest
@@ -8,6 +12,7 @@
The switch is actively attempting to contact the controller at the address
indicated oin oft_config
+
"""
from scapy.all import *
@@ -16,43 +21,73 @@
import time
import signal
import sys
+##@todo Use setup to place OFT modules in path
sys.path.append("../src/python/oftest")
from message import *
from dataplane import *
from controller import *
-from oft_config import *
+import logging
-#debug_level_default = DEBUG_VERBOSE
-dbg_lvl = debug_level_default
+basic_port_map = None
+basic_logger = None
+basic_config = None
+
+def test_set_init(config):
+ """
+ Set up function for basic test classes
+
+ @param config The configuration dictionary; see oft
+ @return TestSuite object for the class; may depend on config
+ """
+
+ global basic_port_map
+ global basic_logger
+ global basic_config
+
+ basic_logger = logging.getLogger("basic")
+ basic_logger.info("Initializing test set")
+ basic_port_map = config["port_map"]
+ basic_config = config
+ return suite()
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(SimpleProtocolTestCase())
+ suite.addTest(SimpleDataPlaneTestCase())
+ suite.addTest(EchoTestCase())
+ suite.addTest(EchoWithDataTestCase())
+ suite.addTest(PacketInTestCase())
+ suite.addTest(PacketOutTestCase())
+ return suite
class SimpleProtocolTestCase(unittest.TestCase):
"""
Root class for setting up the controller
"""
- def dbg(self, level, string):
- debug_log(str(self), dbg_lvl, level, string)
-
def sig_handler(self):
+ basic_logger.critical("Received interrupt signal; exiting")
print "Received interrupt signal; exiting"
self.controller.shutdown()
sys.exit(1)
def setUp(self):
signal.signal(signal.SIGINT, self.sig_handler)
- self.dbg(DEBUG_INFO, "Setup for " + str(self))
- self.controller = Controller()
+ basic_logger.info("Setup for " + str(self))
+ self.controller = Controller(host=basic_config["controller_host"],
+ port=basic_config["controller_port"])
self.controller.start()
self.controller.connect(timeout=20)
- self.dbg(DEBUG_INFO, "Connected " + str(self.controller.switch_addr))
+ basic_logger.info("Connected " + str(self.controller.switch_addr))
def tearDown(self):
- self.dbg(DEBUG_INFO, "Teardown for simple proto test")
+ basic_logger.info("Teardown for simple proto test")
self.controller.shutdown()
# self.controller.join()
def runTest(self):
# Just a simple sanity check as illustration
+ basic_logger.info("Running simple proto test")
self.assertTrue(self.controller.switch_socket is not None,
str(self) + 'No connection to switch')
@@ -63,14 +98,14 @@
def setUp(self):
SimpleProtocolTestCase.setUp(self)
self.dataplane = DataPlane()
- for of_port, ifname in interface_ofport_map.items():
+ for of_port, ifname in basic_port_map.items():
self.dataplane.port_add(ifname, of_port)
def tearDown(self):
- self.dbg(DEBUG_INFO, "Teardown for simple dataplane test")
+ basic_logger.info("Teardown for simple dataplane test")
SimpleProtocolTestCase.tearDown(self)
- self.dataplane.kill(join_threads=False)
- self.dbg(DEBUG_INFO, "Teardown done")
+ self.dataplane.kill(join_threads=True)
+ basic_logger.info("Teardown done")
def runTest(self):
self.assertTrue(self.controller.switch_socket is not None,
@@ -116,8 +151,8 @@
# Poll controller with expect message type packet in
# For now, a random packet from scapy tutorial
- for of_port in interface_ofport_map.keys():
- self.dbg(DEBUG_INFO, "PKT IN test, port " + str(of_port))
+ for of_port in basic_port_map.keys():
+ basic_logger.info("PKT IN test, port " + str(of_port))
pkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\
("GET /index.html HTTP/1.0. port" + str(of_port))
self.dataplane.send(of_port, str(pkt))
@@ -126,8 +161,11 @@
self.assertTrue(response is not None,
'Packet in message not received')
- # Data has CRC on it, so take off last 4 bytes
- self.assertEqual(str(pkt), response.data[:-4],
+ if str(pkt) != response.data:
+ basic_logger.debug("pkt: "+str(pkt)+" resp: " +
+ str(response))
+
+ self.assertEqual(str(pkt), response.data,
'Response packet does not match send packet')
class PacketOutTestCase(SimpleDataPlaneTestCase):
@@ -143,27 +181,30 @@
# These will get put into function
outpkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\
"GET /index.html HTTP/1.0 \n\n"
- msg = packet_out()
- msg.data = str(outpkt)
- act = action_output()
- dp_port = act.port = interface_ofport_map.keys()[0]
- self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
+ of_ports = basic_port_map.keys()
+ of_ports.sort()
+ for dp_port in of_ports:
+ msg = packet_out()
+ msg.data = str(outpkt)
+ act = action_output()
+ act.port = dp_port
+ self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
- self.dbg(DEBUG_INFO, "pkt out to port " + str(dp_port))
- rv = self.controller.message_send(msg)
- self.assertTrue(rv == 0, "Error sending out message")
+ basic_logger.info("PacketOut to: " + str(dp_port))
+ rv = self.controller.message_send(msg)
+ self.assertTrue(rv == 0, "Error sending out message")
- time.sleep(1) # @todo Implement poll timeout for test cases
- (of_port, pkt, pkt_time) = self.dataplane.poll()
+ (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1)
- self.assertTrue(pkt is not None, 'Packet not received')
- if of_port is not None:
- self.assertEqual(of_port, dp_port, "Unexpected receive port")
- self.assertEqual(str(outpkt), str(pkt),
- 'Response packet does not match send packet')
-
+ self.assertTrue(pkt is not None, 'Packet not received')
+ basic_logger.info("PacketOut: got pkt from " + str(of_port))
+ if of_port is not None:
+ self.assertEqual(of_port, dp_port, "Unexpected receive port")
+ self.assertEqual(str(outpkt), str(pkt),
+ 'Response packet does not match send packet')
if __name__ == "__main__":
unittest.main()
+
# suite = unittest.TestLoader().loadTestsFromTestCase(PacketOutTestCase)
# unittest.TextTestRunner(verbosity=2).run(suite)