Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 1 | """ |
| 2 | Basic test cases for the oftest OpenFlow test framework |
| 3 | |
| 4 | Current Assumptions: |
| 5 | |
| 6 | The oftest framework source is in ../src/python/oftest |
| 7 | Configuration of the platform and system is stored in oft_config in that dir |
| 8 | The switch is actively attempting to contact the controller at the address |
| 9 | indicated oin oft_config |
| 10 | |
| 11 | """ |
| 12 | |
| 13 | from scapy.all import * |
| 14 | import unittest |
| 15 | |
| 16 | import time |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 17 | import signal |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 18 | import sys |
| 19 | sys.path.append("../src/python/oftest") |
| 20 | from message import * |
| 21 | from dataplane import * |
| 22 | from controller import * |
| 23 | from oft_config import * |
| 24 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 25 | #debug_level_default = DEBUG_VERBOSE |
| 26 | dbg_lvl = debug_level_default |
| 27 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 28 | class SimpleProtocolTestCase(unittest.TestCase): |
| 29 | """ |
| 30 | Root class for setting up the controller |
| 31 | """ |
| 32 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 33 | def dbg(self, level, string): |
| 34 | debug_log(str(self), dbg_lvl, level, string) |
| 35 | |
| 36 | def sig_handler(self): |
| 37 | print "Received interrupt signal; exiting" |
| 38 | self.controller.shutdown() |
| 39 | sys.exit(1) |
| 40 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 41 | def setUp(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 42 | signal.signal(signal.SIGINT, self.sig_handler) |
| 43 | self.dbg(DEBUG_INFO, "Setup for " + str(self)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 44 | self.controller = Controller() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 45 | self.controller.start() |
| 46 | self.controller.connect(timeout=20) |
| 47 | self.dbg(DEBUG_INFO, "Connected " + str(self.controller.switch_addr)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 48 | |
| 49 | def tearDown(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 50 | self.dbg(DEBUG_INFO, "Teardown for simple proto test") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 51 | self.controller.shutdown() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 52 | # self.controller.join() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 53 | |
| 54 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 55 | # Just a simple sanity check as illustration |
| 56 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 57 | str(self) + 'No connection to switch') |
| 58 | |
| 59 | class SimpleDataPlaneTestCase(SimpleProtocolTestCase): |
| 60 | """ |
| 61 | Root class that sets up the controller and dataplane |
| 62 | """ |
| 63 | def setUp(self): |
| 64 | SimpleProtocolTestCase.setUp(self) |
| 65 | self.dataplane = DataPlane() |
| 66 | for of_port, ifname in interface_ofport_map.items(): |
| 67 | self.dataplane.port_add(ifname, of_port) |
| 68 | |
| 69 | def tearDown(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 70 | self.dbg(DEBUG_INFO, "Teardown for simple dataplane test") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 71 | SimpleProtocolTestCase.tearDown(self) |
| 72 | self.dataplane.kill(join_threads=False) |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 73 | self.dbg(DEBUG_INFO, "Teardown done") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 74 | |
| 75 | def runTest(self): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 76 | self.assertTrue(self.controller.switch_socket is not None, |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 77 | str(self) + 'No connection to switch') |
| 78 | # self.dataplane.show() |
| 79 | # Would like an assert that checks the data plane |
| 80 | |
| 81 | class EchoTestCase(SimpleProtocolTestCase): |
| 82 | """ |
| 83 | Test echo response with no data |
| 84 | """ |
| 85 | def runTest(self): |
| 86 | request = echo_request() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame^] | 87 | response, pkt = self.controller.transact(request) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 88 | self.assertEqual(response.header.type, OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 89 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 90 | self.assertEqual(request.header.xid, response.header.xid, |
| 91 | 'response xid != request xid') |
| 92 | self.assertEqual(len(response.data), 0, 'response data non-empty') |
| 93 | |
| 94 | class EchoWithDataTestCase(SimpleProtocolTestCase): |
| 95 | """ |
| 96 | Test echo response with short string data |
| 97 | """ |
| 98 | def runTest(self): |
| 99 | request = echo_request() |
| 100 | request.data = 'OpenFlow Will Rule The World' |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame^] | 101 | response, pkt = self.controller.transact(request) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 102 | self.assertEqual(response.header.type, OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 103 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 104 | self.assertEqual(request.header.xid, response.header.xid, |
| 105 | 'response xid != request xid') |
| 106 | self.assertEqual(request.data, response.data, |
| 107 | 'response data does not match request') |
| 108 | |
| 109 | class PacketInTestCase(SimpleDataPlaneTestCase): |
| 110 | """ |
| 111 | Test packet in function |
| 112 | """ |
| 113 | def runTest(self): |
| 114 | # Construct packet to send to dataplane |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame^] | 115 | # Send packet to dataplane, once to each port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 116 | # Poll controller with expect message type packet in |
| 117 | # For now, a random packet from scapy tutorial |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame^] | 118 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 119 | for of_port in interface_ofport_map.keys(): |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame^] | 120 | self.dbg(DEBUG_INFO, "PKT IN test, port " + str(of_port)) |
| 121 | pkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\ |
| 122 | ("GET /index.html HTTP/1.0. port" + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 123 | self.dataplane.send(of_port, str(pkt)) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame^] | 124 | #@todo Check for unexpected messages? |
| 125 | (response, raw) = self.controller.poll(OFPT_PACKET_IN, 2) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 126 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame^] | 127 | self.assertTrue(response is not None, |
| 128 | 'Packet in message not received') |
| 129 | # Data has CRC on it, so take off last 4 bytes |
| 130 | self.assertEqual(str(pkt), response.data[:-4], |
| 131 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 132 | |
| 133 | class PacketOutTestCase(SimpleDataPlaneTestCase): |
| 134 | """ |
| 135 | Test packet out function |
| 136 | """ |
| 137 | def runTest(self): |
| 138 | # Construct packet to send to dataplane |
| 139 | # Send packet to dataplane |
| 140 | # Poll controller with expect message type packet in |
| 141 | # For now, a random packet from scapy tutorial |
| 142 | |
| 143 | # These will get put into function |
| 144 | outpkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\ |
| 145 | "GET /index.html HTTP/1.0 \n\n" |
| 146 | msg = packet_out() |
| 147 | msg.data = str(outpkt) |
| 148 | act = action_output() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 149 | dp_port = act.port = interface_ofport_map.keys()[0] |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 150 | self.assertTrue(msg.actions.add(act), 'Could not add action to msg') |
| 151 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 152 | self.dbg(DEBUG_INFO, "pkt out to port " + str(dp_port)) |
| 153 | rv = self.controller.message_send(msg) |
| 154 | self.assertTrue(rv == 0, "Error sending out message") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 155 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 156 | time.sleep(1) # @todo Implement poll timeout for test cases |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame^] | 157 | (of_port, pkt, pkt_time) = self.dataplane.poll() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 158 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 159 | self.assertTrue(pkt is not None, 'Packet not received') |
| 160 | if of_port is not None: |
| 161 | self.assertEqual(of_port, dp_port, "Unexpected receive port") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 162 | self.assertEqual(str(outpkt), str(pkt), |
| 163 | 'Response packet does not match send packet') |
| 164 | |
| 165 | |
| 166 | if __name__ == "__main__": |
| 167 | unittest.main() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 168 | # suite = unittest.TestLoader().loadTestsFromTestCase(PacketOutTestCase) |
| 169 | # unittest.TextTestRunner(verbosity=2).run(suite) |