Skip to content

Refactor healthcheck tests to use one function #2254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 31 additions & 47 deletions tests/by_image/base-notebook/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@
LOGGER = logging.getLogger(__name__)


def get_healthy_status(
container: TrackedContainer,
env: list[str] | None,
cmd: list[str] | None,
user: str | None,
) -> str:
running_container = container.run_detached(
tty=True,
environment=env,
command=cmd,
user=user,
)

# giving some time to let the server start
finish_time = time.time() + 10
sleep_time = 1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sleeping 1 second instead of 0.1, because otherwise we produce too much logs when running pytest directly

while time.time() < finish_time:
time.sleep(sleep_time)

status = get_health(running_container)
if status == "healthy":
return status
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fast exit in case get_health reports the status is healthy


return get_health(running_container)


@pytest.mark.parametrize(
"env,cmd,user",
[
Expand Down Expand Up @@ -60,22 +86,7 @@ def test_healthy(
cmd: list[str] | None,
user: str | None,
) -> None:
running_container = container.run_detached(
tty=True,
environment=env,
command=cmd,
user=user,
)

# giving some time to let the server start
finish_time = time.time() + 10
sleep_time = 0.1
while time.time() < finish_time:
time.sleep(sleep_time)
if get_health(running_container) == "healthy":
return

assert get_health(running_container) == "healthy"
assert get_healthy_status(container, env, cmd, user) == "healthy"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -108,22 +119,7 @@ def test_healthy_with_proxy(
cmd: list[str] | None,
user: str | None,
) -> None:
running_container = container.run_detached(
tty=True,
environment=env,
command=cmd,
user=user,
)

# giving some time to let the server start
finish_time = time.time() + 10
sleep_time = 0.1
while time.time() < finish_time:
time.sleep(sleep_time)
if get_health(running_container) == "healthy":
return

assert get_health(running_container) == "healthy"
assert get_healthy_status(container, env, cmd, user) == "healthy"


@pytest.mark.parametrize(
Expand All @@ -145,18 +141,6 @@ def test_not_healthy(
env: list[str] | None,
cmd: list[str] | None,
) -> None:
running_container = container.run_detached(
tty=True,
environment=env,
command=cmd,
)

# giving some time to let the server start
finish_time = time.time() + 5
Copy link
Member Author

@mathbunnyru mathbunnyru Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 is actually a bug here, because this way it tests nothing - if I put 5 in tests above they give an error, as it's not enough time for our containers

sleep_time = 0.1
while time.time() < finish_time:
time.sleep(sleep_time)
if get_health(running_container) == "healthy":
raise RuntimeError("Container should not be healthy for this testcase")

assert get_health(running_container) != "healthy"
assert (
get_healthy_status(container, env, cmd, user=None) != "healthy"
), "Container should not be healthy for this testcase"