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")) |
Rich Lane | 00549ea | 2013-04-25 13:33:16 -0700 | [diff] [blame] | 39 | self.assertEquals(loxi.version_names[2], "1.1") |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 40 | ofp = loxi.protocol(2) |
| 41 | self.assertEquals(ofp.OFP_VERSION, 2) |
| 42 | self.assertTrue(hasattr(ofp, "action")) |
| 43 | self.assertTrue(hasattr(ofp, "common")) |
| 44 | self.assertTrue(hasattr(ofp, "const")) |
| 45 | self.assertTrue(hasattr(ofp, "message")) |
| 46 | |
| 47 | def test_version(self): |
| 48 | import loxi |
| 49 | self.assertTrue(hasattr(loxi.of11, "ProtocolError")) |
| 50 | self.assertTrue(hasattr(loxi.of11, "OFP_VERSION")) |
| 51 | self.assertEquals(loxi.of11.OFP_VERSION, 2) |
| 52 | self.assertTrue(hasattr(loxi.of11, "action")) |
| 53 | self.assertTrue(hasattr(loxi.of11, "common")) |
| 54 | self.assertTrue(hasattr(loxi.of11, "const")) |
| 55 | self.assertTrue(hasattr(loxi.of11, "message")) |
| 56 | |
| 57 | class TestAllOF11(unittest.TestCase): |
| 58 | """ |
| 59 | Round-trips every class through serialization/deserialization. |
| 60 | Not a replacement for handcoded tests because it only uses the |
| 61 | default member values. |
| 62 | """ |
| 63 | |
| 64 | def setUp(self): |
| 65 | mods = [ofp.action,ofp.message,ofp.common] |
| 66 | self.klasses = [klass for mod in mods |
| 67 | for klass in mod.__dict__.values() |
| 68 | if hasattr(klass, 'show')] |
| 69 | self.klasses.sort(key=lambda x: str(x)) |
| 70 | |
| 71 | def test_serialization(self): |
| 72 | expected_failures = [ |
Rich Lane | 8692ecd | 2013-05-02 11:33:53 -0700 | [diff] [blame] | 73 | ofp.common.flow_stats_entry, |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 74 | ofp.common.table_stats_entry, |
Rich Lane | 8692ecd | 2013-05-02 11:33:53 -0700 | [diff] [blame] | 75 | ofp.message.flow_add, |
| 76 | ofp.message.flow_modify, |
| 77 | ofp.message.flow_modify_strict, |
| 78 | ofp.message.flow_delete, |
| 79 | ofp.message.flow_delete_strict, |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 80 | ] |
| 81 | for klass in self.klasses: |
| 82 | def fn(): |
| 83 | obj = klass() |
| 84 | if hasattr(obj, "xid"): obj.xid = 42 |
| 85 | buf = obj.pack() |
| 86 | obj2 = klass.unpack(buf) |
| 87 | self.assertEquals(obj, obj2) |
| 88 | if klass in expected_failures: |
| 89 | self.assertRaises(Exception, fn) |
| 90 | else: |
| 91 | fn() |
| 92 | |
| 93 | def test_show(self): |
| 94 | expected_failures = [ |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 95 | ] |
| 96 | for klass in self.klasses: |
| 97 | def fn(): |
| 98 | obj = klass() |
| 99 | if hasattr(obj, "xid"): obj.xid = 42 |
| 100 | obj.show() |
| 101 | if klass in expected_failures: |
| 102 | self.assertRaises(Exception, fn) |
| 103 | else: |
| 104 | fn() |
| 105 | |
| 106 | if __name__ == '__main__': |
| 107 | unittest.main() |