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