blob: 367a48a8c0c5b4fbf73425a07d1f91e3b65e2d53 [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
Rich Laneb90a1c42012-10-05 09:16:05 -070014import oftest.dataplane as dataplane
Rich Lane6242d9f2013-01-06 17:35:39 -080015import of10.message as message
Rich Laneb90a1c42012-10-05 09:16:05 -070016
17class SimpleProtocol(unittest.TestCase):
18 """
19 Root class for setting up the controller
20 """
21
22 def setUp(self):
23 logging.info("** START TEST CASE " + str(self))
24 self.controller = controller.Controller(
Dan Talayco69ca4d62012-11-15 11:50:22 -080025 switch=config["switch_ip"],
Rich Laneb90a1c42012-10-05 09:16:05 -070026 host=config["controller_host"],
27 port=config["controller_port"])
Rich Laneb90a1c42012-10-05 09:16:05 -070028 self.controller.start()
Rich Laneb90a1c42012-10-05 09:16:05 -070029
Rich Laned5915042012-12-31 14:58:35 -080030 try:
31 #@todo Add an option to wait for a pkt transaction to ensure version
32 # compatibilty?
33 self.controller.connect(timeout=20)
34
35 # By default, respond to echo requests
36 self.controller.keep_alive = True
37
38 if not self.controller.active:
39 raise Exception("Controller startup failed")
40 if self.controller.switch_addr is None:
41 raise Exception("Controller startup failed (no switch addr)")
42 logging.info("Connected " + str(self.controller.switch_addr))
43 request = message.features_request()
44 reply, pkt = self.controller.transact(request)
45 self.assertTrue(reply is not None,
46 "Did not complete features_request for handshake")
47 self.supported_actions = reply.actions
48 logging.info("Supported actions: " + hex(self.supported_actions))
49 except:
50 self.controller.kill()
51 del self.controller
52 raise
Rich Laneb90a1c42012-10-05 09:16:05 -070053
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()
Rich Lane7c64a422012-12-31 13:46:34 -080077 self.controller.join()
Rich Lanee7b0ecb2012-12-26 10:01:01 -080078 del self.controller
Rich Laneb90a1c42012-10-05 09:16:05 -070079
80 def runTest(self):
81 # Just a simple sanity check as illustration
82 logging.info("Running simple proto test")
83 self.assertTrue(self.controller.switch_socket is not None,
84 str(self) + 'No connection to switch')
85
86 def assertTrue(self, cond, msg):
87 if not cond:
88 logging.error("** FAILED ASSERTION: " + msg)
89 unittest.TestCase.assertTrue(self, cond, msg)
90
91class SimpleDataPlane(SimpleProtocol):
92 """
93 Root class that sets up the controller and dataplane
94 """
95 def setUp(self):
96 SimpleProtocol.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -080097 self.dataplane = oftest.dataplane_instance
98 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -070099
100 def inheritSetup(self, parent):
101 """
102 Inherit the setup of a parent
103
104 See SimpleProtocol.inheritSetup
105 """
106 SimpleProtocol.inheritSetup(self, parent)
107 self.dataplane = parent.dataplane
108
109 def tearDown(self):
110 logging.info("Teardown for simple dataplane test")
111 SimpleProtocol.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700112 logging.info("Teardown done")
113
114 def runTest(self):
115 self.assertTrue(self.controller.switch_socket is not None,
116 str(self) + 'No connection to switch')
117 # self.dataplane.show()
118 # Would like an assert that checks the data plane
119
120class DataPlaneOnly(unittest.TestCase):
121 """
122 Root class that sets up only the dataplane
123 """
124
125 def setUp(self):
Rich Laneb90a1c42012-10-05 09:16:05 -0700126 logging.info("** START DataPlaneOnly CASE " + str(self))
Rich Lane2c7812c2012-12-27 17:52:23 -0800127 self.dataplane = oftest.dataplane_instance
128 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700129
130 def tearDown(self):
131 logging.info("Teardown for simple dataplane test")
Rich Laneb90a1c42012-10-05 09:16:05 -0700132 logging.info("Teardown done")
133
134 def runTest(self):
135 logging.info("DataPlaneOnly")
136 # self.dataplane.show()
137 # Would like an assert that checks the data plane