-
Notifications
You must be signed in to change notification settings - Fork 3k
Test server listening on IPv4/IPv6 #2255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
c691f23
Test server listening on IPv4/IPv6
mathbunnyru 2185322
Set up Docker in create-dev-env
mathbunnyru 07d5526
Show docker version
mathbunnyru 45c143b
Add info about docker client
mathbunnyru 329bf69
Check requests
mathbunnyru 06a4a0a
Show docker client version
mathbunnyru 68d3d65
Try to pass docker sock
mathbunnyru d864483
Fix
mathbunnyru 80793bf
Break fast
mathbunnyru e45b9c6
Revert
mathbunnyru 3a07140
Cleanup
mathbunnyru dc6c26b
Better naming
mathbunnyru d03069a
Always use docker.from_env
mathbunnyru fba8fec
Revert "Always use docker.from_env"
mathbunnyru e618807
Use custom docker client for only one test
mathbunnyru ae3e529
More logs
mathbunnyru 0213080
Use cont_data_dir in test, so workdir doesn't matter
mathbunnyru f7d32e1
Use common variable names
mathbunnyru db9a25e
Move patch to a separate function
mathbunnyru 99f99e3
Try to use set-host option
mathbunnyru e165880
Merge branch 'main' into test_ipv6
mathbunnyru e0b5a54
Use the same docker client in get_health
mathbunnyru 8df7eab
Use .api
mathbunnyru ac3dbd5
Rewrite check_listening.py to use one function for both ipv4 and ipv6
mathbunnyru 1a62fa8
Add links to explain why we need to set up docker manually
mathbunnyru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env python | ||
| # Copyright (c) Jupyter Development Team. | ||
| # Distributed under the terms of the Modified BSD License. | ||
| import socket | ||
| import time | ||
|
|
||
| import requests | ||
|
|
||
|
|
||
| def test_connect() -> None: | ||
| # Give some time for server to start | ||
| finish_time = time.time() + 10 | ||
| sleep_time = 1 | ||
| while time.time() < finish_time: | ||
| time.sleep(sleep_time) | ||
| try: | ||
| requests.get("http://localhost:8888/api") | ||
| break | ||
| except requests.RequestException: | ||
| pass | ||
|
|
||
| # https://docs.python.org/3/library/socket.html#socket.getaddrinfo | ||
| ipv4_addrs = { | ||
| s[4][0] for s in socket.getaddrinfo(socket.gethostname(), None, socket.AF_INET) | ||
| } | ||
| ipv4_addrs.discard("127.0.0.1") | ||
| if len(ipv4_addrs) < 1: | ||
| raise Exception("No external IPv4 addresses found") | ||
| for addr in ipv4_addrs: | ||
| url = f"http://{addr}:8888/api" | ||
| r = requests.get(url) | ||
| r.raise_for_status() | ||
| assert "version" in r.json() | ||
| print(f"Successfully connected to IPv4 {url}") | ||
|
|
||
| ipv6_addrs = { | ||
| s[4][0] for s in socket.getaddrinfo(socket.gethostname(), None, socket.AF_INET6) | ||
| } | ||
| ipv6_addrs.discard("::1") | ||
| if len(ipv6_addrs) < 1: | ||
| raise Exception("No external IPv6 addresses found") | ||
| for addr in ipv6_addrs: | ||
| url = f"http://[{addr}]:8888/api" | ||
| r = requests.get(url) | ||
| r.raise_for_status() | ||
| assert "version" in r.json() | ||
| print(f"Successfully connected to IPv6 {url}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_connect() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # 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", "./data/check_listening.py"] | ||
| r = running_container.exec_run(command) | ||
| LOGGER.info(r.output.decode()) | ||
| assert r.exit_code == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a bug here - we fall through no matter if requests was successful or not, will fix it