mirror of
https://github.com/jupyter/docker-stacks.git
synced 2025-10-12 20:42:57 +00:00
Merge pull request #1623 from mathbunnyru/asalikhov/random_port_binding
Choose random host port in tests
This commit is contained in:
@@ -16,9 +16,11 @@ def test_cli_args(container: TrackedContainer, http_client: requests.Session) ->
|
||||
"""Container should respect notebook server command line args
|
||||
(e.g., disabling token security)"""
|
||||
running_container = container.run_detached(
|
||||
command=["start-notebook.sh", "--NotebookApp.token=''"]
|
||||
command=["start-notebook.sh", "--NotebookApp.token=''"],
|
||||
ports={"8888/tcp": None},
|
||||
)
|
||||
resp = http_client.get("http://localhost:8888")
|
||||
host_port = container.get_host_port("8888/tcp")
|
||||
resp = http_client.get(f"http://localhost:{host_port}")
|
||||
resp.raise_for_status()
|
||||
logs = running_container.logs().decode("utf-8")
|
||||
LOGGER.debug(logs)
|
||||
@@ -35,13 +37,17 @@ def test_unsigned_ssl(
|
||||
"""Container should generate a self-signed SSL certificate
|
||||
and notebook server should use it to enable HTTPS.
|
||||
"""
|
||||
running_container = container.run_detached(environment=["GEN_CERT=yes"])
|
||||
running_container = container.run_detached(
|
||||
environment=["GEN_CERT=yes"],
|
||||
ports={"8888/tcp": None},
|
||||
)
|
||||
host_port = container.get_host_port("8888/tcp")
|
||||
# NOTE: The requests.Session backing the http_client fixture does not retry
|
||||
# properly while the server is booting up. An SSL handshake error seems to
|
||||
# abort the retry logic. Forcing a long sleep for the moment until I have
|
||||
# time to dig more.
|
||||
time.sleep(5)
|
||||
resp = http_client.get("https://localhost:8888", verify=False)
|
||||
resp = http_client.get(f"https://localhost:{host_port}", verify=False)
|
||||
resp.raise_for_status()
|
||||
assert "login_submit" in resp.text
|
||||
logs = running_container.logs().decode("utf-8")
|
||||
|
@@ -51,6 +51,7 @@ def test_start_notebook(
|
||||
tty=True,
|
||||
environment=env,
|
||||
command=["start-notebook.sh"],
|
||||
ports={"8888/tcp": None},
|
||||
)
|
||||
# sleeping some time to let the server start
|
||||
time.sleep(3)
|
||||
@@ -68,7 +69,8 @@ def test_start_notebook(
|
||||
assert len(expected_warnings) == len(warnings)
|
||||
# checking if the server is listening
|
||||
if expected_start:
|
||||
resp = http_client.get("http://localhost:8888")
|
||||
host_port = container.get_host_port("8888/tcp")
|
||||
resp = http_client.get(f"http://localhost:{host_port}")
|
||||
assert resp.status_code == 200, "Server is not listening"
|
||||
|
||||
|
||||
|
@@ -108,6 +108,14 @@ class TrackedContainer:
|
||||
assert rv == 0 or rv["StatusCode"] == 0
|
||||
return logs
|
||||
|
||||
def get_host_port(self, container_port: str) -> str:
|
||||
"""Returns the host port associated with the tracked container's port."""
|
||||
assert isinstance(self.container, Container)
|
||||
self.container.reload()
|
||||
return self.container.attrs["NetworkSettings"]["Ports"][container_port][0][
|
||||
"HostPort"
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_errors(logs: str) -> list[str]:
|
||||
return TrackedContainer._lines_starting_with(logs, "ERROR")
|
||||
@@ -137,7 +145,6 @@ def container(docker_client: docker.DockerClient, image_name: str) -> Container:
|
||||
docker_client,
|
||||
image_name,
|
||||
detach=True,
|
||||
ports={"8888/tcp": 8888},
|
||||
)
|
||||
yield container
|
||||
container.remove()
|
||||
|
@@ -10,7 +10,8 @@ def test_secured_server(
|
||||
container: TrackedContainer, http_client: requests.Session
|
||||
) -> None:
|
||||
"""Notebook server should eventually request user login."""
|
||||
container.run_detached()
|
||||
resp = http_client.get("http://localhost:8888")
|
||||
container.run_detached(ports={"8888/tcp": None})
|
||||
host_port = container.get_host_port("8888/tcp")
|
||||
resp = http_client.get(f"http://localhost:{host_port}")
|
||||
resp.raise_for_status()
|
||||
assert "login_submit" in resp.text, "User login not requested"
|
||||
|
Reference in New Issue
Block a user