blob: 87f31c992b53f1901b27cfc925dc5b339a8fac7a [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
27test_prio = {}
28
29def test_set_init(config):
30 """
31 Set up function for connection test classes
32
33 @param config The configuration dictionary; see oft
34 """
35
36 global cxn_port_map
Ken Chiangd6f5f862012-09-28 15:40:33 -070037 global cxn_config
38
Ken Chiangd6f5f862012-09-28 15:40:33 -070039 cxn_port_map = config["port_map"]
40 cxn_config = config
41
42class BaseHandshake(unittest.TestCase):
43 """
44 Base handshake case to set up controller, but do not send hello.
45 """
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
106test_prio["BaseHandshake"] = -1
107
108class HandshakeNoHello(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -0700109 """
110 TCP connect to switch, but do not sent hello,
111 and wait for disconnect.
112 """
Ken Chiangd6f5f862012-09-28 15:40:33 -0700113 def runTest(self):
114 self.controllerSetup(cxn_config["controller_host"],
115 cxn_config["controller_port"])
116
Rich Lane9a003812012-10-04 17:17:59 -0700117 logging.info("TCP Connected " +
Ken Chiangd6f5f862012-09-28 15:40:33 -0700118 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700119 logging.info("Hello not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700120
121 # wait for controller to die
122 count = 0
123 while self.controller.active and count < self.test_timeout:
124 time.sleep(1)
125 count = count + 1
126 self.assertTrue(not self.controller.active,
127 "Expected controller disconnect, but still active")
128
129class HandshakeNoFeaturesRequest(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -0700130 """
131 TCP connect to switch, send hello, but do not send features request,
132 and wait for disconnect.
133 """
Ken Chiangd6f5f862012-09-28 15:40:33 -0700134 def runTest(self):
135 self.controllerSetup(cxn_config["controller_host"],
136 cxn_config["controller_port"])
137
Rich Lane9a003812012-10-04 17:17:59 -0700138 logging.info("TCP Connected " +
Ken Chiangd6f5f862012-09-28 15:40:33 -0700139 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700140 logging.info("Sending hello")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700141 self.controller.message_send(message.hello())
142
Rich Lane9a003812012-10-04 17:17:59 -0700143 logging.info("Features request not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700144
145 # wait for controller to die
146 count = 0
147 while self.controller.active and count < self.test_timeout:
148 time.sleep(1)
149 count = count + 1
150 self.assertTrue(not self.controller.active,
151 "Expected controller disconnect, but still active")
152
Ken Chiang35a74372012-10-01 15:39:25 -0700153class HandshakeAndKeepalive(BaseHandshake):
154 """
155 Complete handshake and respond to echo request, but otherwise do nothing.
156 Good for manual testing.
157 """
158 def runTest(self):
159 self.controllerSetup(cxn_config["controller_host"],
160 cxn_config["controller_port"])
161
Rich Lane9a003812012-10-04 17:17:59 -0700162 logging.info("TCP Connected " +
Ken Chiang35a74372012-10-01 15:39:25 -0700163 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700164 logging.info("Sending hello")
Ken Chiang35a74372012-10-01 15:39:25 -0700165 self.controller.message_send(message.hello())
166
167 request = message.features_request()
168 reply, pkt = self.controller.transact(request, timeout=20)
169 self.assertTrue(reply is not None,
170 "Did not complete features_request for handshake")
Rich Lane9a003812012-10-04 17:17:59 -0700171 logging.info("Handshake complete with " +
Ken Chiang35a74372012-10-01 15:39:25 -0700172 str(self.controller.switch_addr))
173
174 self.controller.keep_alive = True
175
176 # keep controller up forever
177 while self.controller.active:
178 time.sleep(1)
179
180 self.assertTrue(not self.controller.active,
181 "Expected controller disconnect, but still active")
182
183test_prio["HandshakeAndKeepalive"] = -1
184