blob: f91ae6033e0e4a47466804a2891637b867db2a2a [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"])
Rich Laneb90a1c42012-10-05 09:16:05 -070032 self.controller.start()
Rich Laneb90a1c42012-10-05 09:16:05 -070033
Rich Laned5915042012-12-31 14:58:35 -080034 try:
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 except:
54 self.controller.kill()
55 del self.controller
56 raise
Rich Laneb90a1c42012-10-05 09:16:05 -070057
58 def inheritSetup(self, parent):
59 """
60 Inherit the setup of a parent
61
62 This allows running at test from within another test. Do the
63 following:
64
65 sub_test = SomeTestClass() # Create an instance of the test class
66 sub_test.inheritSetup(self) # Inherit setup of parent
67 sub_test.runTest() # Run the test
68
69 Normally, only the parent's setUp and tearDown are called and
70 the state after the sub_test is run must be taken into account
71 by subsequent operations.
72 """
73 logging.info("** Setup " + str(self) + " inheriting from "
74 + str(parent))
75 self.controller = parent.controller
76 self.supported_actions = parent.supported_actions
77
78 def tearDown(self):
79 logging.info("** END TEST CASE " + str(self))
80 self.controller.shutdown()
Rich Lane7c64a422012-12-31 13:46:34 -080081 self.controller.join()
Rich Lanee7b0ecb2012-12-26 10:01:01 -080082 del self.controller
Rich Laneb90a1c42012-10-05 09:16:05 -070083
84 def runTest(self):
85 # Just a simple sanity check as illustration
86 logging.info("Running simple proto test")
87 self.assertTrue(self.controller.switch_socket is not None,
88 str(self) + 'No connection to switch')
89
90 def assertTrue(self, cond, msg):
91 if not cond:
92 logging.error("** FAILED ASSERTION: " + msg)
93 unittest.TestCase.assertTrue(self, cond, msg)
94
95class SimpleDataPlane(SimpleProtocol):
96 """
97 Root class that sets up the controller and dataplane
98 """
99 def setUp(self):
100 SimpleProtocol.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -0800101 self.dataplane = oftest.dataplane_instance
102 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700103
104 def inheritSetup(self, parent):
105 """
106 Inherit the setup of a parent
107
108 See SimpleProtocol.inheritSetup
109 """
110 SimpleProtocol.inheritSetup(self, parent)
111 self.dataplane = parent.dataplane
112
113 def tearDown(self):
114 logging.info("Teardown for simple dataplane test")
115 SimpleProtocol.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700116 logging.info("Teardown done")
117
118 def runTest(self):
119 self.assertTrue(self.controller.switch_socket is not None,
120 str(self) + 'No connection to switch')
121 # self.dataplane.show()
122 # Would like an assert that checks the data plane
123
124class DataPlaneOnly(unittest.TestCase):
125 """
126 Root class that sets up only the dataplane
127 """
128
129 def setUp(self):
Rich Laneb90a1c42012-10-05 09:16:05 -0700130 logging.info("** START DataPlaneOnly CASE " + str(self))
Rich Lane2c7812c2012-12-27 17:52:23 -0800131 self.dataplane = oftest.dataplane_instance
132 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700133
134 def tearDown(self):
135 logging.info("Teardown for simple dataplane test")
Rich Laneb90a1c42012-10-05 09:16:05 -0700136 logging.info("Teardown done")
137
138 def runTest(self):
139 logging.info("DataPlaneOnly")
140 # self.dataplane.show()
141 # Would like an assert that checks the data plane