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