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