blob: 6ae6fb80b83595523c1726ad9fcc402f15a9823a [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()
33 #@todo Add an option to wait for a pkt transaction to ensure version
34 # compatibilty?
35 self.controller.connect(timeout=20)
36
37 # By default, respond to echo requests
38 self.controller.keep_alive = True
39
40 if not self.controller.active:
41 raise Exception("Controller startup failed")
42 if self.controller.switch_addr is None:
43 raise Exception("Controller startup failed (no switch addr)")
44 logging.info("Connected " + str(self.controller.switch_addr))
45 request = message.features_request()
46 reply, pkt = self.controller.transact(request)
47 self.assertTrue(reply is not None,
48 "Did not complete features_request for handshake")
49 self.supported_actions = reply.actions
50 logging.info("Supported actions: " + hex(self.supported_actions))
51
52 def inheritSetup(self, parent):
53 """
54 Inherit the setup of a parent
55
56 This allows running at test from within another test. Do the
57 following:
58
59 sub_test = SomeTestClass() # Create an instance of the test class
60 sub_test.inheritSetup(self) # Inherit setup of parent
61 sub_test.runTest() # Run the test
62
63 Normally, only the parent's setUp and tearDown are called and
64 the state after the sub_test is run must be taken into account
65 by subsequent operations.
66 """
67 logging.info("** Setup " + str(self) + " inheriting from "
68 + str(parent))
69 self.controller = parent.controller
70 self.supported_actions = parent.supported_actions
71
72 def tearDown(self):
73 logging.info("** END TEST CASE " + str(self))
74 self.controller.shutdown()
Rich Lane7c64a422012-12-31 13:46:34 -080075 self.controller.join()
Rich Lanee7b0ecb2012-12-26 10:01:01 -080076 del self.controller
Rich Laneb90a1c42012-10-05 09:16:05 -070077
78 def runTest(self):
79 # Just a simple sanity check as illustration
80 logging.info("Running simple proto test")
81 self.assertTrue(self.controller.switch_socket is not None,
82 str(self) + 'No connection to switch')
83
84 def assertTrue(self, cond, msg):
85 if not cond:
86 logging.error("** FAILED ASSERTION: " + msg)
87 unittest.TestCase.assertTrue(self, cond, msg)
88
89class SimpleDataPlane(SimpleProtocol):
90 """
91 Root class that sets up the controller and dataplane
92 """
93 def setUp(self):
94 SimpleProtocol.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -080095 self.dataplane = oftest.dataplane_instance
96 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -070097
98 def inheritSetup(self, parent):
99 """
100 Inherit the setup of a parent
101
102 See SimpleProtocol.inheritSetup
103 """
104 SimpleProtocol.inheritSetup(self, parent)
105 self.dataplane = parent.dataplane
106
107 def tearDown(self):
108 logging.info("Teardown for simple dataplane test")
109 SimpleProtocol.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700110 logging.info("Teardown done")
111
112 def runTest(self):
113 self.assertTrue(self.controller.switch_socket is not None,
114 str(self) + 'No connection to switch')
115 # self.dataplane.show()
116 # Would like an assert that checks the data plane
117
118class DataPlaneOnly(unittest.TestCase):
119 """
120 Root class that sets up only the dataplane
121 """
122
123 def setUp(self):
Rich Laneb90a1c42012-10-05 09:16:05 -0700124 logging.info("** START DataPlaneOnly CASE " + str(self))
Rich Lane2c7812c2012-12-27 17:52:23 -0800125 self.dataplane = oftest.dataplane_instance
126 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700127
128 def tearDown(self):
129 logging.info("Teardown for simple dataplane test")
Rich Laneb90a1c42012-10-05 09:16:05 -0700130 logging.info("Teardown done")
131
132 def runTest(self):
133 logging.info("DataPlaneOnly")
134 # self.dataplane.show()
135 # Would like an assert that checks the data plane