blob: da15c9e7daeb7986b89d01c636df584fe3eacb30 [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 Lane2014f9b2012-10-05 15:29:40 -070049 self.test_timeout = test_param_get('handshake_timeout', default=60)
Ken Chiangd6f5f862012-09-28 15:40:33 -070050
51 def inheritSetup(self, parent):
52 """
53 Inherit the setup of a parent
54
55 This allows running at test from within another test. Do the
56 following:
57
58 sub_test = SomeTestClass() # Create an instance of the test class
59 sub_test.inheritSetup(self) # Inherit setup of parent
60 sub_test.runTest() # Run the test
61
62 Normally, only the parent's setUp and tearDown are called and
63 the state after the sub_test is run must be taken into account
64 by subsequent operations.
65 """
Rich Lane9a003812012-10-04 17:17:59 -070066 logging.info("** Setup " + str(self) +
Ken Chiangd6f5f862012-09-28 15:40:33 -070067 " inheriting from " + str(parent))
68 self.controller = parent.controller
69
70 def tearDown(self):
Rich Lane9a003812012-10-04 17:17:59 -070071 logging.info("** END TEST CASE " + str(self))
Ken Chiangd6f5f862012-09-28 15:40:33 -070072 self.controller.shutdown()
73 if self.clean_shutdown:
74 self.controller.join()
75
76 def runTest(self):
77 # do nothing in the base case
78 pass
79
80 def assertTrue(self, cond, msg):
81 if not cond:
Rich Lane9a003812012-10-04 17:17:59 -070082 logging.error("** FAILED ASSERTION: " + msg)
Ken Chiangd6f5f862012-09-28 15:40:33 -070083 unittest.TestCase.assertTrue(self, cond, msg)
84
Ken Chiangd6f5f862012-09-28 15:40:33 -070085class HandshakeNoHello(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -070086 """
87 TCP connect to switch, but do not sent hello,
88 and wait for disconnect.
89 """
Ken Chiangd6f5f862012-09-28 15:40:33 -070090 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -070091 self.controllerSetup(config["controller_host"],
92 config["controller_port"])
Ken Chiangd6f5f862012-09-28 15:40:33 -070093
Rich Lane9a003812012-10-04 17:17:59 -070094 logging.info("TCP Connected " +
Ken Chiangd6f5f862012-09-28 15:40:33 -070095 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -070096 logging.info("Hello not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -070097
98 # wait for controller to die
99 count = 0
100 while self.controller.active and count < self.test_timeout:
101 time.sleep(1)
102 count = count + 1
103 self.assertTrue(not self.controller.active,
104 "Expected controller disconnect, but still active")
105
106class HandshakeNoFeaturesRequest(BaseHandshake):
Ken Chiang35a74372012-10-01 15:39:25 -0700107 """
108 TCP connect to switch, send hello, but do not send features request,
109 and wait for disconnect.
110 """
Ken Chiangd6f5f862012-09-28 15:40:33 -0700111 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -0700112 self.controllerSetup(config["controller_host"],
113 config["controller_port"])
Ken Chiangd6f5f862012-09-28 15:40:33 -0700114
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("Sending hello")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700118 self.controller.message_send(message.hello())
119
Rich Lane9a003812012-10-04 17:17:59 -0700120 logging.info("Features request not sent, waiting for timeout")
Ken Chiangd6f5f862012-09-28 15:40:33 -0700121
122 # wait for controller to die
123 count = 0
124 while self.controller.active and count < self.test_timeout:
125 time.sleep(1)
126 count = count + 1
127 self.assertTrue(not self.controller.active,
128 "Expected controller disconnect, but still active")
129
Ken Chiang35a74372012-10-01 15:39:25 -0700130class HandshakeAndKeepalive(BaseHandshake):
131 """
132 Complete handshake and respond to echo request, but otherwise do nothing.
133 Good for manual testing.
134 """
Rich Laned1d9c282012-10-04 22:07:10 -0700135
136 priority = -1
137
Ken Chiang35a74372012-10-01 15:39:25 -0700138 def runTest(self):
Rich Lane477f4812012-10-04 22:49:00 -0700139 self.controllerSetup(config["controller_host"],
140 config["controller_port"])
Ken Chiang35a74372012-10-01 15:39:25 -0700141
Rich Lane9a003812012-10-04 17:17:59 -0700142 logging.info("TCP Connected " +
Ken Chiang35a74372012-10-01 15:39:25 -0700143 str(self.controller.switch_addr))
Rich Lane9a003812012-10-04 17:17:59 -0700144 logging.info("Sending hello")
Ken Chiang35a74372012-10-01 15:39:25 -0700145 self.controller.message_send(message.hello())
146
147 request = message.features_request()
148 reply, pkt = self.controller.transact(request, timeout=20)
149 self.assertTrue(reply is not None,
150 "Did not complete features_request for handshake")
Rich Lane9a003812012-10-04 17:17:59 -0700151 logging.info("Handshake complete with " +
Ken Chiang35a74372012-10-01 15:39:25 -0700152 str(self.controller.switch_addr))
153
154 self.controller.keep_alive = True
155
156 # keep controller up forever
157 while self.controller.active:
158 time.sleep(1)
159
160 self.assertTrue(not self.controller.active,
161 "Expected controller disconnect, but still active")
162