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.
main
Maxim Zhiltsov 3 years ago committed by GitHub
parent 1ecc607286
commit 760f40d3d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
import logging
import re import re
from http import HTTPStatus from http import HTTPStatus
from pathlib import Path from pathlib import Path
@ -11,7 +12,9 @@ from time import sleep
import pytest import pytest
import requests 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_ROOT_DIR = next(dir.parent for dir in Path(__file__).parents if dir.name == "tests")
CVAT_DB_DIR = ASSETS_DIR / "cvat_db" CVAT_DB_DIR = ASSETS_DIR / "cvat_db"
@ -185,12 +188,24 @@ def delete_compose_files():
filename.unlink(missing_ok=True) filename.unlink(missing_ok=True)
def wait_for_server(): def wait_for_services():
for _ in range(30): for i in range(300):
response = requests.get(get_api_url("users/self")) logger.debug(f"waiting for the server to load ... ({i})")
if response.status_code == HTTPStatus.UNAUTHORIZED: response = requests.get(get_server_url("api/server/health/", format="json"))
break if response.status_code == HTTPStatus.OK:
sleep(5) 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(): def docker_restore_data_volumes():
@ -286,7 +301,7 @@ def pytest_sessionstart(session):
pytest.exit("All testing containers are stopped", returncode=0) pytest.exit("All testing containers are stopped", returncode=0)
start_services(rebuild) start_services(rebuild)
wait_for_server() wait_for_services()
docker_exec_cvat("python manage.py loaddata /tmp/data.json") docker_exec_cvat("python manage.py loaddata /tmp/data.json")
docker_exec_cvat_db( 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 / "restore.sql", f"{db_pod_name}:/tmp/restore.sql")
kube_cp(CVAT_DB_DIR / "data.json", f"{server_pod_name}:/tmp/data.json") 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") kube_exec_cvat("python manage.py loaddata /tmp/data.json")

Loading…
Cancel
Save