2018-07-27 21:08:26 -07:00
|
|
|
"""
|
|
|
|
|
Test configuration commandline tools
|
|
|
|
|
"""
|
2018-07-31 13:24:37 +02:00
|
|
|
|
|
|
|
|
import os
|
2018-07-27 21:08:26 -07:00
|
|
|
import tempfile
|
2018-07-31 13:24:37 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from tljh import config, configurer
|
|
|
|
|
|
|
|
|
|
|
2018-07-27 21:08:26 -07:00
|
|
|
def test_set_no_mutate():
|
|
|
|
|
conf = {}
|
|
|
|
|
|
|
|
|
|
new_conf = config.set_item_in_config(conf, 'a.b', 'c')
|
|
|
|
|
assert new_conf['a']['b'] == 'c'
|
|
|
|
|
assert conf == {}
|
|
|
|
|
|
2018-07-31 13:24:37 +02:00
|
|
|
|
2018-07-27 21:08:26 -07:00
|
|
|
def test_set_one_level():
|
|
|
|
|
conf = {}
|
|
|
|
|
|
|
|
|
|
new_conf = config.set_item_in_config(conf, 'a', 'b')
|
|
|
|
|
assert new_conf['a'] == 'b'
|
|
|
|
|
|
2018-07-31 13:24:37 +02:00
|
|
|
|
2018-07-27 21:08:26 -07:00
|
|
|
def test_set_multi_level():
|
|
|
|
|
conf = {}
|
|
|
|
|
|
|
|
|
|
new_conf = config.set_item_in_config(conf, 'a.b', 'c')
|
|
|
|
|
new_conf = config.set_item_in_config(new_conf, 'a.d', 'e')
|
|
|
|
|
new_conf = config.set_item_in_config(new_conf, 'f', 'g')
|
|
|
|
|
assert new_conf == {
|
|
|
|
|
'a': {'b': 'c', 'd': 'e'},
|
|
|
|
|
'f': 'g'
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 13:24:37 +02:00
|
|
|
|
2018-07-27 21:08:26 -07:00
|
|
|
def test_set_overwrite():
|
|
|
|
|
"""
|
|
|
|
|
We can overwrite already existing config items.
|
|
|
|
|
|
|
|
|
|
This might be surprising destructive behavior to some :D
|
|
|
|
|
"""
|
|
|
|
|
conf = {
|
|
|
|
|
'a': 'b'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_conf = config.set_item_in_config(conf, 'a', 'c')
|
|
|
|
|
assert new_conf == {'a': 'c'}
|
|
|
|
|
|
|
|
|
|
new_conf = config.set_item_in_config(new_conf, 'a.b', 'd')
|
|
|
|
|
assert new_conf == {'a': {'b': 'd'}}
|
|
|
|
|
|
|
|
|
|
new_conf = config.set_item_in_config(new_conf, 'a', 'hi')
|
|
|
|
|
assert new_conf == {'a': 'hi'}
|
|
|
|
|
|
|
|
|
|
|
2019-05-28 16:13:42 +03:00
|
|
|
def test_unset_no_mutate():
|
|
|
|
|
conf = {'a': 'b'}
|
|
|
|
|
|
|
|
|
|
new_conf = config.unset_item_from_config(conf, 'a')
|
|
|
|
|
assert conf == {'a': 'b'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unset_one_level():
|
|
|
|
|
conf = {'a': 'b'}
|
|
|
|
|
|
|
|
|
|
new_conf = config.unset_item_from_config(conf, 'a')
|
|
|
|
|
assert new_conf == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unset_multi_level():
|
|
|
|
|
conf = {
|
|
|
|
|
'a': {'b': 'c', 'd': 'e'},
|
|
|
|
|
'f': 'g'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_conf = config.unset_item_from_config(conf, 'a.b')
|
|
|
|
|
assert new_conf == {
|
|
|
|
|
'a': {'d': 'e'},
|
|
|
|
|
'f': 'g'
|
|
|
|
|
}
|
|
|
|
|
new_conf = config.unset_item_from_config(new_conf, 'a.d')
|
|
|
|
|
assert new_conf == {'f': 'g'}
|
|
|
|
|
new_conf = config.unset_item_from_config(new_conf, 'f')
|
|
|
|
|
assert new_conf == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unset_and_clean_empty_configs():
|
|
|
|
|
conf = {
|
|
|
|
|
'a': {'b': {'c': {'d': {'e': 'f'}}}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_conf = config.unset_item_from_config(conf, 'a.b.c.d.e')
|
|
|
|
|
assert new_conf == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unset_config_error():
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
config.unset_item_from_config({}, 'a')
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
config.unset_item_from_config({'a': 'b'}, 'b')
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
config.unset_item_from_config({'a': {'b': 'c'}}, 'a.z')
|
|
|
|
|
|
|
|
|
|
|
2018-07-28 11:05:29 -07:00
|
|
|
def test_add_to_config_one_level():
|
|
|
|
|
conf = {}
|
|
|
|
|
|
|
|
|
|
new_conf = config.add_item_to_config(conf, 'a.b', 'c')
|
|
|
|
|
assert new_conf == {
|
|
|
|
|
'a': {'b': ['c']}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_to_config_zero_level():
|
|
|
|
|
conf = {}
|
|
|
|
|
|
|
|
|
|
new_conf = config.add_item_to_config(conf, 'a', 'b')
|
|
|
|
|
assert new_conf == {
|
|
|
|
|
'a': ['b']
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 16:24:38 +02:00
|
|
|
|
2018-07-28 11:05:29 -07:00
|
|
|
def test_add_to_config_multiple():
|
|
|
|
|
conf = {}
|
|
|
|
|
|
|
|
|
|
new_conf = config.add_item_to_config(conf, 'a.b.c', 'd')
|
|
|
|
|
assert new_conf == {
|
|
|
|
|
'a': {'b': {'c': ['d']}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_conf = config.add_item_to_config(new_conf, 'a.b.c', 'e')
|
|
|
|
|
assert new_conf == {
|
|
|
|
|
'a': {'b': {'c': ['d', 'e']}}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-28 11:57:11 -07:00
|
|
|
|
|
|
|
|
def test_remove_from_config():
|
|
|
|
|
conf = {}
|
|
|
|
|
|
|
|
|
|
new_conf = config.add_item_to_config(conf, 'a.b.c', 'd')
|
|
|
|
|
new_conf = config.add_item_to_config(new_conf, 'a.b.c', 'e')
|
|
|
|
|
assert new_conf == {
|
|
|
|
|
'a': {'b': {'c': ['d', 'e']}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_conf = config.remove_item_from_config(new_conf, 'a.b.c', 'e')
|
|
|
|
|
assert new_conf == {
|
|
|
|
|
'a': {'b': {'c': ['d']}}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 13:24:37 +02:00
|
|
|
|
2018-07-28 11:57:11 -07:00
|
|
|
def test_remove_from_config_error():
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
config.remove_item_from_config({}, 'a.b.c', 'e')
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
config.remove_item_from_config({'a': 'b'}, 'a.b', 'e')
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
config.remove_item_from_config({'a': ['b']}, 'a', 'e')
|
|
|
|
|
|
|
|
|
|
|
2018-07-31 13:24:37 +02:00
|
|
|
def test_reload_hub():
|
2019-01-22 16:24:38 +02:00
|
|
|
with mock.patch('tljh.systemd.restart_service') as restart_service, mock.patch(
|
|
|
|
|
'tljh.systemd.check_service_active'
|
2019-02-11 09:24:16 +02:00
|
|
|
) as check_active, mock.patch('tljh.config.check_hub_ready') as check_ready:
|
2018-07-31 13:24:37 +02:00
|
|
|
config.reload_component('hub')
|
|
|
|
|
assert restart_service.called_with('jupyterhub')
|
2019-01-22 16:24:38 +02:00
|
|
|
assert check_active.called_with('jupyterhub')
|
2018-07-31 13:24:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_reload_proxy(tljh_dir):
|
2019-02-13 14:59:57 +02:00
|
|
|
with mock.patch("tljh.systemd.restart_service") as restart_service, mock.patch(
|
|
|
|
|
"tljh.systemd.check_service_active"
|
2019-02-22 11:39:40 +01:00
|
|
|
) as check_active:
|
2018-07-31 13:24:37 +02:00
|
|
|
config.reload_component('proxy')
|
|
|
|
|
assert restart_service.called_with('traefik')
|
2019-02-11 09:24:16 +02:00
|
|
|
assert check_active.called_with('traefik')
|
2018-07-31 13:24:37 +02:00
|
|
|
assert os.path.exists(os.path.join(config.STATE_DIR, 'traefik.toml'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cli_no_command(capsys):
|
|
|
|
|
config.main([])
|
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
|
assert "usage:" in captured.out
|
|
|
|
|
assert "positional arguments:" in captured.out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"arg, value",
|
|
|
|
|
[
|
|
|
|
|
("true", True),
|
2019-01-22 16:24:38 +02:00
|
|
|
("FALSE", False)
|
|
|
|
|
]
|
2018-07-31 13:24:37 +02:00
|
|
|
)
|
|
|
|
|
def test_cli_set_bool(tljh_dir, arg, value):
|
|
|
|
|
config.main(["set", "https.enabled", arg])
|
2019-02-22 11:39:40 +01:00
|
|
|
cfg = configurer.load_config()
|
2018-07-31 13:24:37 +02:00
|
|
|
assert cfg['https']['enabled'] == value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cli_set_int(tljh_dir):
|
|
|
|
|
config.main(["set", "https.port", "123"])
|
2019-02-22 11:39:40 +01:00
|
|
|
cfg = configurer.load_config()
|
2018-07-31 13:24:37 +02:00
|
|
|
assert cfg['https']['port'] == 123
|
|
|
|
|
|
|
|
|
|
|
2019-05-28 16:13:42 +03:00
|
|
|
def test_cli_unset(tljh_dir):
|
|
|
|
|
config.main(["set", "foo.bar", "1"])
|
|
|
|
|
config.main(["set", "foo.bar2", "2"])
|
|
|
|
|
cfg = configurer.load_config()
|
|
|
|
|
assert cfg['foo'] == {'bar': 1, 'bar2': 2}
|
|
|
|
|
|
|
|
|
|
config.main(["unset", "foo.bar"])
|
|
|
|
|
cfg = configurer.load_config()
|
|
|
|
|
|
|
|
|
|
assert cfg['foo'] == {'bar2': 2}
|
|
|
|
|
|
|
|
|
|
|
2018-07-31 13:24:37 +02:00
|
|
|
def test_cli_add_float(tljh_dir):
|
|
|
|
|
config.main(["add-item", "foo.bar", "1.25"])
|
2019-02-22 11:39:40 +01:00
|
|
|
cfg = configurer.load_config()
|
2018-07-31 13:24:37 +02:00
|
|
|
assert cfg['foo']['bar'] == [1.25]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cli_remove_int(tljh_dir):
|
|
|
|
|
config.main(["add-item", "foo.bar", "1"])
|
|
|
|
|
config.main(["add-item", "foo.bar", "2"])
|
2019-02-22 11:39:40 +01:00
|
|
|
cfg = configurer.load_config()
|
2018-07-31 13:24:37 +02:00
|
|
|
assert cfg['foo']['bar'] == [1, 2]
|
|
|
|
|
config.main(["remove-item", "foo.bar", "1"])
|
2019-02-22 11:39:40 +01:00
|
|
|
cfg = configurer.load_config()
|
2018-07-31 13:24:37 +02:00
|
|
|
assert cfg['foo']['bar'] == [2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"value, expected",
|
|
|
|
|
[
|
|
|
|
|
("1", 1),
|
|
|
|
|
("1.25", 1.25),
|
|
|
|
|
("x", "x"),
|
|
|
|
|
("1x", "1x"),
|
|
|
|
|
("1.2x", "1.2x"),
|
|
|
|
|
(None, None),
|
|
|
|
|
("", ""),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_parse_value(value, expected):
|
|
|
|
|
assert config.parse_value(value) == expected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_show_config(capsys):
|
2018-07-27 21:08:26 -07:00
|
|
|
"""
|
|
|
|
|
Test stdout output when showing config
|
|
|
|
|
"""
|
|
|
|
|
conf = """
|
|
|
|
|
# Just some test YAML
|
|
|
|
|
a:
|
|
|
|
|
b:
|
|
|
|
|
- h
|
|
|
|
|
- 1
|
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile() as tmp:
|
|
|
|
|
tmp.write(conf.encode())
|
|
|
|
|
tmp.flush()
|
2018-07-31 13:24:37 +02:00
|
|
|
config.show_config(tmp.name)
|
|
|
|
|
out = capsys.readouterr().out
|
|
|
|
|
assert out.strip() == conf
|