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