blob: 3b59bc12dc053aa01519075a903b1970e37f278c [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 Lane9fd05682013-01-10 15:30:38 -080015import ofp
Rich Laneb90a1c42012-10-05 09:16:05 -070016
Rich Lane69fd8e02013-08-23 16:23:42 -070017class BaseTest(unittest.TestCase):
18 def __str__(self):
19 return self.id().replace('.runTest', '')
20
21 def setUp(self):
22 oftest.open_logfile(str(self))
23 logging.info("** START TEST CASE " + str(self))
24
25 def tearDown(self):
26 logging.info("** END TEST CASE " + str(self))
27
28class SimpleProtocol(BaseTest):
Rich Laneb90a1c42012-10-05 09:16:05 -070029 """
30 Root class for setting up the controller
31 """
32
33 def setUp(self):
Rich Lane69fd8e02013-08-23 16:23:42 -070034 BaseTest.setUp(self)
35
Rich Laneb90a1c42012-10-05 09:16:05 -070036 self.controller = controller.Controller(
Dan Talayco69ca4d62012-11-15 11:50:22 -080037 switch=config["switch_ip"],
Rich Laneb90a1c42012-10-05 09:16:05 -070038 host=config["controller_host"],
39 port=config["controller_port"])
Rich Laneb90a1c42012-10-05 09:16:05 -070040 self.controller.start()
Rich Laneb90a1c42012-10-05 09:16:05 -070041
Rich Laned5915042012-12-31 14:58:35 -080042 try:
43 #@todo Add an option to wait for a pkt transaction to ensure version
44 # compatibilty?
45 self.controller.connect(timeout=20)
46
47 # By default, respond to echo requests
48 self.controller.keep_alive = True
49
50 if not self.controller.active:
51 raise Exception("Controller startup failed")
52 if self.controller.switch_addr is None:
53 raise Exception("Controller startup failed (no switch addr)")
54 logging.info("Connected " + str(self.controller.switch_addr))
Rich Lane78ef8b92013-01-10 12:19:23 -080055 request = ofp.message.features_request()
Rich Laned5915042012-12-31 14:58:35 -080056 reply, pkt = self.controller.transact(request)
57 self.assertTrue(reply is not None,
58 "Did not complete features_request for handshake")
Rich Laneb73808c2013-03-11 15:22:23 -070059 if reply.version == 1:
Rich Laneaf428152013-01-10 12:24:44 -080060 self.supported_actions = reply.actions
61 logging.info("Supported actions: " + hex(self.supported_actions))
Rich Laned5915042012-12-31 14:58:35 -080062 except:
63 self.controller.kill()
64 del self.controller
65 raise
Rich Laneb90a1c42012-10-05 09:16:05 -070066
67 def inheritSetup(self, parent):
68 """
69 Inherit the setup of a parent
70
71 This allows running at test from within another test. Do the
72 following:
73
74 sub_test = SomeTestClass() # Create an instance of the test class
75 sub_test.inheritSetup(self) # Inherit setup of parent
76 sub_test.runTest() # Run the test
77
78 Normally, only the parent's setUp and tearDown are called and
79 the state after the sub_test is run must be taken into account
80 by subsequent operations.
81 """
82 logging.info("** Setup " + str(self) + " inheriting from "
83 + str(parent))
84 self.controller = parent.controller
85 self.supported_actions = parent.supported_actions
86
87 def tearDown(self):
Rich Laneb90a1c42012-10-05 09:16:05 -070088 self.controller.shutdown()
Rich Lane7c64a422012-12-31 13:46:34 -080089 self.controller.join()
Rich Lanee7b0ecb2012-12-26 10:01:01 -080090 del self.controller
Rich Lane69fd8e02013-08-23 16:23:42 -070091 BaseTest.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -070092
Rich Laneb90a1c42012-10-05 09:16:05 -070093 def assertTrue(self, cond, msg):
94 if not cond:
95 logging.error("** FAILED ASSERTION: " + msg)
96 unittest.TestCase.assertTrue(self, cond, msg)
97
98class SimpleDataPlane(SimpleProtocol):
99 """
100 Root class that sets up the controller and dataplane
101 """
102 def setUp(self):
103 SimpleProtocol.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -0800104 self.dataplane = oftest.dataplane_instance
105 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700106
107 def inheritSetup(self, parent):
108 """
109 Inherit the setup of a parent
110
111 See SimpleProtocol.inheritSetup
112 """
113 SimpleProtocol.inheritSetup(self, parent)
114 self.dataplane = parent.dataplane
115
116 def tearDown(self):
Rich Laneb90a1c42012-10-05 09:16:05 -0700117 SimpleProtocol.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700118
Rich Lane69fd8e02013-08-23 16:23:42 -0700119class DataPlaneOnly(BaseTest):
Rich Laneb90a1c42012-10-05 09:16:05 -0700120 """
121 Root class that sets up only the dataplane
122 """
123
124 def setUp(self):
Rich Lane69fd8e02013-08-23 16:23:42 -0700125 BaseTest.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -0800126 self.dataplane = oftest.dataplane_instance
127 self.dataplane.flush()
Rich Laneb90a1c42012-10-05 09:16:05 -0700128
129 def tearDown(self):
Rich Lane69fd8e02013-08-23 16:23:42 -0700130 BaseTest.tearDown(self)