2018-08-01 17:06:52 +02:00
|
|
|
"""Test traefik configuration"""
|
|
|
|
|
import os
|
2019-02-18 15:08:53 +02:00
|
|
|
from unittest import mock
|
2018-08-01 17:06:52 +02:00
|
|
|
|
2020-06-02 18:20:28 +03:00
|
|
|
import toml
|
2018-08-01 17:06:52 +02:00
|
|
|
|
|
|
|
|
from tljh import config
|
|
|
|
|
from tljh import traefik
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_traefik(tmpdir):
|
|
|
|
|
traefik_bin = tmpdir.mkdir("bin").join("traefik")
|
|
|
|
|
traefik.ensure_traefik_binary(str(tmpdir))
|
|
|
|
|
assert traefik_bin.exists()
|
2018-08-03 16:11:49 +02:00
|
|
|
# ignore higher-order permission bits, only verify ugo permissions
|
|
|
|
|
assert (traefik_bin.stat().mode & 0o777) == 0o755
|
2018-08-01 17:06:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_default_config(tmpdir, tljh_dir):
|
|
|
|
|
state_dir = tmpdir.mkdir("state")
|
2019-02-22 11:39:40 +01:00
|
|
|
traefik.ensure_traefik_config(str(state_dir))
|
2018-08-01 17:06:52 +02:00
|
|
|
assert state_dir.join("traefik.toml").exists()
|
|
|
|
|
traefik_toml = os.path.join(state_dir, "traefik.toml")
|
|
|
|
|
with open(traefik_toml) as f:
|
|
|
|
|
toml_cfg = f.read()
|
|
|
|
|
# print config for debugging on failure
|
|
|
|
|
print(config.CONFIG_FILE)
|
|
|
|
|
print(toml_cfg)
|
|
|
|
|
cfg = toml.loads(toml_cfg)
|
|
|
|
|
assert cfg["defaultEntryPoints"] == ["http"]
|
2019-02-11 11:13:22 +02:00
|
|
|
assert len(cfg["entryPoints"]["auth_api"]["auth"]["basic"]["users"]) == 1
|
|
|
|
|
# runtime generated entry, value not testable
|
|
|
|
|
cfg["entryPoints"]["auth_api"]["auth"]["basic"]["users"] = [""]
|
|
|
|
|
|
2019-01-22 16:24:38 +02:00
|
|
|
assert cfg["entryPoints"] == {
|
|
|
|
|
"http": {"address": ":80"},
|
|
|
|
|
"auth_api": {
|
2019-02-12 14:55:01 +02:00
|
|
|
"address": "127.0.0.1:8099",
|
2019-01-22 16:24:38 +02:00
|
|
|
"auth": {
|
2019-02-11 11:13:22 +02:00
|
|
|
"basic": {"users": [""]}
|
2019-01-22 16:24:38 +02:00
|
|
|
},
|
2019-02-11 11:13:22 +02:00
|
|
|
"whiteList": {"sourceRange": ["127.0.0.1"]}
|
2019-01-22 16:24:38 +02:00
|
|
|
},
|
2018-08-01 17:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_letsencrypt_config(tljh_dir):
|
|
|
|
|
state_dir = config.STATE_DIR
|
|
|
|
|
config.set_config_value(config.CONFIG_FILE, "https.enabled", True)
|
|
|
|
|
config.set_config_value(
|
|
|
|
|
config.CONFIG_FILE, "https.letsencrypt.email", "fake@jupyter.org"
|
|
|
|
|
)
|
|
|
|
|
config.set_config_value(
|
|
|
|
|
config.CONFIG_FILE, "https.letsencrypt.domains", ["testing.jovyan.org"]
|
|
|
|
|
)
|
2019-02-22 11:39:40 +01:00
|
|
|
traefik.ensure_traefik_config(str(state_dir))
|
2018-08-01 17:06:52 +02:00
|
|
|
traefik_toml = os.path.join(state_dir, "traefik.toml")
|
|
|
|
|
with open(traefik_toml) as f:
|
|
|
|
|
toml_cfg = f.read()
|
|
|
|
|
# print config for debugging on failure
|
|
|
|
|
print(config.CONFIG_FILE)
|
|
|
|
|
print(toml_cfg)
|
|
|
|
|
cfg = toml.loads(toml_cfg)
|
|
|
|
|
assert cfg["defaultEntryPoints"] == ["http", "https"]
|
|
|
|
|
assert "acme" in cfg
|
2019-02-11 11:13:22 +02:00
|
|
|
assert len(cfg["entryPoints"]["auth_api"]["auth"]["basic"]["users"]) == 1
|
|
|
|
|
# runtime generated entry, value not testable
|
|
|
|
|
cfg["entryPoints"]["auth_api"]["auth"]["basic"]["users"] = [""]
|
|
|
|
|
|
2018-08-01 17:06:52 +02:00
|
|
|
assert cfg["entryPoints"] == {
|
|
|
|
|
"http": {"address": ":80", "redirect": {"entryPoint": "https"}},
|
2020-01-31 12:35:34 +02:00
|
|
|
"https": {"address": ":443", "tls": {"minVersion": "VersionTLS12"}},
|
2019-01-22 16:24:38 +02:00
|
|
|
"auth_api": {
|
2019-02-12 14:55:01 +02:00
|
|
|
"address": "127.0.0.1:8099",
|
2019-01-22 16:24:38 +02:00
|
|
|
"auth": {
|
2019-02-11 11:13:22 +02:00
|
|
|
"basic": {"users": [""]}
|
2019-01-22 16:24:38 +02:00
|
|
|
},
|
2019-02-11 11:13:22 +02:00
|
|
|
"whiteList": {"sourceRange": ["127.0.0.1"]}
|
2019-01-22 16:24:38 +02:00
|
|
|
},
|
2018-08-01 17:06:52 +02:00
|
|
|
}
|
|
|
|
|
assert cfg["acme"] == {
|
|
|
|
|
"email": "fake@jupyter.org",
|
|
|
|
|
"storage": "acme.json",
|
|
|
|
|
"entryPoint": "https",
|
|
|
|
|
"httpChallenge": {"entryPoint": "http"},
|
|
|
|
|
"domains": [{"main": "testing.jovyan.org"}],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_manual_ssl_config(tljh_dir):
|
|
|
|
|
state_dir = config.STATE_DIR
|
|
|
|
|
config.set_config_value(config.CONFIG_FILE, "https.enabled", True)
|
|
|
|
|
config.set_config_value(config.CONFIG_FILE, "https.tls.key", "/path/to/ssl.key")
|
|
|
|
|
config.set_config_value(config.CONFIG_FILE, "https.tls.cert", "/path/to/ssl.cert")
|
2019-02-22 11:39:40 +01:00
|
|
|
traefik.ensure_traefik_config(str(state_dir))
|
2018-08-01 17:06:52 +02:00
|
|
|
traefik_toml = os.path.join(state_dir, "traefik.toml")
|
|
|
|
|
with open(traefik_toml) as f:
|
|
|
|
|
toml_cfg = f.read()
|
|
|
|
|
# print config for debugging on failure
|
|
|
|
|
print(config.CONFIG_FILE)
|
|
|
|
|
print(toml_cfg)
|
|
|
|
|
cfg = toml.loads(toml_cfg)
|
|
|
|
|
assert cfg["defaultEntryPoints"] == ["http", "https"]
|
|
|
|
|
assert "acme" not in cfg
|
2019-02-11 11:13:22 +02:00
|
|
|
assert len(cfg["entryPoints"]["auth_api"]["auth"]["basic"]["users"]) == 1
|
|
|
|
|
# runtime generated entry, value not testable
|
|
|
|
|
cfg["entryPoints"]["auth_api"]["auth"]["basic"]["users"] = [""]
|
2018-08-01 17:06:52 +02:00
|
|
|
assert cfg["entryPoints"] == {
|
|
|
|
|
"http": {"address": ":80", "redirect": {"entryPoint": "https"}},
|
|
|
|
|
"https": {
|
|
|
|
|
"address": ":443",
|
2018-08-03 16:17:22 +02:00
|
|
|
"tls": {
|
2020-01-31 12:35:34 +02:00
|
|
|
"minVersion": "VersionTLS12",
|
2018-08-03 16:17:22 +02:00
|
|
|
"certificates": [
|
|
|
|
|
{"certFile": "/path/to/ssl.cert", "keyFile": "/path/to/ssl.key"}
|
|
|
|
|
]
|
|
|
|
|
},
|
2018-08-01 17:06:52 +02:00
|
|
|
},
|
2019-01-22 16:24:38 +02:00
|
|
|
"auth_api": {
|
2019-02-12 14:55:01 +02:00
|
|
|
"address": "127.0.0.1:8099",
|
2019-01-22 16:24:38 +02:00
|
|
|
"auth": {
|
2019-02-11 11:13:22 +02:00
|
|
|
"basic": {"users": [""]}
|
2019-01-22 16:24:38 +02:00
|
|
|
},
|
2019-02-11 11:13:22 +02:00
|
|
|
"whiteList": {"sourceRange": ["127.0.0.1"]}
|
2019-01-22 16:24:38 +02:00
|
|
|
},
|
2018-08-01 17:06:52 +02:00
|
|
|
}
|
2020-06-02 18:20:28 +03:00
|
|
|
|
|
|
|
|
def test_extra_config(tmpdir, tljh_dir):
|
|
|
|
|
extra_config_dir = os.path.join(tljh_dir, config.CONFIG_DIR, "traefik_config.d")
|
|
|
|
|
|
|
|
|
|
state_dir = tmpdir.mkdir("state")
|
|
|
|
|
|
|
|
|
|
extra_config = {
|
|
|
|
|
# modify existing value
|
|
|
|
|
"dashboard": True,
|
|
|
|
|
# modify existing value with multiple levels
|
|
|
|
|
"entryPoints": {
|
|
|
|
|
"auth_api": {
|
|
|
|
|
"address": "127.0.0.1:9999"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
# add new setting
|
|
|
|
|
"checkNewVersion": False
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 19:09:19 +03:00
|
|
|
with open(os.path.join(extra_config_dir, "extra.toml"), "w+") as extra_config_file:
|
2020-06-02 18:20:28 +03:00
|
|
|
toml.dump(extra_config, extra_config_file)
|
|
|
|
|
|
|
|
|
|
# This merges the 2 configs
|
|
|
|
|
traefik.ensure_traefik_config(str(state_dir))
|
|
|
|
|
traefik_toml = os.path.join(state_dir, "traefik.toml")
|
|
|
|
|
|
|
|
|
|
# Read back the merged config
|
|
|
|
|
toml_cfg = toml.load(traefik_toml)
|
|
|
|
|
|
|
|
|
|
assert extra_config.items() <= toml_cfg.items()
|