blob: 7eb68bcfc282bb29e0483d6484b0e14ccb0d5186 [file] [log] [blame]
Sreeju Sreedhare3fefd92019-04-02 15:57:15 -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
17"""
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
26import os
27
28import oftest
29from oftest import config
30import oftest.controller as controller
31import oftest.dataplane as dataplane
32import ofp
33from ofdpa_utils import *
34
35class BaseTest(unittest.TestCase):
36 def __str__(self):
37 return self.id().replace('.runTest', '')
38
39 def setUp(self):
40 if config["force_ofdpa_restart"]:
41 logging.info("Restarting OFDPA")
42 forceOfdpaRestart( config["force_ofdpa_restart"]);
43 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))
48 if config["force_ofdpa_restart"]:
49 forceOfdpaStop( config["force_ofdpa_restart"]);
50
51
52class SimpleProtocol(BaseTest):
53 """
54 Root class for setting up the controller
55 """
56
57 def setUp(self):
58 BaseTest.setUp(self)
59 self.controller = controller.Controller(
60 switch=config["switch_ip"],
61 host=config["controller_host"],
62 port=config["controller_port"])
63 self.controller.start()
64
65 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
72 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))
77 request = ofp.message.features_request()
78 reply, pkt = self.controller.transact(request)
79 self.assertTrue(reply is not None,
80 "Did not complete features_request for handshake")
81 if reply.version == 1:
82 self.supported_actions = reply.actions
83 logging.info("Supported actions: " + hex(self.supported_actions))
84 except:
85 self.controller.kill()
86 del self.controller
87 raise
88
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):
110 self.controller.shutdown()
111 self.controller.join()
112 del self.controller
113 BaseTest.tearDown(self)
114
115 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)
126 self.dataplane = oftest.dataplane_instance
127 self.dataplane.flush()
128 if config["log_dir"] != None:
129 filename = os.path.join(config["log_dir"], str(self)) + ".pcap"
130 self.dataplane.start_pcap(filename)
131
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):
142 if config["log_dir"] != None:
143 self.dataplane.stop_pcap()
144 SimpleProtocol.tearDown(self)
145
146class DataPlaneOnly(BaseTest):
147 """
148 Root class that sets up only the dataplane
149 """
150
151 def setUp(self):
152 BaseTest.setUp(self)
153 self.dataplane = oftest.dataplane_instance
154 self.dataplane.flush()
155 if config["log_dir"] != None:
156 filename = os.path.join(config["log_dir"], str(self)) + ".pcap"
157 self.dataplane.start_pcap(filename)
158
159 def tearDown(self):
160 if config["log_dir"] != None:
161 self.dataplane.stop_pcap()
162 BaseTest.tearDown(self)