blob: 7eb68bcfc282bb29e0483d6484b0e14ccb0d5186 [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Rich Laneb90a1c42012-10-05 09:16:05 -070017"""
18Base classes for test cases
19
20Tests will usually inherit from one of these classes to have the controller
21and/or dataplane automatically set up.
22"""
23
24import logging
25import unittest
Rich Lane472aaea2013-08-27 09:27:38 -070026import os
Rich Laneb90a1c42012-10-05 09:16:05 -070027
Rich Lane2c7812c2012-12-27 17:52:23 -080028import oftest
Rich Laneb90a1c42012-10-05 09:16:05 -070029from oftest import config
30import oftest.controller as controller
Rich Laneb90a1c42012-10-05 09:16:05 -070031import oftest.dataplane as dataplane
Rich Lane9fd05682013-01-10 15:30:38 -080032import ofp
Flavio Castrob01d0aa2016-07-20 16:14:48 -070033from ofdpa_utils import *
Rich Laneb90a1c42012-10-05 09:16:05 -070034
Rich Lane69fd8e02013-08-23 16:23:42 -070035class BaseTest(unittest.TestCase):
36 def __str__(self):
37 return self.id().replace('.runTest', '')
38
39 def setUp(self):
Flavio Castrob01d0aa2016-07-20 16:14:48 -070040 if config["force_ofdpa_restart"]:
41 logging.info("Restarting OFDPA")
42 forceOfdpaRestart( config["force_ofdpa_restart"]);
Rich Lane69fd8e02013-08-23 16:23:42 -070043 oftest.open_logfile(str(self))
44 logging.info("** START TEST CASE " + str(self))
45
46 def tearDown(self):
47 logging.info("** END TEST CASE " + str(self))
Flavio Castrob01d0aa2016-07-20 16:14:48 -070048 if config["force_ofdpa_restart"]:
49 forceOfdpaStop( config["force_ofdpa_restart"]);
50
Rich Lane69fd8e02013-08-23 16:23:42 -070051
52class SimpleProtocol(BaseTest):
Rich Laneb90a1c42012-10-05 09:16:05 -070053 """
54 Root class for setting up the controller
55 """
56
57 def setUp(self):
Rich Lane69fd8e02013-08-23 16:23:42 -070058 BaseTest.setUp(self)
Rich Laneb90a1c42012-10-05 09:16:05 -070059 self.controller = controller.Controller(
Dan Talayco69ca4d62012-11-15 11:50:22 -080060 switch=config["switch_ip"],
Rich Laneb90a1c42012-10-05 09:16:05 -070061 host=config["controller_host"],
62 port=config["controller_port"])
Rich Laneb90a1c42012-10-05 09:16:05 -070063 self.controller.start()
Rich Laneb90a1c42012-10-05 09:16:05 -070064
Rich Laned5915042012-12-31 14:58:35 -080065 try:
66 #@todo Add an option to wait for a pkt transaction to ensure version
67 # compatibilty?
68 self.controller.connect(timeout=20)
69
70 # By default, respond to echo requests
71 self.controller.keep_alive = True
Rich Laned5915042012-12-31 14:58:35 -080072 if not self.controller.active:
73 raise Exception("Controller startup failed")
74 if self.controller.switch_addr is None:
75 raise Exception("Controller startup failed (no switch addr)")
76 logging.info("Connected " + str(self.controller.switch_addr))
Rich Lane78ef8b92013-01-10 12:19:23 -080077 request = ofp.message.features_request()
Rich Laned5915042012-12-31 14:58:35 -080078 reply, pkt = self.controller.transact(request)
79 self.assertTrue(reply is not None,
80 "Did not complete features_request for handshake")
Rich Laneb73808c2013-03-11 15:22:23 -070081 if reply.version == 1:
Rich Laneaf428152013-01-10 12:24:44 -080082 self.supported_actions = reply.actions
83 logging.info("Supported actions: " + hex(self.supported_actions))
Rich Laned5915042012-12-31 14:58:35 -080084 except:
85 self.controller.kill()
86 del self.controller
87 raise
Rich Laneb90a1c42012-10-05 09:16:05 -070088
89 def inheritSetup(self, parent):
90 """
91 Inherit the setup of a parent
92
93 This allows running at test from within another test. Do the
94 following:
95
96 sub_test = SomeTestClass() # Create an instance of the test class
97 sub_test.inheritSetup(self) # Inherit setup of parent
98 sub_test.runTest() # Run the test
99
100 Normally, only the parent's setUp and tearDown are called and
101 the state after the sub_test is run must be taken into account
102 by subsequent operations.
103 """
104 logging.info("** Setup " + str(self) + " inheriting from "
105 + str(parent))
106 self.controller = parent.controller
107 self.supported_actions = parent.supported_actions
108
109 def tearDown(self):
Rich Laneb90a1c42012-10-05 09:16:05 -0700110 self.controller.shutdown()
Rich Lane7c64a422012-12-31 13:46:34 -0800111 self.controller.join()
Rich Lanee7b0ecb2012-12-26 10:01:01 -0800112 del self.controller
Rich Lane69fd8e02013-08-23 16:23:42 -0700113 BaseTest.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700114
Rich Laneb90a1c42012-10-05 09:16:05 -0700115 def assertTrue(self, cond, msg):
116 if not cond:
117 logging.error("** FAILED ASSERTION: " + msg)
118 unittest.TestCase.assertTrue(self, cond, msg)
119
120class SimpleDataPlane(SimpleProtocol):
121 """
122 Root class that sets up the controller and dataplane
123 """
124 def setUp(self):
125 SimpleProtocol.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -0800126 self.dataplane = oftest.dataplane_instance
127 self.dataplane.flush()
Rich Lane472aaea2013-08-27 09:27:38 -0700128 if config["log_dir"] != None:
129 filename = os.path.join(config["log_dir"], str(self)) + ".pcap"
130 self.dataplane.start_pcap(filename)
Rich Laneb90a1c42012-10-05 09:16:05 -0700131
132 def inheritSetup(self, parent):
133 """
134 Inherit the setup of a parent
135
136 See SimpleProtocol.inheritSetup
137 """
138 SimpleProtocol.inheritSetup(self, parent)
139 self.dataplane = parent.dataplane
140
141 def tearDown(self):
Rich Lane472aaea2013-08-27 09:27:38 -0700142 if config["log_dir"] != None:
143 self.dataplane.stop_pcap()
Rich Laneb90a1c42012-10-05 09:16:05 -0700144 SimpleProtocol.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700145
Rich Lane69fd8e02013-08-23 16:23:42 -0700146class DataPlaneOnly(BaseTest):
Rich Laneb90a1c42012-10-05 09:16:05 -0700147 """
148 Root class that sets up only the dataplane
149 """
150
151 def setUp(self):
Rich Lane69fd8e02013-08-23 16:23:42 -0700152 BaseTest.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -0800153 self.dataplane = oftest.dataplane_instance
154 self.dataplane.flush()
Rich Lane472aaea2013-08-27 09:27:38 -0700155 if config["log_dir"] != None:
156 filename = os.path.join(config["log_dir"], str(self)) + ".pcap"
157 self.dataplane.start_pcap(filename)
Rich Laneb90a1c42012-10-05 09:16:05 -0700158
159 def tearDown(self):
Rich Lane472aaea2013-08-27 09:27:38 -0700160 if config["log_dir"] != None:
161 self.dataplane.stop_pcap()
Rich Lane69fd8e02013-08-23 16:23:42 -0700162 BaseTest.tearDown(self)