From 0a93135b4eb1d9a369cba7c147068b519784a645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Abdelkader=20Mart=C3=ADnez=20P=C3=A9rez?= Date: Tue, 20 Aug 2019 10:29:58 +0200 Subject: [PATCH] Check for reachable API in initial step. --- spec/test/features/steps/steps.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/spec/test/features/steps/steps.py b/spec/test/features/steps/steps.py index 145891c..c95a2d1 100644 --- a/spec/test/features/steps/steps.py +++ b/spec/test/features/steps/steps.py @@ -1,8 +1,11 @@ import subprocess from time import sleep +import shlex +import socket +from contextlib import suppress import requests -from environconfig import EnvironConfig, StringVar +from environconfig import EnvironConfig, StringVar, IntVar class Env(EnvironConfig): @@ -15,17 +18,31 @@ class Env(EnvironConfig): #: Where the Data API is KAPOW_DATAAPI_URL = StringVar(default="http://localhost:8080") + KAPOW_BOOT_TIMEOUT = IntVar(default=10) @given('I have a just started Kapow! server') @given('I have a running Kapow! server') def step_impl(context): context.server = subprocess.Popen( - Env.KAPOW_SERVER_CMD, + shlex.split(Env.KAPOW_SERVER_CMD), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, - shell=True) - is_running = context.server.poll() is None - assert is_running, "Server is not running!" + shell=False) + + # Check process is running with reachable APIs + open_ports = False + for _ in range(Env.KAPOW_BOOT_TIMEOUT): + is_running = context.server.poll() is None + assert is_running, "Server is not running!" + with suppress(requests.exceptions.ConnectionError): + open_ports = ( + requests.head(Env.KAPOW_CONTROLAPI_URL, timeout=1).status_code + and requests.head(Env.KAPOW_DATAAPI_URL, timeout=1).status_code) + if open_ports: + break + sleep(1) + + assert open_ports, "API is unreachable after KAPOW_BOOT_TIMEOUT" @when('I request a routes listing')