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