blob: 056f6e94e0eb383bda9c2cfad5e569e917ff6bb3 [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 -070016from oftest.testutils import *
17
18class SimpleProtocol(unittest.TestCase):
19 """
20 Root class for setting up the controller
21 """
22
23 def setUp(self):
24 logging.info("** START TEST CASE " + str(self))
25 self.controller = controller.Controller(
Dan Talayco69ca4d62012-11-15 11:50:22 -080026 switch=config["switch_ip"],
Rich Laneb90a1c42012-10-05 09:16:05 -070027 host=config["controller_host"],
28 port=config["controller_port"])
Rich Laneb90a1c42012-10-05 09:16:05 -070029 self.controller.start()
Rich Laneb90a1c42012-10-05 09:16:05 -070030
Rich Laned5915042012-12-31 14:58:35 -080031 try:
32 #@todo Add an option to wait for a pkt transaction to ensure version
33 # compatibilty?
34 self.controller.connect(timeout=20)
35
36 # By default, respond to echo requests
37 self.controller.keep_alive = True
38
39 if not self.controller.active:
40 raise Exception("Controller startup failed")
41 if self.controller.switch_addr is None:
42 raise Exception("Controller startup failed (no switch addr)")
43 logging.info("Connected " + str(self.controller.switch_addr))
44 request = message.features_request()
45 reply, pkt = self.controller.transact(request)
46 self.assertTrue(reply is not None,
47 "Did not complete features_request for handshake")
48 self.supported_actions = reply.actions
49 logging.info("Supported actions: " + hex(self.supported_actions))
50 except:
51 self.controller.kill()
52 del self.controller
53 raise
Rich Laneb90a1c42012-10-05 09:16:05 -070054
55 def inheritSetup(self, parent):
56 """
57 Inherit the setup of a parent
58
59 This allows running at test from within another test. Do the
60 following:
61
62 sub_test = SomeTestClass() # Create an instance of the test class
63 sub_test.inheritSetup(self) # Inherit setup of parent
64 sub_test.runTest() # Run the test
65
66 Normally, only the parent's setUp and tearDown are called and
67 the state after the sub_test is run must be taken into account
68 by subsequent operations.
69 """
70 logging.info("** Setup " + str(self) + " inheriting from "
71 + str(parent))
72 self.controller = parent.controller
73 self.supported_actions = parent.supported_actions
74
75 def tearDown(self):
76 logging.info("** END TEST CASE " + str(self))
77 self.controller.shutdown()
Rich Lane7c64a422012-12-31 13:46:34 -080078 self.controller.join()
Rich Lanee7b0ecb2012-12-26 10:01:01 -080079 del self.controller
Rich Laneb90a1c42012-10-05 09:16:05 -070080
81 def runTest(self):
82 # Just a simple sanity check as illustration
83 logging.info("Running simple proto test")
84 self.assertTrue(self.controller.switch_socket is not None,
85 str(self) + 'No connection to switch')
86
87 def assertTrue(self, cond, msg):
88 if not cond:
89 logging.error("** FAILED ASSERTION: " + msg)
90 unittest.TestCase.assertTrue(self, cond, msg)
91
92class SimpleDataPlane(SimpleProtocol):
93 """
94 Root class that sets up the controller and dataplane
95 """
96 def setUp(self):
97 SimpleProtocol.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -080098 self.dataplane = oftest.dataplane_instance
99 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700100
101 def inheritSetup(self, parent):
102 """
103 Inherit the setup of a parent
104
105 See SimpleProtocol.inheritSetup
106 """
107 SimpleProtocol.inheritSetup(self, parent)
108 self.dataplane = parent.dataplane
109
110 def tearDown(self):
111 logging.info("Teardown for simple dataplane test")
112 SimpleProtocol.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700113 logging.info("Teardown done")
114
115 def runTest(self):
116 self.assertTrue(self.controller.switch_socket is not None,
117 str(self) + 'No connection to switch')
118 # self.dataplane.show()
119 # Would like an assert that checks the data plane
120
121class DataPlaneOnly(unittest.TestCase):
122 """
123 Root class that sets up only the dataplane
124 """
125
126 def setUp(self):
Rich Laneb90a1c42012-10-05 09:16:05 -0700127 logging.info("** START DataPlaneOnly CASE " + str(self))
Rich Lane2c7812c2012-12-27 17:52:23 -0800128 self.dataplane = oftest.dataplane_instance
129 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700130
131 def tearDown(self):
132 logging.info("Teardown for simple dataplane test")
Rich Laneb90a1c42012-10-05 09:16:05 -0700133 logging.info("Teardown done")
134
135 def runTest(self):
136 logging.info("DataPlaneOnly")
137 # self.dataplane.show()
138 # Would like an assert that checks the data plane