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