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 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 13 | from oftest import config |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 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 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 22 | class BaseHandshake(unittest.TestCase): |
| 23 | """ |
| 24 | Base handshake case to set up controller, but do not send hello. |
| 25 | """ |
| 26 | |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 27 | priority = -1 |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 28 | controllers = [] |
| 29 | default_timeout = 2 |
| 30 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 31 | def controllerSetup(self, host, port): |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 32 | con = controller.Controller(host=host,port=port) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 33 | |
| 34 | # clean_shutdown should be set to False to force quit app |
| 35 | self.clean_shutdown = True |
| 36 | # disable initial hello so hello is under control of test |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 37 | con.initial_hello = False |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 38 | |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 39 | con.start() |
| 40 | self.controllers.append(con) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 41 | |
| 42 | def setUp(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 43 | logging.info("** START TEST CASE " + str(self)) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 44 | |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 45 | self.default_timeout = test_param_get('default_timeout', |
| 46 | default=2) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 47 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 48 | def tearDown(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 49 | logging.info("** END TEST CASE " + str(self)) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 50 | for con in self.controllers: |
| 51 | con.shutdown() |
| 52 | if self.clean_shutdown: |
| 53 | con.join() |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 54 | |
| 55 | def runTest(self): |
| 56 | # do nothing in the base case |
| 57 | pass |
| 58 | |
| 59 | def assertTrue(self, cond, msg): |
| 60 | if not cond: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 61 | logging.error("** FAILED ASSERTION: " + msg) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 62 | unittest.TestCase.assertTrue(self, cond, msg) |
| 63 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 64 | class HandshakeNoHello(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 65 | """ |
| 66 | TCP connect to switch, but do not sent hello, |
| 67 | and wait for disconnect. |
| 68 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 69 | def runTest(self): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 70 | self.controllerSetup(config["controller_host"], |
| 71 | config["controller_port"]) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 72 | self.controllers[0].connect(self.default_timeout) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 73 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 74 | logging.info("TCP Connected " + |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 75 | str(self.controllers[0].switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 76 | logging.info("Hello not sent, waiting for timeout") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 77 | |
| 78 | # wait for controller to die |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 79 | self.assertTrue(self.controllers[0].wait_disconnected(timeout=10), |
| 80 | "Not notified of controller disconnect") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 81 | |
| 82 | class HandshakeNoFeaturesRequest(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 83 | """ |
| 84 | TCP connect to switch, send hello, but do not send features request, |
| 85 | and wait for disconnect. |
| 86 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 87 | def runTest(self): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 88 | self.controllerSetup(config["controller_host"], |
| 89 | config["controller_port"]) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 90 | self.controllers[0].connect(self.default_timeout) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 91 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 92 | logging.info("TCP Connected " + |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 93 | str(self.controllers[0].switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 94 | logging.info("Sending hello") |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 95 | self.controllers[0].message_send(message.hello()) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 96 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 97 | logging.info("Features request not sent, waiting for timeout") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 98 | |
| 99 | # wait for controller to die |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 100 | self.assertTrue(self.controllers[0].wait_disconnected(timeout=10), |
| 101 | "Not notified of controller disconnect") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 102 | |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 103 | class HandshakeAndKeepalive(BaseHandshake): |
| 104 | """ |
| 105 | Complete handshake and respond to echo request, but otherwise do nothing. |
| 106 | Good for manual testing. |
| 107 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 108 | |
| 109 | priority = -1 |
| 110 | |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 111 | def runTest(self): |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 112 | self.num_controllers = test_param_get('num_controllers', default=1) |
Ken Chiang | 265fc49 | 2012-10-09 16:16:18 -0700 | [diff] [blame] | 113 | self.controller_timeout = test_param_get('controller_timeout', |
| 114 | default=-1) |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 115 | |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 116 | for i in range(self.num_controllers): |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 117 | self.controllerSetup(config["controller_host"], |
| 118 | config["controller_port"]+i) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 119 | for i in range(self.num_controllers): |
| 120 | self.controllers[i].handshake_done = False |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 121 | |
Ken Chiang | 265fc49 | 2012-10-09 16:16:18 -0700 | [diff] [blame] | 122 | # try to maintain switch connections for specified timeout |
| 123 | # -1 means forever |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 124 | while True: |
| 125 | for con in self.controllers: |
| 126 | if con.switch_socket and con.handshake_done: |
Ken Chiang | 265fc49 | 2012-10-09 16:16:18 -0700 | [diff] [blame] | 127 | if (self.controller_timeout < 0 or |
| 128 | con.count < self.controller_timeout): |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 129 | logging.info(con.host + ":" + str(con.port) + |
| 130 | ": maintaining connection to " + |
| 131 | str(con.switch_addr)) |
Ken Chiang | 265fc49 | 2012-10-09 16:16:18 -0700 | [diff] [blame] | 132 | con.count = con.count + 1 |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 133 | else: |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 134 | logging.info(con.host + ":" + str(con.port) + |
| 135 | ": disconnecting from " + |
| 136 | str(con.switch_addr)) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 137 | con.disconnect() |
| 138 | con.handshake_done = False |
Ken Chiang | 265fc49 | 2012-10-09 16:16:18 -0700 | [diff] [blame] | 139 | con.count = 0 |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 140 | time.sleep(1) |
| 141 | else: |
| 142 | #@todo Add an option to wait for a pkt transaction to |
| 143 | # ensure version compatibilty? |
| 144 | con.connect(self.default_timeout) |
| 145 | if not con.switch_socket: |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 146 | logging.info("Did not connect to switch") |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 147 | continue |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 148 | logging.info("TCP Connected " + str(con.switch_addr)) |
| 149 | logging.info("Sending hello") |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 150 | con.message_send(message.hello()) |
| 151 | request = message.features_request() |
| 152 | reply, pkt = con.transact(request, |
| 153 | timeout=self.default_timeout) |
| 154 | if reply: |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 155 | logging.info("Handshake complete with " + |
| 156 | str(con.switch_addr)) |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 157 | con.handshake_done = True |
| 158 | con.keep_alive = True |
Ken Chiang | 265fc49 | 2012-10-09 16:16:18 -0700 | [diff] [blame] | 159 | con.count = 0 |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 160 | else: |
Ken Chiang | e875baf | 2012-10-09 15:24:40 -0700 | [diff] [blame] | 161 | logging.info("Did not complete features_request " + |
| 162 | "for handshake") |
Ken Chiang | adc950f | 2012-10-05 13:50:03 -0700 | [diff] [blame] | 163 | con.disconnect() |
| 164 | con.handshake_done = False |
Ken Chiang | 265fc49 | 2012-10-09 16:16:18 -0700 | [diff] [blame] | 165 | con.count = 0 |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 166 | |