blob: e851b642ff18869d4495f78c241fd8030e037f29 [file] [log] [blame]
sgovindacc736782017-05-02 20:06:37 +05301#!/usr/bin/env python
2#
3# Copyright 2017 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Stephane Barbariecd51f992017-09-07 16:37:02 -040017from time import time, sleep
sgovindacc736782017-05-02 20:06:37 +053018import logging
19import os
20import json
21from unittest import TestCase,main
22
23this_dir = os.path.abspath(os.path.dirname(__file__))
24
Richard Jankowski8f52afb2018-03-29 14:19:11 -040025from tests.itests.test_utils import run_command_to_completion_with_raw_stdout
Stephane Barbariecd51f992017-09-07 16:37:02 -040026from voltha.protos.device_pb2 import Device
27from google.protobuf.json_format import MessageToDict
28from tests.itests.voltha.rest_base import RestBase
29from common.utils.consulhelpers import get_endpoint_from_consul
sgovindacc736782017-05-02 20:06:37 +053030
31log = logging.getLogger(__name__)
32
33DOCKER_COMPOSE_FILE = "compose/docker-compose-ofagent-test.yml"
Stephane Barbariecd51f992017-09-07 16:37:02 -040034LOCAL_CONSUL = "localhost:8500"
sgovindacc736782017-05-02 20:06:37 +053035
36command_defs = dict(
37 docker_images="docker images",
38 docker_stop="docker stop",
39 docker_rm="docker rm",
40 docker_voltha_logs="docker logs -f compose_voltha_1",
41 docker_compose_logs="docker-compose -f {} logs".format(
42 DOCKER_COMPOSE_FILE),
43 docker_stop_and_remove_all_containers="docker stop `docker ps -q` ; "
44 "docker rm `docker ps -a -q`",
45 docker_compose_start_all="docker-compose -f {} up -d "
46 .format(DOCKER_COMPOSE_FILE),
47 docker_compose_stop="docker-compose -f {} stop"
48 .format(DOCKER_COMPOSE_FILE),
49 docker_compose_rm_f="docker-compose -f {} rm -f"
50 .format(DOCKER_COMPOSE_FILE),
51 docker_compose_ps="docker-compose -f {} ps".format(DOCKER_COMPOSE_FILE),
52 docker_ps="docker ps",
53 onos_form_cluster="./tests/itests/ofagent/onos-form-cluster",
54 onos1_ip="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos1",
55 onos2_ip ="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos2",
56 onos3_ip="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos3",
sgovindacc736782017-05-02 20:06:37 +053057 onos1_devices="curl -u karaf:karaf http://localhost:8181/onos/v1/devices",
58 onos2_devices="curl -u karaf:karaf http://localhost:8182/onos/v1/devices",
59 onos3_devices="curl -u karaf:karaf http://localhost:8183/onos/v1/devices")
60
Stephane Barbariecd51f992017-09-07 16:37:02 -040061class TestOFAGENT_MultiController(RestBase):
sgovindacc736782017-05-02 20:06:37 +053062 # Test OFAgent Support for Multiple controller
63 def setUp(self):
64 # Run Voltha,OFAgent,3 ONOS and form ONOS cluster.
65 print "Starting all containers ..."
66 cmd = command_defs['docker_compose_start_all']
67 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
68 self.assertEqual(rc, 0)
69 print "Waiting for all containers to be ready ..."
Stephane Barbariecd51f992017-09-07 16:37:02 -040070 sleep(80)
sgovindacc736782017-05-02 20:06:37 +053071 cmd = command_defs['onos1_ip']
72 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
73 self.assertEqual(rc, 0)
74 onos1_ip = out
75 print "ONOS1 IP is {}".format(onos1_ip)
76 cmd = command_defs['onos2_ip']
77 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
78 self.assertEqual(rc, 0)
79 onos2_ip = out
80 print "ONOS2 IP is {}".format(onos2_ip)
81 cmd = command_defs['onos3_ip']
82 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
83 self.assertEqual(rc, 0)
84 onos3_ip = out
85 print "ONOS3 IP is {}".format(onos3_ip)
86 cmd = command_defs['onos_form_cluster'] + ' {} {} {}'.format(onos1_ip.strip(),onos2_ip.strip(),onos3_ip.strip())
87 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
88 self.assertEqual(rc, 0)
89 print "Cluster Output :{} ".format(out)
90
Stephane Barbariecd51f992017-09-07 16:37:02 -040091 self.get_rest_endpoint()
92
sgovindacc736782017-05-02 20:06:37 +053093 def tearDown(self):
94 # Stopping and Removing Voltha,OFAgent,3 ONOS.
95 print "Stopping and removing all containers ..."
96 cmd = command_defs['docker_compose_stop']
97 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
98 self.assertEqual(rc, 0)
99 print "Waiting for all containers to be stopped ..."
Stephane Barbariecd51f992017-09-07 16:37:02 -0400100 sleep(1)
sgovindacc736782017-05-02 20:06:37 +0530101 cmd = command_defs['docker_compose_rm_f']
102 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
103 self.assertEqual(rc, 0)
104
Stephane Barbariecd51f992017-09-07 16:37:02 -0400105 def wait_till(self, msg, predicate, interval=0.1, timeout=5.0):
106 deadline = time() + timeout
107 while time() < deadline:
108 if predicate():
109 return
110 sleep(interval)
111 self.fail('Timed out while waiting for condition: {}'.format(msg))
112
113 def get_rest_endpoint(self):
114 # Retrieve details on the REST entry point
Richard Jankowski461cb972018-04-11 15:36:27 -0400115 rest_endpoint = get_endpoint_from_consul(LOCAL_CONSUL, 'voltha-envoy-8443')
Stephane Barbariecd51f992017-09-07 16:37:02 -0400116
117 # Construct the base_url
118 self.base_url = 'https://' + rest_endpoint
119
120 def add_device(self):
121 print "Adding device"
122
123 device = Device(
124 type='simulated_olt',
125 mac_address='01:0c:e2:31:40:00'
126 )
127 device = self.post('/api/v1/devices', MessageToDict(device),
128 expected_http_code=200)
129
130 print "Added device - id:{}, type:{}".format(device['id'], device['type'])
131 sleep(5)
132
133 return device
134
135 def enable_device(self, device_id):
136 print "Enabling device - id:{}".format(device_id)
137
138 path = '/api/v1/devices/{}'.format(device_id)
139 self.post(path + '/enable', expected_http_code=200)
140 device = self.get(path)
141 self.assertEqual(device['admin_state'], 'ENABLED')
142
143 self.wait_till(
144 'admin state moves to ACTIVATING or ACTIVE',
145 lambda: self.get(path)['oper_status'] in ('ACTIVATING', 'ACTIVE'),
146 timeout=0.5)
147
148 # eventually, it shall move to active state and by then we shall have
149 # device details filled, connect_state set, and device ports created
150 self.wait_till(
151 'admin state ACTIVE',
152 lambda: self.get(path)['oper_status'] == 'ACTIVE',
153 timeout=0.5)
154 device = self.get(path)
155 images = device['images']
156 image = images['image']
157 image_1 = image[0]
158 version = image_1['version']
159 self.assertNotEqual(version, '')
160 self.assertEqual(device['connect_status'], 'REACHABLE')
161
162 ports = self.get(path + '/ports')['items']
163 self.assertEqual(len(ports), 2)
164
165 sleep(30)
166 print "Enabled device - id:{}".format(device_id)
167
sgovindacc736782017-05-02 20:06:37 +0530168 def test_ofagent_controller_failover(self):
Stephane Barbariecd51f992017-09-07 16:37:02 -0400169 olt_device = self.add_device()
sgovindacc736782017-05-02 20:06:37 +0530170 print "Output of ADD OLT is {} {} {}".format(olt_device, type(olt_device), olt_device['id'])
Stephane Barbariecd51f992017-09-07 16:37:02 -0400171 sleep(5)
172 self.enable_device(olt_device['id'])
sgovindacc736782017-05-02 20:06:37 +0530173 print "Waiting for OLT device to be activated ..."
Stephane Barbariecd51f992017-09-07 16:37:02 -0400174 sleep(80)
sgovindacc736782017-05-02 20:06:37 +0530175 cmd = command_defs['onos1_devices']
176 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
177 self.assertEqual(rc, 0)
178 onos1_devices = json.loads(out)
179 onos1_role = onos1_devices['devices'][0]['role']
180 print "Role of ONOS1 is {}".format(onos1_role)
181 cmd = command_defs['onos2_devices']
182 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
183 self.assertEqual(rc, 0)
184 onos2_devices = json.loads(out)
185 onos2_role = onos2_devices['devices'][0]['role']
186 print "Role of ONOS2 is {}".format(onos2_role)
187 cmd = command_defs['onos3_devices']
188 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
189 self.assertEqual(rc, 0)
190 onos3_devices = json.loads(out)
191 onos3_role = onos3_devices['devices'][0]['role']
192 print "Role of ONOS3 is {}".format(onos3_role)
193 if onos1_role == "MASTER":
194 cmd = command_defs['docker_stop']+ ' onos1'
195 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
196 self.assertEqual(rc, 0)
197 print "Waiting for ONOS to Elect New Master"
Stephane Barbariecd51f992017-09-07 16:37:02 -0400198 sleep(20)
sgovindacc736782017-05-02 20:06:37 +0530199 cmd = command_defs['onos2_devices']
200 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
201 self.assertEqual(rc, 0)
202 onos2_devices = json.loads(out)
203 onos2_role = onos2_devices['devices'][0]['role']
204 print "Role of ONOS2 is {}".format(onos2_role)
205 cmd = command_defs['onos3_devices']
206 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
207 self.assertEqual(rc, 0)
208 onos3_devices = json.loads(out)
209 onos3_role = onos3_devices['devices'][0]['role']
210 print "Role of ONOS3 is {}".format(onos3_role)
211 assert (onos3_role == "MASTER" or onos2_role == "MASTER"), "Exception,New Master Election Failed"
212 elif onos2_role == "MASTER":
213 cmd = command_defs['docker_stop']+ ' onos2'
214 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
215 self.assertEqual(rc, 0)
216 print "Waiting for ONOS to Elect New Master"
Stephane Barbariecd51f992017-09-07 16:37:02 -0400217 sleep(20)
sgovindacc736782017-05-02 20:06:37 +0530218 cmd = command_defs['onos1_devices']
219 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
220 self.assertEqual(rc, 0)
221 onos1_devices = json.loads(out)
222 onos1_role = onos1_devices['devices'][0]['role']
223 print "Role of ONOS1 is {}".format(onos1_role)
224 cmd = command_defs['onos3_devices']
225 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
226 self.assertEqual(rc, 0)
227 onos3_devices = json.loads(out)
228 onos3_role = onos3_devices['devices'][0]['role']
229 print "Role of ONOS3 is {}".format(onos3_role)
230 assert (onos3_role == "MASTER" or onos1_role == "MASTER"), "Exception,New Master Election Failed"
231 elif onos3_role == "MASTER":
232 cmd = command_defs['docker_stop']+ ' onos3'
233 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
234 self.assertEqual(rc, 0)
235 print "Waiting for ONOS to Elect New Master"
Stephane Barbariecd51f992017-09-07 16:37:02 -0400236 sleep(20)
sgovindacc736782017-05-02 20:06:37 +0530237 cmd = command_defs['onos1_devices']
238 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
239 self.assertEqual(rc, 0)
240 onos1_devices = json.loads(out)
241 onos1_role = onos1_devices['devices'][0]['role']
242 print "Role of ONOS1 is {}".format(onos1_role)
243 cmd = command_defs['onos2_devices']
244 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
245 self.assertEqual(rc, 0)
246 onos2_devices = json.loads(out)
247 onos2_role = onos2_devices['devices'][0]['role']
248 print "Role of ONOS2 is {}".format(onos2_role)
249 assert (onos1_role == "MASTER" or onos2_role == "MASTER"), "Exception,New Master Election Failed"
250