blob: 1e42a3b28ca455d4c44a6c39dd670e6b0f25b83a [file] [log] [blame]
Khen Nursimulu37a9bf82016-10-16 20:11:31 -04001#!/usr/bin/env python
2#
3# Copyright 2016 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#
17import subprocess
18import time
19import logging
alshabib16c0da72017-01-19 12:26:02 -060020from common.utils.consulhelpers import verify_all_services_healthy
Khen Nursimulu37a9bf82016-10-16 20:11:31 -040021import os
22import json
23from unittest import TestCase
24import re
25
26this_dir = os.path.abspath(os.path.dirname(__file__))
27
28from test_utils import run_command_to_completion_with_raw_stdout, \
29 is_open, \
30 is_valid_ip, \
31 run_long_running_command_with_timeout, \
32 run_command_to_completion_with_stdout_in_list
33
34log = logging.getLogger(__name__)
35
alshabib16c0da72017-01-19 12:26:02 -060036LOCAL_CONSUL = "localhost:8500"
37LOCAL_CONSUL_URL = "http://%s" % LOCAL_CONSUL
Khen Nursimulu37a9bf82016-10-16 20:11:31 -040038LOCAL_CONSUL_DNS = "@localhost -p 8600"
39DOCKER_COMPOSE_FILE = "compose/docker-compose-system-test.yml"
40DOCKER_COMPOSE_FILE_SERVICES_COUNT = 7
41
42command_defs = dict(
43 makefile_fetch_images="grep \"docker pull\" Makefile",
44 make="make",
45 make_fetch="make fetch",
46 remove_env_directory="rm -rf venv-linux",
47 make_clean="make clean",
48 docker_images="docker images",
49 docker_stop="docker stop",
50 docker_rm="docker rm",
51 fluentd_logs="less /tmp/fluentd/data.log",
52 docker_voltha_logs="docker logs -f compose_voltha_1",
53 docker_compose_logs="docker-compose -f {} logs".format(
54 DOCKER_COMPOSE_FILE),
55 docker_stop_and_remove_all_containers="docker stop `docker ps -q` ; "
56 "docker rm `docker ps -a -q`",
57 docker_start_voltha="docker run -ti --rm cord/voltha",
58 docker_start_voltha_with_consul_ip="docker run -ti --rm --net="
59 "compose_default cord/voltha "
Khen Nursimulu96bb5322016-11-09 20:16:03 -080060 "/voltha/voltha/main.py --consul=",
Khen Nursimulu37a9bf82016-10-16 20:11:31 -040061 docker_get_consul_ip="docker inspect "
62 "compose_consul_1 | jq -r "
63 "'.[0].NetworkSettings.Networks."
64 "compose_default.IPAddress'",
65 docker_compose_start_consul="docker-compose -f {} up -d "
66 "consul".format(DOCKER_COMPOSE_FILE),
67 docker_compose_start_all="docker-compose -f {} up -d "
68 .format(DOCKER_COMPOSE_FILE),
69 docker_compose_stop="docker-compose -f {} stop"
70 .format(DOCKER_COMPOSE_FILE),
71 docker_compose_rm_f="docker-compose -f {} rm -f"
72 .format(DOCKER_COMPOSE_FILE),
73 docker_compose_ps="docker-compose -f {} ps".format(DOCKER_COMPOSE_FILE),
74 docker_ps="docker ps",
75 docker_ps_count="docker ps -q | wc -l",
76 docker_compose_is_consul_up="docker-compose -f {} ps | grep consul"
77 .format(DOCKER_COMPOSE_FILE),
78 consul_get_leader_ip_port="curl -s {}/v1/status/leader | jq -r ."
79 .format(LOCAL_CONSUL_URL),
80 docker_compose_services_running="docker-compose -f {} ps -q"
81 .format(DOCKER_COMPOSE_FILE),
82 docker_compose_services_running_count="docker-compose -f {} ps -q | "
83 "grep Up "
84 "| wc -l".format(
85 DOCKER_COMPOSE_FILE),
86 docker_compose_services="docker-compose -f {} config --services"
87 .format(DOCKER_COMPOSE_FILE),
88 consul_get_services="curl -s {}/v1/catalog/services | jq -r ."
89 .format(LOCAL_CONSUL_URL),
90 consul_get_srv_voltha_health="curl -s {}/v1/catalog/service/voltha-health "
91 "| jq -r .".format(LOCAL_CONSUL_URL),
92 kafka_client_run_10_secs="python kafka/kafka-consumer.py -r 10",
93 consul_get_voltha_rest_a_record="dig {} voltha-health.service.consul"
94 .format(LOCAL_CONSUL_DNS),
95 consul_get_voltha_rest_ip="dig {} +short voltha-health.service.consul"
96 .format(LOCAL_CONSUL_DNS),
97 consul_get_voltha_service_port="dig {} +short "
98 "voltha-health.service.consul SRV | "
99 " awk \'{{print $3}}'"
100 .format(LOCAL_CONSUL_DNS),
101 docker_compose_scale_voltha_to_10="docker-compose -f {} scale "
102 "voltha=10".format(DOCKER_COMPOSE_FILE),
103 docker_compose_scaled_voltha_ps="docker-compose -f {} ps voltha | "
104 "grep Up | wc -l"
105 .format(DOCKER_COMPOSE_FILE),
106 consul_verify_voltha_registration="curl -s {}"
107 "/v1/kv/service/voltha/members?recurse |"
108 " jq -r .".format(LOCAL_CONSUL_DNS)
109)
110
111
112class BuildMdTests(TestCase):
113 # docker_client = Client(base_url='unix://var/run/docker.sock')
114
115 def test_01_setup(self):
116 print "Test_01_setup_Start:------------------"
117 t0 = time.time()
118
119 try:
120 # remove the venv-linux directory
121 print "Remove venv-linux ..."
122 cmd = command_defs['remove_env_directory']
123 rm_venv, err, rc = run_command_to_completion_with_raw_stdout(cmd)
124 self.assertEqual(rc, 0)
125
126 # make clean
127 print "Make clean ..."
128 cmd = command_defs['make_clean']
129 mk_clean, err, rc = run_command_to_completion_with_raw_stdout(cmd)
130 self.assertEqual(rc, 0)
131
132 # source the env
133 print "Source environment ..."
134 self._source_env()
135
136 finally:
137 print "Test_01_setup_End:------------------ took {} " \
138 "secs\n\n".format(time.time() - t0)
139
140 def test_02_make_fetch(self):
141 print "Test_02_make_fetch_Start:------------------"
142 t0 = time.time()
143
144 try:
145 # Get list of images to fetch from the Makefile
146 print "Get list of images to fetch ..."
147 cmd = command_defs['makefile_fetch_images']
148 makefile_images_to_fetch, err, rc \
149 = run_command_to_completion_with_stdout_in_list(cmd)
150 self.assertEqual(rc, 0)
151
152 images_to_fetch = []
153 for image in makefile_images_to_fetch:
154 tmp = ''.join(image.split())
155 images_to_fetch.append(tmp[len('dockerpull'):])
156
157 # make fetch
158 print "Fetching images {} ...".format(images_to_fetch)
159 cmd = command_defs['make_fetch']
160 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
161 self.assertEqual(rc, 0)
162
163 # verify that the images have been downloaded
164 print "Verify images downloaded and present locally ..."
165 cmd = command_defs['docker_images']
166 local_images, err, rc = \
167 run_command_to_completion_with_stdout_in_list(cmd)
168 self.assertEqual(rc, 0)
169
170 local_images_list = []
171 for local_image in local_images:
172 words = local_image.split()
173 local_images_list.append('{}:{}'.format(words[0], words[1]))
174
175 intersection_list = [i for i in images_to_fetch if
176 i in local_images_list]
177 assert len(intersection_list) == len(images_to_fetch)
178
179 finally:
180 print "Test_02_make_fetch_End:------------------ took {} " \
181 "secs \n\n".format(time.time() - t0)
182
183 def test_03_make(self):
184 print "Test_03_make_Start:------------------"
185 t0 = time.time()
186 try:
187 cmd = command_defs['make']
188 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
189 self.assertEqual(rc, 0)
190 finally:
Khen Nursimulua54b6632016-10-18 18:01:25 -0400191 print "Test_03_make_Start:------------------ took {} secs \n\n" \
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400192 .format(time.time() - t0)
193
194 def test_04_run_voltha_standalone_without_consul(self):
195 print "Test_04_run_voltha_standalone_without_consul_Start:------------" \
196 "------"
197 t0 = time.time()
198
199 try:
200 # Run voltha for 10 secs and verity the following lines are displayed
201 # (a subset of output messages along with a flag when found)
202 print "Start voltha ..."
203 expected_output_subset = [
204 'main.print_banner {event: (to stop: press Ctrl-C), '
205 'instance_id:',
Khen Nursimulu96bb5322016-11-09 20:16:03 -0800206 'coordinator.__init__ {event: initializing-coordinator,',
207 'grpc_server.start {event: started',
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400208 'main.<lambda> {event: twisted-reactor-started',
209 'main.startup_components {event: started-internal-services,',
Khen Nursimulu9b9f1ad2017-01-10 15:43:32 -0500210 'kafka_proxy.start {event: started,',
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400211 'coordinator._backoff {retry_in: 5, event: consul-not-up,'
212 ]
213
214 cmd = command_defs['docker_start_voltha']
215 command_output = run_long_running_command_with_timeout(cmd, 10)
216
217 # There should at least be 1 line in the output
218 self.assertGreater(len(command_output), 0)
219
220 # Verify that the output contained the expected_output_subset -
221 # save the docker instance id
222 print "Verify voltha started correctly ..."
223 instance_id = None
224 for ext_output in expected_output_subset:
225 match_str = next(
226 (out for out in command_output if ext_output in out),
227 None)
228 self.assertIsNotNone(match_str)
229 if "instance_id" in ext_output:
230 instance_id = re.findall(r'[0-9a-f]+', match_str)[-1]
231
232 # Now stop the voltha docker that was created
233 print "Stop voltha ..."
234 self._stop_docker_container_by_id(instance_id)
235
236
237 finally:
238 # Remove any created container
239 self._stop_and_remove_all_containers()
240
241 print "Test_04_run_voltha_standalone_without_consul_End" \
242 ":------------------ took {} secs \n\n".format(
243 time.time() - t0)
244
245 def test_05_run_consul_only(self):
246 print "Test_05_run_consul_only_Start:------------------ "
247 t0 = time.time()
248
249 try:
250 # run consul
251 print "Start consul ..."
252 self._run_consul()
253
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400254 print "Waiting for consul to be ready ..."
Khen Nursimulua54b6632016-10-18 18:01:25 -0400255 rc = self._wait_for_consul_to_be_ready()
256 self.assertEqual(rc, 0)
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400257
258 # Get the docker IP address and port number of the consul instance
259 print "Get consul leader IP ..."
260 cmd = command_defs['consul_get_leader_ip_port']
261 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
262 self.assertEqual(rc, 0)
263
264 # validate that the returned ip:port is valid and open
265 print "Verify consul IP and port is reachable ..."
266 self.assertTrue(is_open(out))
267
268 finally:
269 # clean up all created containers for this test
270 print "Stop consul ..."
271 self._stop_and_remove_all_containers()
272
273 print "Test_05_run_consul_only_End:------------------ took {} secs" \
274 "\n\n".format(time.time() - t0)
275
276 def test_06_run_voltha_standalone_with_consul_only(self):
277 print "Test_06_run_voltha_standalone_with_consul_only_Start:----------" \
278 "-------- "
279 t0 = time.time()
280
281 try:
282 # run consul first
283 print "Start consul ..."
284 self._run_consul()
285
286 # get consul ip
287 print "Get consul IP ..."
288 cmd = command_defs['docker_get_consul_ip']
289 consul_ip, err, rc = run_command_to_completion_with_raw_stdout(cmd)
290 self.assertEqual(rc, 0)
291 self.assertIsNotNone(consul_ip)
292
293 # start voltha now for 15 secs and verify it can now connect to
294 # consul - following message in the output
295 print "Start voltha with consul IP ..."
296 expected_pattern = ['coordinator', 'event: created-consul-session']
297 cmd = command_defs['docker_start_voltha_with_consul_ip'] + \
298 '{}:8500'.format(consul_ip.strip())
299 command_output = run_long_running_command_with_timeout(cmd, 10)
300
301 # Verify the output of voltha and get the container instance id
302 print "Verify voltha is registered with consul ..."
303 instance_id = None
304 for out in command_output:
305 if all(ep for ep in expected_pattern if ep in out):
306 self.assertTrue(True)
307 instance_id = re.findall(r'[0-9a-f]+', out)[-1]
308 break
309
310 self.assertIsNotNone(instance_id)
311
312 # Verify Voltha's self-registration with consul
313 expected_output = ['ModifyIndex', 'CreateIndex', 'Session',
314 'Value',
315 'Flags', 'Key', 'LockIndex']
316
317 cmd = command_defs['consul_verify_voltha_registration']
318 registration_info, err, rc = \
319 run_command_to_completion_with_raw_stdout(cmd)
320 self.assertEqual(rc, 0)
321 try:
322 jr_info = json.loads(registration_info)
323 intersect_elems = [e for e in jr_info[0] if
324 e in expected_output]
325 self.assertEqual(len(expected_output), len(intersect_elems))
326 except Exception as e:
327 self.assertRaises(e)
328
329 # stop voltha
330 print "Stop voltha ..."
331 self._stop_docker_container_by_id(instance_id)
332
333 # check the service has deregistered
334 print "Verify voltha is no longer registered in consul..."
335 cmd = command_defs['consul_verify_voltha_registration']
336 registration_info, err, rc = \
337 run_command_to_completion_with_raw_stdout(cmd)
338 self.assertEqual(rc, 0)
339 self.assertEqual(registration_info, '')
340
341 finally:
342 # clean up all created containers for this test
343 print "Stop consul ..."
344 self._stop_and_remove_all_containers()
345
346 print "Test_06_run_voltha_standalone_with_consul_only_End:--------" \
347 "---------- took {} " \
348 "secs \n\n".format(time.time() - t0)
349
350 def test_07_start_all_containers(self):
351 print "Test_07_start_all_containers_Start:------------------ "
352 t0 = time.time()
353
354 try:
355 # Pre-test - clean up all running docker containers
356 print "Pre-test: Removing all running containers ..."
357 self._stop_and_remove_all_containers()
358
359 # get a list of services in the docker-compose file
360 print "Getting list of services in docker compose file ..."
361 cmd = command_defs['docker_compose_services']
362 services, err, rc = run_command_to_completion_with_raw_stdout(cmd)
363 self.assertEqual(rc, 0)
364 docker_service_list = services.split()
365 self.assertGreaterEqual(len(docker_service_list),
366 DOCKER_COMPOSE_FILE_SERVICES_COUNT)
367
368 # start all the containers
369 print "Starting all containers ..."
370 cmd = command_defs['docker_compose_start_all']
371 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
372 self.assertEqual(rc, 0)
373
Khen Nursimulua54b6632016-10-18 18:01:25 -0400374 print "Waiting for all containers to be ready ..."
alshabib16c0da72017-01-19 12:26:02 -0600375 time.sleep(10)
376 rc = verify_all_services_healthy(LOCAL_CONSUL)
377 if not rc:
378 print "Not all services are up"
379 self.assertEqual(rc, True)
Khen Nursimulua54b6632016-10-18 18:01:25 -0400380
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400381 # verify that all containers are running
382 print "Verify all services are running using docker command ..."
383 for service in docker_service_list:
384 cmd = command_defs['docker_compose_ps'] + ' {} | wc -l'.format(
385 service)
386 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
387 self.assertEqual(rc, 0)
388 self.assertGreaterEqual(out, 3) # 2 are for headers
389
390 # Verify that 'docker ps' return the same number of running process
391 cmd = command_defs['docker_ps_count']
392 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
393 self.assertEqual(rc, 0)
Khen Nursimulu283d7682016-11-11 16:37:32 -0500394 self.assertGreaterEqual(out.split(), [str(len(
395 docker_service_list))])
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400396
397 # Retrieve the list of services from consul and validate against
398 # the list obtained from docker composed
399 print "Verify all services are registered in consul ..."
400 expected_services = ['consul-rest', 'fluentd-intake',
401 'chameleon-rest', 'voltha-grpc',
402 'voltha-health',
Khen Nursimulu9b9f1ad2017-01-10 15:43:32 -0500403 'consul-8600', 'zookeeper', 'consul',
404 'kafka']
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400405
406 cmd = command_defs['consul_get_services']
407 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
408 self.assertEqual(rc, 0)
409 try:
410 consul_services = json.loads(out)
411 intersected_services = [s for s in expected_services if
412 s in consul_services]
413 self.assertEqual(len(intersected_services),
414 len(expected_services))
415 # services_match = 0
416 # for d_service in docker_service_list:
417 # for c_service in consul_services:
418 # if c_service.find(d_service) != -1:
419 # services_match += 1
420 # print d_service, c_service
421 # break
422 # self.assertEqual(services_match, len(docker_service_list))
423 except Exception as e:
424 self.assertRaises(e)
425
426 # Verify the service record of the voltha service
427 print "Verify the service record of voltha in consul ..."
428 expected_srv_elements = ['ModifyIndex', 'CreateIndex',
429 'ServiceEnableTagOverride', 'Node',
430 'Address', 'TaggedAddresses', 'ServiceID',
431 'ServiceName', 'ServiceTags',
432 'ServiceAddress', 'ServicePort']
433 cmd = command_defs['consul_get_srv_voltha_health']
434 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
435 self.assertEqual(rc, 0)
436 try:
437 srv = json.loads(out)
438 intersect_elems = [e for e in srv[0] if
439 e in expected_srv_elements]
440 self.assertEqual(len(expected_srv_elements),
441 len(intersect_elems))
442 except Exception as e:
443 self.assertRaises(e)
444
445 # Verify kafka client is receiving the messages
446 print "Verify kafka client is receiving the heartbeat messages ..."
Zsolt Harasztib5d72f12017-01-15 20:44:02 -0800447 expected_pattern = ['voltha.heartbeat', 'heartbeat']
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400448 cmd = command_defs['kafka_client_run_10_secs']
449 kafka_client_output = run_long_running_command_with_timeout(cmd,
Khen Nursimulua54b6632016-10-18 18:01:25 -0400450 20)
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400451
452 # Verify the kafka client output
453 # instance id
454 found = False
455 for out in kafka_client_output:
456 if all(ep for ep in expected_pattern if ep in out):
457 found = True
458 break
459 self.assertTrue(found)
460
461 # verify docker-compose logs are being produced - just get the
462 # first work of each line
463 print "Verify docker compose logs has output from all the services " \
464 "..."
465 expected_output = ['voltha_1', 'fluentd_1', 'consul_1',
466 'registrator_1', 'kafka_1', 'zookeeper_1',
Khen Nursimulu9b9f1ad2017-01-10 15:43:32 -0500467 'chameleon_1', 'ofagent_1', 'netconf_1']
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400468 cmd = command_defs['docker_compose_logs']
469 docker_compose_logs = run_long_running_command_with_timeout(cmd, 5,
470 0)
471 intersected_logs = [l for l in expected_output if
472 l in docker_compose_logs]
473 self.assertEqual(len(intersected_logs), len(expected_output))
474
475 # TODO: file in /tmp/fluentd/ cannot be found
476 # # verify fluentd logs are being produced - we will just verify
477 # that there are "voltha.logging" in the logs
478 # os.environ["PYTHONPATH"] += os.pathsep + "/tmp/fluentd/"
479 # os.environ['PATH'] += os.pathsep + "/tmp/fluentd/"
480 # expected_output=['voltha.logging']
481 # cmd = command_defs['fluentd_logs']
482 # fluentd_logs, err = run_command_to_completion_with_raw_stdout(cmd)
483 # # self.assertIsNone(err)
484 # print err
485 # intersected_logs = [l for l in expected_output if
486 # l in fluentd_logs]
487 # self.assertEqual(len(intersected_logs), len(expected_output))
488
489 # verify docker voltha logs are being produced - we will just verify
490 # some
491 # key messages in the logs
492 print "Verify docker voltha logs are produced ..."
493 expected_output = ['kafka_proxy.send_message',
494 'coordinator._renew_session', 'main.heartbeat']
495 cmd = command_defs['docker_voltha_logs']
496 docker_voltha_logs = run_long_running_command_with_timeout(cmd,
Khen Nursimulu96bb5322016-11-09 20:16:03 -0800497 0.5, 3)
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400498 intersected_logs = [l for l in expected_output if
499 l in docker_voltha_logs]
500 self.assertEqual(len(intersected_logs), len(expected_output))
501
502 finally:
503 print "Stopping all containers ..."
504 # clean up all created containers for this test
505 self._stop_and_remove_all_containers()
506
507 print "Test_07_start_all_containers_End:------------------ took {}" \
508 " secs \n\n".format(time.time() - t0)
509
510 def test_08_stop_all_containers_started_using_docker_compose(self):
511 print "Test_08_stop_all_containers_started_using_docker_compose_Start:" \
512 "------------------ "
513 t0 = time.time()
514
515 try:
516 # commands to stop and clear the docker images
517 cmds = [command_defs['docker_compose_stop'],
518 command_defs['docker_compose_rm_f']]
519
520 print "Stopping all containers ..."
521 for cmd in cmds:
522 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
523 self.assertEqual(rc, 0)
524
525 # Verify that no docker process is running
526 print "Verify no containers is running..."
527 cmd = command_defs['docker_compose_services_running']
528 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
529 self.assertEqual(rc, 0)
530
531 finally:
532 print "Test_08_stop_all_containers_started_using_docker_compose_:" \
533 "------------------ took {} secs \n\n".format(
534 time.time() - t0)
535
536 def test_09_dig_consul_command(self):
537 print "Test_09_dig_consul_command_Start:------------------"
538 t0 = time.time()
539
540 try:
541 # start all containers
542 print "Start all containers..."
543 self._start_all_containers()
544
Khen Nursimulua54b6632016-10-18 18:01:25 -0400545 print "Waiting for all containers to be ready ..."
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400546 time.sleep(10)
alshabib16c0da72017-01-19 12:26:02 -0600547 rc = verify_all_services_healthy(LOCAL_CONSUL)
548 if not rc:
549 print "Not all services are up"
550 self.assertEqual(rc, True)
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400551
552 # Get the IP address(es) for voltha's REST interface
553 print "Get IP of Voltha REST interface..."
554 cmd = command_defs['consul_get_voltha_rest_a_record']
555 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
556 self.assertEqual(rc, 0)
557 self.assertGreaterEqual(out.find("voltha-health.service.consul"),
558 0)
559
560 # Get only the ip address
561 cmd = command_defs['consul_get_voltha_rest_ip']
562 ip, err, rc = run_command_to_completion_with_raw_stdout(cmd)
563 self.assertEqual(rc, 0)
564 self.assertTrue(is_valid_ip(ip))
565
566 # Get the exposed service port
567 print "Get Voltha exposed service port..."
568 cmd = command_defs['consul_get_voltha_service_port']
569 port, err, rc = run_command_to_completion_with_raw_stdout(cmd)
570 self.assertEqual(rc, 0)
571 # Verify that we can connect to the port using the previously
572 # acquired ip
573 print "Verify connectivity with voltha ip and port..."
574 self.assertTrue(is_open('{}:{}'.format(ip, port)))
575 finally:
576 print "Stopping all containers ..."
577 # clean up all created containers for this test
578 self._stop_and_remove_all_containers()
579
580 print "Test_09_dig_consul_command_Start_End:------------------" \
581 "took {} secs \n\n".format(time.time() - t0)
582
583 def test_10_scale_voltha(self):
584 print "Test_10_scale_voltha_Start:------------------"
585 t0 = time.time()
586
587 try:
588 # start all containers
589 print "Start all containers..."
590 self._start_all_containers()
591
Khen Nursimulua54b6632016-10-18 18:01:25 -0400592 print "Waiting for all containers to be ready ..."
593 time.sleep(10)
594 rc, not_found_list = self._wait_for_all_containers_to_ready()
595 if rc:
596 print "Not found patterns:{}".format(not_found_list)
597 self.assertEqual(rc, 0)
598
Khen Nursimulu37a9bf82016-10-16 20:11:31 -0400599 # Scale voltha to 10 instances
600 print "Scale voltha to 10 instances ..."
601 cmd = command_defs['docker_compose_scale_voltha_to_10']
602 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
603 self.assertEqual(rc, 0)
604
605 # Verify that 10 instances are running
606 print "Verify 10 instances of voltha are running ..."
607 cmd = command_defs['docker_compose_scaled_voltha_ps']
608 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
609 self.assertEqual(rc, 0)
610 self.assertEqual(out.split(), ['10'])
611 finally:
612 print "Stopping all containers ..."
613 # clean up all created containers for this test
614 self._stop_and_remove_all_containers()
615
616 print "Test_10_scale_voltha_End:------------------took {} secs " \
617 "\n\n".format(time.time() - t0)
618
619 def _start_all_containers(self):
620 cmd = command_defs['docker_compose_start_all']
621 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
622 self.assertEqual(rc, 0)
623
624 def _run_consul(self):
625 # run consul
626 cmd = command_defs['docker_compose_start_consul']
627 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
628 self.assertEqual(rc, 0)
629
630 # verify consul is up
631 cmd = command_defs['docker_compose_is_consul_up']
632 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
633 self.assertEqual(rc, 0)
634 self.assertIn('compose_consul_1', out)
635
636 def _stop_and_remove_all_containers(self):
637 # check if there are any running containers first
638 cmd = command_defs['docker_ps']
639 out, err, rc = run_command_to_completion_with_stdout_in_list(cmd)
640 self.assertEqual(rc, 0)
641 if len(out) > 1: # not counting docker ps header
642 cmd = command_defs['docker_stop_and_remove_all_containers']
643 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
644 self.assertEqual(rc, 0)
645
646 def _stop_docker_container_by_id(self, instance_id):
647 # stop
648 cmd = command_defs['docker_stop'] + " {}".format(instance_id)
649 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
650 self.assertEqual(rc, 0)
651
652 # remove
653 cmd = command_defs['docker_rm'] + " {}".format(instance_id)
654 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
655 self.assertEqual(rc, 0)
656
657 def _source_env(self):
658 # Go to voltha root directory
659 res = os.system('cd {}'.format(this_dir))
660 assert res == 0
661
662 # set the env
663 command = ['bash', '-c', '. env.sh']
664 proc = subprocess.Popen(command, stdout=subprocess.PIPE,
665 stderr=subprocess.PIPE)
666
667 if proc.wait() != 0:
668 err_msg = "Failed to source the environment'"
669 raise RuntimeError(err_msg)
670
671 env = os.environ.copy()
672 return env
Khen Nursimulua54b6632016-10-18 18:01:25 -0400673
674 def _wait_for_consul_to_be_ready(self):
675 # Consul is ready when it's leader ip and port is set. The maximum
676 # time to wait of 60 secs as consul should be ready by then
677 max_wait_time = 60
678 t0 = time.time()
679
680 while True:
681 # Get the docker IP address and port number of the consul instance
682 print "waiting for consul to be ready ..."
683 cmd = command_defs['consul_get_leader_ip_port']
684 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
685 out = out.strip()
686 if rc != 0:
687 # Something is wrong, return
688 return -1 # error
689 elif out is not None and out != '':
690 return 0 # found something
691 elif time.time() - t0 > max_wait_time:
692 return -1 # consul should have come up by this time
693 else:
694 time.sleep(2) # constant sleep for testing