Skip to content

Commit

Permalink
test_: use health endpoint from python
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Dec 16, 2024
1 parent f508cb7 commit bc8398d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
51 changes: 24 additions & 27 deletions tests-functional/clients/status_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ def __init__(self, await_signals=[]):
host_port = random.choice(option.status_backend_port_range)

self.container = self._start_container(host_port)
assert self._wait_for_container_healthy()
url = f"http://127.0.0.1:{host_port}"
option.status_backend_port_range.remove(host_port)

self.base_url = url
self.api_url = f"{url}/statusgo"
self.ws_url = f"{url}".replace("http", "ws")
self.rpc_url = f"{url}/statusgo/CallRPC"

RpcClient.__init__(self, self.rpc_url)
SignalClient.__init__(self, self.ws_url, await_signals)

self.wait_for_healthy()

websocket_thread = threading.Thread(target=self._connect)
websocket_thread.daemon = True
websocket_thread.start()
Expand Down Expand Up @@ -72,14 +74,6 @@ def _start_container(self, host_port):
"mode": "rw",
}
},
# Add health check
"healthcheck": {
"Test": ["CMD-SHELL", "curl -f http://localhost:3333/health || exit 1"],
"Interval": 1 * NANOSECONDS_PER_SECOND,
"Timeout": 1 * NANOSECONDS_PER_SECOND,
"StartPeriod": 1 * NANOSECONDS_PER_SECOND,
"Retries": 0,
},
}
if "FUNCTIONAL_TESTS_DOCKER_UID" in os.environ:
container_args["user"] = os.environ["FUNCTIONAL_TESTS_DOCKER_UID"]
Expand All @@ -92,26 +86,29 @@ def _start_container(self, host_port):
option.status_backend_containers.append(container.id)
return container

def _wait_for_container_healthy(self, timeout=5):
def wait_for_healthy(self, timeout=10):
start_time = time.time()
while time.time() - start_time < timeout:
self.container.reload() # Reload container attributes
status = self.container.attrs['State'].get('Health', {}).get('Status')
if status == 'healthy':
logging.debug("Container is healthy!")
return True
if status in ['unhealthy', 'exited', 'dead']:
return False
time.sleep(0.1) # Wait before checking again
logging.error("Timeout reached while waiting for container health.")
return False

def api_request(self, method, data, url=None):
while time.time() - start_time <= timeout:
try:
self.health(enable_logging=False)
logging.info(f"StatusBackend is healthy after {time.time() - start_time} seconds")
return
except Exception as e:
time.sleep(0.1)
raise TimeoutError(
f"StatusBackend was not healthy after {timeout} seconds")

def health(self, enable_logging=True):
return self.api_request("health", data=[], url=self.base_url, enable_logging=enable_logging)

def api_request(self, method, data, url=None, enable_logging=True):
url = url if url else self.api_url
url = f"{url}/{method}"
logging.info(f"Sending POST request to url {url} with data: {json.dumps(data, sort_keys=True, indent=4)}")
if enable_logging:
logging.info(f"Sending POST request to url {url} with data: {json.dumps(data, sort_keys=True, indent=4)}")
response = requests.post(url, json=data)
logging.info(f"Got response: {response.content}")
if enable_logging:
logging.info(f"Got response: {response.content}")
return response

def verify_is_valid_api_response(self, response):
Expand All @@ -126,8 +123,8 @@ def verify_is_valid_api_response(self, response):
except KeyError:
pass

def api_valid_request(self, method, data):
response = self.api_request(method, data)
def api_valid_request(self, method, data, url=None):
response = self.api_request(method, data, url)
self.verify_is_valid_api_response(response)
return response

Expand Down
5 changes: 4 additions & 1 deletion tests-functional/tests/test_init_status_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def test_init_app(self):
backend_client.restore_account_and_login()

assert backend_client is not None
backend_client.verify_json_schema(
backend_client.wait_for_login(),
"signal_node_login",
)
backend_client.verify_json_schema(
backend_client.wait_for_signal(SignalType.MEDIASERVER_STARTED.value),
"signal_mediaserver_started",
Expand All @@ -35,7 +39,6 @@ def test_init_app(self):
backend_client.wait_for_signal(SignalType.NODE_READY.value),
"signal_node_ready",
)
backend_client.verify_json_schema(backend_client.wait_for_login(), "signal_node_login")


@pytest.mark.rpc
Expand Down

0 comments on commit bc8398d

Please sign in to comment.