From 760f40d3d83c6e7aa0edfc56412a6d9b6070548b Mon Sep 17 00:00:00 2001 From: Maxim Zhiltsov Date: Tue, 20 Dec 2022 18:56:06 +0300 Subject: [PATCH] Wait for OPA to load rules in tests startup (#5483) OPA can take some time to load rules, but our tests don't wait for OPA, and start right after the server is loaded. Sometimes it works, but in other times the tests may fail because OPA is still loading the rules. This PR allows to wait for OPA during the test suite startup. --- tests/python/shared/fixtures/init.py | 33 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/tests/python/shared/fixtures/init.py b/tests/python/shared/fixtures/init.py index b586a93bb726..2153ec64c7b5 100644 --- a/tests/python/shared/fixtures/init.py +++ b/tests/python/shared/fixtures/init.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +import logging import re from http import HTTPStatus from pathlib import Path @@ -11,7 +12,9 @@ import pytest import requests -from shared.utils.config import ASSETS_DIR, get_api_url +from shared.utils.config import ASSETS_DIR, get_server_url + +logger = logging.getLogger(__name__) CVAT_ROOT_DIR = next(dir.parent for dir in Path(__file__).parents if dir.name == "tests") CVAT_DB_DIR = ASSETS_DIR / "cvat_db" @@ -185,12 +188,24 @@ def delete_compose_files(): filename.unlink(missing_ok=True) -def wait_for_server(): - for _ in range(30): - response = requests.get(get_api_url("users/self")) - if response.status_code == HTTPStatus.UNAUTHORIZED: - break - sleep(5) +def wait_for_services(): + for i in range(300): + logger.debug(f"waiting for the server to load ... ({i})") + response = requests.get(get_server_url("api/server/health/", format="json")) + if response.status_code == HTTPStatus.OK: + logger.debug("the server has finished loading!") + return + else: + try: + statuses = response.json() + logger.debug(f"server status: \n{statuses}") + except Exception as e: + logger.debug(f"an error occurred during the server status checking: {e}") + sleep(1) + + raise Exception( + "Failed to reach the server during the specified period. Please check the configuration." + ) def docker_restore_data_volumes(): @@ -286,7 +301,7 @@ def pytest_sessionstart(session): pytest.exit("All testing containers are stopped", returncode=0) start_services(rebuild) - wait_for_server() + wait_for_services() docker_exec_cvat("python manage.py loaddata /tmp/data.json") docker_exec_cvat_db( @@ -303,7 +318,7 @@ def pytest_sessionstart(session): kube_cp(CVAT_DB_DIR / "restore.sql", f"{db_pod_name}:/tmp/restore.sql") kube_cp(CVAT_DB_DIR / "data.json", f"{server_pod_name}:/tmp/data.json") - wait_for_server() + wait_for_services() kube_exec_cvat("python manage.py loaddata /tmp/data.json")