blob: 5524434d020ed4cf069bb41d00f3c066c326b072 [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
Ken Chiangadc950f2012-10-05 13:50:03 -070079 self.assertTrue(self.controllers[0].wait_disconnected(timeout=10),
80 "Not notified of controller disconnect")
Ken Chiangd6f5f862012-09-28 15:40:33 -070081
82class HandshakeNoFeaturesRequest(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070083 """
84 TCP connect to switch, send hello, but do not send features request,
85 and wait for disconnect.
86 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070087 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070088 self.controllerSetup(config["controller_host"],
89 config["controller_port"])
Ken Chiangadc950f2012-10-05 13:50:03 -070090 self.controllers[0].connect(self.default_timeout)
Ken Chiangd6f5f862012-09-28 15:40:33 -070091
Rich Lane9a003812012-10-04 17:17:59 -070092 logging.info("TCP Connected " +
Ken Chiange875baf2012-10-09 15:24:40 -070093 str(self.controllers[0].switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070094 logging.info("Sending hello")
Ken Chiangadc950f2012-10-05 13:50:03 -070095 self.controllers[0].message_send(message.hello())
Ken Chiangd6f5f862012-09-28 15:40:33 -070096
Rich Lane9a003812012-10-04 17:17:59 -070097 logging.info("Features request not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070098
99 # wait for controller to die
Ken Chiangadc950f2012-10-05 13:50:03 -0700100 self.assertTrue(self.controllers[0].wait_disconnected(timeout=10),
101 "Not notified of controller disconnect")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700102
Ken Chiang35a74372012-10-01 15:39:25 -0700103class HandshakeAndKeepalive(BaseHandshake):
104 """
105 Complete handshake and respond to echo request, but otherwise do nothing.
106 Good for manual testing.
107 """
Rich Laned1d9c282012-10-04 22:07:10 -0700108
109 priority = -1
110
Ken Chiang35a74372012-10-01 15:39:25 -0700111 def runTest(self):
Ken Chiange875baf2012-10-09 15:24:40 -0700112 self.num_controllers = test_param_get('num_controllers', default=1)
Ken Chiang265fc492012-10-09 16:16:18 -0700113 self.controller_timeout = test_param_get('controller_timeout',
114 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 Chiang265fc492012-10-09 16:16:18 -0700122 # try to maintain switch connections for specified timeout
123 # -1 means forever
Ken Chiangadc950f2012-10-05 13:50:03 -0700124 while True:
125 for con in self.controllers:
126 if con.switch_socket and con.handshake_done:
Ken Chiang265fc492012-10-09 16:16:18 -0700127 if (self.controller_timeout < 0 or
128 con.count < self.controller_timeout):
Ken Chiange875baf2012-10-09 15:24:40 -0700129 logging.info(con.host + ":" + str(con.port) +
130 ": maintaining connection to " +
131 str(con.switch_addr))
Ken Chiang265fc492012-10-09 16:16:18 -0700132 con.count = con.count + 1
Ken Chiangadc950f2012-10-05 13:50:03 -0700133 else:
Ken Chiange875baf2012-10-09 15:24:40 -0700134 logging.info(con.host + ":" + str(con.port) +
135 ": disconnecting from " +
136 str(con.switch_addr))
Ken Chiangadc950f2012-10-05 13:50:03 -0700137 con.disconnect()
138 con.handshake_done = False
Ken Chiang265fc492012-10-09 16:16:18 -0700139 con.count = 0
Ken Chiangadc950f2012-10-05 13:50:03 -0700140 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 Chiange875baf2012-10-09 15:24:40 -0700146 logging.info("Did not connect to switch")
Ken Chiangadc950f2012-10-05 13:50:03 -0700147 continue
Ken Chiange875baf2012-10-09 15:24:40 -0700148 logging.info("TCP Connected " + str(con.switch_addr))
149 logging.info("Sending hello")
Ken Chiangadc950f2012-10-05 13:50:03 -0700150 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 Chiange875baf2012-10-09 15:24:40 -0700155 logging.info("Handshake complete with " +
156 str(con.switch_addr))
Ken Chiangadc950f2012-10-05 13:50:03 -0700157 con.handshake_done = True
158 con.keep_alive = True
Ken Chiang265fc492012-10-09 16:16:18 -0700159 con.count = 0
Ken Chiangadc950f2012-10-05 13:50:03 -0700160 else:
Ken Chiange875baf2012-10-09 15:24:40 -0700161 logging.info("Did not complete features_request " +
162 "for handshake")
Ken Chiangadc950f2012-10-05 13:50:03 -0700163 con.disconnect()
164 con.handshake_done = False
Ken Chiang265fc492012-10-09 16:16:18 -0700165 con.count = 0
Ken Chiang35a74372012-10-01 15:39:25 -0700166