blob: d3113919984d85c3c4b16e9a5df033cab9bdbc57 [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
Rich Lane477f4812012-10-04 22:49:00 -070013from oftest import config
Ken Chiangd6f5f862012-09-28 15:40:33 -070014import oftest.controller as controller
15import oftest.cstruct as ofp
16import oftest.message as message
17import oftest.dataplane as dataplane
18import oftest.action as action
19
Rich Laneda3b5ad2012-10-03 09:05:32 -070020from oftest.testutils import *
Ken Chiangd6f5f862012-09-28 15:40:33 -070021
Ken Chiangd6f5f862012-09-28 15:40:33 -070022class BaseHandshake(unittest.TestCase):
23 """
24 Base handshake case to set up controller, but do not send hello.
25 """
26
Rich Laned1d9c282012-10-04 22:07:10 -070027 priority = -1
Ken Chiangadc950f2012-10-05 13:50:03 -070028 controllers = []
29 default_timeout = 2
30
Ken Chiangd6f5f862012-09-28 15:40:33 -070031 def controllerSetup(self, host, port):
Ken Chiangadc950f2012-10-05 13:50:03 -070032 con = controller.Controller(host=host,port=port)
Ken Chiangd6f5f862012-09-28 15:40:33 -070033
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 Chiangadc950f2012-10-05 13:50:03 -070037 con.initial_hello = False
Ken Chiangd6f5f862012-09-28 15:40:33 -070038
Ken Chiangadc950f2012-10-05 13:50:03 -070039 con.start()
40 self.controllers.append(con)
Ken Chiangd6f5f862012-09-28 15:40:33 -070041
42 def setUp(self):
Rich Lane9a003812012-10-04 17:17:59 -070043 logging.info("** START TEST CASE " + str(self))
Ken Chiangd6f5f862012-09-28 15:40:33 -070044
Ken Chiange875baf2012-10-09 15:24:40 -070045 self.default_timeout = test_param_get('default_timeout',
46 default=2)
Ken Chiangd6f5f862012-09-28 15:40:33 -070047
Ken Chiangd6f5f862012-09-28 15:40:33 -070048 def tearDown(self):
Rich Lane9a003812012-10-04 17:17:59 -070049 logging.info("** END TEST CASE " + str(self))
Ken Chiangadc950f2012-10-05 13:50:03 -070050 for con in self.controllers:
51 con.shutdown()
52 if self.clean_shutdown:
53 con.join()
Ken Chiangd6f5f862012-09-28 15:40:33 -070054
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 Lane9a003812012-10-04 17:17:59 -070061 logging.error("** FAILED ASSERTION: " + msg)
Ken Chiangd6f5f862012-09-28 15:40:33 -070062 unittest.TestCase.assertTrue(self, cond, msg)
63
Ken Chiangd6f5f862012-09-28 15:40:33 -070064class HandshakeNoHello(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070065 """
66 TCP connect to switch, but do not sent hello,
67 and wait for disconnect.
68 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070069 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070070 self.controllerSetup(config["controller_host"],
71 config["controller_port"])
Ken Chiangadc950f2012-10-05 13:50:03 -070072 self.controllers[0].connect(self.default_timeout)
Ken Chiangd6f5f862012-09-28 15:40:33 -070073
Rich Lane9a003812012-10-04 17:17:59 -070074 logging.info("TCP Connected " +
Ken Chiange875baf2012-10-09 15:24:40 -070075 str(self.controllers[0].switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070076 logging.info("Hello not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070077
78 # wait for controller to die
79 count = 0
Ken Chiangadc950f2012-10-05 13:50:03 -070080 self.assertTrue(self.controllers[0].wait_disconnected(timeout=10),
81 "Not notified of controller disconnect")
Ken Chiangd6f5f862012-09-28 15:40:33 -070082
83class HandshakeNoFeaturesRequest(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070084 """
85 TCP connect to switch, send hello, but do not send features request,
86 and wait for disconnect.
87 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070088 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070089 self.controllerSetup(config["controller_host"],
90 config["controller_port"])
Ken Chiangadc950f2012-10-05 13:50:03 -070091 self.controllers[0].connect(self.default_timeout)
Ken Chiangd6f5f862012-09-28 15:40:33 -070092
Rich Lane9a003812012-10-04 17:17:59 -070093 logging.info("TCP Connected " +
Ken Chiange875baf2012-10-09 15:24:40 -070094 str(self.controllers[0].switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070095 logging.info("Sending hello")
Ken Chiangadc950f2012-10-05 13:50:03 -070096 self.controllers[0].message_send(message.hello())
Ken Chiangd6f5f862012-09-28 15:40:33 -070097
Rich Lane9a003812012-10-04 17:17:59 -070098 logging.info("Features request not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070099
100 # wait for controller to die
101 count = 0
Ken Chiangadc950f2012-10-05 13:50:03 -0700102 self.assertTrue(self.controllers[0].wait_disconnected(timeout=10),
103 "Not notified of controller disconnect")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700104
Ken Chiang35a74372012-10-01 15:39:25 -0700105class HandshakeAndKeepalive(BaseHandshake):
106 """
107 Complete handshake and respond to echo request, but otherwise do nothing.
108 Good for manual testing.
109 """
Rich Laned1d9c282012-10-04 22:07:10 -0700110
111 priority = -1
112
Ken Chiang35a74372012-10-01 15:39:25 -0700113 def runTest(self):
Ken Chiange875baf2012-10-09 15:24:40 -0700114 self.num_controllers = test_param_get('num_controllers', default=1)
Ken Chiang35a74372012-10-01 15:39:25 -0700115
Ken Chiangadc950f2012-10-05 13:50:03 -0700116 for i in range(self.num_controllers):
Ken Chiange875baf2012-10-09 15:24:40 -0700117 self.controllerSetup(config["controller_host"],
118 config["controller_port"]+i)
Ken Chiangadc950f2012-10-05 13:50:03 -0700119 for i in range(self.num_controllers):
120 self.controllers[i].handshake_done = False
Ken Chiang35a74372012-10-01 15:39:25 -0700121
Ken Chiangadc950f2012-10-05 13:50:03 -0700122 # try to maintain switch connections forever
123 count = 0
124 while True:
125 for con in self.controllers:
126 if con.switch_socket and con.handshake_done:
127 if count < 7:
Ken Chiange875baf2012-10-09 15:24:40 -0700128 logging.info(con.host + ":" + str(con.port) +
129 ": maintaining connection to " +
130 str(con.switch_addr))
Ken Chiangadc950f2012-10-05 13:50:03 -0700131 count = count + 1
132 else:
Ken Chiange875baf2012-10-09 15:24:40 -0700133 logging.info(con.host + ":" + str(con.port) +
134 ": disconnecting from " +
135 str(con.switch_addr))
Ken Chiangadc950f2012-10-05 13:50:03 -0700136 con.disconnect()
137 con.handshake_done = False
138 count = 0
139 time.sleep(1)
140 else:
141 #@todo Add an option to wait for a pkt transaction to
142 # ensure version compatibilty?
143 con.connect(self.default_timeout)
144 if not con.switch_socket:
Ken Chiange875baf2012-10-09 15:24:40 -0700145 logging.info("Did not connect to switch")
Ken Chiangadc950f2012-10-05 13:50:03 -0700146 continue
Ken Chiange875baf2012-10-09 15:24:40 -0700147 logging.info("TCP Connected " + str(con.switch_addr))
148 logging.info("Sending hello")
Ken Chiangadc950f2012-10-05 13:50:03 -0700149 con.message_send(message.hello())
150 request = message.features_request()
151 reply, pkt = con.transact(request,
152 timeout=self.default_timeout)
153 if reply:
Ken Chiange875baf2012-10-09 15:24:40 -0700154 logging.info("Handshake complete with " +
155 str(con.switch_addr))
Ken Chiangadc950f2012-10-05 13:50:03 -0700156 con.handshake_done = True
157 con.keep_alive = True
158 else:
Ken Chiange875baf2012-10-09 15:24:40 -0700159 logging.info("Did not complete features_request " +
160 "for handshake")
Ken Chiangadc950f2012-10-05 13:50:03 -0700161 con.disconnect()
162 con.handshake_done = False
Ken Chiang35a74372012-10-01 15:39:25 -0700163