Merge pull request #33 from compose-viz/dev-testcase

feat: new components
This commit is contained in:
Xyphuz 2022-06-03 17:29:10 +08:00 committed by GitHub
commit 1f2d7ff136
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 200 additions and 0 deletions

View file

@ -16,6 +16,12 @@ class Service:
depends_on: List[str] = [],
links: List[str] = [],
extends: Optional[Extends] = None,
cgroup_parent: Optional[str] = None,
container_name: Optional[str] = None,
devices: List[str] = [],
env_file: List[str] = [],
expose: List[str] = [],
profiles: List[str] = [],
) -> None:
self._name = name
self._image = image
@ -25,6 +31,12 @@ class Service:
self._depends_on = depends_on
self._links = links
self._extends = extends
self.cgroup_parent = cgroup_parent
self.container_name = container_name
self.devices = devices
self.env_file = env_file
self.expose = expose
self.profiles = profiles
@property
def name(self):

View file

@ -176,6 +176,32 @@ class Parser:
if service_data.links is not None:
service_links = service_data.links
cgroup_parent: Optional[str] = None
if service_data.cgroup_parent is not None:
cgroup_parent = service_data.cgroup_parent
container_name: Optional[str] = None
if service_data.container_name is not None:
container_name = service_data.container_name
env_file: List[str] = []
if service_data.env_file is not None:
if type(service_data.env_file) is spec.StringOrList:
if type(service_data.env_file.__root__) is spec.ListOfStrings:
env_file = service_data.env_file.__root__.__root__
elif type(service_data.env_file.__root__) is str:
env_file.append(service_data.env_file.__root__)
expose: List[str] = []
if service_data.expose is not None:
for port in service_data.expose:
expose.append(str(port))
profiles: List[str] = []
if service_data.profiles is not None:
if type(service_data.profiles) is spec.ListOfStrings:
profiles = service_data.profiles.__root__
services.append(
Service(
name=service_name,
@ -186,6 +212,11 @@ class Parser:
depends_on=service_depends_on,
volumes=service_volumes,
links=service_links,
cgroup_parent=cgroup_parent,
container_name=container_name,
env_file=env_file,
expose=expose,
profiles=profiles,
)
)

View file

@ -241,6 +241,91 @@ from compose_viz.parser import Parser
],
),
),
(
"cgroup_parent/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
cgroup_parent="system",
),
],
),
),
(
"container_name/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
container_name="myfrontend",
),
],
),
),
(
"env_file/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
env_file=["a.env"],
),
Service(
name="backend",
image="awesome/backend",
env_file=["b.env"],
),
Service(
name="db",
image="awesome/db",
env_file=["c.env", "d.env"],
),
],
),
),
(
"expose/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
expose=["27118"],
),
Service(
name="backend",
image="awesome/backend",
expose=["27017", "27018"],
),
],
),
),
(
"profiles/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
profiles=["frontend"],
),
Service(
name="phpmyadmin",
image="phpmyadmin",
profiles=["debug"],
),
Service(
name="db",
image="awesome/db",
profiles=["db", "sql"],
),
],
),
),
],
)
def test_parse_file(test_file_path: str, expected: Compose) -> None:
@ -275,3 +360,10 @@ def test_parse_file(test_file_path: str, expected: Compose) -> None:
if (actual_service.extends is not None) and (expected_service.extends is not None):
assert actual_service.extends.service_name == expected_service.extends.service_name
assert actual_service.extends.from_file == expected_service.extends.from_file
assert actual_service.cgroup_parent == expected_service.cgroup_parent
assert actual_service.container_name == expected_service.container_name
assert actual_service.expose == expected_service.expose
assert actual_service.env_file == expected_service.env_file
assert actual_service.profiles == expected_service.profiles

View file

@ -0,0 +1,6 @@
version: "3.9"
services:
frontend:
image: awesome/frontend
cgroup_parent: "system"

View file

@ -0,0 +1,6 @@
version: "3.9"
services:
frontend:
image: awesome/frontend
container_name: "myfrontend"

View file

@ -0,0 +1,12 @@
version: "3.9"
services:
frontend:
image: awesome/frontend
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
backend:
image: awesome/backend
devices:
- "/dev/ttyUSB1:/dev/ttyUSB1"
- "/dev/ttyUSB2:/dev/ttyUSB2"

View file

@ -0,0 +1,15 @@
version: "3.9"
services:
frontend:
image: awesome/frontend
env_file: a.env
backend:
image: awesome/backend
env_file:
- b.env
db:
image: awesome/db
env_file:
- c.env
- d.env

View file

@ -0,0 +1,12 @@
version: "3.9"
services:
frontend:
image: awesome/frontend
expose:
- "27118"
backend:
image: awesome/backend
expose:
- "27017"
- "27018"

View file

@ -0,0 +1,14 @@
version: "3.9"
services:
frontend:
image: awesome/frontend
profiles: ["frontend"]
phpmyadmin:
image: phpmyadmin
profiles:
- debug
db:
image: awesome/db
profiles:
- db
- sql