1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-21 20:33:00 +00:00

pytests projects

This commit is contained in:
Jörn-Michael Miehe 2021-10-14 19:18:24 +02:00
parent 92802aa5ea
commit 8d688b28b1
2 changed files with 102 additions and 5 deletions

View file

@ -95,7 +95,7 @@ class Config(BaseModel):
Path("/bin/bash"), Path("/bin/bash"),
] ]
projects: Optional[List[_Project]] projects: List[_Project] = []
environment: Dict[str, Optional[str]] = {} environment: Dict[str, Optional[str]] = {}
@ -152,6 +152,40 @@ class Config(BaseModel):
return yml_string return yml_string
@validator("projects", pre=True)
@classmethod
def unify_projects(cls, value):
"""parse different projects notations"""
if value is None:
# empty projects list
return []
elif isinstance(value, list):
# handle projects list
result = []
for entry in value:
# ignore empties
if entry is not None:
if isinstance(entry, dict):
# handle single project dict
result.append(entry)
else:
# handle single project name
result.append({"name": str(entry)})
return result
elif isinstance(value, dict):
# handle single project dict
return [value]
else:
# handle single project name
return [{"name": str(value)}]
@validator("environment", pre=True) @validator("environment", pre=True)
@classmethod @classmethod
def unify_environment(cls, value) -> Dict[str, Optional[str]]: def unify_environment(cls, value) -> Dict[str, Optional[str]]:

View file

@ -15,13 +15,78 @@ def test_default():
assert c.version == version assert c.version == version
assert len(c.shells) == 1 assert len(c.shells) == 1
assert c.shells[0] == Path("/bin/bash") assert c.shells[0] == Path("/bin/bash")
assert c.projects is None assert c.projects == []
assert c.environment == {} assert c.environment == {}
assert c.storage.directory == Path("/var/local/kiwi") assert c.storage.directory == Path("/var/local/kiwi")
assert c.network.name == "kiwi_hub" assert c.network.name == "kiwi_hub"
assert c.network.cidr == IPv4Network("10.22.46.0/24") assert c.network.cidr == IPv4Network("10.22.46.0/24")
##########
# PROJECTS
##########
def test_proj_empty():
c = Config(projects=None)
assert c.projects == []
c = Config(projects=[])
assert c.projects == []
def test_proj_long():
c = Config(projects=[{
"name": "project",
"enabled": False,
"override_storage": {"directory": "/test/directory"},
}])
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
assert not p.enabled
assert p.override_storage is not None
assert p.override_storage.directory == Path("/test/directory")
def test_proj_short():
c = Config(projects=[{
"project": False,
}])
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
assert not p.enabled
assert p.override_storage is None
def test_proj_dict():
c = Config(projects={"name": "project"})
assert c == Config(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_proj_name():
c = Config(projects="project")
assert c == Config(projects=["project"])
assert len(c.projects) == 1
p = c.projects[0]
assert p.name == "project"
assert p.enabled
assert p.override_storage is None
############# #############
# ENVIRONMENT # ENVIRONMENT
############# #############
@ -37,9 +102,7 @@ def test_env_dict():
assert c.environment == {} assert c.environment == {}
c = Config(environment={ c = Config(environment={"variable": "value"})
"variable": "value"
})
assert len(c.environment) == 1 assert len(c.environment) == 1
assert "variable" in c.environment assert "variable" in c.environment