blob: fe7bcdf74ff84bdd34c4d33abb6adee66dd79b9e [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
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040020 # Set a default for the gRPC timeout (seconds)
21 grpc_timeout = 10
22
sgovindacc736782017-05-02 20:06:37 +053023 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 Jankowskic9d89202018-01-25 10:25:10 -050029 def gen_container_name(self):
30 instance_id = os.environ.get('HOSTNAME', 'localhost')
31 return instance_id
32
sgovindacc736782017-05-02 20:06:37 +053033 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 Jankowskic9d89202018-01-25 10:25:10 -050050 my_name = self.gen_container_name()
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040051 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, self.grpc_timeout,
52 controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053053 self.assertEqual(test_connection_init.consul_endpoint,consul_endpoint)
sathishg23ac35c2017-10-14 03:21:43 +053054 self.assertEqual(test_connection_init.vcore_endpoint, voltha_endpoint)
sgovindacc736782017-05-02 20:06:37 +053055 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 Jankowskic9d89202018-01-25 10:25:10 -050059 my_name = self.gen_container_name()
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040060 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, self.grpc_timeout,
61 controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053062 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 Jankowskic9d89202018-01-25 10:25:10 -050068 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053069 devices,device = self.gen_devices()
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040070 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, self.grpc_timeout,
71 controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053072 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 Jankowskic9d89202018-01-25 10:25:10 -050076 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053077 devices,device = self.gen_devices()
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040078 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, self.grpc_timeout,
79 controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053080 test_connection_init.create_agent(device)
81
82 def test_delete_agent(self):
83 consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints()
Richard Jankowskic9d89202018-01-25 10:25:10 -050084 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053085 devices,device = self.gen_devices()
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040086 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, self.grpc_timeout,
87 controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +053088 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
sathishg23ac35c2017-10-14 03:21:43 +053092 self.assertTrue('\'NoneType\' object has no attribute \'disconnect\'' in str(context.exception))
sgovindacc736782017-05-02 20:06:37 +053093
94 def test_forward_packet_in(self):
95 consul_endpoint, voltha_endpoint, controller_endpoints = self.gen_endpoints()
Richard Jankowskic9d89202018-01-25 10:25:10 -050096 my_name = self.gen_container_name()
sgovindacc736782017-05-02 20:06:37 +053097 devices,device = self.gen_devices()
98 packet_in = self.gen_packet_in()
Richard Jankowskidb9a86e2018-09-17 13:33:29 -040099 test_connection_init = ConnectionManager(consul_endpoint, voltha_endpoint, self.grpc_timeout,
100 controller_endpoints, my_name)
sgovindacc736782017-05-02 20:06:37 +0530101 test_connection_init.create_agent(device)
102 test_connection_init.forward_packet_in(device.id, packet_in)
103
104if __name__ == '__main__':
sathishg23ac35c2017-10-14 03:21:43 +0530105 main()