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