diff --git a/base-notebook/test/test_container_options.py b/base-notebook/test/test_container_options.py index 936d4576c1..65e85d24c1 100644 --- a/base-notebook/test/test_container_options.py +++ b/base-notebook/test/test_container_options.py @@ -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") diff --git a/base-notebook/test/test_start_container.py b/base-notebook/test/test_start_container.py index da403e6257..7d9b30f9e2 100644 --- a/base-notebook/test/test_start_container.py +++ b/base-notebook/test/test_start_container.py @@ -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" diff --git a/conftest.py b/conftest.py index eef09e6293..a002626566 100644 --- a/conftest.py +++ b/conftest.py @@ -108,6 +108,14 @@ def run_and_wait( 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() diff --git a/test/test_notebook.py b/test/test_notebook.py index bc0181dad9..163e1ecd26 100644 --- a/test/test_notebook.py +++ b/test/test_notebook.py @@ -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"