blob: 4ca022cce9f7a1d98bfdf259e7281b27684e51b7 [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001# 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 Jankowskic9d89202018-01-25 10:25:10 -050014import os
sgovindacc736782017-05-02 20:06:37 +053015from unittest import TestCase, main
16from connection_mgr import ConnectionManager
17
18class 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 Jankowskic9d89202018-01-25 10:25:10 -050026 def gen_container_name(self):
27 instance_id = os.environ.get('HOSTNAME', 'localhost')
28 return instance_id
29
sgovindacc736782017-05-02 20:06:37 +053030 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 Jankowskic9d89202018-01-25 10:25:10 -050047 my_name = self.gen_container_name()
48 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053049 self.assertEqual(test_connection_init.consul_endpoint,consul_endpoint)
sathishg23ac35c2017-10-14 03:21:43 +053050 self.assertEqual(test_connection_init.vcore_endpoint, voltha_endpoint)
sgovindacc736782017-05-02 20:06:37 +053051 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 Jankowskic9d89202018-01-25 10:25:10 -050055 my_name = self.gen_container_name()
56 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053057 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 Jankowskic9d89202018-01-25 10:25:10 -050063 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053064 devices,device = self.gen_devices()
Richard Jankowskic9d89202018-01-25 10:25:10 -050065 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053066 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 Jankowskic9d89202018-01-25 10:25:10 -050070 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053071 devices,device = self.gen_devices()
Richard Jankowskic9d89202018-01-25 10:25:10 -050072 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053073 test_connection_init.create_agent(device)
74
75 def test_delete_agent(self):
76 consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints()
Richard Jankowskic9d89202018-01-25 10:25:10 -050077 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053078 devices,device = self.gen_devices()
Richard Jankowskic9d89202018-01-25 10:25:10 -050079 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053080 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
sathishg23ac35c2017-10-14 03:21:43 +053084 self.assertTrue('\'NoneType\' object has no attribute \'disconnect\'' in str(context.exception))
sgovindacc736782017-05-02 20:06:37 +053085
86 def test_forward_packet_in(self):
87 consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints()
Richard Jankowskic9d89202018-01-25 10:25:10 -050088 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053089 devices,device = self.gen_devices()
90 packet_in = self.gen_packet_in()
Richard Jankowskic9d89202018-01-25 10:25:10 -050091 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053092 test_connection_init.create_agent(device)
93 test_connection_init.forward_packet_in(device.id, packet_in)
94
95if __name__ == '__main__':
sathishg23ac35c2017-10-14 03:21:43 +053096 main()