Files
docker-stacks/tests/by_image/base-notebook/test_ips.py
Ayaz Salikhov b35f1554d6 Test server listening on IPv4/IPv6 (#2255)
* Test server listening on IPv4/IPv6

* Set up Docker in create-dev-env

* Show docker version

* Add info about docker client

* Check requests

* Show docker client version

* Try to pass docker sock

* Fix

* Break fast

* Revert

* Cleanup

* Better naming

* Always use docker.from_env

* Revert "Always use docker.from_env"

This reverts commit d03069ac28.

* Use custom docker client for only one test

* More logs

* Use cont_data_dir in test, so workdir doesn't matter

* Use common variable names

* Move patch to a separate function

* Try to use set-host option

* Use the same docker client in get_health

* Use .api

* Rewrite check_listening.py to use one function for both ipv4 and ipv6

* Add links to explain why we need to set up docker manually
2025-03-20 17:12:22 +00:00

51 lines
1.7 KiB
Python

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import logging
from collections.abc import Generator
from pathlib import Path
from random import randint
import docker
import pytest # type: ignore
from tests.utils.tracked_container import TrackedContainer
LOGGER = logging.getLogger(__name__)
THIS_DIR = Path(__file__).parent.resolve()
@pytest.fixture(scope="session")
def ipv6_network(docker_client: docker.DockerClient) -> Generator[str, None, None]:
"""Create a dual-stack IPv6 docker network"""
# Doesn't have to be routable since we're testing inside the container
subnet64 = "fc00:" + ":".join(hex(randint(0, 2**16))[2:] for _ in range(3))
name = subnet64.replace(":", "-")
docker_client.networks.create(
name,
ipam=docker.types.IPAMPool(
subnet=subnet64 + "::/64",
gateway=subnet64 + "::1",
),
enable_ipv6=True,
internal=True,
)
yield name
docker_client.networks.get(name).remove()
def test_ipv46(container: TrackedContainer, ipv6_network: str) -> None:
"""Check server is listening on the expected IP families"""
host_data_dir = THIS_DIR / "data"
cont_data_dir = "/home/jovyan/data"
LOGGER.info("Testing that server is listening on IPv4 and IPv6 ...")
running_container = container.run_detached(
network=ipv6_network,
volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro,z"}},
tty=True,
)
command = ["python", f"{cont_data_dir}/check_listening.py"]
exec_result = running_container.exec_run(command)
LOGGER.info(exec_result.output.decode())
assert exec_result.exit_code == 0