Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 14 | from unittest import TestCase, main |
| 15 | from of_protocol_handler import OpenFlowProtocolHandler |
| 16 | import loxi.of13 as ofp |
| 17 | |
| 18 | class TestOF_Protocol_handler(TestCase): |
| 19 | |
| 20 | def gen_packet_in(self): |
| 21 | packet_in = 1 |
| 22 | return packet_in |
| 23 | |
| 24 | def gen_device(self): |
| 25 | device =lambda: None |
| 26 | device.id = "1" |
| 27 | device.datapath_id = 1 |
| 28 | device.desc = '{mfr_desc: "cord porject" hw_desc: "simualted pon" sw_desc: "simualted pon"\ |
| 29 | serial_num: "2f150d56afa2405eba3ba24e33ce8df9" dp_desc: "n/a"}' |
| 30 | device.switch_features = '{ n_buffers: 256 n_tables: 2 capabilities: 15 }' |
| 31 | device.root_device_id = "a245bd8bb8b8" |
| 32 | return device |
| 33 | |
| 34 | def gen_generic_obj(self): |
| 35 | generic_obj = lambda: None |
| 36 | return generic_obj |
| 37 | |
| 38 | def gen_role_req(self): |
| 39 | req = self.gen_generic_obj() |
| 40 | req.role = ofp.OFPCR_ROLE_MASTER |
| 41 | return req |
| 42 | |
| 43 | def test_handle_flow_mod_request_role_salve(self): |
| 44 | generic_obj = self.gen_generic_obj() |
| 45 | device = self.gen_device() |
| 46 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 47 | of_proto_handler.role = ofp.OFPCR_ROLE_SLAVE |
| 48 | with self.assertRaises(Exception) as context: |
| 49 | of_proto_handler.handle_flow_mod_request(generic_obj) |
| 50 | print context.exception |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 51 | self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception)) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 52 | |
| 53 | def test_handle_flow_mod_request_role_master(self): |
| 54 | generic_obj = self.gen_generic_obj() |
| 55 | device = self.gen_device() |
| 56 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 57 | of_proto_handler.role = ofp.OFPCR_ROLE_MASTER |
| 58 | of_proto_handler.handle_flow_mod_request(generic_obj) |
| 59 | |
| 60 | def test_handle_role_request(self): |
| 61 | generic_obj = self.gen_generic_obj() |
| 62 | req = self.gen_role_req() |
| 63 | device = self.gen_device() |
| 64 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 65 | with self.assertRaises(Exception) as context: |
| 66 | of_proto_handler.handle_role_request(req) |
| 67 | self.assertEqual(of_proto_handler.role,req.role) |
| 68 | print context.exception |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 69 | self.assertTrue('\'function\' object has no attribute \'generation_is_defined\'' in str(context.exception)) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 70 | |
| 71 | def test_forward_packet_in_role_none(self): |
| 72 | packet_in = self.gen_packet_in() |
| 73 | generic_obj = self.gen_generic_obj() |
| 74 | device = self.gen_device() |
| 75 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 76 | of_proto_handler.forward_packet_in(packet_in) |
| 77 | |
| 78 | def test_forward_packet_in_role_master(self): |
| 79 | packet_in = self.gen_packet_in() |
| 80 | generic_obj = self.gen_generic_obj() |
| 81 | device = self.gen_device() |
| 82 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 83 | of_proto_handler.role = ofp.OFPCR_ROLE_MASTER |
| 84 | with self.assertRaises(Exception) as context: |
| 85 | of_proto_handler.forward_packet_in(packet_in) |
| 86 | print context.exception |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 87 | self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception)) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 88 | |
| 89 | if __name__ == '__main__': |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 90 | main() |