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