compose-viz/tests/test_parse_file.py

267 lines
9.5 KiB
Python
Raw Normal View History

2022-05-18 06:19:21 +02:00
import pytest
2022-05-18 17:28:18 +02:00
2022-05-07 18:42:14 +02:00
from compose_viz.compose import Compose
2022-05-18 10:03:33 +02:00
from compose_viz.extends import Extends
2022-05-18 17:28:18 +02:00
from compose_viz.parser import Parser
from compose_viz.port import Port, Protocol
2022-05-18 17:28:18 +02:00
from compose_viz.service import Service
from compose_viz.volume import Volume, VolumeType
2022-05-07 18:42:14 +02:00
2022-05-18 17:28:18 +02:00
@pytest.mark.parametrize(
2022-05-24 10:02:47 +02:00
"test_file_path, expected",
2022-05-18 17:28:18 +02:00
[
(
2022-05-24 10:02:47 +02:00
"builds/docker-compose",
2022-05-18 17:28:18 +02:00
Compose(
2022-05-24 10:02:47 +02:00
services=[
2022-05-18 17:28:18 +02:00
Service(
name="frontend",
2022-05-25 11:46:53 +02:00
image="build from './frontend', image: awesome/frontend",
2022-05-18 17:28:18 +02:00
),
Service(
name="backend",
2022-05-25 11:46:53 +02:00
image="build from 'backend' using '../backend.Dockerfile'",
),
Service(
name="db",
image="build from './db'",
2022-05-18 17:28:18 +02:00
),
2022-05-24 10:02:47 +02:00
],
2022-05-18 17:28:18 +02:00
),
),
(
2022-05-24 10:02:47 +02:00
"depends_on/docker-compose",
2022-05-18 17:28:18 +02:00
Compose(
2022-05-24 10:02:47 +02:00
services=[
2022-05-18 17:28:18 +02:00
Service(
name="frontend",
2022-05-24 10:02:47 +02:00
image="awesome/frontend",
depends_on=[
"db",
"redis",
2022-05-21 17:19:01 +02:00
],
2022-05-18 17:28:18 +02:00
),
Service(
name="backend",
image="awesome/backend",
2022-05-24 10:02:47 +02:00
depends_on=[
"db",
"redis",
2022-05-21 17:19:01 +02:00
],
2022-05-18 17:28:18 +02:00
),
Service(
name="db",
2022-05-24 10:02:47 +02:00
image="mysql",
2022-05-18 17:28:18 +02:00
),
Service(
name="redis",
image="redis",
),
2022-05-24 10:02:47 +02:00
],
2022-05-18 17:28:18 +02:00
),
),
(
2022-05-24 10:02:47 +02:00
"extends/docker-compose",
2022-05-18 17:28:18 +02:00
Compose(
2022-05-24 10:02:47 +02:00
services=[
2022-05-18 17:28:18 +02:00
Service(
2022-05-24 10:02:47 +02:00
name="base",
image="alpine:latest",
2022-05-18 17:28:18 +02:00
),
Service(
2022-05-24 10:02:47 +02:00
name="derive_from_base",
image="alpine:edge",
extends=Extends(
service_name="base",
),
2022-05-18 17:28:18 +02:00
),
Service(
2022-05-24 10:02:47 +02:00
name="derive_from_file",
extends=Extends(
service_name="web",
from_file="web.yml",
),
2022-05-18 17:28:18 +02:00
),
2022-05-24 10:02:47 +02:00
],
2022-05-18 17:28:18 +02:00
),
),
(
2022-05-24 10:02:47 +02:00
"links/docker-compose",
2022-05-18 17:28:18 +02:00
Compose(
2022-05-24 10:02:47 +02:00
services=[
2022-05-18 17:28:18 +02:00
Service(
name="frontend",
2022-05-24 10:02:47 +02:00
image="awesome/frontend",
links=[
"db:database",
2022-05-21 17:19:01 +02:00
],
2022-05-18 17:28:18 +02:00
),
Service(
2022-05-24 10:02:47 +02:00
name="db",
image="mysql",
2022-05-18 17:28:18 +02:00
),
2022-05-24 10:02:47 +02:00
],
2022-05-18 17:28:18 +02:00
),
),
(
2022-05-24 10:02:47 +02:00
"networks/docker-compose",
2022-05-18 17:28:18 +02:00
Compose(
2022-05-24 10:02:47 +02:00
services=[
2022-05-18 17:28:18 +02:00
Service(
name="frontend",
2022-05-24 10:02:47 +02:00
image="awesome/frontend",
networks=[
"front-tier",
"back-tier",
],
2022-05-18 17:28:18 +02:00
),
Service(
name="monitoring",
image="awesome/monitoring",
2022-05-24 10:02:47 +02:00
networks=[
"admin",
2022-05-21 17:19:01 +02:00
],
2022-05-18 17:28:18 +02:00
),
Service(
name="backend",
image="awesome/backend",
2022-05-24 10:02:47 +02:00
networks=[
"back-tier",
"admin",
2022-05-21 17:19:01 +02:00
],
2022-05-18 17:28:18 +02:00
),
2022-05-24 10:02:47 +02:00
],
2022-05-18 17:28:18 +02:00
),
),
(
2022-05-24 10:02:47 +02:00
"ports/docker-compose",
2022-05-18 17:28:18 +02:00
Compose(
2022-05-24 10:02:47 +02:00
services=[
2022-05-18 17:28:18 +02:00
Service(
name="frontend",
2022-05-24 10:02:47 +02:00
image="awesome/frontend",
2022-05-21 17:19:01 +02:00
ports=[
2022-05-24 10:02:47 +02:00
Port(
host_port="0.0.0.0:3000",
container_port="3000",
),
Port(
host_port="0.0.0.0:3000-3005",
container_port="3000-3005",
),
Port(
host_port="0.0.0.0:9090-9091",
container_port="8080-8081",
),
Port(
host_port="0.0.0.0:49100",
container_port="22",
),
Port(
host_port="127.0.0.1:8001",
container_port="8001",
),
Port(
host_port="127.0.0.1:5000-5010",
container_port="5000-5010",
),
Port(
host_port="0.0.0.0:6060",
container_port="6060",
protocol=Protocol.udp,
),
Port(
host_port="127.0.0.1:8080",
container_port="80",
protocol=Protocol.tcp,
),
Port(
host_port="0.0.0.0:443",
container_port="443",
),
2022-05-21 17:19:01 +02:00
],
2022-05-18 17:28:18 +02:00
),
2022-05-24 10:02:47 +02:00
],
2022-05-18 17:28:18 +02:00
),
),
(
2022-05-24 10:02:47 +02:00
"volumes/docker-compose",
2022-05-18 17:28:18 +02:00
Compose(
2022-05-24 10:02:47 +02:00
services=[
2022-05-18 17:28:18 +02:00
Service(
name="backend",
image="awesome/backend",
2022-05-21 11:41:26 +02:00
volumes=[
Volume(
2022-05-24 10:02:47 +02:00
source="./data",
target="/data",
2022-05-21 11:41:26 +02:00
),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
2022-05-18 17:28:18 +02:00
),
Service(
name="common",
image="busybox",
volumes=[
2022-05-21 11:41:26 +02:00
Volume(
2022-05-24 10:02:47 +02:00
source="common-volume",
target="/var/lib/backup/data",
2022-05-21 11:41:26 +02:00
),
],
2022-05-18 17:28:18 +02:00
),
Service(
2022-05-24 10:02:47 +02:00
name="cli",
extends=Extends(
service_name="common",
),
2022-05-21 11:41:26 +02:00
volumes=[
Volume(
2022-05-24 10:02:47 +02:00
source="cli-volume",
target="/var/lib/backup/data",
access_mode="ro,z",
2022-05-21 11:41:26 +02:00
),
],
2022-05-18 17:28:18 +02:00
),
2022-05-24 10:02:47 +02:00
],
2022-05-18 17:28:18 +02:00
),
),
],
)
2022-05-24 10:02:47 +02:00
def test_parse_file(test_file_path: str, expected: Compose) -> None:
2022-05-07 18:42:14 +02:00
parser = Parser()
2022-05-24 10:02:47 +02:00
actual = parser.parse(f"tests/ymls/{test_file_path}.yml")
2022-05-07 18:42:14 +02:00
assert len(actual.services) == len(expected.services)
for actual_service, expected_service in zip(actual.services, expected.services):
assert actual_service.name == expected_service.name
assert actual_service.image == expected_service.image
2022-05-21 17:19:01 +02:00
assert len(actual_service.ports) == len(expected_service.ports)
for actual_port, expected_port in zip(actual_service.ports, expected_service.ports):
assert actual_port.host_port == expected_port.host_port
assert actual_port.container_port == expected_port.container_port
assert actual_port.protocol == expected_port.protocol
assert actual_service.networks == expected_service.networks
2022-05-21 11:41:26 +02:00
assert len(actual_service.volumes) == len(expected_service.volumes)
for actual_volume, expected_volume in zip(actual_service.volumes, expected_service.volumes):
assert actual_volume.source == expected_volume.source
assert actual_volume.target == expected_volume.target
assert actual_volume.type == expected_volume.type
assert actual_service.depends_on == expected_service.depends_on
assert actual_service.links == expected_service.links
2022-05-19 10:06:16 +02:00
assert (actual_service.extends is not None) == (expected_service.extends is not 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