Merge pull request #1623 from mathbunnyru/asalikhov/random_port_binding

Choose random host port in tests
This commit is contained in:
Ayaz Salikhov
2022-02-14 17:07:35 +03:00
committed by GitHub
4 changed files with 24 additions and 8 deletions

View File

@@ -16,9 +16,11 @@ def test_cli_args(container: TrackedContainer, http_client: requests.Session) ->
"""Container should respect notebook server command line args """Container should respect notebook server command line args
(e.g., disabling token security)""" (e.g., disabling token security)"""
running_container = container.run_detached( 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() resp.raise_for_status()
logs = running_container.logs().decode("utf-8") logs = running_container.logs().decode("utf-8")
LOGGER.debug(logs) LOGGER.debug(logs)
@@ -35,13 +37,17 @@ def test_unsigned_ssl(
"""Container should generate a self-signed SSL certificate """Container should generate a self-signed SSL certificate
and notebook server should use it to enable HTTPS. 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 # 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 # 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 # abort the retry logic. Forcing a long sleep for the moment until I have
# time to dig more. # time to dig more.
time.sleep(5) 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() resp.raise_for_status()
assert "login_submit" in resp.text assert "login_submit" in resp.text
logs = running_container.logs().decode("utf-8") logs = running_container.logs().decode("utf-8")

View File

@@ -51,6 +51,7 @@ def test_start_notebook(
tty=True, tty=True,
environment=env, environment=env,
command=["start-notebook.sh"], command=["start-notebook.sh"],
ports={"8888/tcp": None},
) )
# sleeping some time to let the server start # sleeping some time to let the server start
time.sleep(3) time.sleep(3)
@@ -68,7 +69,8 @@ def test_start_notebook(
assert len(expected_warnings) == len(warnings) assert len(expected_warnings) == len(warnings)
# checking if the server is listening # checking if the server is listening
if expected_start: 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" assert resp.status_code == 200, "Server is not listening"

View File

@@ -108,6 +108,14 @@ class TrackedContainer:
assert rv == 0 or rv["StatusCode"] == 0 assert rv == 0 or rv["StatusCode"] == 0
return logs 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 @staticmethod
def get_errors(logs: str) -> list[str]: def get_errors(logs: str) -> list[str]:
return TrackedContainer._lines_starting_with(logs, "ERROR") return TrackedContainer._lines_starting_with(logs, "ERROR")
@@ -137,7 +145,6 @@ def container(docker_client: docker.DockerClient, image_name: str) -> Container:
docker_client, docker_client,
image_name, image_name,
detach=True, detach=True,
ports={"8888/tcp": 8888},
) )
yield container yield container
container.remove() container.remove()

View File

@@ -10,7 +10,8 @@ def test_secured_server(
container: TrackedContainer, http_client: requests.Session container: TrackedContainer, http_client: requests.Session
) -> None: ) -> None:
"""Notebook server should eventually request user login.""" """Notebook server should eventually request user login."""
container.run_detached() container.run_detached(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() resp.raise_for_status()
assert "login_submit" in resp.text, "User login not requested" assert "login_submit" in resp.text, "User login not requested"