blob: eeea564402591bc52348dd23701498fb85fea5e9 [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
28
Ken Chiangd6f5f862012-09-28 15:40:33 -070029 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 Lane9a003812012-10-04 17:17:59 -070047 logging.info("** START TEST CASE " + str(self))
Ken Chiangd6f5f862012-09-28 15:40:33 -070048
Rich Lane477f4812012-10-04 22:49:00 -070049 self.test_timeout = test_param_get(config,
Ken Chiangd6f5f862012-09-28 15:40:33 -070050 'handshake_timeout') or 60
51
52 def inheritSetup(self, parent):
53 """
54 Inherit the setup of a parent
55
56 This allows running at test from within another test. Do the
57 following:
58
59 sub_test = SomeTestClass() # Create an instance of the test class
60 sub_test.inheritSetup(self) # Inherit setup of parent
61 sub_test.runTest() # Run the test
62
63 Normally, only the parent's setUp and tearDown are called and
64 the state after the sub_test is run must be taken into account
65 by subsequent operations.
66 """
Rich Lane9a003812012-10-04 17:17:59 -070067 logging.info("** Setup " + str(self) +
Ken Chiangd6f5f862012-09-28 15:40:33 -070068 " inheriting from " + str(parent))
69 self.controller = parent.controller
70
71 def tearDown(self):
Rich Lane9a003812012-10-04 17:17:59 -070072 logging.info("** END TEST CASE " + str(self))
Ken Chiangd6f5f862012-09-28 15:40:33 -070073 self.controller.shutdown()
74 if self.clean_shutdown:
75 self.controller.join()
76
77 def runTest(self):
78 # do nothing in the base case
79 pass
80
81 def assertTrue(self, cond, msg):
82 if not cond:
Rich Lane9a003812012-10-04 17:17:59 -070083 logging.error("** FAILED ASSERTION: " + msg)
Ken Chiangd6f5f862012-09-28 15:40:33 -070084 unittest.TestCase.assertTrue(self, cond, msg)
85
Ken Chiangd6f5f862012-09-28 15:40:33 -070086class HandshakeNoHello(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070087 """
88 TCP connect to switch, but do not sent hello,
89 and wait for disconnect.
90 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070091 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070092 self.controllerSetup(config["controller_host"],
93 config["controller_port"])
Ken Chiangd6f5f862012-09-28 15:40:33 -070094
Rich Lane9a003812012-10-04 17:17:59 -070095 logging.info("TCP Connected " +
Ken Chiangd6f5f862012-09-28 15:40:33 -070096 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070097 logging.info("Hello not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070098
99 # wait for controller to die
100 count = 0
101 while self.controller.active and count < self.test_timeout:
102 time.sleep(1)
103 count = count + 1
104 self.assertTrue(not self.controller.active,
105 "Expected controller disconnect, but still active")
106
107class HandshakeNoFeaturesRequest(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -0700108 """
109 TCP connect to switch, send hello, but do not send features request,
110 and wait for disconnect.
111 """
Ken Chiangd6f5f862012-09-28 15:40:33 -0700112 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -0700113 self.controllerSetup(config["controller_host"],
114 config["controller_port"])
Ken Chiangd6f5f862012-09-28 15:40:33 -0700115
Rich Lane9a003812012-10-04 17:17:59 -0700116 logging.info("TCP Connected " +
Ken Chiangd6f5f862012-09-28 15:40:33 -0700117 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700118 logging.info("Sending hello")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700119 self.controller.message_send(message.hello())
120
Rich Lane9a003812012-10-04 17:17:59 -0700121 logging.info("Features request not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700122
123 # wait for controller to die
124 count = 0
125 while self.controller.active and count < self.test_timeout:
126 time.sleep(1)
127 count = count + 1
128 self.assertTrue(not self.controller.active,
129 "Expected controller disconnect, but still active")
130
Ken Chiang35a74372012-10-01 15:39:25 -0700131class HandshakeAndKeepalive(BaseHandshake):
132 """
133 Complete handshake and respond to echo request, but otherwise do nothing.
134 Good for manual testing.
135 """
Rich Laned1d9c282012-10-04 22:07:10 -0700136
137 priority = -1
138
Ken Chiang35a74372012-10-01 15:39:25 -0700139 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -0700140 self.controllerSetup(config["controller_host"],
141 config["controller_port"])
Ken Chiang35a74372012-10-01 15:39:25 -0700142
Rich Lane9a003812012-10-04 17:17:59 -0700143 logging.info("TCP Connected " +
Ken Chiang35a74372012-10-01 15:39:25 -0700144 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700145 logging.info("Sending hello")
Ken Chiang35a74372012-10-01 15:39:25 -0700146 self.controller.message_send(message.hello())
147
148 request = message.features_request()
149 reply, pkt = self.controller.transact(request, timeout=20)
150 self.assertTrue(reply is not None,
151 "Did not complete features_request for handshake")
Rich Lane9a003812012-10-04 17:17:59 -0700152 logging.info("Handshake complete with " +
Ken Chiang35a74372012-10-01 15:39:25 -0700153 str(self.controller.switch_addr))
154
155 self.controller.keep_alive = True
156
157 # keep controller up forever
158 while self.controller.active:
159 time.sleep(1)
160
161 self.assertTrue(not self.controller.active,
162 "Expected controller disconnect, but still active")
163