Skip to content

Commit 2e39b99

Browse files
authored
Merge pull request #1623 from mathbunnyru/asalikhov/random_port_binding
Choose random host port in tests
2 parents 1ef8838 + c89d5ac commit 2e39b99

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

base-notebook/test/test_container_options.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ def test_cli_args(container: TrackedContainer, http_client: requests.Session) ->
1616
"""Container should respect notebook server command line args
1717
(e.g., disabling token security)"""
1818
running_container = container.run_detached(
19-
command=["start-notebook.sh", "--NotebookApp.token=''"]
19+
command=["start-notebook.sh", "--NotebookApp.token=''"],
20+
ports={"8888/tcp": None},
2021
)
21-
resp = http_client.get("http://localhost:8888")
22+
host_port = container.get_host_port("8888/tcp")
23+
resp = http_client.get(f"http://localhost:{host_port}")
2224
resp.raise_for_status()
2325
logs = running_container.logs().decode("utf-8")
2426
LOGGER.debug(logs)
@@ -35,13 +37,17 @@ def test_unsigned_ssl(
3537
"""Container should generate a self-signed SSL certificate
3638
and notebook server should use it to enable HTTPS.
3739
"""
38-
running_container = container.run_detached(environment=["GEN_CERT=yes"])
40+
running_container = container.run_detached(
41+
environment=["GEN_CERT=yes"],
42+
ports={"8888/tcp": None},
43+
)
44+
host_port = container.get_host_port("8888/tcp")
3945
# NOTE: The requests.Session backing the http_client fixture does not retry
4046
# properly while the server is booting up. An SSL handshake error seems to
4147
# abort the retry logic. Forcing a long sleep for the moment until I have
4248
# time to dig more.
4349
time.sleep(5)
44-
resp = http_client.get("https://localhost:8888", verify=False)
50+
resp = http_client.get(f"https://localhost:{host_port}", verify=False)
4551
resp.raise_for_status()
4652
assert "login_submit" in resp.text
4753
logs = running_container.logs().decode("utf-8")

base-notebook/test/test_start_container.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def test_start_notebook(
5151
tty=True,
5252
environment=env,
5353
command=["start-notebook.sh"],
54+
ports={"8888/tcp": None},
5455
)
5556
# sleeping some time to let the server start
5657
time.sleep(3)
@@ -68,7 +69,8 @@ def test_start_notebook(
6869
assert len(expected_warnings) == len(warnings)
6970
# checking if the server is listening
7071
if expected_start:
71-
resp = http_client.get("http://localhost:8888")
72+
host_port = container.get_host_port("8888/tcp")
73+
resp = http_client.get(f"http://localhost:{host_port}")
7274
assert resp.status_code == 200, "Server is not listening"
7375

7476

conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ def run_and_wait(
108108
assert rv == 0 or rv["StatusCode"] == 0
109109
return logs
110110

111+
def get_host_port(self, container_port: str) -> str:
112+
"""Returns the host port associated with the tracked container's port."""
113+
assert isinstance(self.container, Container)
114+
self.container.reload()
115+
return self.container.attrs["NetworkSettings"]["Ports"][container_port][0][
116+
"HostPort"
117+
]
118+
111119
@staticmethod
112120
def get_errors(logs: str) -> list[str]:
113121
return TrackedContainer._lines_starting_with(logs, "ERROR")
@@ -137,7 +145,6 @@ def container(docker_client: docker.DockerClient, image_name: str) -> Container:
137145
docker_client,
138146
image_name,
139147
detach=True,
140-
ports={"8888/tcp": 8888},
141148
)
142149
yield container
143150
container.remove()

test/test_notebook.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def test_secured_server(
1010
container: TrackedContainer, http_client: requests.Session
1111
) -> None:
1212
"""Notebook server should eventually request user login."""
13-
container.run_detached()
14-
resp = http_client.get("http://localhost:8888")
13+
container.run_detached(ports={"8888/tcp": None})
14+
host_port = container.get_host_port("8888/tcp")
15+
resp = http_client.get(f"http://localhost:{host_port}")
1516
resp.raise_for_status()
1617
assert "login_submit" in resp.text, "User login not requested"

0 commit comments

Comments
 (0)