Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/tests/test_server.py
Show First 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | def test_load_and_check_config_invalid_configuration_toplevel(tmpdir): | ||||
with pytest.raises(KeyError) as e: | with pytest.raises(KeyError) as e: | ||||
load_and_check_config(config_path) | load_and_check_config(config_path) | ||||
assert e.value.args[0] == "Invalid configuration; missing objstorage config entry" | assert e.value.args[0] == "Invalid configuration; missing objstorage config entry" | ||||
def test_load_and_check_config_invalid_configuration(tmpdir): | def test_load_and_check_config_invalid_configuration(tmpdir): | ||||
"""Invalid configuration raises""" | """Invalid configuration raises""" | ||||
for data, missing_keys in [ | config_path = prepare_config_file( | ||||
({"objstorage": {"something": "useless"}}, ["cls", "args"]), | tmpdir, content={"objstorage": {"something": "useless"}} | ||||
({"objstorage": {"cls": "something"}}, ["args"]), | ) | ||||
ardumont: jsyk, we can (and use) and mostly do now the simplified version of:
```
with pytest.raises… | |||||
]: | |||||
config_path = prepare_config_file(tmpdir, content=data) | |||||
with pytest.raises(KeyError) as e: | with pytest.raises(KeyError) as e: | ||||
load_and_check_config(config_path) | load_and_check_config(config_path) | ||||
assert e.value.args[0] == "Invalid configuration; missing %s config entry" % ( | assert "missing cls config entry" in e.value.args[0] | ||||
", ".join(missing_keys), | |||||
) | |||||
def test_load_and_check_config_invalid_configuration_level2(tmpdir): | def test_load_and_check_config_invalid_configuration_level2(tmpdir): | ||||
"""Invalid configuration at 2nd level raises""" | """Invalid configuration at 2nd level raises""" | ||||
config = { | config = { | ||||
"objstorage": { | "objstorage": { | ||||
"cls": "pathslicing", | "cls": "pathslicing", | ||||
"args": {"root": "root", "slicing": "slicing",}, | "args": {"root": "root", "slicing": "slicing",}, | ||||
"client_max_size": "10", | "client_max_size": "10", | ||||
} | } | ||||
} | } | ||||
for key in ("root", "slicing"): | for key in ("root", "slicing"): | ||||
c = copy.deepcopy(config) | c = copy.deepcopy(config) | ||||
c["objstorage"]["args"].pop(key) | c["objstorage"]["args"].pop(key) | ||||
config_path = prepare_config_file(tmpdir, c) | config_path = prepare_config_file(tmpdir, c) | ||||
with pytest.raises(KeyError) as e: | with pytest.raises(KeyError) as e: | ||||
load_and_check_config(config_path) | load_and_check_config(config_path) | ||||
assert ( | assert "missing %s config entry" % key in e.value.args[0] | ||||
e.value.args[0] | |||||
== "Invalid configuration; missing args.%s config entry" % key | |||||
) | |||||
def test_load_and_check_config_fine(tmpdir): | def test_load_and_check_config_fine(tmpdir): | ||||
"""pathslicing configuration fine loads ok""" | """pathslicing configuration fine loads ok""" | ||||
config = { | config = { | ||||
"objstorage": { | "objstorage": { | ||||
"cls": "pathslicing", | "cls": "pathslicing", | ||||
"args": {"root": "root", "slicing": "slicing",}, | "args": {"root": "root", "slicing": "slicing",}, | ||||
} | } | ||||
} | } | ||||
config_path = prepare_config_file(tmpdir, config) | config_path = prepare_config_file(tmpdir, config) | ||||
cfg = load_and_check_config(config_path) | cfg = load_and_check_config(config_path) | ||||
assert cfg == config | assert cfg == config | ||||
def test_load_and_check_config_no_args(tmpdir): | |||||
"""pathslicing configuration with no args key loads ok""" | |||||
config = { | |||||
"objstorage": {"cls": "pathslicing", "root": "root", "slicing": "slicing"} | |||||
} | |||||
config_path = prepare_config_file(tmpdir, config) | |||||
cfg = load_and_check_config(config_path) | |||||
assert cfg == config | |||||
def test_load_and_check_config_fine2(tmpdir): | def test_load_and_check_config_fine2(tmpdir): | ||||
config = {"client_max_size": "10", "objstorage": {"cls": "remote", "args": {}}} | config = {"client_max_size": "10", "objstorage": {"cls": "memory", "args": {}}} | ||||
config_path = prepare_config_file(tmpdir, config) | |||||
cfg = load_and_check_config(config_path) | |||||
assert cfg == config | |||||
def test_load_and_check_config_only_cls(tmpdir): | |||||
config = {"client_max_size": "10", "objstorage": {"cls": "memory"}} | |||||
config_path = prepare_config_file(tmpdir, config) | config_path = prepare_config_file(tmpdir, config) | ||||
cfg = load_and_check_config(config_path) | cfg = load_and_check_config(config_path) | ||||
assert cfg == config | assert cfg == config | ||||
Done Inline ActionsAnd for this ^ 3 scenarios, I would be tempted to use pytest parametrize functionality [1]: @pytest.mark.parametrize("config", [ {"objstorage": {"cls": "pathslicing", "root": "root", "slicing": "slicing"}}, {"client_max_size": "10", "objstorage": {"cls": "memory", "args": {}}}, {"client_max_size": "10", "objstorage": {"cls": "memory"}} ]) def test_load_and_check_config(config, tmpdir): config_path = prepare_config_file(tmpdir, config) cfg = load_and_check_config(config_path) assert cfg == config [1] unsure whether the tmpdir fixture injection is working with this though ¯\_(ツ)_/¯ ardumont: And for this ^ 3 scenarios, I would be tempted to use pytest parametrize functionality [1]… |
jsyk, we can (and use) and mostly do now the simplified version of: