mirror of
https://github.com/jupyterhub/the-littlest-jupyterhub.git
synced 2025-12-18 21:54:05 +08:00
Refactor proxy tests
This commit is contained in:
@@ -12,12 +12,36 @@ import pytest
|
|||||||
from tljh.config import (
|
from tljh.config import (
|
||||||
reload_component,
|
reload_component,
|
||||||
set_config_value,
|
set_config_value,
|
||||||
|
unset_config_value,
|
||||||
CONFIG_FILE,
|
CONFIG_FILE,
|
||||||
CONFIG_DIR,
|
CONFIG_DIR,
|
||||||
STATE_DIR,
|
STATE_DIR,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def send_request(url, max_sleep, validate_cert=True, username=None, password=None):
|
||||||
|
resp = None
|
||||||
|
for i in range(max_sleep):
|
||||||
|
time.sleep(i)
|
||||||
|
try:
|
||||||
|
req = HTTPRequest(
|
||||||
|
url,
|
||||||
|
method="GET",
|
||||||
|
auth_username=username,
|
||||||
|
auth_password=password,
|
||||||
|
validate_cert=validate_cert,
|
||||||
|
follow_redirects=True,
|
||||||
|
max_redirects=15,
|
||||||
|
)
|
||||||
|
resp = HTTPClient().fetch(req)
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
pass
|
||||||
|
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
def test_manual_https(preserve_config):
|
def test_manual_https(preserve_config):
|
||||||
ssl_dir = "/etc/tljh-ssl-test"
|
ssl_dir = "/etc/tljh-ssl-test"
|
||||||
key = ssl_dir + "/ssl.key"
|
key = ssl_dir + "/ssl.key"
|
||||||
@@ -61,28 +85,25 @@ def test_manual_https(preserve_config):
|
|||||||
# verify that our certificate was loaded by traefik
|
# verify that our certificate was loaded by traefik
|
||||||
assert server_cert == file_cert
|
assert server_cert == file_cert
|
||||||
|
|
||||||
for i in range(10):
|
# verify that we can still connect to the hub
|
||||||
time.sleep(i)
|
resp = send_request(
|
||||||
# verify that we can still connect to the hub
|
url="https://127.0.0.1/hub/api", max_sleep=10, validate_cert=False
|
||||||
try:
|
)
|
||||||
req = HTTPRequest(
|
|
||||||
"https://127.0.0.1/hub/api", method="GET", validate_cert=False
|
|
||||||
)
|
|
||||||
resp = HTTPClient().fetch(req)
|
|
||||||
break
|
|
||||||
except Exception as e:
|
|
||||||
pass
|
|
||||||
assert resp.code == 200
|
assert resp.code == 200
|
||||||
|
|
||||||
|
|
||||||
# cleanup
|
# cleanup
|
||||||
shutil.rmtree(ssl_dir)
|
shutil.rmtree(ssl_dir)
|
||||||
|
set_config_value(CONFIG_FILE, "https.enabled", False)
|
||||||
|
|
||||||
|
reload_component("proxy")
|
||||||
|
|
||||||
|
|
||||||
def test_extra_traefik_static_config():
|
def test_extra_traefik_static_config():
|
||||||
extra_config_dir = os.path.join(CONFIG_DIR, "traefik_config.d")
|
extra_static_config_dir = os.path.join(CONFIG_DIR, "traefik_config.d")
|
||||||
os.makedirs(extra_config_dir, exist_ok=True)
|
os.makedirs(extra_static_config_dir, exist_ok=True)
|
||||||
|
|
||||||
extra_config = {
|
extra_static_config = {
|
||||||
"entryPoints": {"no_auth_api": {"address": "127.0.0.1:9999"}},
|
"entryPoints": {"no_auth_api": {"address": "127.0.0.1:9999"}},
|
||||||
"api": {"dashboard": True, "entrypoint": "no_auth_api"},
|
"api": {"dashboard": True, "entrypoint": "no_auth_api"},
|
||||||
}
|
}
|
||||||
@@ -102,25 +123,22 @@ def test_extra_traefik_static_config():
|
|||||||
|
|
||||||
assert success == True
|
assert success == True
|
||||||
|
|
||||||
# Load the extra config
|
# write the extra static config
|
||||||
with open(os.path.join(extra_config_dir, "extra.toml"), "w+") as extra_config_file:
|
with open(
|
||||||
toml.dump(extra_config, extra_config_file)
|
os.path.join(extra_static_config_dir, "extra.toml"), "w+"
|
||||||
|
) as extra_config_file:
|
||||||
|
toml.dump(extra_static_config, extra_config_file)
|
||||||
|
|
||||||
|
# load the extra config
|
||||||
reload_component("proxy")
|
reload_component("proxy")
|
||||||
|
|
||||||
for i in range(5):
|
# the new dashboard entrypoint shouldn't require authentication anymore
|
||||||
time.sleep(i)
|
resp = send_request(url="http://127.0.0.1:9999/dashboard/", max_sleep=5)
|
||||||
try:
|
|
||||||
# The new dashboard entrypoint shouldn't require authentication anymore
|
|
||||||
req = HTTPRequest("http://127.0.0.1:9999/dashboard/", method="GET")
|
|
||||||
resp = HTTPClient().fetch(req)
|
|
||||||
break
|
|
||||||
except ConnectionRefusedError:
|
|
||||||
pass
|
|
||||||
# If the request didn't get through after 5 tries, this should fail
|
|
||||||
assert resp.code == 200
|
assert resp.code == 200
|
||||||
|
|
||||||
# cleanup
|
# cleanup
|
||||||
os.remove(os.path.join(extra_config_dir, "extra.toml"))
|
os.remove(os.path.join(extra_static_config_dir, "extra.toml"))
|
||||||
|
open(os.path.join(STATE_DIR, "traefik.toml"), "w").close()
|
||||||
reload_component("proxy")
|
reload_component("proxy")
|
||||||
|
|
||||||
|
|
||||||
@@ -128,30 +146,34 @@ def test_extra_traefik_dynamic_config():
|
|||||||
dynamic_config_dir = os.path.join(STATE_DIR, "rules")
|
dynamic_config_dir = os.path.join(STATE_DIR, "rules")
|
||||||
os.makedirs(dynamic_config_dir, exist_ok=True)
|
os.makedirs(dynamic_config_dir, exist_ok=True)
|
||||||
|
|
||||||
extra_config = {
|
extra_dynamic_config = {
|
||||||
"frontends": {
|
"frontends": {
|
||||||
"test": {
|
"test": {
|
||||||
"backend": "test",
|
"backend": "test",
|
||||||
"routes": {"rule1": {"rule": "Path: /test/proxy"}},
|
"routes": {
|
||||||
|
"rule1": {"rule": "PathPrefixStrip: /the/hub/runs/here/too"}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"backends": {
|
"backends": {
|
||||||
"test": {"servers": {"server1": {"url": "https://mybinder.org/"}}}
|
# redirect to hub
|
||||||
|
"test": {"servers": {"server1": {"url": "http://127.0.0.1:15001"}}}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
# Load the extra config
|
# write the extra dynamic config
|
||||||
with open(
|
with open(
|
||||||
os.path.join(dynamic_config_dir, "extra_rules.toml"), "w+"
|
os.path.join(dynamic_config_dir, "extra_rules.toml"), "w+"
|
||||||
) as extra_config_file:
|
) as extra_config_file:
|
||||||
toml.dump(extra_config, extra_config_file)
|
toml.dump(extra_dynamic_config, extra_config_file)
|
||||||
|
|
||||||
|
# load the extra config
|
||||||
reload_component("proxy")
|
reload_component("proxy")
|
||||||
|
|
||||||
req = HTTPRequest("http://127.0.0.1/test/", method="GET")
|
# test extra dynamic config
|
||||||
resp = HTTPClient().fetch(req)
|
resp = send_request(url="http://127.0.0.1/the/hub/runs/here/too", max_sleep=5)
|
||||||
print(resp)
|
|
||||||
assert resp.code == 200
|
assert resp.code == 200
|
||||||
|
assert resp.effective_url == "http://127.0.0.1/hub/login"
|
||||||
|
|
||||||
# cleanup
|
# cleanup
|
||||||
# os.remove(os.path.join(dynamic_config_dir, "extra_rules.toml"))
|
os.remove(os.path.join(dynamic_config_dir, "extra_rules.toml"))
|
||||||
# reload_component("proxy")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user