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