Skip to content

Commit

Permalink
Merge pull request #1623 from mathbunnyru/asalikhov/random_port_binding
Browse files Browse the repository at this point in the history
Choose random host port in tests
  • Loading branch information
mathbunnyru authored Feb 14, 2022
2 parents 1ef8838 + c89d5ac commit 2e39b99
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
14 changes: 10 additions & 4 deletions base-notebook/test/test_container_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion base-notebook/test/test_start_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"


Expand Down
9 changes: 8 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
5 changes: 3 additions & 2 deletions test/test_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 2e39b99

Please sign in to comment.