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