diff --git a/integration-tests/test_proxy.py b/integration-tests/test_proxy.py index ced51a3..f755470 100644 --- a/integration-tests/test_proxy.py +++ b/integration-tests/test_proxy.py @@ -12,12 +12,36 @@ import pytest from tljh.config import ( reload_component, set_config_value, + unset_config_value, CONFIG_FILE, CONFIG_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): ssl_dir = "/etc/tljh-ssl-test" key = ssl_dir + "/ssl.key" @@ -61,28 +85,25 @@ def test_manual_https(preserve_config): # verify that our certificate was loaded by traefik assert server_cert == file_cert - for i in range(10): - time.sleep(i) - # verify that we can still connect to the hub - 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 + # verify that we can still connect to the hub + resp = send_request( + url="https://127.0.0.1/hub/api", max_sleep=10, validate_cert=False + ) assert resp.code == 200 + # cleanup shutil.rmtree(ssl_dir) + set_config_value(CONFIG_FILE, "https.enabled", False) + + reload_component("proxy") def test_extra_traefik_static_config(): - extra_config_dir = os.path.join(CONFIG_DIR, "traefik_config.d") - os.makedirs(extra_config_dir, exist_ok=True) + extra_static_config_dir = os.path.join(CONFIG_DIR, "traefik_config.d") + os.makedirs(extra_static_config_dir, exist_ok=True) - extra_config = { + extra_static_config = { "entryPoints": {"no_auth_api": {"address": "127.0.0.1:9999"}}, "api": {"dashboard": True, "entrypoint": "no_auth_api"}, } @@ -102,25 +123,22 @@ def test_extra_traefik_static_config(): assert success == True - # Load the extra config - with open(os.path.join(extra_config_dir, "extra.toml"), "w+") as extra_config_file: - toml.dump(extra_config, extra_config_file) + # write the extra static config + with open( + 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") - for i in range(5): - time.sleep(i) - 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 + # the new dashboard entrypoint shouldn't require authentication anymore + resp = send_request(url="http://127.0.0.1:9999/dashboard/", max_sleep=5) assert resp.code == 200 # 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") @@ -128,30 +146,34 @@ def test_extra_traefik_dynamic_config(): dynamic_config_dir = os.path.join(STATE_DIR, "rules") os.makedirs(dynamic_config_dir, exist_ok=True) - extra_config = { + extra_dynamic_config = { "frontends": { "test": { "backend": "test", - "routes": {"rule1": {"rule": "Path: /test/proxy"}}, + "routes": { + "rule1": {"rule": "PathPrefixStrip: /the/hub/runs/here/too"} + }, } }, "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( os.path.join(dynamic_config_dir, "extra_rules.toml"), "w+" ) 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") - req = HTTPRequest("http://127.0.0.1/test/", method="GET") - resp = HTTPClient().fetch(req) - print(resp) + # test extra dynamic config + resp = send_request(url="http://127.0.0.1/the/hub/runs/here/too", max_sleep=5) assert resp.code == 200 + assert resp.effective_url == "http://127.0.0.1/hub/login" # cleanup - # os.remove(os.path.join(dynamic_config_dir, "extra_rules.toml")) - # reload_component("proxy") + os.remove(os.path.join(dynamic_config_dir, "extra_rules.toml"))