blob: 2429cbbcbf348f24b59ade39c471f535c5e7958d [file] [log] [blame]
Richard Jankowskic9d89202018-01-25 10:25:10 -05001import os
sgovindacc736782017-05-02 20:06:37 +05302from unittest import TestCase, main
3from connection_mgr import ConnectionManager
4
5class 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 Jankowskic9d89202018-01-25 10:25:10 -050013 def gen_container_name(self):
14 instance_id = os.environ.get('HOSTNAME', 'localhost')
15 return instance_id
16
sgovindacc736782017-05-02 20:06:37 +053017 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 Jankowskic9d89202018-01-25 10:25:10 -050034 my_name = self.gen_container_name()
35 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053036 self.assertEqual(test_connection_init.consul_endpoint,consul_endpoint)
sathishg23ac35c2017-10-14 03:21:43 +053037 self.assertEqual(test_connection_init.vcore_endpoint, voltha_endpoint)
sgovindacc736782017-05-02 20:06:37 +053038 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 Jankowskic9d89202018-01-25 10:25:10 -050042 my_name = self.gen_container_name()
43 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053044 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 Jankowskic9d89202018-01-25 10:25:10 -050050 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053051 devices,device = self.gen_devices()
Richard Jankowskic9d89202018-01-25 10:25:10 -050052 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053053 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 Jankowskic9d89202018-01-25 10:25:10 -050057 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053058 devices,device = self.gen_devices()
Richard Jankowskic9d89202018-01-25 10:25:10 -050059 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053060 test_connection_init.create_agent(device)
61
62 def test_delete_agent(self):
63 consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints()
Richard Jankowskic9d89202018-01-25 10:25:10 -050064 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053065 devices,device = self.gen_devices()
Richard Jankowskic9d89202018-01-25 10:25:10 -050066 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053067 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
sathishg23ac35c2017-10-14 03:21:43 +053071 self.assertTrue('\'NoneType\' object has no attribute \'disconnect\'' in str(context.exception))
sgovindacc736782017-05-02 20:06:37 +053072
73 def test_forward_packet_in(self):
74 consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints()
Richard Jankowskic9d89202018-01-25 10:25:10 -050075 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053076 devices,device = self.gen_devices()
77 packet_in = self.gen_packet_in()
Richard Jankowskic9d89202018-01-25 10:25:10 -050078 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053079 test_connection_init.create_agent(device)
80 test_connection_init.forward_packet_in(device.id, packet_in)
81
82if __name__ == '__main__':
sathishg23ac35c2017-10-14 03:21:43 +053083 main()