Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
Replace sleep command by testing whether the service ports have opened
Browse files Browse the repository at this point in the history
  • Loading branch information
WGierke committed Nov 19, 2017
1 parent 514ceec commit e33dbd3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tests/test_docker.sh
Expand Up @@ -22,4 +22,4 @@ docker-compose -f local.yml run interface python manage.py test
docker-compose -f local.yml run interface python manage.py makemigrations --dry-run --check || { echo "ERROR: there were changes in the models, but migration listed above have not been created and are not saved in version control"; exit 1; }

# Start the docker containers and discard the output, otherwise we could hit the Travis log length limit. After 60 seconds test the routes.
docker-compose -f local.yml up &> /dev/null & sleep 60; python tests/test_routes.py || { echo "ERROR: test_routes.py did not pass. Check above for details."; exit 1; }
docker-compose -f local.yml up &> /dev/null & python tests/test_routes.py || { echo "ERROR: test_routes.py did not pass. Check above for details."; exit 1; }
46 changes: 33 additions & 13 deletions tests/test_routes.py
@@ -1,12 +1,15 @@
import sys
import time

import requests

TIMEOUT = 60
LOCALHOST = "http://localhost:{}/"
SERVICE_PORTS = {"INTERFACE": {"PORT": 8000, "SITES": ["api"]},
"VUE": {"PORT": 8080, "SITES": []},
"PREDICTION": {"PORT": 8001, "SITES": ["classify/predict/",
"identify/predict/",
"segment/predict/"]},
SERVICE_PORTS = {"INTERFACE": {"PORT": 8000, "SITES": ["api"]},
"VUE": {"PORT": 8080, "SITES": []},
"PREDICTION": {"PORT": 8001, "SITES": ["classify/predict/",
"identify/predict/",
"segment/predict/"]},
"DOCUMENTATION": {"PORT": 8002, "SITES": []}}


Expand All @@ -23,13 +26,30 @@ def page_errors(url):

if __name__ == '__main__':
one_failed = False
start_time = time.time()
services_to_test = list(SERVICE_PORTS.keys())

while time.time() - start_time < TIMEOUT:
for service in services_to_test:
try:
base_url = LOCALHOST.format(SERVICE_PORTS[service]["PORT"])
response = requests.get(base_url)
except requests.exceptions.ConnectionError as e:
print("{} - not up yet. Continuing with next service...".format(base_url))
continue
except Exception as e:
print("{} - Unexpected exception occurred: {}".format(base_url, e))
one_failed = True

one_failed |= page_errors(base_url)
for site in SERVICE_PORTS[service]["SITES"]:
one_failed |= page_errors(base_url + site)
services_to_test.remove(service)

for service in SERVICE_PORTS.keys():
base_url = LOCALHOST.format(SERVICE_PORTS[service]["PORT"])
one_failed |= page_errors(base_url)
for site in SERVICE_PORTS[service]["SITES"]:
page_errors(base_url + site)
if not services_to_test:
sys.exit(1 if one_failed else 0)
time.sleep(1)

if one_failed:
sys.exit(1)
sys.exit(0)
closed_ports = [SERVICE_PORTS[service]["PORT"] for service in SERVICE_PORTS.keys()]
print("Time limit of {} seconds exceeded. Following ports did not open in time: {}".format(TIMEOUT, closed_ports))
sys.exit(1)

0 comments on commit e33dbd3

Please sign in to comment.