Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 1 | """ |
| 2 | Connection test cases |
| 3 | |
| 4 | """ |
| 5 | |
| 6 | import time |
| 7 | import signal |
| 8 | import sys |
| 9 | import logging |
| 10 | |
| 11 | import unittest |
| 12 | import random |
| 13 | |
| 14 | import oftest.controller as controller |
| 15 | import oftest.cstruct as ofp |
| 16 | import oftest.message as message |
| 17 | import oftest.dataplane as dataplane |
| 18 | import oftest.action as action |
| 19 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 20 | from oftest.testutils import * |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 21 | |
| 22 | #@var cxn_port_map Local copy of the configuration map from OF port |
| 23 | # numbers to OS interfaces |
| 24 | cxn_port_map = None |
| 25 | #@var cxn_logger Local logger object |
| 26 | cxn_logger = None |
| 27 | #@var cxn_config Local copy of global configuration data |
| 28 | cxn_config = None |
| 29 | |
| 30 | test_prio = {} |
| 31 | |
| 32 | def test_set_init(config): |
| 33 | """ |
| 34 | Set up function for connection test classes |
| 35 | |
| 36 | @param config The configuration dictionary; see oft |
| 37 | """ |
| 38 | |
| 39 | global cxn_port_map |
| 40 | global cxn_logger |
| 41 | global cxn_config |
| 42 | |
| 43 | cxn_logger = logging.getLogger("cxn") |
| 44 | cxn_logger.info("Initializing test set") |
| 45 | cxn_port_map = config["port_map"] |
| 46 | cxn_config = config |
| 47 | |
| 48 | class BaseHandshake(unittest.TestCase): |
| 49 | """ |
| 50 | Base handshake case to set up controller, but do not send hello. |
| 51 | """ |
| 52 | |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 53 | controllers = [] |
| 54 | default_timeout = 2 |
| 55 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 56 | def sig_handler(self, v1, v2): |
| 57 | cxn_logger.critical("Received interrupt signal; exiting") |
| 58 | print "Received interrupt signal; exiting" |
| 59 | self.clean_shutdown = False |
| 60 | self.tearDown() |
| 61 | sys.exit(1) |
| 62 | |
| 63 | def controllerSetup(self, host, port): |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 64 | con = controller.Controller(host=host,port=port) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 65 | |
| 66 | # clean_shutdown should be set to False to force quit app |
| 67 | self.clean_shutdown = True |
| 68 | # disable initial hello so hello is under control of test |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 69 | con.initial_hello = False |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 70 | |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 71 | con.start() |
| 72 | self.controllers.append(con) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 73 | |
| 74 | def setUp(self): |
| 75 | self.logger = cxn_logger |
| 76 | self.config = cxn_config |
| 77 | #@todo Test cases shouldn't monkey with signals; move SIGINT handler |
| 78 | # to top-level oft |
| 79 | try: |
| 80 | signal.signal(signal.SIGINT, self.sig_handler) |
| 81 | except ValueError, e: |
| 82 | cxn_logger.info("Could not set SIGINT handler: %s" % e) |
| 83 | cxn_logger.info("** START TEST CASE " + str(self)) |
| 84 | |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 85 | self.default_timeout = test_param_get(cxn_config, |
| 86 | 'default_timeout') or 2 |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 87 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 88 | def tearDown(self): |
| 89 | cxn_logger.info("** END TEST CASE " + str(self)) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 90 | for con in self.controllers: |
| 91 | con.shutdown() |
| 92 | if self.clean_shutdown: |
| 93 | con.join() |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 94 | |
| 95 | def runTest(self): |
| 96 | # do nothing in the base case |
| 97 | pass |
| 98 | |
| 99 | def assertTrue(self, cond, msg): |
| 100 | if not cond: |
| 101 | cxn_logger.error("** FAILED ASSERTION: " + msg) |
| 102 | unittest.TestCase.assertTrue(self, cond, msg) |
| 103 | |
| 104 | test_prio["BaseHandshake"] = -1 |
| 105 | |
| 106 | class HandshakeNoHello(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 107 | """ |
| 108 | TCP connect to switch, but do not sent hello, |
| 109 | and wait for disconnect. |
| 110 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 111 | def runTest(self): |
| 112 | self.controllerSetup(cxn_config["controller_host"], |
| 113 | cxn_config["controller_port"]) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 114 | self.controllers[0].connect(self.default_timeout) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 115 | cxn_logger.info("TCP Connected " + |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 116 | str(self.controllers[0].switch_addr)) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 117 | cxn_logger.info("Hello not sent, waiting for timeout") |
| 118 | |
| 119 | # wait for controller to die |
| 120 | count = 0 |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 121 | self.assertTrue(self.controllers[0].wait_disconnected(timeout=10), |
| 122 | "Not notified of controller disconnect") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 123 | |
| 124 | class HandshakeNoFeaturesRequest(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 125 | """ |
| 126 | TCP connect to switch, send hello, but do not send features request, |
| 127 | and wait for disconnect. |
| 128 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 129 | def runTest(self): |
| 130 | self.controllerSetup(cxn_config["controller_host"], |
| 131 | cxn_config["controller_port"]) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 132 | self.controllers[0].connect(self.default_timeout) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 133 | cxn_logger.info("TCP Connected " + |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 134 | str(self.controllers[0].switch_addr)) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 135 | cxn_logger.info("Sending hello") |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 136 | self.controllers[0].message_send(message.hello()) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 137 | |
| 138 | cxn_logger.info("Features request not sent, waiting for timeout") |
| 139 | |
| 140 | # wait for controller to die |
| 141 | count = 0 |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 142 | self.assertTrue(self.controllers[0].wait_disconnected(timeout=10), |
| 143 | "Not notified of controller disconnect") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 144 | |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 145 | class HandshakeAndKeepalive(BaseHandshake): |
| 146 | """ |
| 147 | Complete handshake and respond to echo request, but otherwise do nothing. |
| 148 | Good for manual testing. |
| 149 | """ |
| 150 | def runTest(self): |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 151 | self.num_controllers = test_param_get(cxn_config, |
| 152 | 'num_controllers') or 1 |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 153 | |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 154 | for i in range(self.num_controllers): |
| 155 | self.controllerSetup(cxn_config["controller_host"], |
| 156 | cxn_config["controller_port"]+i) |
| 157 | for i in range(self.num_controllers): |
| 158 | self.controllers[i].handshake_done = False |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 159 | |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame^] | 160 | # try to maintain switch connections forever |
| 161 | count = 0 |
| 162 | while True: |
| 163 | for con in self.controllers: |
| 164 | if con.switch_socket and con.handshake_done: |
| 165 | if count < 7: |
| 166 | cxn_logger.info(con.host + ":" + str(con.port) + |
| 167 | ": maintaining connection to " + |
| 168 | str(con.switch_addr)) |
| 169 | count = count + 1 |
| 170 | else: |
| 171 | cxn_logger.info(con.host + ":" + str(con.port) + |
| 172 | ": disconnecting from " + |
| 173 | str(con.switch_addr)) |
| 174 | con.disconnect() |
| 175 | con.handshake_done = False |
| 176 | count = 0 |
| 177 | time.sleep(1) |
| 178 | else: |
| 179 | #@todo Add an option to wait for a pkt transaction to |
| 180 | # ensure version compatibilty? |
| 181 | con.connect(self.default_timeout) |
| 182 | if not con.switch_socket: |
| 183 | cxn_logger.info("Did not connect to switch") |
| 184 | continue |
| 185 | cxn_logger.info("TCP Connected " + str(con.switch_addr)) |
| 186 | cxn_logger.info("Sending hello") |
| 187 | con.message_send(message.hello()) |
| 188 | request = message.features_request() |
| 189 | reply, pkt = con.transact(request, |
| 190 | timeout=self.default_timeout) |
| 191 | if reply: |
| 192 | cxn_logger.info("Handshake complete with " + |
| 193 | str(con.switch_addr)) |
| 194 | con.handshake_done = True |
| 195 | con.keep_alive = True |
| 196 | else: |
| 197 | cxn_logger.info("Did not complete features_request for handshake") |
| 198 | con.disconnect() |
| 199 | con.handshake_done = False |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 200 | |
| 201 | test_prio["HandshakeAndKeepalive"] = -1 |
| 202 | |