Test classes

This commit is contained in:
Jörn-Michael Miehe 2021-10-26 16:11:28 +02:00
parent 0ecee93241
commit 8d2c1e1fac
3 changed files with 431 additions and 482 deletions

View file

@ -16,7 +16,8 @@ class UnCoercible:
raise ValueError
def test_default():
class TestDefault:
def test(self):
import toml
c = KiwiConfig()
@ -56,33 +57,24 @@ def test_default():
assert c.kiwi_yml == yml_string
#########
# VERSION
#########
def test_version_valid():
class TestVersion:
def test_valid(self):
c = KiwiConfig(version="0.0.0")
assert c.version == "0.0.0"
c = KiwiConfig(version="0.0")
assert c.version == "0.0"
c = KiwiConfig(version="0")
assert c.version == "0"
c = KiwiConfig(version=1.0)
assert c.version == "1.0"
c = KiwiConfig(version=1)
assert c.version == "1"
def test_version_invalid():
def test_invalid(self):
# definitely not a version
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(version="dnaf")
@ -92,7 +84,7 @@ def test_version_invalid():
assert error["msg"].find("string does not match regex") != -1
assert error["type"] == "value_error.str.regex"
# barely a version
# almost a version
with pytest.raises(ValidationError) as exc_info:
c = KiwiConfig(version="0.0.0alpha")
print(c.version)
@ -103,19 +95,15 @@ def test_version_invalid():
assert error["type"] == "value_error.str.regex"
########
# SHELLS
########
def test_shells_empty():
class TestShells:
def test_empty(self):
c = KiwiConfig(shells=None)
assert c == KiwiConfig(shells=[])
assert c.shells == []
def test_shells_list():
def test_list(self):
c = KiwiConfig(shells=["/bin/sh", "sh"])
assert len(c.shells) == 2
@ -127,15 +115,13 @@ def test_shells_list():
assert len(c.shells) == 1
assert c.shells[0] == Path("/bin/bash")
def test_shells_dict():
def test_dict(self):
c = KiwiConfig(shells={"/bin/bash": None})
assert len(c.shells) == 1
assert c.shells[0] == Path("/bin/bash")
def test_shells_coercible():
def test_coercible(self):
c = KiwiConfig(shells="/bin/bash")
assert c == KiwiConfig(shells=Path("/bin/bash"))
@ -148,8 +134,7 @@ def test_shells_coercible():
assert len(c.shells) == 1
assert c.shells[0] == Path("123")
def test_shells_uncoercible():
def test_uncoercible(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(shells=UnCoercible())
@ -167,19 +152,15 @@ def test_shells_uncoercible():
assert error["type"] == "type_error.path"
##########
# PROJECTS
##########
def test_proj_empty():
class TestProject:
def test_empty(self):
c = KiwiConfig(projects=None)
assert c == KiwiConfig(projects=[])
assert c.projects == []
def test_proj_long():
def test_long(self):
kiwi_dict = {
"name": "project",
"enabled": False,
@ -195,8 +176,7 @@ def test_proj_long():
assert c.kiwi_dict["projects"][0] == kiwi_dict
def test_proj_storage_str():
def test_storage_str(self):
kiwi_dict = {
"name": "project",
"enabled": False,
@ -210,8 +190,7 @@ def test_proj_storage_str():
assert not p.enabled
assert p.override_storage is not None
def test_proj_storage_list():
def test_storage_list(self):
kiwi_dict = {
"name": "project",
"enabled": False,
@ -225,8 +204,7 @@ def test_proj_storage_list():
assert not p.enabled
assert p.override_storage is not None
def test_proj_storage_invalid():
def test_storage_invalid(self):
kiwi_dict = {
"name": "project",
"enabled": False,
@ -240,8 +218,7 @@ def test_proj_storage_invalid():
assert error["msg"] == "Invalid Storage Format"
assert error["type"] == "value_error"
def test_proj_short():
def test_short(self):
kiwi_dict = {
"project": False,
}
@ -254,8 +231,7 @@ def test_proj_short():
assert p.override_storage is None
assert p.kiwi_dict == kiwi_dict
def test_proj_dict():
def test_dict(self):
c = KiwiConfig(projects={"name": "project"})
assert c == KiwiConfig(projects=[{"name": "project"}])
@ -266,8 +242,7 @@ def test_proj_dict():
assert p.enabled
assert p.override_storage is None
def test_proj_invalid_dict():
def test_invalid_dict(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(projects={
"random key 1": "random value 1",
@ -279,8 +254,7 @@ def test_proj_invalid_dict():
assert error["msg"] == "Invalid Project Format"
assert error["type"] == "value_error"
def test_proj_coercible():
def test_coercible(self):
c = KiwiConfig(projects="project")
assert c == KiwiConfig(projects=["project"])
@ -291,8 +265,7 @@ def test_proj_coercible():
assert p.enabled
assert p.override_storage is None
def test_proj_uncoercible():
def test_uncoercible(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(projects=["valid", UnCoercible()])
@ -310,17 +283,13 @@ def test_proj_uncoercible():
assert error["type"] == "value_error"
#############
# ENVIRONMENT
#############
def test_env_empty():
class TestEnvironment:
def test_empty(self):
c = KiwiConfig(environment=None)
assert c.environment == {}
def test_env_dict():
def test_dict(self):
c = KiwiConfig(environment={})
assert c.environment == {}
@ -334,8 +303,7 @@ def test_env_dict():
assert c.kiwi_dict["environment"] == kiwi_dict
def test_env_list():
def test_list(self):
c = KiwiConfig(environment=[])
assert c.environment == {}
@ -364,8 +332,7 @@ def test_env_list():
assert "123" in c.environment
assert c.environment["123"] is None
def test_env_coercible():
def test_coercible(self):
c = KiwiConfig(environment="variable=value")
assert len(c.environment) == 1
@ -390,8 +357,7 @@ def test_env_coercible():
assert "123.4" in c.environment
assert c.environment["123.4"] is None
def test_env_uncoercible():
def test_uncoercible(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(environment=UnCoercible())
@ -409,11 +375,8 @@ def test_env_uncoercible():
assert error["type"] == "value_error"
#########
# STORAGE
#########
def test_storage_empty():
class TestStorage:
def test_empty(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(storage=None)
@ -422,16 +385,14 @@ def test_storage_empty():
assert error["msg"] == "No Storage Given"
assert error["type"] == "value_error"
def test_storage_dict():
def test_dict(self):
kiwi_dict = {"directory": "/test/directory"}
c = KiwiConfig(storage=kiwi_dict)
assert c.storage.directory == Path("/test/directory")
assert c.storage.kiwi_dict == kiwi_dict
def test_storage_invalid_dict():
def test_invalid_dict(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(storage={"random key": "random value"})
@ -440,20 +401,17 @@ def test_storage_invalid_dict():
assert error["msg"] == "Invalid Storage Format"
assert error["type"] == "value_error"
def test_storage_str():
def test_str(self):
c = KiwiConfig(storage="/test/directory")
assert c.storage.directory == Path("/test/directory")
def test_storage_list():
def test_list(self):
c = KiwiConfig(storage=["/test/directory"])
assert c.storage.directory == Path("/test/directory")
def test_storage_invalid():
def test_invalid(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(storage=True)
@ -463,11 +421,8 @@ def test_storage_invalid():
assert error["type"] == "value_error"
#########
# NETWORK
#########
def test_network_empty():
class TestNetwork:
def test_empty(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(network=None)
@ -476,8 +431,7 @@ def test_network_empty():
assert error["msg"] == "No Network Given"
assert error["type"] == "value_error"
def test_network_dict():
def test_dict(self):
kiwi_dict = {
"name": "test_hub",
"cidr": "1.2.3.4/32",
@ -493,8 +447,7 @@ def test_network_dict():
assert c.network.cidr == IPv4Network("1.2.3.4/32")
assert c.network.kiwi_dict == kiwi_dict
def test_network_invalid_dict():
def test_invalid_dict(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(network={"name": "test_hub"})
@ -514,8 +467,7 @@ def test_network_invalid_dict():
assert error["msg"] == "value is not a valid IPv4 network"
assert error["type"] == "value_error.ipv4network"
def test_network_invalid():
def test_invalid(self):
with pytest.raises(ValidationError) as exc_info:
KiwiConfig(network=True)

View file

@ -3,7 +3,8 @@ from pathlib import Path
from kiwi_scp.instance import Instance
def test_example():
class TestDefault:
def test_example(self):
i = Instance(Path("example"))
assert i.config is not None
@ -13,15 +14,13 @@ def test_example():
assert p.name == "hello-world.project"
def test_empty():
def test_empty(self):
i = Instance()
assert i.config is not None
assert len(i.config.projects) == 0
def test_no_such_dir():
def test_no_such_dir(self):
nonexistent_path = Path("nonexistent")
i = Instance(nonexistent_path)

View file

@ -3,7 +3,8 @@ from pathlib import Path
from kiwi_scp.instance import Service
def test_no_description():
class TestDefault:
def test_no_description(self):
s = Service.from_description(
name="s",
description={},
@ -12,8 +13,7 @@ def test_no_description():
assert s.name == "s"
assert s.configs == []
def test_no_configs():
def test_no_configs(self):
s = Service.from_description(
name="s",
description={
@ -24,8 +24,7 @@ def test_no_configs():
assert s.name == "s"
assert s.configs == []
def test_no_configs_in_volumes():
def test_no_configs_in_volumes(self):
s = Service.from_description(
name="s",
description={
@ -41,8 +40,7 @@ def test_no_configs_in_volumes():
assert s.name == "s"
assert s.configs == []
def test_with_configs():
def test_with_configs(self):
s = Service.from_description(
name="s",
description={