Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/tests/test_server.py
Show All 25 Lines | def prepare_config_file(tmpdir, content, name="config.yml"): | ||||
if isinstance(content, dict): # convert if needed | if isinstance(content, dict): # convert if needed | ||||
content = yaml.dump(content) | content = yaml.dump(content) | ||||
config_path.write_text(content, encoding="utf-8") | config_path.write_text(content, encoding="utf-8") | ||||
# pytest on python3.5 does not support LocalPath manipulation, so | # pytest on python3.5 does not support LocalPath manipulation, so | ||||
# convert path to string | # convert path to string | ||||
return str(config_path) | return str(config_path) | ||||
def test_load_and_check_config_no_configuration(): | @pytest.mark.parametrize("storage_class", [None, ""]) | ||||
def test_load_and_check_config_no_configuration(storage_class): | |||||
"""Inexistent configuration files raises""" | """Inexistent configuration files raises""" | ||||
with pytest.raises(EnvironmentError) as e: | with pytest.raises(EnvironmentError, match="Configuration file must be defined"): | ||||
load_and_check_config(None) | load_and_check_config(storage_class) | ||||
assert e.value.args[0] == "Configuration file must be defined" | |||||
def test_load_and_check_config_inexistent_file(): | |||||
config_path = "/some/inexistent/config.yml" | config_path = "/some/inexistent/config.yml" | ||||
with pytest.raises(FileNotFoundError) as e: | expected_error = f"Configuration file {config_path} does not exist" | ||||
with pytest.raises(FileNotFoundError, match=expected_error): | |||||
load_and_check_config(config_path) | load_and_check_config(config_path) | ||||
assert e.value.args[0] == "Configuration file %s does not exist" % (config_path,) | |||||
def test_load_and_check_config_wrong_configuration(tmpdir): | def test_load_and_check_config_wrong_configuration(tmpdir): | ||||
"""Wrong configuration raises""" | """Wrong configuration raises""" | ||||
config_path = prepare_config_file(tmpdir, "something: useless") | config_path = prepare_config_file(tmpdir, "something: useless") | ||||
with pytest.raises(KeyError) as e: | with pytest.raises(KeyError, match="Missing '%storage' configuration"): | ||||
load_and_check_config(config_path) | load_and_check_config(config_path) | ||||
assert e.value.args[0] == "Missing '%storage' configuration" | |||||
def test_load_and_check_config_local_config_fine(tmpdir): | def test_load_and_check_config_local_config_fine(tmpdir): | ||||
"""'Remote configuration is fine""" | """'local' complete configuration is fine""" | ||||
config = { | config = {"storage": {"cls": "local", "db": "db", "objstorage": "something",}} | ||||
"storage": {"cls": "local", "args": {"db": "db", "objstorage": "something",}} | |||||
} | |||||
config_path = prepare_config_file(tmpdir, config) | config_path = prepare_config_file(tmpdir, config) | ||||
cfg = load_and_check_config(config_path, type="local") | cfg = load_and_check_config(config_path) | ||||
assert cfg == config | |||||
def test_load_and_check_config_remote_config_fine(tmpdir): | |||||
"""'Remote configuration is fine""" | |||||
config = {"storage": {"cls": "remote", "args": {}}} | |||||
config_path = prepare_config_file(tmpdir, config) | |||||
cfg = load_and_check_config(config_path, type="any") | |||||
assert cfg == config | assert cfg == config |