blob: 7127a916ce899bc4b67a486c6444de12803fc3be [file] [log] [blame]
Ken Chiangd6f5f862012-09-28 15:40:33 -07001"""
2Connection test cases
3
4"""
5
6import time
Ken Chiangd6f5f862012-09-28 15:40:33 -07007import sys
8import logging
9
10import unittest
11import random
12
13import oftest.controller as controller
14import oftest.cstruct as ofp
15import oftest.message as message
16import oftest.dataplane as dataplane
17import oftest.action as action
18
Rich Laneda3b5ad2012-10-03 09:05:32 -070019from oftest.testutils import *
Ken Chiangd6f5f862012-09-28 15:40:33 -070020
21#@var cxn_port_map Local copy of the configuration map from OF port
22# numbers to OS interfaces
23cxn_port_map = None
Ken Chiangd6f5f862012-09-28 15:40:33 -070024#@var cxn_config Local copy of global configuration data
25cxn_config = None
26
Ken Chiangd6f5f862012-09-28 15:40:33 -070027def 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 Chiangd6f5f862012-09-28 15:40:33 -070035 global cxn_config
36
Ken Chiangd6f5f862012-09-28 15:40:33 -070037 cxn_port_map = config["port_map"]
38 cxn_config = config
39
40class BaseHandshake(unittest.TestCase):
41 """
42 Base handshake case to set up controller, but do not send hello.
43 """
44
Rich Laned1d9c282012-10-04 22:07:10 -070045 priority = -1
46
Ken Chiangd6f5f862012-09-28 15:40:33 -070047 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 Chiangd6f5f862012-09-28 15:40:33 -070065 self.config = cxn_config
Rich Lane9a003812012-10-04 17:17:59 -070066 logging.info("** START TEST CASE " + str(self))
Ken Chiangd6f5f862012-09-28 15:40:33 -070067
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 Chiangd6f5f862012-09-28 15:40:33 -070086 self.config = parent.config
Rich Lane9a003812012-10-04 17:17:59 -070087 logging.info("** Setup " + str(self) +
Ken Chiangd6f5f862012-09-28 15:40:33 -070088 " inheriting from " + str(parent))
89 self.controller = parent.controller
90
91 def tearDown(self):
Rich Lane9a003812012-10-04 17:17:59 -070092 logging.info("** END TEST CASE " + str(self))
Ken Chiangd6f5f862012-09-28 15:40:33 -070093 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 Lane9a003812012-10-04 17:17:59 -0700103 logging.error("** FAILED ASSERTION: " + msg)
Ken Chiangd6f5f862012-09-28 15:40:33 -0700104 unittest.TestCase.assertTrue(self, cond, msg)
105
Ken Chiangd6f5f862012-09-28 15:40:33 -0700106class HandshakeNoHello(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -0700107 """
108 TCP connect to switch, but do not sent hello,
109 and wait for disconnect.
110 """
Ken Chiangd6f5f862012-09-28 15:40:33 -0700111 def runTest(self):
112 self.controllerSetup(cxn_config["controller_host"],
113 cxn_config["controller_port"])
114
Rich Lane9a003812012-10-04 17:17:59 -0700115 logging.info("TCP Connected " +
Ken Chiangd6f5f862012-09-28 15:40:33 -0700116 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700117 logging.info("Hello not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700118
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
127class HandshakeNoFeaturesRequest(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -0700128 """
129 TCP connect to switch, send hello, but do not send features request,
130 and wait for disconnect.
131 """
Ken Chiangd6f5f862012-09-28 15:40:33 -0700132 def runTest(self):
133 self.controllerSetup(cxn_config["controller_host"],
134 cxn_config["controller_port"])
135
Rich Lane9a003812012-10-04 17:17:59 -0700136 logging.info("TCP Connected " +
Ken Chiangd6f5f862012-09-28 15:40:33 -0700137 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700138 logging.info("Sending hello")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700139 self.controller.message_send(message.hello())
140
Rich Lane9a003812012-10-04 17:17:59 -0700141 logging.info("Features request not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700142
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 Chiang35a74372012-10-01 15:39:25 -0700151class HandshakeAndKeepalive(BaseHandshake):
152 """
153 Complete handshake and respond to echo request, but otherwise do nothing.
154 Good for manual testing.
155 """
Rich Laned1d9c282012-10-04 22:07:10 -0700156
157 priority = -1
158
Ken Chiang35a74372012-10-01 15:39:25 -0700159 def runTest(self):
160 self.controllerSetup(cxn_config["controller_host"],
161 cxn_config["controller_port"])
162
Rich Lane9a003812012-10-04 17:17:59 -0700163 logging.info("TCP Connected " +
Ken Chiang35a74372012-10-01 15:39:25 -0700164 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700165 logging.info("Sending hello")
Ken Chiang35a74372012-10-01 15:39:25 -0700166 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 Lane9a003812012-10-04 17:17:59 -0700172 logging.info("Handshake complete with " +
Ken Chiang35a74372012-10-01 15:39:25 -0700173 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