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. |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 14 | import os |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 15 | from unittest import TestCase, main |
| 16 | from connection_mgr import ConnectionManager |
| 17 | |
| 18 | class TestConection_mgr(TestCase): |
| 19 | |
| 20 | def gen_endpoints(self): |
| 21 | consul_endpoint = "localhost:8500" |
| 22 | voltha_endpoint= "localhost:8880" |
| 23 | controller_endpoints = ["localhost:6633","localhost:6644","localhost:6655"] |
| 24 | return (consul_endpoint,voltha_endpoint,controller_endpoints) |
| 25 | |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 26 | def gen_container_name(self): |
| 27 | instance_id = os.environ.get('HOSTNAME', 'localhost') |
| 28 | return instance_id |
| 29 | |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 30 | def gen_devices(self): |
| 31 | device =lambda: None |
| 32 | device.id = "1" |
| 33 | device.datapath_id = 1 |
| 34 | device.desc = '{mfr_desc: "cord porject" hw_desc: "simualted pon" sw_desc: "simualted pon"\ |
| 35 | serial_num: "2f150d56afa2405eba3ba24e33ce8df9" dp_desc: "n/a"}' |
| 36 | device.switch_features = '{ n_buffers: 256 n_tables: 2 capabilities: 15 }' |
| 37 | device.root_device_id = "a245bd8bb8b8" |
| 38 | devices = [device] |
| 39 | return devices,device |
| 40 | |
| 41 | def gen_packet_in(self): |
| 42 | packet_in = 1 |
| 43 | return packet_in |
| 44 | |
| 45 | def test_connection_mgr_init(self): |
| 46 | consul_endpoint,voltha_endpoint,controller_endpoints = self.gen_endpoints() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 47 | my_name = self.gen_container_name() |
| 48 | test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 49 | self.assertEqual(test_connection_init.consul_endpoint,consul_endpoint) |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 50 | self.assertEqual(test_connection_init.vcore_endpoint, voltha_endpoint) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 51 | self.assertEqual(test_connection_init.controller_endpoints, controller_endpoints) |
| 52 | |
| 53 | def test_resolve_endpoint(self): |
| 54 | consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 55 | my_name = self.gen_container_name() |
| 56 | test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 57 | host,port = test_connection_init.resolve_endpoint(endpoint=consul_endpoint) |
| 58 | assert isinstance(port, int) |
| 59 | assert isinstance(host, basestring) |
| 60 | |
| 61 | def test_refresh_agent_connections(self): |
| 62 | consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 63 | my_name = self.gen_container_name() |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 64 | devices,device = self.gen_devices() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 65 | test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 66 | test_connection_init.refresh_agent_connections(devices) |
| 67 | |
| 68 | def test_create_agent(self): |
| 69 | consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 70 | my_name = self.gen_container_name() |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 71 | devices,device = self.gen_devices() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 72 | test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 73 | test_connection_init.create_agent(device) |
| 74 | |
| 75 | def test_delete_agent(self): |
| 76 | consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 77 | my_name = self.gen_container_name() |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 78 | devices,device = self.gen_devices() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 79 | test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 80 | test_connection_init.create_agent(device) |
| 81 | with self.assertRaises(Exception) as context: |
| 82 | test_connection_init.delete_agent(device.datapath_id) |
| 83 | print context.exception |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 84 | self.assertTrue('\'NoneType\' object has no attribute \'disconnect\'' in str(context.exception)) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 85 | |
| 86 | def test_forward_packet_in(self): |
| 87 | consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 88 | my_name = self.gen_container_name() |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 89 | devices,device = self.gen_devices() |
| 90 | packet_in = self.gen_packet_in() |
Richard Jankowski | c9d8920 | 2018-01-25 10:25:10 -0500 | [diff] [blame] | 91 | test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 92 | test_connection_init.create_agent(device) |
| 93 | test_connection_init.forward_packet_in(device.id, packet_in) |
| 94 | |
| 95 | if __name__ == '__main__': |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 96 | main() |