From 5892237eaefc7dbbb34e6bc6b34cd038259421c2 Mon Sep 17 00:00:00 2001 From: uccuz Date: Fri, 3 Jun 2022 16:52:12 +0800 Subject: [PATCH 1/7] chore: add cgroup_parent and container_name yaml --- tests/ymls/cgroup_parent/docker-compose.yml | 6 ++++++ tests/ymls/container_name/docker-compose.yml | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 tests/ymls/cgroup_parent/docker-compose.yml create mode 100644 tests/ymls/container_name/docker-compose.yml diff --git a/tests/ymls/cgroup_parent/docker-compose.yml b/tests/ymls/cgroup_parent/docker-compose.yml new file mode 100644 index 0000000..bc22662 --- /dev/null +++ b/tests/ymls/cgroup_parent/docker-compose.yml @@ -0,0 +1,6 @@ +version: "3.9" + +services: + frontend: + image: awesome/frontend + cgroup_parent: "system" \ No newline at end of file diff --git a/tests/ymls/container_name/docker-compose.yml b/tests/ymls/container_name/docker-compose.yml new file mode 100644 index 0000000..f3e6794 --- /dev/null +++ b/tests/ymls/container_name/docker-compose.yml @@ -0,0 +1,6 @@ +version: "3.9" + +services: + frontend: + image: awesome/frontend + container_name: "myfrontend" \ No newline at end of file From 603f552551243849e9f39919887183ebe4ae89c1 Mon Sep 17 00:00:00 2001 From: uccuz Date: Fri, 3 Jun 2022 16:55:17 +0800 Subject: [PATCH 2/7] chore: add cgroup and container_name test case --- tests/test_parse_file.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_parse_file.py b/tests/test_parse_file.py index 7bf3cdc..1eaa166 100644 --- a/tests/test_parse_file.py +++ b/tests/test_parse_file.py @@ -241,6 +241,31 @@ 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", + ), + ], + ), + ), + ], ) def test_parse_file(test_file_path: str, expected: Compose) -> None: @@ -275,3 +300,8 @@ 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 + From 1076459e7cc89c0c0413151462c761c29ff8e84b Mon Sep 17 00:00:00 2001 From: uccuz Date: Fri, 3 Jun 2022 16:56:36 +0800 Subject: [PATCH 3/7] chore: add devices, env_file, expose and profiles --- tests/ymls/devices/docker-compose.yml | 12 ++++++++++++ tests/ymls/env_file/docker-compose.yml | 15 +++++++++++++++ tests/ymls/expose/docker-compose.yml | 12 ++++++++++++ tests/ymls/profiles/docker-compose.yml | 14 ++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 tests/ymls/devices/docker-compose.yml create mode 100644 tests/ymls/env_file/docker-compose.yml create mode 100644 tests/ymls/expose/docker-compose.yml create mode 100644 tests/ymls/profiles/docker-compose.yml diff --git a/tests/ymls/devices/docker-compose.yml b/tests/ymls/devices/docker-compose.yml new file mode 100644 index 0000000..474d385 --- /dev/null +++ b/tests/ymls/devices/docker-compose.yml @@ -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" \ No newline at end of file diff --git a/tests/ymls/env_file/docker-compose.yml b/tests/ymls/env_file/docker-compose.yml new file mode 100644 index 0000000..d687153 --- /dev/null +++ b/tests/ymls/env_file/docker-compose.yml @@ -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 diff --git a/tests/ymls/expose/docker-compose.yml b/tests/ymls/expose/docker-compose.yml new file mode 100644 index 0000000..473be64 --- /dev/null +++ b/tests/ymls/expose/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.9" + +services: + frontend: + image: awesome/frontend + expose: + - "27118" + backend: + image: awesome/backend + expose: + - "27017" + - "27018" \ No newline at end of file diff --git a/tests/ymls/profiles/docker-compose.yml b/tests/ymls/profiles/docker-compose.yml new file mode 100644 index 0000000..64d755f --- /dev/null +++ b/tests/ymls/profiles/docker-compose.yml @@ -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 \ No newline at end of file From f6615ca1b9e2921dd15fd03527c8279f6babea40 Mon Sep 17 00:00:00 2001 From: uccuz Date: Fri, 3 Jun 2022 16:57:06 +0800 Subject: [PATCH 4/7] feat: add service new member --- compose_viz/models/service.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/compose_viz/models/service.py b/compose_viz/models/service.py index 89740ba..dba1fd3 100644 --- a/compose_viz/models/service.py +++ b/compose_viz/models/service.py @@ -16,6 +16,12 @@ class Service: depends_on: List[str] = [], links: List[str] = [], extends: Optional[Extends] = None, + cgroup_parent: str = None, + container_name: 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): From 50589db102707bf5c2d83cfc6a5c7c36a13bc82f Mon Sep 17 00:00:00 2001 From: uccuz Date: Fri, 3 Jun 2022 16:57:40 +0800 Subject: [PATCH 5/7] chore: create env, expose, profiles test case --- tests/test_parse_file.py | 84 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/tests/test_parse_file.py b/tests/test_parse_file.py index 1eaa166..54a8c86 100644 --- a/tests/test_parse_file.py +++ b/tests/test_parse_file.py @@ -265,7 +265,86 @@ from compose_viz.parser import Parser ], ), ), - + ( + "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: @@ -305,3 +384,6 @@ def test_parse_file(test_file_path: str, expected: Compose) -> None: 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 \ No newline at end of file From 747dd06be1454d2bc757e730a4e470a44b10d28d Mon Sep 17 00:00:00 2001 From: uccuz Date: Fri, 3 Jun 2022 16:57:58 +0800 Subject: [PATCH 6/7] feat: implement new test case --- compose_viz/parser.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index 63139d4..4d631fc 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -176,6 +176,35 @@ 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: + expose = service_data.expose + + 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 +215,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, ) ) From eca68a8190526043d7e3f32fe44c15702875dd01 Mon Sep 17 00:00:00 2001 From: Xyphuz Date: Fri, 3 Jun 2022 17:19:43 +0800 Subject: [PATCH 7/7] chore: apply pre-commit hooks --- compose_viz/models/service.py | 4 +- compose_viz/parser.py | 17 ++++---- tests/test_parse_file.py | 44 ++++++-------------- tests/ymls/cgroup_parent/docker-compose.yml | 2 +- tests/ymls/container_name/docker-compose.yml | 2 +- tests/ymls/devices/docker-compose.yml | 2 +- tests/ymls/expose/docker-compose.yml | 2 +- tests/ymls/profiles/docker-compose.yml | 2 +- 8 files changed, 26 insertions(+), 49 deletions(-) diff --git a/compose_viz/models/service.py b/compose_viz/models/service.py index dba1fd3..abd94f6 100644 --- a/compose_viz/models/service.py +++ b/compose_viz/models/service.py @@ -16,8 +16,8 @@ class Service: depends_on: List[str] = [], links: List[str] = [], extends: Optional[Extends] = None, - cgroup_parent: str = None, - container_name: str = None, + cgroup_parent: Optional[str] = None, + container_name: Optional[str] = None, devices: List[str] = [], env_file: List[str] = [], expose: List[str] = [], diff --git a/compose_viz/parser.py b/compose_viz/parser.py index 4d631fc..6f296d4 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -176,35 +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): + 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): + 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: - expose = service_data.expose - + 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, diff --git a/tests/test_parse_file.py b/tests/test_parse_file.py index 54a8c86..226d755 100644 --- a/tests/test_parse_file.py +++ b/tests/test_parse_file.py @@ -248,7 +248,7 @@ from compose_viz.parser import Parser Service( name="frontend", image="awesome/frontend", - cgroup_parent="system", + cgroup_parent="system", ), ], ), @@ -260,7 +260,7 @@ from compose_viz.parser import Parser Service( name="frontend", image="awesome/frontend", - container_name="myfrontend", + container_name="myfrontend", ), ], ), @@ -272,24 +272,17 @@ from compose_viz.parser import Parser Service( name="frontend", image="awesome/frontend", - env_file=[ - "a.env" - ], + env_file=["a.env"], ), Service( name="backend", image="awesome/backend", - env_file=[ - "b.env" - ], + env_file=["b.env"], ), Service( name="db", image="awesome/db", - env_file=[ - "c.env", - "d.env" - ], + env_file=["c.env", "d.env"], ), ], ), @@ -301,46 +294,34 @@ from compose_viz.parser import Parser Service( name="frontend", image="awesome/frontend", - expose=[ - "27118" - ], + expose=["27118"], ), Service( name="backend", image="awesome/backend", - expose=[ - "27017", - "27018" - ], + expose=["27017", "27018"], ), ], ), ), - ( + ( "profiles/docker-compose", Compose( services=[ Service( name="frontend", image="awesome/frontend", - profiles=[ - "frontend" - ], + profiles=["frontend"], ), Service( name="phpmyadmin", image="phpmyadmin", - profiles=[ - "debug" - ], + profiles=["debug"], ), Service( name="db", image="awesome/db", - profiles=[ - "db", - "sql" - ], + profiles=["db", "sql"], ), ], ), @@ -380,10 +361,9 @@ def test_parse_file(test_file_path: str, expected: Compose) -> 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 \ No newline at end of file + assert actual_service.profiles == expected_service.profiles diff --git a/tests/ymls/cgroup_parent/docker-compose.yml b/tests/ymls/cgroup_parent/docker-compose.yml index bc22662..6d41ad4 100644 --- a/tests/ymls/cgroup_parent/docker-compose.yml +++ b/tests/ymls/cgroup_parent/docker-compose.yml @@ -3,4 +3,4 @@ version: "3.9" services: frontend: image: awesome/frontend - cgroup_parent: "system" \ No newline at end of file + cgroup_parent: "system" diff --git a/tests/ymls/container_name/docker-compose.yml b/tests/ymls/container_name/docker-compose.yml index f3e6794..08047c2 100644 --- a/tests/ymls/container_name/docker-compose.yml +++ b/tests/ymls/container_name/docker-compose.yml @@ -3,4 +3,4 @@ version: "3.9" services: frontend: image: awesome/frontend - container_name: "myfrontend" \ No newline at end of file + container_name: "myfrontend" diff --git a/tests/ymls/devices/docker-compose.yml b/tests/ymls/devices/docker-compose.yml index 474d385..f3fa6f8 100644 --- a/tests/ymls/devices/docker-compose.yml +++ b/tests/ymls/devices/docker-compose.yml @@ -9,4 +9,4 @@ services: image: awesome/backend devices: - "/dev/ttyUSB1:/dev/ttyUSB1" - - "/dev/ttyUSB2:/dev/ttyUSB2" \ No newline at end of file + - "/dev/ttyUSB2:/dev/ttyUSB2" diff --git a/tests/ymls/expose/docker-compose.yml b/tests/ymls/expose/docker-compose.yml index 473be64..1e1bdee 100644 --- a/tests/ymls/expose/docker-compose.yml +++ b/tests/ymls/expose/docker-compose.yml @@ -9,4 +9,4 @@ services: image: awesome/backend expose: - "27017" - - "27018" \ No newline at end of file + - "27018" diff --git a/tests/ymls/profiles/docker-compose.yml b/tests/ymls/profiles/docker-compose.yml index 64d755f..39282af 100644 --- a/tests/ymls/profiles/docker-compose.yml +++ b/tests/ymls/profiles/docker-compose.yml @@ -11,4 +11,4 @@ services: image: awesome/db profiles: - db - - sql \ No newline at end of file + - sql