Skip to content
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

Add localhost env var #208

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include versioneer.py
include jupyterhub_traefik_proxy/_version.py
include requirements.txt
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"Programming Language :: Python",
"Programming Language :: Python :: 3",
],
packages=find_packages(),
packages=find_packages(exclude=["performance", "tests"]),
include_package_data=True,
entry_points={
"jupyterhub.proxies": [
Expand Down
Empty file added tests/__init__.py
Empty file.
48 changes: 30 additions & 18 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from urllib.parse import urlparse

import pytest
import utils
from certipy import Certipy
from consul.aio import Consul
from jupyterhub.utils import exponential_backoff
Expand All @@ -24,6 +23,8 @@
from jupyterhub_traefik_proxy.fileprovider import TraefikFileProviderProxy
from jupyterhub_traefik_proxy.traefik_utils import deep_merge

from . import utils

HERE = Path(__file__).parent.resolve()
config_files = HERE / "config_files"

Expand Down Expand Up @@ -54,6 +55,17 @@ class Config:
traefik_api_user = "api_admin"
traefik_api_pass = "admin"

# a single location for overloading the domain part of test URLs, e.g.
# to force ipv4
#
# JHTP_TEST_LOCALHOST="127.0.0.1" python -m pytest tests ...
#
# or ipv6
#
# JHTP_TEST_LOCALHOST="::1" python -m pytest tests ...
#
localhost = os.environ.get("JHTP_TEST_LOCALHOST", "localhost")

# The URL that should be proxied to jupyterhub
# Putting here, can easily change between http and https
public_url = "https://127.0.0.1:8000"
Expand Down Expand Up @@ -166,7 +178,7 @@ async def no_auth_consul_proxy(launch_consul, proxy_args):
Consul acl disabled.
"""
proxy = TraefikConsulProxy(
consul_url=f"http://127.0.0.1:{Config.consul_port}",
consul_url=f"http://{Config.localhost}:{Config.consul_port}",
should_start=True,
**proxy_args,
)
Expand All @@ -182,7 +194,7 @@ async def auth_consul_proxy(launch_consul_auth, proxy_args):
Consul acl enabled.
"""
proxy = TraefikConsulProxy(
consul_url=f"http://127.0.0.1:{Config.consul_auth_port}",
consul_url=f"http://{Config.localhost}:{Config.consul_auth_port}",
consul_password=Config.consul_token,
should_start=True,
**proxy_args,
Expand Down Expand Up @@ -223,13 +235,13 @@ def _make_etcd_proxy(*, auth=False, client_ca=None, **extra_kwargs):
client_ca = str(client_ca)
kwargs.update(
dict(
etcd_url="https://localhost:2379",
etcd_url=f"https://{Config.localhost}:2379",
etcd_username=Config.etcd_user,
etcd_password=Config.etcd_password,
etcd_client_kwargs=dict(
grpc_options=[
("grpc.ssl_target_name_override", "localhost"),
("grpc.default_authority", "localhost"),
("grpc.ssl_target_name_override", Config.localhost),
("grpc.default_authority", Config.localhost),
],
ca_cert=client_ca,
),
Expand Down Expand Up @@ -363,7 +375,7 @@ async def external_file_proxy_toml(launch_traefik_file, dynamic_config_dir, prox
@pytest.fixture
async def external_consul_proxy(launch_traefik_consul, proxy_args):
proxy = TraefikConsulProxy(
consul_url=f"http://127.0.0.1:{Config.consul_port}",
consul_url=f"http://{Config.localhost}:{Config.consul_port}",
should_start=False,
**proxy_args,
)
Expand All @@ -374,7 +386,7 @@ async def external_consul_proxy(launch_traefik_consul, proxy_args):
@pytest.fixture
async def auth_external_consul_proxy(launch_traefik_consul_auth, proxy_args):
proxy = TraefikConsulProxy(
consul_url=f"http://127.0.0.1:{Config.consul_auth_port}",
consul_url=f"http://{Config.localhost}:{Config.consul_auth_port}",
consul_password=Config.consul_token,
should_start=False,
**proxy_args,
Expand Down Expand Up @@ -473,7 +485,7 @@ def launch_traefik_consul(launch_traefik, launch_consul):
@pytest.fixture
def launch_traefik_consul_auth(launch_traefik, launch_consul_auth):
return launch_traefik(
f"--providers.consul.endpoints=http://127.0.0.1:{Config.consul_auth_port}",
f"--providers.consul.endpoints=http://{Config.localhost}:{Config.consul_auth_port}",
env={"CONSUL_HTTP_TOKEN": Config.consul_token},
)

Expand Down Expand Up @@ -549,16 +561,16 @@ async def launch_etcd_auth(etcd_ssl_key_cert, etcd_client_ca):
f"--peer-trusted-ca-file={etcd_client_ca}",
f"--cert-file={cert}",
f"--key-file={key}",
"--initial-cluster=default=https://localhost:2380",
"--initial-advertise-peer-urls=https://localhost:2380",
"--listen-peer-urls=https://localhost:2380",
"--listen-client-urls=https://localhost:2379",
"--advertise-client-urls=https://localhost:2379",
f"--initial-cluster=default=https://{Config.localhost}:2380",
f"--initial-advertise-peer-urls=https://{Config.localhost}:2380",
f"--listen-peer-urls=https://{Config.localhost}:2380",
f"--listen-client-urls=https://{Config.localhost}:2379",
f"--advertise-client-urls=https://{Config.localhost}:2379",
"--log-level=debug",
],
)
etcdctl_args = [
"--endpoints=localhost:2379",
f"--endpoints={Config.localhost}:2379",
"--user",
f"{Config.etcd_user}:{Config.etcd_password}",
f"--cacert={etcd_client_ca}",
Expand All @@ -574,11 +586,11 @@ async def launch_etcd_auth(etcd_ssl_key_cert, etcd_client_ca):
c = etcd3.client(
user=Config.etcd_user,
password=Config.etcd_password,
host="localhost",
host=Config.localhost,
port=2379,
grpc_options=[
("grpc.ssl_target_name_override", "localhost"),
("grpc.default_authority", "localhost"),
("grpc.ssl_target_name_override", Config.localhost),
("grpc.default_authority", Config.localhost),
],
ca_cert=etcd_client_ca,
)
Expand Down
4 changes: 3 additions & 1 deletion tests/dummy_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import websockets

from .conftest import Config


async def process_request(path, request_headers, port):
if path.endswith("/ws"):
Expand All @@ -31,7 +33,7 @@ async def send_port(websocket, path):
async def main(port):
async with websockets.serve(
send_port,
host="127.0.0.1",
host=Config.localhost,
port=port,
process_request=partial(process_request, port=port),
):
Expand Down
10 changes: 7 additions & 3 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from urllib.parse import quote, urlparse

import pytest
import utils
import websockets
from jupyterhub.objects import Hub, Server
from jupyterhub.user import User
Expand All @@ -23,6 +22,9 @@

from jupyterhub_traefik_proxy.proxy import TraefikProxy

from . import utils
from .conftest import Config

# Mark all tests in this file as slow
pytestmark = [pytest.mark.slow]

Expand All @@ -47,7 +49,9 @@ def __init__(self, name="", *, user, **kwargs):
self.proxy_spec = url_path_join(self.user.proxy_spec, name, "/")

def start(self):
self.server = Server.from_url("http://127.0.0.1:%i" % randint(1025, 65535))
self.server = Server.from_url(
f"http://{Config.localhost}:{randint(1025, 65535)}"
)

def stop(self):
self.server = None
Expand Down Expand Up @@ -116,7 +120,7 @@ async def _launch_backends(n=1):
already_available = len(running_backends)
for i in range(already_available, n):
port = base_port + i
url = f"http://127.0.0.1:{port}"
url = f"http://{Config.localhost}:{port}"
print(f"Launching backend on {url}")
backend = subprocess.Popen([sys.executable, dummy_server_path, str(port)])
running_backends.append(backend)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_traefik_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

from jupyterhub_traefik_proxy import traefik_utils

from .conftest import Config


# Mark all tests in this file as asyncio
def test_roundtrip_routes():
pytest.mark.asyncio
routes = {
"backends": {
"backend1": {
"servers": {"server1": {"url": "http://127.0.0.1:9009", "weight": 1}}
"servers": {
"server1": {"url": f"http://{Config.localhost}:9009", "weight": 1}
}
}
},
"frontends": {
Expand Down