blob: 38621afad06147fcdbafd18565fc9d4c8369fa62 [file] [log] [blame]
Rich Laneb90a1c42012-10-05 09:16:05 -07001"""
2Base classes for test cases
3
4Tests will usually inherit from one of these classes to have the controller
5and/or dataplane automatically set up.
6"""
7
8import logging
9import unittest
10
Rich Lane2c7812c2012-12-27 17:52:23 -080011import oftest
Rich Laneb90a1c42012-10-05 09:16:05 -070012from oftest import config
13import oftest.controller as controller
14import oftest.cstruct as ofp
15import oftest.message as message
16import oftest.dataplane as dataplane
17import oftest.action as action
18
19from oftest.testutils import *
20
21class SimpleProtocol(unittest.TestCase):
22 """
23 Root class for setting up the controller
24 """
25
26 def setUp(self):
27 logging.info("** START TEST CASE " + str(self))
28 self.controller = controller.Controller(
Dan Talayco69ca4d62012-11-15 11:50:22 -080029 switch=config["switch_ip"],
Rich Laneb90a1c42012-10-05 09:16:05 -070030 host=config["controller_host"],
31 port=config["controller_port"])
32 # clean_shutdown should be set to False to force quit app
33 self.clean_shutdown = True
34 self.controller.start()
35 #@todo Add an option to wait for a pkt transaction to ensure version
36 # compatibilty?
37 self.controller.connect(timeout=20)
38
39 # By default, respond to echo requests
40 self.controller.keep_alive = True
41
42 if not self.controller.active:
43 raise Exception("Controller startup failed")
44 if self.controller.switch_addr is None:
45 raise Exception("Controller startup failed (no switch addr)")
46 logging.info("Connected " + str(self.controller.switch_addr))
47 request = message.features_request()
48 reply, pkt = self.controller.transact(request)
49 self.assertTrue(reply is not None,
50 "Did not complete features_request for handshake")
51 self.supported_actions = reply.actions
52 logging.info("Supported actions: " + hex(self.supported_actions))
53
54 def inheritSetup(self, parent):
55 """
56 Inherit the setup of a parent
57
58 This allows running at test from within another test. Do the
59 following:
60
61 sub_test = SomeTestClass() # Create an instance of the test class
62 sub_test.inheritSetup(self) # Inherit setup of parent
63 sub_test.runTest() # Run the test
64
65 Normally, only the parent's setUp and tearDown are called and
66 the state after the sub_test is run must be taken into account
67 by subsequent operations.
68 """
69 logging.info("** Setup " + str(self) + " inheriting from "
70 + str(parent))
71 self.controller = parent.controller
72 self.supported_actions = parent.supported_actions
73
74 def tearDown(self):
75 logging.info("** END TEST CASE " + str(self))
76 self.controller.shutdown()
77 #@todo Review if join should be done on clean_shutdown
78 if self.clean_shutdown:
79 self.controller.join()
Rich Lanee7b0ecb2012-12-26 10:01:01 -080080 del self.controller
Rich Laneb90a1c42012-10-05 09:16:05 -070081
82 def runTest(self):
83 # Just a simple sanity check as illustration
84 logging.info("Running simple proto test")
85 self.assertTrue(self.controller.switch_socket is not None,
86 str(self) + 'No connection to switch')
87
88 def assertTrue(self, cond, msg):
89 if not cond:
90 logging.error("** FAILED ASSERTION: " + msg)
91 unittest.TestCase.assertTrue(self, cond, msg)
92
93class SimpleDataPlane(SimpleProtocol):
94 """
95 Root class that sets up the controller and dataplane
96 """
97 def setUp(self):
98 SimpleProtocol.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -080099 self.dataplane = oftest.dataplane_instance
100 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700101
102 def inheritSetup(self, parent):
103 """
104 Inherit the setup of a parent
105
106 See SimpleProtocol.inheritSetup
107 """
108 SimpleProtocol.inheritSetup(self, parent)
109 self.dataplane = parent.dataplane
110
111 def tearDown(self):
112 logging.info("Teardown for simple dataplane test")
113 SimpleProtocol.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700114 logging.info("Teardown done")
115
116 def runTest(self):
117 self.assertTrue(self.controller.switch_socket is not None,
118 str(self) + 'No connection to switch')
119 # self.dataplane.show()
120 # Would like an assert that checks the data plane
121
122class DataPlaneOnly(unittest.TestCase):
123 """
124 Root class that sets up only the dataplane
125 """
126
127 def setUp(self):
128 self.clean_shutdown = True
129 logging.info("** START DataPlaneOnly CASE " + str(self))
Rich Lane2c7812c2012-12-27 17:52:23 -0800130 self.dataplane = oftest.dataplane_instance
131 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700132
133 def tearDown(self):
134 logging.info("Teardown for simple dataplane test")
Rich Laneb90a1c42012-10-05 09:16:05 -0700135 logging.info("Teardown done")
136
137 def runTest(self):
138 logging.info("DataPlaneOnly")
139 # self.dataplane.show()
140 # Would like an assert that checks the data plane