Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2013, Big Switch Networks, Inc. |
| 3 | # |
| 4 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 5 | # the following special exception: |
| 6 | # |
| 7 | # LOXI Exception |
| 8 | # |
| 9 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 10 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 11 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 12 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 13 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 14 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 15 | # |
| 16 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 17 | # |
| 18 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 19 | # a copy of the EPL at: |
| 20 | # |
| 21 | # http://www.eclipse.org/legal/epl-v10.html |
| 22 | # |
| 23 | # Unless required by applicable law or agreed to in writing, software |
| 24 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 26 | # EPL for the specific language governing permissions and limitations |
| 27 | # under the EPL. |
| 28 | import unittest |
| 29 | |
| 30 | try: |
| 31 | import loxi.of11 as ofp |
| 32 | except ImportError: |
| 33 | exit("loxi package not found. Try setting PYTHONPATH.") |
| 34 | |
| 35 | class TestImports(unittest.TestCase): |
| 36 | def test_toplevel(self): |
| 37 | import loxi |
| 38 | self.assertTrue(hasattr(loxi, "ProtocolError")) |
| 39 | ofp = loxi.protocol(2) |
| 40 | self.assertEquals(ofp.OFP_VERSION, 2) |
| 41 | self.assertTrue(hasattr(ofp, "action")) |
| 42 | self.assertTrue(hasattr(ofp, "common")) |
| 43 | self.assertTrue(hasattr(ofp, "const")) |
| 44 | self.assertTrue(hasattr(ofp, "message")) |
| 45 | |
| 46 | def test_version(self): |
| 47 | import loxi |
| 48 | self.assertTrue(hasattr(loxi.of11, "ProtocolError")) |
| 49 | self.assertTrue(hasattr(loxi.of11, "OFP_VERSION")) |
| 50 | self.assertEquals(loxi.of11.OFP_VERSION, 2) |
| 51 | self.assertTrue(hasattr(loxi.of11, "action")) |
| 52 | self.assertTrue(hasattr(loxi.of11, "common")) |
| 53 | self.assertTrue(hasattr(loxi.of11, "const")) |
| 54 | self.assertTrue(hasattr(loxi.of11, "message")) |
| 55 | |
| 56 | class TestAllOF11(unittest.TestCase): |
| 57 | """ |
| 58 | Round-trips every class through serialization/deserialization. |
| 59 | Not a replacement for handcoded tests because it only uses the |
| 60 | default member values. |
| 61 | """ |
| 62 | |
| 63 | def setUp(self): |
| 64 | mods = [ofp.action,ofp.message,ofp.common] |
| 65 | self.klasses = [klass for mod in mods |
| 66 | for klass in mod.__dict__.values() |
| 67 | if hasattr(klass, 'show')] |
| 68 | self.klasses.sort(key=lambda x: str(x)) |
| 69 | |
| 70 | def test_serialization(self): |
| 71 | expected_failures = [ |
| 72 | ofp.common.flow_stats_entry, |
| 73 | ofp.common.group_desc_stats_entry, |
| 74 | ofp.common.instruction, |
| 75 | ofp.common.instruction_apply_actions, |
| 76 | ofp.common.instruction_clear_actions, |
| 77 | ofp.common.instruction_experimenter, |
| 78 | ofp.common.instruction_goto_table, |
| 79 | ofp.common.instruction_header, |
| 80 | ofp.common.instruction_write_actions, |
| 81 | ofp.common.instruction_write_metadata, |
| 82 | ofp.common.match_v2, |
| 83 | ofp.common.table_stats_entry, |
| 84 | ofp.message.aggregate_stats_request, |
| 85 | ofp.message.flow_add, |
| 86 | ofp.message.flow_delete, |
| 87 | ofp.message.flow_delete_strict, |
| 88 | ofp.message.flow_modify, |
| 89 | ofp.message.flow_modify_strict, |
| 90 | ofp.message.flow_removed, |
| 91 | ofp.message.flow_stats_request, |
| 92 | ofp.message.group_desc_stats_reply, |
| 93 | ofp.message.group_mod, |
| 94 | ofp.message.group_stats_reply, |
| 95 | ] |
| 96 | for klass in self.klasses: |
| 97 | def fn(): |
| 98 | obj = klass() |
| 99 | if hasattr(obj, "xid"): obj.xid = 42 |
| 100 | buf = obj.pack() |
| 101 | obj2 = klass.unpack(buf) |
| 102 | self.assertEquals(obj, obj2) |
| 103 | if klass in expected_failures: |
| 104 | self.assertRaises(Exception, fn) |
| 105 | else: |
| 106 | fn() |
| 107 | |
| 108 | def test_show(self): |
| 109 | expected_failures = [ |
| 110 | ofp.common.flow_stats_entry, |
| 111 | ofp.common.group_desc_stats_entry, |
| 112 | ofp.common.instruction, |
| 113 | ofp.common.instruction_apply_actions, |
| 114 | ofp.common.instruction_clear_actions, |
| 115 | ofp.common.instruction_experimenter, |
| 116 | ofp.common.instruction_goto_table, |
| 117 | ofp.common.instruction_header, |
| 118 | ofp.common.instruction_write_actions, |
| 119 | ofp.common.instruction_write_metadata, |
| 120 | ofp.common.match_v2, |
| 121 | ofp.message.aggregate_stats_request, |
| 122 | ofp.message.flow_add, |
| 123 | ofp.message.flow_delete, |
| 124 | ofp.message.flow_delete_strict, |
| 125 | ofp.message.flow_modify, |
| 126 | ofp.message.flow_modify_strict, |
| 127 | ofp.message.flow_removed, |
| 128 | ofp.message.flow_stats_request, |
| 129 | ] |
| 130 | for klass in self.klasses: |
| 131 | def fn(): |
| 132 | obj = klass() |
| 133 | if hasattr(obj, "xid"): obj.xid = 42 |
| 134 | obj.show() |
| 135 | if klass in expected_failures: |
| 136 | self.assertRaises(Exception, fn) |
| 137 | else: |
| 138 | fn() |
| 139 | |
| 140 | if __name__ == '__main__': |
| 141 | unittest.main() |