Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 1 | """ |
| 2 | Connection test cases |
| 3 | |
| 4 | """ |
| 5 | |
| 6 | import time |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 7 | import sys |
| 8 | import logging |
| 9 | |
| 10 | import unittest |
| 11 | import random |
| 12 | |
| 13 | import oftest.controller as controller |
| 14 | import oftest.cstruct as ofp |
| 15 | import oftest.message as message |
| 16 | import oftest.dataplane as dataplane |
| 17 | import oftest.action as action |
| 18 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 19 | from oftest.testutils import * |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 20 | |
| 21 | #@var cxn_port_map Local copy of the configuration map from OF port |
| 22 | # numbers to OS interfaces |
| 23 | cxn_port_map = None |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 24 | #@var cxn_config Local copy of global configuration data |
| 25 | cxn_config = None |
| 26 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 27 | def test_set_init(config): |
| 28 | """ |
| 29 | Set up function for connection test classes |
| 30 | |
| 31 | @param config The configuration dictionary; see oft |
| 32 | """ |
| 33 | |
| 34 | global cxn_port_map |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 35 | global cxn_config |
| 36 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 37 | cxn_port_map = config["port_map"] |
| 38 | cxn_config = config |
| 39 | |
| 40 | class BaseHandshake(unittest.TestCase): |
| 41 | """ |
| 42 | Base handshake case to set up controller, but do not send hello. |
| 43 | """ |
| 44 | |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame^] | 45 | priority = -1 |
| 46 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 47 | def controllerSetup(self, host, port): |
| 48 | self.controller = controller.Controller(host=host,port=port) |
| 49 | |
| 50 | # clean_shutdown should be set to False to force quit app |
| 51 | self.clean_shutdown = True |
| 52 | # disable initial hello so hello is under control of test |
| 53 | self.controller.initial_hello = False |
| 54 | |
| 55 | self.controller.start() |
| 56 | #@todo Add an option to wait for a pkt transaction to ensure version |
| 57 | # compatibilty? |
| 58 | self.controller.connect(timeout=10) |
| 59 | self.assertTrue(self.controller.active, |
| 60 | "Controller startup failed, not active") |
| 61 | self.assertTrue(self.controller.switch_addr is not None, |
| 62 | "Controller startup failed, no switch addr") |
| 63 | |
| 64 | def setUp(self): |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 65 | self.config = cxn_config |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 66 | logging.info("** START TEST CASE " + str(self)) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 67 | |
| 68 | self.test_timeout = test_param_get(cxn_config, |
| 69 | 'handshake_timeout') or 60 |
| 70 | |
| 71 | def inheritSetup(self, parent): |
| 72 | """ |
| 73 | Inherit the setup of a parent |
| 74 | |
| 75 | This allows running at test from within another test. Do the |
| 76 | following: |
| 77 | |
| 78 | sub_test = SomeTestClass() # Create an instance of the test class |
| 79 | sub_test.inheritSetup(self) # Inherit setup of parent |
| 80 | sub_test.runTest() # Run the test |
| 81 | |
| 82 | Normally, only the parent's setUp and tearDown are called and |
| 83 | the state after the sub_test is run must be taken into account |
| 84 | by subsequent operations. |
| 85 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 86 | self.config = parent.config |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 87 | logging.info("** Setup " + str(self) + |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 88 | " inheriting from " + str(parent)) |
| 89 | self.controller = parent.controller |
| 90 | |
| 91 | def tearDown(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 92 | logging.info("** END TEST CASE " + str(self)) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 93 | self.controller.shutdown() |
| 94 | if self.clean_shutdown: |
| 95 | self.controller.join() |
| 96 | |
| 97 | def runTest(self): |
| 98 | # do nothing in the base case |
| 99 | pass |
| 100 | |
| 101 | def assertTrue(self, cond, msg): |
| 102 | if not cond: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 103 | logging.error("** FAILED ASSERTION: " + msg) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 104 | unittest.TestCase.assertTrue(self, cond, msg) |
| 105 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 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"]) |
| 114 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 115 | logging.info("TCP Connected " + |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 116 | str(self.controller.switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 117 | logging.info("Hello not sent, waiting for timeout") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 118 | |
| 119 | # wait for controller to die |
| 120 | count = 0 |
| 121 | while self.controller.active and count < self.test_timeout: |
| 122 | time.sleep(1) |
| 123 | count = count + 1 |
| 124 | self.assertTrue(not self.controller.active, |
| 125 | "Expected controller disconnect, but still active") |
| 126 | |
| 127 | class HandshakeNoFeaturesRequest(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 128 | """ |
| 129 | TCP connect to switch, send hello, but do not send features request, |
| 130 | and wait for disconnect. |
| 131 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 132 | def runTest(self): |
| 133 | self.controllerSetup(cxn_config["controller_host"], |
| 134 | cxn_config["controller_port"]) |
| 135 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 136 | logging.info("TCP Connected " + |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 137 | str(self.controller.switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 138 | logging.info("Sending hello") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 139 | self.controller.message_send(message.hello()) |
| 140 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 141 | logging.info("Features request not sent, waiting for timeout") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 142 | |
| 143 | # wait for controller to die |
| 144 | count = 0 |
| 145 | while self.controller.active and count < self.test_timeout: |
| 146 | time.sleep(1) |
| 147 | count = count + 1 |
| 148 | self.assertTrue(not self.controller.active, |
| 149 | "Expected controller disconnect, but still active") |
| 150 | |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 151 | class HandshakeAndKeepalive(BaseHandshake): |
| 152 | """ |
| 153 | Complete handshake and respond to echo request, but otherwise do nothing. |
| 154 | Good for manual testing. |
| 155 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame^] | 156 | |
| 157 | priority = -1 |
| 158 | |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 159 | def runTest(self): |
| 160 | self.controllerSetup(cxn_config["controller_host"], |
| 161 | cxn_config["controller_port"]) |
| 162 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 163 | logging.info("TCP Connected " + |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 164 | str(self.controller.switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 165 | logging.info("Sending hello") |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 166 | self.controller.message_send(message.hello()) |
| 167 | |
| 168 | request = message.features_request() |
| 169 | reply, pkt = self.controller.transact(request, timeout=20) |
| 170 | self.assertTrue(reply is not None, |
| 171 | "Did not complete features_request for handshake") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 172 | logging.info("Handshake complete with " + |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 173 | str(self.controller.switch_addr)) |
| 174 | |
| 175 | self.controller.keep_alive = True |
| 176 | |
| 177 | # keep controller up forever |
| 178 | while self.controller.active: |
| 179 | time.sleep(1) |
| 180 | |
| 181 | self.assertTrue(not self.controller.active, |
| 182 | "Expected controller disconnect, but still active") |
| 183 | |