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 | 6242d9f | 2013-01-06 17:35:39 -0800 | [diff] [blame] | 15 | import of10.message as message |
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)) |
| 43 | request = message.features_request() |
| 44 | reply, pkt = self.controller.transact(request) |
| 45 | self.assertTrue(reply is not None, |
| 46 | "Did not complete features_request for handshake") |
| 47 | self.supported_actions = reply.actions |
| 48 | logging.info("Supported actions: " + hex(self.supported_actions)) |
| 49 | except: |
| 50 | self.controller.kill() |
| 51 | del self.controller |
| 52 | raise |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 53 | |
| 54 | def inheritSetup(self, parent): |
| 55 | """ |
| 56 | Inherit the setup of a parent |
| 57 | |
| 58 | This allows running at test from within another test. Do the |
| 59 | following: |
| 60 | |
| 61 | sub_test = SomeTestClass() # Create an instance of the test class |
| 62 | sub_test.inheritSetup(self) # Inherit setup of parent |
| 63 | sub_test.runTest() # Run the test |
| 64 | |
| 65 | Normally, only the parent's setUp and tearDown are called and |
| 66 | the state after the sub_test is run must be taken into account |
| 67 | by subsequent operations. |
| 68 | """ |
| 69 | logging.info("** Setup " + str(self) + " inheriting from " |
| 70 | + str(parent)) |
| 71 | self.controller = parent.controller |
| 72 | self.supported_actions = parent.supported_actions |
| 73 | |
| 74 | def tearDown(self): |
| 75 | logging.info("** END TEST CASE " + str(self)) |
| 76 | self.controller.shutdown() |
Rich Lane | 7c64a42 | 2012-12-31 13:46:34 -0800 | [diff] [blame] | 77 | self.controller.join() |
Rich Lane | e7b0ecb | 2012-12-26 10:01:01 -0800 | [diff] [blame] | 78 | del self.controller |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 79 | |
| 80 | def runTest(self): |
| 81 | # Just a simple sanity check as illustration |
| 82 | logging.info("Running simple proto test") |
| 83 | self.assertTrue(self.controller.switch_socket is not None, |
| 84 | str(self) + 'No connection to switch') |
| 85 | |
| 86 | def assertTrue(self, cond, msg): |
| 87 | if not cond: |
| 88 | logging.error("** FAILED ASSERTION: " + msg) |
| 89 | unittest.TestCase.assertTrue(self, cond, msg) |
| 90 | |
| 91 | class SimpleDataPlane(SimpleProtocol): |
| 92 | """ |
| 93 | Root class that sets up the controller and dataplane |
| 94 | """ |
| 95 | def setUp(self): |
| 96 | SimpleProtocol.setUp(self) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 97 | self.dataplane = oftest.dataplane_instance |
| 98 | self.dataplane.flush() |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 99 | |
| 100 | def inheritSetup(self, parent): |
| 101 | """ |
| 102 | Inherit the setup of a parent |
| 103 | |
| 104 | See SimpleProtocol.inheritSetup |
| 105 | """ |
| 106 | SimpleProtocol.inheritSetup(self, parent) |
| 107 | self.dataplane = parent.dataplane |
| 108 | |
| 109 | def tearDown(self): |
| 110 | logging.info("Teardown for simple dataplane test") |
| 111 | SimpleProtocol.tearDown(self) |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 112 | logging.info("Teardown done") |
| 113 | |
| 114 | def runTest(self): |
| 115 | self.assertTrue(self.controller.switch_socket is not None, |
| 116 | str(self) + 'No connection to switch') |
| 117 | # self.dataplane.show() |
| 118 | # Would like an assert that checks the data plane |
| 119 | |
| 120 | class DataPlaneOnly(unittest.TestCase): |
| 121 | """ |
| 122 | Root class that sets up only the dataplane |
| 123 | """ |
| 124 | |
| 125 | def setUp(self): |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 126 | logging.info("** START DataPlaneOnly CASE " + str(self)) |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 127 | self.dataplane = oftest.dataplane_instance |
| 128 | self.dataplane.flush() |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 129 | |
| 130 | def tearDown(self): |
| 131 | logging.info("Teardown for simple dataplane test") |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 132 | logging.info("Teardown done") |
| 133 | |
| 134 | def runTest(self): |
| 135 | logging.info("DataPlaneOnly") |
| 136 | # self.dataplane.show() |
| 137 | # Would like an assert that checks the data plane |