Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 1 | """ |
| 2 | Basic test cases for the oftest OpenFlow test framework |
| 3 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 4 | It is recommended that these definitions be kept in their own |
| 5 | namespace as different groups of tests will likely define |
| 6 | similar identifiers. |
| 7 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 8 | Current Assumptions: |
| 9 | |
| 10 | The oftest framework source is in ../src/python/oftest |
| 11 | Configuration of the platform and system is stored in oft_config in that dir |
| 12 | The switch is actively attempting to contact the controller at the address |
| 13 | indicated oin oft_config |
| 14 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 15 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 16 | """ |
| 17 | |
| 18 | from scapy.all import * |
| 19 | import unittest |
| 20 | |
| 21 | import time |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 22 | import signal |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 23 | import sys |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 24 | ##@todo Use setup to place OFT modules in path |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 25 | sys.path.append("../src/python/oftest") |
| 26 | from message import * |
| 27 | from dataplane import * |
| 28 | from controller import * |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 29 | import logging |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 30 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 31 | basic_port_map = None |
| 32 | basic_logger = None |
| 33 | basic_config = None |
| 34 | |
| 35 | def test_set_init(config): |
| 36 | """ |
| 37 | Set up function for basic test classes |
| 38 | |
| 39 | @param config The configuration dictionary; see oft |
| 40 | @return TestSuite object for the class; may depend on config |
| 41 | """ |
| 42 | |
| 43 | global basic_port_map |
| 44 | global basic_logger |
| 45 | global basic_config |
| 46 | |
| 47 | basic_logger = logging.getLogger("basic") |
| 48 | basic_logger.info("Initializing test set") |
| 49 | basic_port_map = config["port_map"] |
| 50 | basic_config = config |
| 51 | return suite() |
| 52 | |
| 53 | def suite(): |
| 54 | suite = unittest.TestSuite() |
| 55 | suite.addTest(SimpleProtocolTestCase()) |
| 56 | suite.addTest(SimpleDataPlaneTestCase()) |
| 57 | suite.addTest(EchoTestCase()) |
| 58 | suite.addTest(EchoWithDataTestCase()) |
| 59 | suite.addTest(PacketInTestCase()) |
| 60 | suite.addTest(PacketOutTestCase()) |
| 61 | return suite |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 62 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 63 | class SimpleProtocolTestCase(unittest.TestCase): |
| 64 | """ |
| 65 | Root class for setting up the controller |
| 66 | """ |
| 67 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 68 | def sig_handler(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 69 | basic_logger.critical("Received interrupt signal; exiting") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 70 | print "Received interrupt signal; exiting" |
| 71 | self.controller.shutdown() |
| 72 | sys.exit(1) |
| 73 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 74 | def setUp(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 75 | signal.signal(signal.SIGINT, self.sig_handler) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 76 | basic_logger.info("Setup for " + str(self)) |
| 77 | self.controller = Controller(host=basic_config["controller_host"], |
| 78 | port=basic_config["controller_port"]) |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 79 | self.controller.start() |
| 80 | self.controller.connect(timeout=20) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 81 | basic_logger.info("Connected " + str(self.controller.switch_addr)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 82 | |
| 83 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 84 | basic_logger.info("Teardown for simple proto test") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 85 | self.controller.shutdown() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 86 | # self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 87 | |
| 88 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 89 | # Just a simple sanity check as illustration |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 90 | basic_logger.info("Running simple proto test") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 91 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 92 | str(self) + 'No connection to switch') |
| 93 | |
| 94 | class SimpleDataPlaneTestCase(SimpleProtocolTestCase): |
| 95 | """ |
| 96 | Root class that sets up the controller and dataplane |
| 97 | """ |
| 98 | def setUp(self): |
| 99 | SimpleProtocolTestCase.setUp(self) |
| 100 | self.dataplane = DataPlane() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 101 | for of_port, ifname in basic_port_map.items(): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 102 | self.dataplane.port_add(ifname, of_port) |
| 103 | |
| 104 | def tearDown(self): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 105 | basic_logger.info("Teardown for simple dataplane test") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 106 | SimpleProtocolTestCase.tearDown(self) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 107 | self.dataplane.kill(join_threads=True) |
| 108 | basic_logger.info("Teardown done") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 109 | |
| 110 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 111 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 112 | str(self) + 'No connection to switch') |
| 113 | # self.dataplane.show() |
| 114 | # Would like an assert that checks the data plane |
| 115 | |
| 116 | class EchoTestCase(SimpleProtocolTestCase): |
| 117 | """ |
| 118 | Test echo response with no data |
| 119 | """ |
| 120 | def runTest(self): |
| 121 | request = echo_request() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 122 | response, pkt = self.controller.transact(request) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 123 | self.assertEqual(response.header.type, OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 124 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 125 | self.assertEqual(request.header.xid, response.header.xid, |
| 126 | 'response xid != request xid') |
| 127 | self.assertEqual(len(response.data), 0, 'response data non-empty') |
| 128 | |
| 129 | class EchoWithDataTestCase(SimpleProtocolTestCase): |
| 130 | """ |
| 131 | Test echo response with short string data |
| 132 | """ |
| 133 | def runTest(self): |
| 134 | request = echo_request() |
| 135 | request.data = 'OpenFlow Will Rule The World' |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 136 | response, pkt = self.controller.transact(request) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 137 | self.assertEqual(response.header.type, OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 138 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 139 | self.assertEqual(request.header.xid, response.header.xid, |
| 140 | 'response xid != request xid') |
| 141 | self.assertEqual(request.data, response.data, |
| 142 | 'response data does not match request') |
| 143 | |
| 144 | class PacketInTestCase(SimpleDataPlaneTestCase): |
| 145 | """ |
| 146 | Test packet in function |
| 147 | """ |
| 148 | def runTest(self): |
| 149 | # Construct packet to send to dataplane |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 150 | # Send packet to dataplane, once to each port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 151 | # Poll controller with expect message type packet in |
| 152 | # For now, a random packet from scapy tutorial |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 153 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 154 | for of_port in basic_port_map.keys(): |
| 155 | basic_logger.info("PKT IN test, port " + str(of_port)) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 156 | pkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\ |
| 157 | ("GET /index.html HTTP/1.0. port" + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 158 | self.dataplane.send(of_port, str(pkt)) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 159 | #@todo Check for unexpected messages? |
| 160 | (response, raw) = self.controller.poll(OFPT_PACKET_IN, 2) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 161 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 162 | self.assertTrue(response is not None, |
| 163 | 'Packet in message not received') |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 164 | if str(pkt) != response.data: |
| 165 | basic_logger.debug("pkt: "+str(pkt)+" resp: " + |
| 166 | str(response)) |
| 167 | |
| 168 | self.assertEqual(str(pkt), response.data, |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 169 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 170 | |
| 171 | class PacketOutTestCase(SimpleDataPlaneTestCase): |
| 172 | """ |
| 173 | Test packet out function |
| 174 | """ |
| 175 | def runTest(self): |
| 176 | # Construct packet to send to dataplane |
| 177 | # Send packet to dataplane |
| 178 | # Poll controller with expect message type packet in |
| 179 | # For now, a random packet from scapy tutorial |
| 180 | |
| 181 | # These will get put into function |
| 182 | outpkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\ |
| 183 | "GET /index.html HTTP/1.0 \n\n" |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 184 | of_ports = basic_port_map.keys() |
| 185 | of_ports.sort() |
| 186 | for dp_port in of_ports: |
| 187 | msg = packet_out() |
| 188 | msg.data = str(outpkt) |
| 189 | act = action_output() |
| 190 | act.port = dp_port |
| 191 | self.assertTrue(msg.actions.add(act), 'Could not add action to msg') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 192 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 193 | basic_logger.info("PacketOut to: " + str(dp_port)) |
| 194 | rv = self.controller.message_send(msg) |
| 195 | self.assertTrue(rv == 0, "Error sending out message") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 196 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 197 | (of_port, pkt, pkt_time) = self.dataplane.poll(timeout=1) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 198 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 199 | self.assertTrue(pkt is not None, 'Packet not received') |
| 200 | basic_logger.info("PacketOut: got pkt from " + str(of_port)) |
| 201 | if of_port is not None: |
| 202 | self.assertEqual(of_port, dp_port, "Unexpected receive port") |
| 203 | self.assertEqual(str(outpkt), str(pkt), |
| 204 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 205 | |
| 206 | if __name__ == "__main__": |
| 207 | unittest.main() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 208 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 209 | # suite = unittest.TestLoader().loadTestsFromTestCase(PacketOutTestCase) |
| 210 | # unittest.TextTestRunner(verbosity=2).run(suite) |