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