Skip to content

Commit 951dec9

Browse files
authored
Refactor healthcheck tests to use one function (#2254)
1 parent ea8faad commit 951dec9

File tree

1 file changed

+31
-47
lines changed

1 file changed

+31
-47
lines changed

tests/by_image/base-notebook/test_healthcheck.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@
1111
LOGGER = logging.getLogger(__name__)
1212

1313

14+
def get_healthy_status(
15+
container: TrackedContainer,
16+
env: list[str] | None,
17+
cmd: list[str] | None,
18+
user: str | None,
19+
) -> str:
20+
running_container = container.run_detached(
21+
tty=True,
22+
environment=env,
23+
command=cmd,
24+
user=user,
25+
)
26+
27+
# giving some time to let the server start
28+
finish_time = time.time() + 10
29+
sleep_time = 1
30+
while time.time() < finish_time:
31+
time.sleep(sleep_time)
32+
33+
status = get_health(running_container)
34+
if status == "healthy":
35+
return status
36+
37+
return get_health(running_container)
38+
39+
1440
@pytest.mark.parametrize(
1541
"env,cmd,user",
1642
[
@@ -60,22 +86,7 @@ def test_healthy(
6086
cmd: list[str] | None,
6187
user: str | None,
6288
) -> None:
63-
running_container = container.run_detached(
64-
tty=True,
65-
environment=env,
66-
command=cmd,
67-
user=user,
68-
)
69-
70-
# giving some time to let the server start
71-
finish_time = time.time() + 10
72-
sleep_time = 0.1
73-
while time.time() < finish_time:
74-
time.sleep(sleep_time)
75-
if get_health(running_container) == "healthy":
76-
return
77-
78-
assert get_health(running_container) == "healthy"
89+
assert get_healthy_status(container, env, cmd, user) == "healthy"
7990

8091

8192
@pytest.mark.parametrize(
@@ -108,22 +119,7 @@ def test_healthy_with_proxy(
108119
cmd: list[str] | None,
109120
user: str | None,
110121
) -> None:
111-
running_container = container.run_detached(
112-
tty=True,
113-
environment=env,
114-
command=cmd,
115-
user=user,
116-
)
117-
118-
# giving some time to let the server start
119-
finish_time = time.time() + 10
120-
sleep_time = 0.1
121-
while time.time() < finish_time:
122-
time.sleep(sleep_time)
123-
if get_health(running_container) == "healthy":
124-
return
125-
126-
assert get_health(running_container) == "healthy"
122+
assert get_healthy_status(container, env, cmd, user) == "healthy"
127123

128124

129125
@pytest.mark.parametrize(
@@ -145,18 +141,6 @@ def test_not_healthy(
145141
env: list[str] | None,
146142
cmd: list[str] | None,
147143
) -> None:
148-
running_container = container.run_detached(
149-
tty=True,
150-
environment=env,
151-
command=cmd,
152-
)
153-
154-
# giving some time to let the server start
155-
finish_time = time.time() + 5
156-
sleep_time = 0.1
157-
while time.time() < finish_time:
158-
time.sleep(sleep_time)
159-
if get_health(running_container) == "healthy":
160-
raise RuntimeError("Container should not be healthy for this testcase")
161-
162-
assert get_health(running_container) != "healthy"
144+
assert (
145+
get_healthy_status(container, env, cmd, user=None) != "healthy"
146+
), "Container should not be healthy for this testcase"

0 commit comments

Comments
 (0)