Skip to content

Commit 100187f

Browse files
authored
Make free_host_port a fixture (#2267)
* Make free_host_port a fixture * Remove redundant comment
1 parent 2ce2c06 commit 100187f

File tree

8 files changed

+35
-42
lines changed

8 files changed

+35
-42
lines changed

tests/by_image/base-notebook/test_container_options.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
import pytest # type: ignore
77
import requests
88

9-
from tests.utils.find_free_port import find_free_port
109
from tests.utils.tracked_container import TrackedContainer
1110

1211
LOGGER = logging.getLogger(__name__)
1312

1413

15-
def test_cli_args(container: TrackedContainer, http_client: requests.Session) -> None:
14+
def test_cli_args(
15+
container: TrackedContainer, http_client: requests.Session, free_host_port: int
16+
) -> None:
1617
"""Image should respect command line args (e.g., disabling token security)"""
17-
host_port = find_free_port()
1818
container.run_detached(
1919
command=["start-notebook.py", "--IdentityProvider.token=''"],
20-
ports={"8888/tcp": host_port},
20+
ports={"8888/tcp": free_host_port},
2121
)
22-
resp = http_client.get(f"http://localhost:{host_port}")
22+
resp = http_client.get(f"http://localhost:{free_host_port}")
2323
resp.raise_for_status()
2424
logs = container.get_logs()
2525
LOGGER.debug(logs)
@@ -54,22 +54,21 @@ def test_nb_user_change(container: TrackedContainer) -> None:
5454

5555
@pytest.mark.filterwarnings("ignore:Unverified HTTPS request")
5656
def test_unsigned_ssl(
57-
container: TrackedContainer, http_client: requests.Session
57+
container: TrackedContainer, http_client: requests.Session, free_host_port: int
5858
) -> None:
5959
"""Container should generate a self-signed SSL certificate
6060
and Jupyter Server should use it to enable HTTPS.
6161
"""
62-
host_port = find_free_port()
6362
container.run_detached(
6463
environment=["GEN_CERT=yes"],
65-
ports={"8888/tcp": host_port},
64+
ports={"8888/tcp": free_host_port},
6665
)
6766
# NOTE: The requests.Session backing the http_client fixture
6867
# does not retry properly while the server is booting up.
6968
# An SSL handshake error seems to abort the retry logic.
7069
# Forcing a long sleep for the moment until I have time to dig more.
7170
time.sleep(1)
72-
resp = http_client.get(f"https://localhost:{host_port}", verify=False)
71+
resp = http_client.get(f"https://localhost:{free_host_port}", verify=False)
7372
resp.raise_for_status()
7473
assert "login_submit" in resp.text
7574
logs = container.get_logs()
@@ -94,18 +93,18 @@ def test_unsigned_ssl(
9493
def test_custom_internal_port(
9594
container: TrackedContainer,
9695
http_client: requests.Session,
96+
free_host_port: int,
9797
env: dict[str, str],
9898
) -> None:
9999
"""Container should be accessible from the host
100100
when using custom internal port"""
101-
host_port = find_free_port()
102101
internal_port = env.get("JUPYTER_PORT", 8888)
103102
container.run_detached(
104103
command=["start-notebook.py", "--IdentityProvider.token=''"],
105104
environment=env,
106-
ports={internal_port: host_port},
105+
ports={internal_port: free_host_port},
107106
)
108-
resp = http_client.get(f"http://localhost:{host_port}")
107+
resp = http_client.get(f"http://localhost:{free_host_port}")
109108
resp.raise_for_status()
110109
logs = container.get_logs()
111110
LOGGER.debug(logs)

tests/by_image/base-notebook/test_notebook.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
# Distributed under the terms of the Modified BSD License.
33
import requests
44

5-
from tests.utils.find_free_port import find_free_port
65
from tests.utils.tracked_container import TrackedContainer
76

87

98
def test_secured_server(
10-
container: TrackedContainer, http_client: requests.Session
9+
container: TrackedContainer, http_client: requests.Session, free_host_port: int
1110
) -> None:
1211
"""Jupyter Server should eventually request user login."""
13-
host_port = find_free_port()
14-
container.run_detached(ports={"8888/tcp": host_port})
15-
resp = http_client.get(f"http://localhost:{host_port}")
12+
container.run_detached(ports={"8888/tcp": free_host_port})
13+
resp = http_client.get(f"http://localhost:{free_host_port}")
1614
resp.raise_for_status()
1715
assert "login_submit" in resp.text, "User login not requested"

tests/by_image/base-notebook/test_start_container.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import pytest # type: ignore
77
import requests
88

9-
from tests.utils.find_free_port import find_free_port
109
from tests.utils.tracked_container import TrackedContainer
1110

1211
LOGGER = logging.getLogger(__name__)
@@ -32,6 +31,7 @@
3231
def test_start_notebook(
3332
container: TrackedContainer,
3433
http_client: requests.Session,
34+
free_host_port: int,
3535
env: list[str] | None,
3636
expected_command: str,
3737
expected_start: bool,
@@ -41,8 +41,7 @@ def test_start_notebook(
4141
LOGGER.info(
4242
f"Test that the start-notebook.py launches the {expected_command} server from the env {env} ..."
4343
)
44-
host_port = find_free_port()
45-
container.run_detached(environment=env, ports={"8888/tcp": host_port})
44+
container.run_detached(environment=env, ports={"8888/tcp": free_host_port})
4645
# sleeping some time to let the server start
4746
time.sleep(2)
4847
logs = container.get_logs()
@@ -59,7 +58,7 @@ def test_start_notebook(
5958
assert len(expected_warnings) == len(warnings)
6059
# checking if the server is listening
6160
if expected_start:
62-
resp = http_client.get(f"http://localhost:{host_port}")
61+
resp = http_client.get(f"http://localhost:{free_host_port}")
6362
assert resp.status_code == 200, "Server is not listening"
6463

6564

tests/by_image/datascience-notebook/test_pluto_datascience.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
def test_pluto_proxy(
10-
container: TrackedContainer, http_client: requests.Session
10+
container: TrackedContainer, http_client: requests.Session, free_host_port: int
1111
) -> None:
1212
"""Pluto proxy starts Pluto correctly"""
13-
check_pluto_proxy(container, http_client)
13+
check_pluto_proxy(container, http_client, free_host_port)

tests/by_image/julia-notebook/test_pluto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
def test_pluto_proxy(
10-
container: TrackedContainer, http_client: requests.Session
10+
container: TrackedContainer, http_client: requests.Session, free_host_port: int
1111
) -> None:
1212
"""Pluto proxy starts Pluto correctly"""
13-
check_pluto_proxy(container, http_client)
13+
check_pluto_proxy(container, http_client, free_host_port)

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
# Distributed under the terms of the Modified BSD License.
33
import logging
44
import os
5+
import socket
56
from collections.abc import Generator
7+
from contextlib import closing
68

79
import docker
810
import pytest # type: ignore
@@ -54,3 +56,12 @@ def container(
5456
)
5557
yield container
5658
container.remove()
59+
60+
61+
@pytest.fixture(scope="function")
62+
def free_host_port() -> Generator[int]:
63+
"""Finds a free port on the host machine"""
64+
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
65+
s.bind(("", 0))
66+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
67+
yield s.getsockname()[1]

tests/shared_checks/pluto_check.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,24 @@
66

77
import requests
88

9-
from tests.utils.find_free_port import find_free_port
109
from tests.utils.tracked_container import TrackedContainer
1110

1211
LOGGER = logging.getLogger(__name__)
1312

1413

1514
def check_pluto_proxy(
16-
container: TrackedContainer, http_client: requests.Session
15+
container: TrackedContainer, http_client: requests.Session, free_host_port: int
1716
) -> None:
18-
host_port = find_free_port()
1917
token = secrets.token_hex()
2018
container.run_detached(
2119
command=[
2220
"start-notebook.py",
2321
f"--IdentityProvider.token={token}",
2422
],
25-
ports={"8888/tcp": host_port},
23+
ports={"8888/tcp": free_host_port},
2624
)
2725
# Give the server a bit of time to start
2826
time.sleep(2)
29-
resp = http_client.get(f"http://localhost:{host_port}/pluto?token={token}")
27+
resp = http_client.get(f"http://localhost:{free_host_port}/pluto?token={token}")
3028
resp.raise_for_status()
3129
assert "Pluto.jl notebooks" in resp.text, "Pluto.jl text not found in /pluto page"

tests/utils/find_free_port.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)