blob: 3f044fc03c3257356239818d2f3edf89dea6f1cd [file] [log] [blame]
Ken Chiangd6f5f862012-09-28 15:40:33 -07001"""
2Connection test cases
3
4"""
5
6import time
7import signal
8import sys
9import logging
10
11import unittest
12import random
13
14import 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
20from testutils import *
21
22#@var cxn_port_map Local copy of the configuration map from OF port
23# numbers to OS interfaces
24cxn_port_map = None
25#@var cxn_logger Local logger object
26cxn_logger = None
27#@var cxn_config Local copy of global configuration data
28cxn_config = None
29
30test_prio = {}
31
32def test_set_init(config):
33 """
34 Set up function for connection test classes
35
36 @param config The configuration dictionary; see oft
37 """
38
39 global cxn_port_map
40 global cxn_logger
41 global cxn_config
42
43 cxn_logger = logging.getLogger("cxn")
44 cxn_logger.info("Initializing test set")
45 cxn_port_map = config["port_map"]
46 cxn_config = config
47
48class BaseHandshake(unittest.TestCase):
49 """
50 Base handshake case to set up controller, but do not send hello.
51 """
52
53 def sig_handler(self, v1, v2):
54 cxn_logger.critical("Received interrupt signal; exiting")
55 print "Received interrupt signal; exiting"
56 self.clean_shutdown = False
57 self.tearDown()
58 sys.exit(1)
59
60 def controllerSetup(self, host, port):
61 self.controller = controller.Controller(host=host,port=port)
62
63 # clean_shutdown should be set to False to force quit app
64 self.clean_shutdown = True
65 # disable initial hello so hello is under control of test
66 self.controller.initial_hello = False
67
68 self.controller.start()
69 #@todo Add an option to wait for a pkt transaction to ensure version
70 # compatibilty?
71 self.controller.connect(timeout=10)
72 self.assertTrue(self.controller.active,
73 "Controller startup failed, not active")
74 self.assertTrue(self.controller.switch_addr is not None,
75 "Controller startup failed, no switch addr")
76
77 def setUp(self):
78 self.logger = cxn_logger
79 self.config = cxn_config
80 #@todo Test cases shouldn't monkey with signals; move SIGINT handler
81 # to top-level oft
82 try:
83 signal.signal(signal.SIGINT, self.sig_handler)
84 except ValueError, e:
85 cxn_logger.info("Could not set SIGINT handler: %s" % e)
86 cxn_logger.info("** START TEST CASE " + str(self))
87
88 self.test_timeout = test_param_get(cxn_config,
89 'handshake_timeout') or 60
90
91 def inheritSetup(self, parent):
92 """
93 Inherit the setup of a parent
94
95 This allows running at test from within another test. Do the
96 following:
97
98 sub_test = SomeTestClass() # Create an instance of the test class
99 sub_test.inheritSetup(self) # Inherit setup of parent
100 sub_test.runTest() # Run the test
101
102 Normally, only the parent's setUp and tearDown are called and
103 the state after the sub_test is run must be taken into account
104 by subsequent operations.
105 """
106 self.logger = parent.logger
107 self.config = parent.config
108 cxn_logger.info("** Setup " + str(self) +
109 " inheriting from " + str(parent))
110 self.controller = parent.controller
111
112 def tearDown(self):
113 cxn_logger.info("** END TEST CASE " + str(self))
114 self.controller.shutdown()
115 if self.clean_shutdown:
116 self.controller.join()
117
118 def runTest(self):
119 # do nothing in the base case
120 pass
121
122 def assertTrue(self, cond, msg):
123 if not cond:
124 cxn_logger.error("** FAILED ASSERTION: " + msg)
125 unittest.TestCase.assertTrue(self, cond, msg)
126
127test_prio["BaseHandshake"] = -1
128
129class HandshakeNoHello(BaseHandshake):
130 def runTest(self):
131 self.controllerSetup(cxn_config["controller_host"],
132 cxn_config["controller_port"])
133
134 cxn_logger.info("TCP Connected " +
135 str(self.controller.switch_addr))
136 cxn_logger.info("Hello not sent, waiting for timeout")
137
138 # wait for controller to die
139 count = 0
140 while self.controller.active and count < self.test_timeout:
141 time.sleep(1)
142 count = count + 1
143 self.assertTrue(not self.controller.active,
144 "Expected controller disconnect, but still active")
145
146class HandshakeNoFeaturesRequest(BaseHandshake):
147 def runTest(self):
148 self.controllerSetup(cxn_config["controller_host"],
149 cxn_config["controller_port"])
150
151 cxn_logger.info("TCP Connected " +
152 str(self.controller.switch_addr))
153 cxn_logger.info("Sending hello")
154 self.controller.message_send(message.hello())
155
156 cxn_logger.info("Features request not sent, waiting for timeout")
157
158 # wait for controller to die
159 count = 0
160 while self.controller.active and count < self.test_timeout:
161 time.sleep(1)
162 count = count + 1
163 self.assertTrue(not self.controller.active,
164 "Expected controller disconnect, but still active")
165