1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-25 14:03:00 +00:00
kiwi-scp/tests/test_config.py

470 lines
14 KiB
Python
Raw Normal View History

2021-10-14 02:34:09 +00:00
from ipaddress import IPv4Network
from pathlib import Path
import pytest
from pydantic import ValidationError
from kiwi_scp.config import KiwiConfig
2021-12-02 16:06:13 +00:00
from kiwi_scp.yaml import YAML
2021-10-14 02:34:09 +00:00
2021-10-15 17:39:14 +00:00
class UnCoercible:
"""A class that doesn't have a string representation"""
2021-10-15 17:39:14 +00:00
def __str__(self):
raise ValueError
2021-10-26 14:11:28 +00:00
class TestDefault:
def test(self):
import toml
2021-10-25 10:07:49 +00:00
2021-10-26 14:11:28 +00:00
c = KiwiConfig()
version = toml.load("./pyproject.toml")["tool"]["poetry"]["version"]
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
assert c == KiwiConfig.from_default()
2021-10-20 12:32:45 +00:00
2021-10-26 14:11:28 +00:00
assert c.version == version
assert len(c.shells) == 1
assert c.shells[0] == Path("/bin/bash")
assert c.projects == []
assert c.environment == {}
assert c.storage.directory == Path("/var/local/kiwi")
assert c.network.name == "kiwi_hub"
assert c.network.cidr == IPv4Network("10.22.46.0/24")
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
kiwi_dict = {
"version": version,
"shells": ["/bin/bash"],
"storage": {"directory": "/var/local/kiwi"},
"network": {
"name": "kiwi_hub",
"cidr": "10.22.46.0/24",
},
}
assert c.kiwi_dict == kiwi_dict
2021-10-28 11:54:02 +00:00
assert c.kiwi_yml == YAML().dump_kiwi_yml(kiwi_dict)
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
class TestVersion:
def test_valid(self):
c = KiwiConfig(version="0.0.0")
assert c.version == "0.0.0"
2021-10-26 14:11:28 +00:00
c = KiwiConfig(version="0.0")
assert c.version == "0.0"
2021-10-26 14:11:28 +00:00
c = KiwiConfig(version="0")
assert c.version == "0"
2021-10-26 14:11:28 +00:00
c = KiwiConfig(version=1.0)
assert c.version == "1.0"
2021-10-26 14:11:28 +00:00
c = KiwiConfig(version=1)
assert c.version == "1"
2021-10-26 14:11:28 +00:00
def test_invalid(self):
# definitely not a version
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(version="dnaf")
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"].find("string does not match regex") != -1
assert error["type"] == "value_error.str.regex"
2021-10-26 14:11:28 +00:00
# almost a version
with pytest.raises(ValidationError) as exc_info:
c = KiwiConfig(version="0.0.0alpha")
print(c.version)
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"].find("string does not match regex") != -1
assert error["type"] == "value_error.str.regex"
2021-10-26 14:11:28 +00:00
class TestShells:
def test_empty(self):
c = KiwiConfig(shells=None)
2021-10-26 14:11:28 +00:00
assert c == KiwiConfig(shells=[])
2021-10-26 14:11:28 +00:00
assert c.shells == []
2021-10-26 14:11:28 +00:00
def test_list(self):
c = KiwiConfig(shells=["/bin/sh", "sh"])
2021-10-26 14:11:28 +00:00
assert len(c.shells) == 2
assert c.shells[0] == Path("/bin/sh")
assert c.shells[1] == Path("sh")
2021-10-26 14:11:28 +00:00
c = KiwiConfig(shells=["/bin/bash"])
assert len(c.shells) == 1
assert c.shells[0] == Path("/bin/bash")
2021-10-26 14:11:28 +00:00
def test_dict(self):
c = KiwiConfig(shells={"/bin/bash": None})
2021-10-26 14:11:28 +00:00
assert len(c.shells) == 1
assert c.shells[0] == Path("/bin/bash")
def test_coercible(self):
c = KiwiConfig(shells="/bin/bash")
assert c == KiwiConfig(shells=Path("/bin/bash"))
assert len(c.shells) == 1
assert c.shells[0] == Path("/bin/bash")
c = KiwiConfig(shells=123)
assert len(c.shells) == 1
assert c.shells[0] == Path("123")
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
def test_uncoercible(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(shells=UnCoercible())
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Shells Format"
assert error["type"] == "value_error"
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(shells=["/bin/bash", UnCoercible()])
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "value is not a valid path"
assert error["type"] == "type_error.path"
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
class TestProject:
def test_empty(self):
c = KiwiConfig(projects=None)
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert c == KiwiConfig(projects=[])
assert c.projects == []
2021-10-15 17:39:14 +00:00
2021-10-28 13:39:11 +00:00
assert c.get_project_config("invalid") is None
2021-10-26 14:19:33 +00:00
2021-10-26 14:11:28 +00:00
def test_long(self):
kiwi_dict = {
"name": "project",
"enabled": False,
"override_storage": {"directory": "/test/directory"},
}
c = KiwiConfig(projects=[kiwi_dict])
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
2021-10-26 14:19:33 +00:00
assert p == c.get_project_config("project")
2021-10-26 14:11:28 +00:00
assert not p.enabled
assert p.override_storage is not None
assert c.kiwi_dict["projects"][0] == kiwi_dict
def test_storage_str(self):
kiwi_dict = {
"name": "project",
"enabled": False,
"override_storage": "/test/directory",
}
c = KiwiConfig(projects=[kiwi_dict])
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
assert not p.enabled
assert p.override_storage is not None
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
def test_storage_list(self):
kiwi_dict = {
"name": "project",
"enabled": False,
"override_storage": ["/test/directory"],
}
c = KiwiConfig(projects=[kiwi_dict])
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
assert not p.enabled
assert p.override_storage is not None
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
def test_storage_invalid(self):
kiwi_dict = {
"name": "project",
"enabled": False,
"override_storage": True,
}
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(projects=[kiwi_dict])
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Storage Format"
assert error["type"] == "value_error"
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
def test_short(self):
kiwi_dict = {
"project": False,
}
c = KiwiConfig(projects=[kiwi_dict])
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
assert not p.enabled
assert p.override_storage is None
assert p.kiwi_dict == kiwi_dict
def test_dict(self):
c = KiwiConfig(projects={"name": "project"})
assert c == KiwiConfig(projects=[{"name": "project"}])
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
assert p.enabled
assert p.override_storage is None
def test_invalid_dict(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(projects={
"random key 1": "random value 1",
"random key 2": "random value 2",
})
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Project Format"
assert error["type"] == "value_error"
def test_coercible(self):
c = KiwiConfig(projects="project")
assert c == KiwiConfig(projects=["project"])
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
assert p.enabled
assert p.override_storage is None
def test_uncoercible(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(projects=["valid", UnCoercible()])
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Projects Format"
assert error["type"] == "value_error"
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(projects=UnCoercible())
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Projects Format"
assert error["type"] == "value_error"
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
class TestEnvironment:
def test_empty(self):
c = KiwiConfig(environment=None)
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert c.environment == {}
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
def test_dict(self):
c = KiwiConfig(environment={})
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert c.environment == {}
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
kiwi_dict = {"variable": "value"}
c = KiwiConfig(environment=kiwi_dict)
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert len(c.environment) == 1
assert "variable" in c.environment
assert c.environment["variable"] == "value"
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
assert c.kiwi_dict["environment"] == kiwi_dict
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
def test_list(self):
c = KiwiConfig(environment=[])
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
assert c.environment == {}
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
c = KiwiConfig(environment=[
"variable=value",
])
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
assert len(c.environment) == 1
assert "variable" in c.environment
assert c.environment["variable"] == "value"
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
c = KiwiConfig(environment=[
"variable",
])
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
assert len(c.environment) == 1
assert "variable" in c.environment
assert c.environment["variable"] is None
2021-10-26 14:11:28 +00:00
c = KiwiConfig(environment=[
123,
])
2021-10-26 14:11:28 +00:00
assert len(c.environment) == 1
assert "123" in c.environment
assert c.environment["123"] is None
2021-10-26 14:11:28 +00:00
def test_coercible(self):
c = KiwiConfig(environment="variable=value")
2021-10-26 14:11:28 +00:00
assert len(c.environment) == 1
assert "variable" in c.environment
assert c.environment["variable"] == "value"
2021-10-26 14:11:28 +00:00
c = KiwiConfig(environment="variable")
2021-10-26 14:11:28 +00:00
assert len(c.environment) == 1
assert "variable" in c.environment
assert c.environment["variable"] is None
2021-10-26 14:11:28 +00:00
c = KiwiConfig(environment=123)
2021-10-26 14:11:28 +00:00
assert len(c.environment) == 1
assert "123" in c.environment
assert c.environment["123"] is None
2021-10-26 14:11:28 +00:00
c = KiwiConfig(environment=123.4)
2021-10-26 14:11:28 +00:00
assert len(c.environment) == 1
assert "123.4" in c.environment
assert c.environment["123.4"] is None
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
def test_uncoercible(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(environment=UnCoercible())
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Environment Format"
assert error["type"] == "value_error"
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(environment=["valid", UnCoercible()])
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Environment Format"
assert error["type"] == "value_error"
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
class TestStorage:
def test_empty(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(storage=None)
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "No Storage Given"
assert error["type"] == "value_error"
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
def test_dict(self):
kiwi_dict = {"directory": "/test/directory"}
c = KiwiConfig(storage=kiwi_dict)
2021-10-14 17:18:24 +00:00
2021-10-26 14:11:28 +00:00
assert c.storage.directory == Path("/test/directory")
assert c.storage.kiwi_dict == kiwi_dict
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
def test_invalid_dict(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(storage={"random key": "random value"})
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Storage Format"
assert error["type"] == "value_error"
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
def test_str(self):
c = KiwiConfig(storage="/test/directory")
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
assert c.storage.directory == Path("/test/directory")
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
def test_list(self):
c = KiwiConfig(storage=["/test/directory"])
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
assert c.storage.directory == Path("/test/directory")
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
def test_invalid(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(storage=True)
2021-10-14 02:34:09 +00:00
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Storage Format"
assert error["type"] == "value_error"
2021-10-15 17:39:14 +00:00
2021-10-26 14:11:28 +00:00
class TestNetwork:
def test_empty(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(network=None)
2021-10-26 14:11:28 +00:00
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "No Network Given"
assert error["type"] == "value_error"
2021-10-26 14:11:28 +00:00
def test_dict(self):
kiwi_dict = {
"name": "test_hub",
2021-10-26 14:11:28 +00:00
"cidr": "1.2.3.4/32",
}
c = KiwiConfig(network=kiwi_dict)
2021-10-26 14:11:28 +00:00
assert c == KiwiConfig(network={
"name": "TEST_HUB",
"cidr": "1.2.3.4/32",
})
2021-10-26 14:11:28 +00:00
assert c.network.name == "test_hub"
assert c.network.cidr == IPv4Network("1.2.3.4/32")
assert c.network.kiwi_dict == kiwi_dict
def test_invalid_dict(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(network={"name": "test_hub"})
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "field required"
assert error["type"] == "value_error.missing"
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(network={
"name": "test_hub",
"cidr": "1.2.3.4/123",
})
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "value is not a valid IPv4 network"
assert error["type"] == "value_error.ipv4network"
def test_invalid(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(network=True)
assert len(exc_info.value.errors()) == 1
error = exc_info.value.errors()[0]
assert error["msg"] == "Invalid Network Format"
assert error["type"] == "value_error"