Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 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 Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 17 | """ |
| 18 | Base classes for test cases |
| 19 | |
| 20 | Tests will usually inherit from one of these classes to have the controller |
| 21 | and/or dataplane automatically set up. |
| 22 | """ |
| 23 | |
| 24 | import logging |
| 25 | import unittest |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 26 | import os |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 27 | |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 28 | import oftest |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 29 | from oftest import config |
| 30 | import oftest.controller as controller |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 31 | import oftest.dataplane as dataplane |
Rich Lane | 9fd0568 | 2013-01-10 15:30:38 -0800 | [diff] [blame] | 32 | import ofp |
Flavio Castro | b01d0aa | 2016-07-20 16:14:48 -0700 | [diff] [blame] | 33 | from ofdpa_utils import * |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 34 | |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 35 | class BaseTest(unittest.TestCase): |
| 36 | def __str__(self): |
| 37 | return self.id().replace('.runTest', '') |
| 38 | |
| 39 | def setUp(self): |
Flavio Castro | b01d0aa | 2016-07-20 16:14:48 -0700 | [diff] [blame] | 40 | if config["force_ofdpa_restart"]: |
| 41 | logging.info("Restarting OFDPA") |
| 42 | forceOfdpaRestart( config["force_ofdpa_restart"]); |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 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)) |
Flavio Castro | b01d0aa | 2016-07-20 16:14:48 -0700 | [diff] [blame] | 48 | if config["force_ofdpa_restart"]: |
| 49 | forceOfdpaStop( config["force_ofdpa_restart"]); |
| 50 | |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 51 | |
| 52 | class SimpleProtocol(BaseTest): |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 53 | """ |
| 54 | Root class for setting up the controller |
| 55 | """ |
| 56 | |
| 57 | def setUp(self): |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 58 | BaseTest.setUp(self) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 59 | self.controller = controller.Controller( |
Dan Talayco | 69ca4d6 | 2012-11-15 11:50:22 -0800 | [diff] [blame] | 60 | switch=config["switch_ip"], |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 61 | host=config["controller_host"], |
| 62 | port=config["controller_port"]) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 63 | self.controller.start() |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 64 | |
Rich Lane | d591504 | 2012-12-31 14:58:35 -0800 | [diff] [blame] | 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 |
Rich Lane | d591504 | 2012-12-31 14:58:35 -0800 | [diff] [blame] | 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)) |
Rich Lane | 78ef8b9 | 2013-01-10 12:19:23 -0800 | [diff] [blame] | 77 | request = ofp.message.features_request() |
Rich Lane | d591504 | 2012-12-31 14:58:35 -0800 | [diff] [blame] | 78 | reply, pkt = self.controller.transact(request) |
| 79 | self.assertTrue(reply is not None, |
| 80 | "Did not complete features_request for handshake") |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 81 | if reply.version == 1: |
Rich Lane | af42815 | 2013-01-10 12:24:44 -0800 | [diff] [blame] | 82 | self.supported_actions = reply.actions |
| 83 | logging.info("Supported actions: " + hex(self.supported_actions)) |
Rich Lane | d591504 | 2012-12-31 14:58:35 -0800 | [diff] [blame] | 84 | except: |
| 85 | self.controller.kill() |
| 86 | del self.controller |
| 87 | raise |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 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): |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 110 | self.controller.shutdown() |
Rich Lane | 7c64a42 | 2012-12-31 13:46:34 -0800 | [diff] [blame] | 111 | self.controller.join() |
Rich Lane | e7b0ecb | 2012-12-26 10:01:01 -0800 | [diff] [blame] | 112 | del self.controller |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 113 | BaseTest.tearDown(self) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 114 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 115 | def assertTrue(self, cond, msg): |
| 116 | if not cond: |
| 117 | logging.error("** FAILED ASSERTION: " + msg) |
| 118 | unittest.TestCase.assertTrue(self, cond, msg) |
| 119 | |
| 120 | class SimpleDataPlane(SimpleProtocol): |
| 121 | """ |
| 122 | Root class that sets up the controller and dataplane |
| 123 | """ |
| 124 | def setUp(self): |
| 125 | SimpleProtocol.setUp(self) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 126 | self.dataplane = oftest.dataplane_instance |
| 127 | self.dataplane.flush() |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 128 | if config["log_dir"] != None: |
| 129 | filename = os.path.join(config["log_dir"], str(self)) + ".pcap" |
| 130 | self.dataplane.start_pcap(filename) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 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): |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 142 | if config["log_dir"] != None: |
| 143 | self.dataplane.stop_pcap() |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 144 | SimpleProtocol.tearDown(self) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 145 | |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 146 | class DataPlaneOnly(BaseTest): |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 147 | """ |
| 148 | Root class that sets up only the dataplane |
| 149 | """ |
| 150 | |
| 151 | def setUp(self): |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 152 | BaseTest.setUp(self) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 153 | self.dataplane = oftest.dataplane_instance |
| 154 | self.dataplane.flush() |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 155 | if config["log_dir"] != None: |
| 156 | filename = os.path.join(config["log_dir"], str(self)) + ".pcap" |
| 157 | self.dataplane.start_pcap(filename) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 158 | |
| 159 | def tearDown(self): |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 160 | if config["log_dir"] != None: |
| 161 | self.dataplane.stop_pcap() |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 162 | BaseTest.tearDown(self) |