Skip to content

Commit

Permalink
Wait for OPA to load rules in tests startup (#5483)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zhiltsov-max authored Dec 20, 2022
1 parent 1ecc607 commit 760f40d
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions tests/python/shared/fixtures/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MIT

import logging
import re
from http import HTTPStatus
from pathlib import Path
Expand All @@ -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"
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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(
Expand All @@ -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")

Expand Down

0 comments on commit 760f40d

Please sign in to comment.