compose-viz/tests/test_parse_file.py

417 lines
15 KiB
Python
Raw Permalink Normal View History

2022-05-18 06:19:21 +02:00
import pytest
2022-05-18 17:28:18 +02:00
2022-05-27 08:17:04 +02:00
from compose_viz.models.compose import Compose
2022-06-06 18:41:45 +02:00
from compose_viz.models.device import Device
2022-05-27 08:17:04 +02:00
from compose_viz.models.extends import Extends
from compose_viz.models.port import Port, Protocol
from compose_viz.models.service import Service
from compose_viz.models.volume import Volume, VolumeType
2022-05-18 17:28:18 +02:00
from compose_viz.parser import Parser
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="0.0.0.0:7777",
container_port="7777",
),
Port(
host_port="${BIND_IP:-127.0.0.1}:8080",
container_port="8080",
),
2022-05-24 10:02:47 +02:00
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-27 14:26:44 +02:00
Service(
name="tmp",
image="awesome/nginx",
volumes=[
Volume(
source="/app",
target="/app",
type=VolumeType.tmpfs,
),
],
),
2022-05-24 10:02:47 +02:00
],
2022-05-18 17:28:18 +02:00
),
),
(
"cgroup_parent/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
2022-06-03 11:19:43 +02:00
cgroup_parent="system",
),
],
),
),
(
"container_name/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
2022-06-03 11:19:43 +02:00
container_name="myfrontend",
),
],
),
),
(
"env_file/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
2022-06-03 11:19:43 +02:00
env_file=["a.env"],
),
Service(
name="backend",
image="awesome/backend",
2022-06-03 11:19:43 +02:00
env_file=["b.env"],
),
Service(
name="db",
image="awesome/db",
2022-06-03 11:19:43 +02:00
env_file=["c.env", "d.env"],
),
],
),
),
(
"expose/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
2022-06-03 11:19:43 +02:00
expose=["27118"],
),
Service(
name="backend",
image="awesome/backend",
2022-06-03 11:19:43 +02:00
expose=["27017", "27018"],
),
],
),
),
2022-06-03 11:19:43 +02:00
(
"profiles/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
2022-06-03 11:19:43 +02:00
profiles=["frontend"],
),
Service(
name="phpmyadmin",
image="phpmyadmin",
2022-06-03 11:19:43 +02:00
profiles=["debug"],
),
Service(
name="db",
image="awesome/db",
2022-06-03 11:19:43 +02:00
profiles=["db", "sql"],
),
],
),
),
2022-06-06 18:41:45 +02:00
(
"devices/docker-compose",
Compose(
services=[
Service(
name="frontend",
image="awesome/frontend",
devices=[
Device(
host_path="/dev/ttyUSB0",
container_path="/dev/ttyUSB1",
)
],
),
Service(
name="backend",
image="awesome/backend",
devices=[
Device(
host_path="/dev/ttyUSB2",
container_path="/dev/ttyUSB3",
),
Device(
host_path="/dev/sda",
container_path="/dev/xvda",
cgroup_permissions="rwm",
),
],
),
],
),
),
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
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
2022-06-03 11:19:43 +02:00
assert actual_service.profiles == expected_service.profiles
2022-06-06 18:41:45 +02:00
assert len(actual_service.devices) == len(expected_service.devices)
for actual_device, expected_device in zip(actual_service.devices, expected_service.devices):
assert actual_device.host_path == expected_device.host_path
assert actual_device.container_path == expected_device.container_path
assert actual_device.cgroup_permissions == expected_device.cgroup_permissions