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 |
| 28 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 29 | def controllerSetup(self, host, port): |
| 30 | self.controller = controller.Controller(host=host,port=port) |
| 31 | |
| 32 | # clean_shutdown should be set to False to force quit app |
| 33 | self.clean_shutdown = True |
| 34 | # disable initial hello so hello is under control of test |
| 35 | self.controller.initial_hello = False |
| 36 | |
| 37 | self.controller.start() |
| 38 | #@todo Add an option to wait for a pkt transaction to ensure version |
| 39 | # compatibilty? |
| 40 | self.controller.connect(timeout=10) |
| 41 | self.assertTrue(self.controller.active, |
| 42 | "Controller startup failed, not active") |
| 43 | self.assertTrue(self.controller.switch_addr is not None, |
| 44 | "Controller startup failed, no switch addr") |
| 45 | |
| 46 | def setUp(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 47 | logging.info("** START TEST CASE " + str(self)) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 48 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 49 | self.test_timeout = test_param_get('handshake_timeout', default=60) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 50 | |
| 51 | def inheritSetup(self, parent): |
| 52 | """ |
| 53 | Inherit the setup of a parent |
| 54 | |
| 55 | This allows running at test from within another test. Do the |
| 56 | following: |
| 57 | |
| 58 | sub_test = SomeTestClass() # Create an instance of the test class |
| 59 | sub_test.inheritSetup(self) # Inherit setup of parent |
| 60 | sub_test.runTest() # Run the test |
| 61 | |
| 62 | Normally, only the parent's setUp and tearDown are called and |
| 63 | the state after the sub_test is run must be taken into account |
| 64 | by subsequent operations. |
| 65 | """ |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 66 | logging.info("** Setup " + str(self) + |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 67 | " inheriting from " + str(parent)) |
| 68 | self.controller = parent.controller |
| 69 | |
| 70 | def tearDown(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 71 | logging.info("** END TEST CASE " + str(self)) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 72 | self.controller.shutdown() |
| 73 | if self.clean_shutdown: |
| 74 | self.controller.join() |
| 75 | |
| 76 | def runTest(self): |
| 77 | # do nothing in the base case |
| 78 | pass |
| 79 | |
| 80 | def assertTrue(self, cond, msg): |
| 81 | if not cond: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 82 | logging.error("** FAILED ASSERTION: " + msg) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 83 | unittest.TestCase.assertTrue(self, cond, msg) |
| 84 | |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 85 | class HandshakeNoHello(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 86 | """ |
| 87 | TCP connect to switch, but do not sent hello, |
| 88 | and wait for disconnect. |
| 89 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 90 | def runTest(self): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 91 | self.controllerSetup(config["controller_host"], |
| 92 | config["controller_port"]) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 93 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 94 | logging.info("TCP Connected " + |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 95 | str(self.controller.switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 96 | logging.info("Hello not sent, waiting for timeout") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 97 | |
| 98 | # wait for controller to die |
| 99 | count = 0 |
| 100 | while self.controller.active and count < self.test_timeout: |
| 101 | time.sleep(1) |
| 102 | count = count + 1 |
| 103 | self.assertTrue(not self.controller.active, |
| 104 | "Expected controller disconnect, but still active") |
| 105 | |
| 106 | class HandshakeNoFeaturesRequest(BaseHandshake): |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 107 | """ |
| 108 | TCP connect to switch, send hello, but do not send features request, |
| 109 | and wait for disconnect. |
| 110 | """ |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 111 | def runTest(self): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 112 | self.controllerSetup(config["controller_host"], |
| 113 | config["controller_port"]) |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 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("Sending hello") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 118 | self.controller.message_send(message.hello()) |
| 119 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 120 | logging.info("Features request not sent, waiting for timeout") |
Ken Chiang | d6f5f86 | 2012-09-28 15:40:33 -0700 | [diff] [blame] | 121 | |
| 122 | # wait for controller to die |
| 123 | count = 0 |
| 124 | while self.controller.active and count < self.test_timeout: |
| 125 | time.sleep(1) |
| 126 | count = count + 1 |
| 127 | self.assertTrue(not self.controller.active, |
| 128 | "Expected controller disconnect, but still active") |
| 129 | |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 130 | class HandshakeAndKeepalive(BaseHandshake): |
| 131 | """ |
| 132 | Complete handshake and respond to echo request, but otherwise do nothing. |
| 133 | Good for manual testing. |
| 134 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 135 | |
| 136 | priority = -1 |
| 137 | |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 138 | def runTest(self): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 139 | self.controllerSetup(config["controller_host"], |
| 140 | config["controller_port"]) |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 141 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 142 | logging.info("TCP Connected " + |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 143 | str(self.controller.switch_addr)) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 144 | logging.info("Sending hello") |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 145 | self.controller.message_send(message.hello()) |
| 146 | |
| 147 | request = message.features_request() |
| 148 | reply, pkt = self.controller.transact(request, timeout=20) |
| 149 | self.assertTrue(reply is not None, |
| 150 | "Did not complete features_request for handshake") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 151 | logging.info("Handshake complete with " + |
Ken Chiang | 35a7437 | 2012-10-01 15:39:25 -0700 | [diff] [blame] | 152 | str(self.controller.switch_addr)) |
| 153 | |
| 154 | self.controller.keep_alive = True |
| 155 | |
| 156 | # keep controller up forever |
| 157 | while self.controller.active: |
| 158 | time.sleep(1) |
| 159 | |
| 160 | self.assertTrue(not self.controller.active, |
| 161 | "Expected controller disconnect, but still active") |
| 162 | |