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