"Services" class
This commit is contained in:
parent
a5a0ca9a63
commit
ed29243b71
3 changed files with 38 additions and 33 deletions
|
@ -1,7 +1,7 @@
|
||||||
import functools
|
import functools
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Generator, List
|
from typing import Generator, List, Tuple, Optional
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import click
|
import click
|
||||||
|
@ -24,7 +24,7 @@ class Service:
|
||||||
"service": {
|
"service": {
|
||||||
self.name: self.description
|
self.name: self.description
|
||||||
}
|
}
|
||||||
})
|
}).strip()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def configs(self) -> Generator[Path, None, None]:
|
def configs(self) -> Generator[Path, None, None]:
|
||||||
|
@ -36,7 +36,7 @@ class Service:
|
||||||
cd_match = _RE_CONFDIR.match(host_part)
|
cd_match = _RE_CONFDIR.match(host_part)
|
||||||
|
|
||||||
if cd_match:
|
if cd_match:
|
||||||
yield cd_match.group(1)
|
yield Path(cd_match.group(1))
|
||||||
|
|
||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
|
@ -50,7 +50,8 @@ class Services:
|
||||||
service.name: service.description
|
service.name: service.description
|
||||||
for service in self.content
|
for service in self.content
|
||||||
}
|
}
|
||||||
})
|
}).strip()
|
||||||
|
|
||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
class Instance:
|
class Instance:
|
||||||
|
@ -68,18 +69,21 @@ class Instance:
|
||||||
with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf:
|
with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf:
|
||||||
return YAML().load(cf)
|
return YAML().load(cf)
|
||||||
|
|
||||||
def get_services(self, project_name: str) -> Services:
|
def get_services(self, project_name: str, service_names: Optional[Tuple[str]] = None) -> Services:
|
||||||
yml = Instance._parse_compose_file(self.directory.joinpath(project_name))
|
yml = Instance._parse_compose_file(self.directory.joinpath(project_name))
|
||||||
|
services = [
|
||||||
return Services(project_name, [
|
|
||||||
Service(name, description)
|
Service(name, description)
|
||||||
for name, description in yml["services"].items()
|
for name, description in yml["services"].items()
|
||||||
])
|
]
|
||||||
|
|
||||||
def get_service(self, project_name: str, service_name: str) -> Service:
|
if service_names is None:
|
||||||
yml = Instance._parse_compose_file(self.directory.joinpath(project_name))
|
return Services(project_name, services)
|
||||||
|
else:
|
||||||
return Service(service_name, yml["services"][service_name])
|
return Services(project_name, [
|
||||||
|
service
|
||||||
|
for service in services
|
||||||
|
if service.name in service_names
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
pass_instance = click.make_pass_decorator(Instance, ensure=True)
|
pass_instance = click.make_pass_decorator(Instance, ensure=True)
|
||||||
|
|
|
@ -14,14 +14,13 @@ class TestDefault:
|
||||||
|
|
||||||
assert p.name == "hello-world.project"
|
assert p.name == "hello-world.project"
|
||||||
|
|
||||||
ss = list(i.get_services(p.name))
|
ss = i.get_services(p.name)
|
||||||
|
|
||||||
assert len(ss) == 5
|
assert len(ss.content) == 5
|
||||||
|
|
||||||
s = ss[0]
|
s = ss.content[0]
|
||||||
|
|
||||||
assert s.name == "greeter"
|
assert s.name == "greeter"
|
||||||
assert s == i.get_service(p.name, s.name)
|
|
||||||
|
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
i = Instance()
|
i = Instance()
|
||||||
|
|
|
@ -1,60 +1,62 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from ruamel.yaml import CommentedMap
|
||||||
|
|
||||||
from kiwi_scp.instance import Service
|
from kiwi_scp.instance import Service
|
||||||
|
|
||||||
|
|
||||||
class TestDefault:
|
class TestDefault:
|
||||||
def test_no_description(self):
|
def test_empty(self):
|
||||||
s = Service.from_description(
|
s = Service(
|
||||||
name="s",
|
name="s",
|
||||||
description={},
|
description=CommentedMap(),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert s.name == "s"
|
assert s.name == "s"
|
||||||
assert s.configs == []
|
assert list(s.configs) == []
|
||||||
|
|
||||||
def test_no_configs(self):
|
def test_no_configs(self):
|
||||||
s = Service.from_description(
|
s = Service(
|
||||||
name="s",
|
name="s",
|
||||||
description={
|
description=CommentedMap({
|
||||||
"image": "repo/image:tag",
|
"image": "repo/image:tag",
|
||||||
},
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert s.name == "s"
|
assert s.name == "s"
|
||||||
assert s.configs == []
|
assert list(s.configs) == []
|
||||||
|
|
||||||
def test_no_configs_in_volumes(self):
|
def test_no_configs_in_volumes(self):
|
||||||
s = Service.from_description(
|
s = Service(
|
||||||
name="s",
|
name="s",
|
||||||
description={
|
description=CommentedMap({
|
||||||
"image": "repo/image:tag",
|
"image": "repo/image:tag",
|
||||||
"volumes": [
|
"volumes": [
|
||||||
"docker_volume/third/dir:/path/to/third/mountpoint",
|
"docker_volume/third/dir:/path/to/third/mountpoint",
|
||||||
"${TARGETDIR}/some/dir:/path/to/some/mountpoint",
|
"${TARGETDIR}/some/dir:/path/to/some/mountpoint",
|
||||||
"$TARGETDIR/other/dir:/path/to/other/mountpoint",
|
"$TARGETDIR/other/dir:/path/to/other/mountpoint",
|
||||||
]
|
]
|
||||||
},
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert s.name == "s"
|
assert s.name == "s"
|
||||||
assert s.configs == []
|
assert list(s.configs) == []
|
||||||
|
|
||||||
def test_with_configs(self):
|
def test_with_configs(self):
|
||||||
s = Service.from_description(
|
s = Service(
|
||||||
name="s",
|
name="s",
|
||||||
description={
|
description=CommentedMap({
|
||||||
"image": "repo/image:tag",
|
"image": "repo/image:tag",
|
||||||
"volumes": [
|
"volumes": [
|
||||||
"${CONFDIR}/some/config:/path/to/some/config",
|
"${CONFDIR}/some/config:/path/to/some/config",
|
||||||
"$CONFDIR/other/config:/path/to/other/config",
|
"$CONFDIR/other/config:/path/to/other/config",
|
||||||
]
|
]
|
||||||
},
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert s.name == "s"
|
assert s.name == "s"
|
||||||
assert len(s.configs) == 2
|
assert len(list(s.configs)) == 2
|
||||||
assert s.configs == [
|
assert list(s.configs) == [
|
||||||
Path("some/config"),
|
Path("some/config"),
|
||||||
Path("other/config"),
|
Path("other/config"),
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue