Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 1 | """ |
| 2 | Base classes for test cases |
| 3 | |
| 4 | Tests will usually inherit from one of these classes to have the controller |
| 5 | and/or dataplane automatically set up. |
| 6 | """ |
| 7 | |
| 8 | import logging |
| 9 | import unittest |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 10 | import os |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 11 | |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 12 | import oftest |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 13 | from oftest import config |
| 14 | import oftest.controller as controller |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 15 | import oftest.dataplane as dataplane |
Rich Lane | 9fd0568 | 2013-01-10 15:30:38 -0800 | [diff] [blame] | 16 | import ofp |
Flavio Castro | b01d0aa | 2016-07-20 16:14:48 -0700 | [diff] [blame] | 17 | from ofdpa_utils import * |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 18 | |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 19 | class BaseTest(unittest.TestCase): |
| 20 | def __str__(self): |
| 21 | return self.id().replace('.runTest', '') |
| 22 | |
| 23 | def setUp(self): |
Flavio Castro | b01d0aa | 2016-07-20 16:14:48 -0700 | [diff] [blame] | 24 | if config["force_ofdpa_restart"]: |
| 25 | logging.info("Restarting OFDPA") |
| 26 | forceOfdpaRestart( config["force_ofdpa_restart"]); |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 27 | oftest.open_logfile(str(self)) |
| 28 | logging.info("** START TEST CASE " + str(self)) |
| 29 | |
| 30 | def tearDown(self): |
| 31 | logging.info("** END TEST CASE " + str(self)) |
Flavio Castro | b01d0aa | 2016-07-20 16:14:48 -0700 | [diff] [blame] | 32 | if config["force_ofdpa_restart"]: |
| 33 | forceOfdpaStop( config["force_ofdpa_restart"]); |
| 34 | |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 35 | |
| 36 | class SimpleProtocol(BaseTest): |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 37 | """ |
| 38 | Root class for setting up the controller |
| 39 | """ |
| 40 | |
| 41 | def setUp(self): |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 42 | BaseTest.setUp(self) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 43 | self.controller = controller.Controller( |
Dan Talayco | 69ca4d6 | 2012-11-15 11:50:22 -0800 | [diff] [blame] | 44 | switch=config["switch_ip"], |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 45 | host=config["controller_host"], |
| 46 | port=config["controller_port"]) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 47 | self.controller.start() |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 48 | |
Rich Lane | d591504 | 2012-12-31 14:58:35 -0800 | [diff] [blame] | 49 | try: |
| 50 | #@todo Add an option to wait for a pkt transaction to ensure version |
| 51 | # compatibilty? |
| 52 | self.controller.connect(timeout=20) |
| 53 | |
| 54 | # By default, respond to echo requests |
| 55 | self.controller.keep_alive = True |
Rich Lane | d591504 | 2012-12-31 14:58:35 -0800 | [diff] [blame] | 56 | if not self.controller.active: |
| 57 | raise Exception("Controller startup failed") |
| 58 | if self.controller.switch_addr is None: |
| 59 | raise Exception("Controller startup failed (no switch addr)") |
| 60 | logging.info("Connected " + str(self.controller.switch_addr)) |
Rich Lane | 78ef8b9 | 2013-01-10 12:19:23 -0800 | [diff] [blame] | 61 | request = ofp.message.features_request() |
Rich Lane | d591504 | 2012-12-31 14:58:35 -0800 | [diff] [blame] | 62 | reply, pkt = self.controller.transact(request) |
| 63 | self.assertTrue(reply is not None, |
| 64 | "Did not complete features_request for handshake") |
Rich Lane | b73808c | 2013-03-11 15:22:23 -0700 | [diff] [blame] | 65 | if reply.version == 1: |
Rich Lane | af42815 | 2013-01-10 12:24:44 -0800 | [diff] [blame] | 66 | self.supported_actions = reply.actions |
| 67 | logging.info("Supported actions: " + hex(self.supported_actions)) |
Rich Lane | d591504 | 2012-12-31 14:58:35 -0800 | [diff] [blame] | 68 | except: |
| 69 | self.controller.kill() |
| 70 | del self.controller |
| 71 | raise |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 72 | |
| 73 | def inheritSetup(self, parent): |
| 74 | """ |
| 75 | Inherit the setup of a parent |
| 76 | |
| 77 | This allows running at test from within another test. Do the |
| 78 | following: |
| 79 | |
| 80 | sub_test = SomeTestClass() # Create an instance of the test class |
| 81 | sub_test.inheritSetup(self) # Inherit setup of parent |
| 82 | sub_test.runTest() # Run the test |
| 83 | |
| 84 | Normally, only the parent's setUp and tearDown are called and |
| 85 | the state after the sub_test is run must be taken into account |
| 86 | by subsequent operations. |
| 87 | """ |
| 88 | logging.info("** Setup " + str(self) + " inheriting from " |
| 89 | + str(parent)) |
| 90 | self.controller = parent.controller |
| 91 | self.supported_actions = parent.supported_actions |
| 92 | |
| 93 | def tearDown(self): |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 94 | self.controller.shutdown() |
Rich Lane | 7c64a42 | 2012-12-31 13:46:34 -0800 | [diff] [blame] | 95 | self.controller.join() |
Rich Lane | e7b0ecb | 2012-12-26 10:01:01 -0800 | [diff] [blame] | 96 | del self.controller |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 97 | BaseTest.tearDown(self) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 98 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 99 | def assertTrue(self, cond, msg): |
| 100 | if not cond: |
| 101 | logging.error("** FAILED ASSERTION: " + msg) |
| 102 | unittest.TestCase.assertTrue(self, cond, msg) |
| 103 | |
| 104 | class SimpleDataPlane(SimpleProtocol): |
| 105 | """ |
| 106 | Root class that sets up the controller and dataplane |
| 107 | """ |
| 108 | def setUp(self): |
| 109 | SimpleProtocol.setUp(self) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 110 | self.dataplane = oftest.dataplane_instance |
| 111 | self.dataplane.flush() |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 112 | if config["log_dir"] != None: |
| 113 | filename = os.path.join(config["log_dir"], str(self)) + ".pcap" |
| 114 | self.dataplane.start_pcap(filename) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 115 | |
| 116 | def inheritSetup(self, parent): |
| 117 | """ |
| 118 | Inherit the setup of a parent |
| 119 | |
| 120 | See SimpleProtocol.inheritSetup |
| 121 | """ |
| 122 | SimpleProtocol.inheritSetup(self, parent) |
| 123 | self.dataplane = parent.dataplane |
| 124 | |
| 125 | def tearDown(self): |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 126 | if config["log_dir"] != None: |
| 127 | self.dataplane.stop_pcap() |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 128 | SimpleProtocol.tearDown(self) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 129 | |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 130 | class DataPlaneOnly(BaseTest): |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 131 | """ |
| 132 | Root class that sets up only the dataplane |
| 133 | """ |
| 134 | |
| 135 | def setUp(self): |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 136 | BaseTest.setUp(self) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 137 | self.dataplane = oftest.dataplane_instance |
| 138 | self.dataplane.flush() |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 139 | if config["log_dir"] != None: |
| 140 | filename = os.path.join(config["log_dir"], str(self)) + ".pcap" |
| 141 | self.dataplane.start_pcap(filename) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 142 | |
| 143 | def tearDown(self): |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 144 | if config["log_dir"] != None: |
| 145 | self.dataplane.stop_pcap() |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 146 | BaseTest.tearDown(self) |