compose-viz/tests/test_parse_file.py

581 lines
16 KiB
Python
Raw Normal View History

2022-05-18 06:19:21 +02:00
import pytest
2022-05-07 18:42:14 +02:00
from compose_viz.parser import Parser
from compose_viz.compose import Compose
from compose_viz.service import Service
2022-05-18 10:03:33 +02:00
from compose_viz.extends import Extends
2022-05-07 18:42:14 +02:00
2022-05-18 08:13:07 +02:00
@pytest.mark.parametrize('test_input,expected',[
2022-05-18 06:19:21 +02:00
('tests/in/000001.yaml',Compose([
2022-05-07 18:42:14 +02:00
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
),
2022-05-18 06:19:21 +02:00
])),
('tests/in/000010.yaml',Compose([
Service(
name='base',
image='busybox',
),
Service(
name='common',
image='busybox',
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 06:19:21 +02:00
),
Service(
name='cli',
2022-05-18 08:21:51 +02:00
extends=['common'],
2022-05-18 08:13:07 +02:00
),
])),
('tests/in/000011.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 08:13:07 +02:00
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 06:19:21 +02:00
),
])),
('tests/in/000100.yaml',Compose([
Service(
2022-05-18 10:08:15 +02:00
name='web',
2022-05-18 08:13:07 +02:00
ports=['8000:5000'],
2022-05-18 06:19:21 +02:00
),
Service(
name='redis',
image='redis:alpine',
),
])),
2022-05-18 08:13:07 +02:00
('tests/in/000101.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
2022-05-18 08:21:51 +02:00
ports=['8000:5000'],
2022-05-18 08:13:07 +02:00
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
2022-05-18 08:21:51 +02:00
ports=['8000:5001'],
2022-05-18 08:13:07 +02:00
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
2022-05-18 08:21:51 +02:00
ports=['8000:5010'],
2022-05-18 08:13:07 +02:00
networks=['back-tier', 'admin'],
),
])),
('tests/in/000110.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
ports=['8000:5000'],
),
Service(
name='monitoring',
image='awesome/monitoring',
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 08:13:07 +02:00
),
Service(
name='backend',
image='awesome/backend',
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 08:13:07 +02:00
ports=['8000:5001'],
),
])),
('tests/in/000111.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
2022-05-18 10:03:33 +02:00
ports=['8000:5000'],
2022-05-18 08:13:07 +02:00
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 08:13:07 +02:00
),
Service(
name='backend',
image='awesome/backend',
ports=['8000:5001'],
networks=['back-tier', 'admin'],
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 08:13:07 +02:00
),
])),
2022-05-18 06:19:21 +02:00
('tests/in/001000.yaml',Compose([
Service(
name='web',
2022-05-18 08:13:07 +02:00
depends_on=['db','redis'],
),
Service(
name='redis',
image='redis',
),
Service(
name='db',
image='postgres',
),
])),
('tests/in/001001.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
depends_on=['monitoring','backend'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
),
])),
('tests/in/001010.yaml',Compose([
Service(
name='web',
depends_on=['db','redis'],
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='redis'),
2022-05-18 06:19:21 +02:00
),
Service(
name='redis',
2022-05-18 08:13:07 +02:00
image='redis',
2022-05-18 06:19:21 +02:00
),
Service(
name='db',
2022-05-18 08:13:07 +02:00
image='postgres',
),
])),
('tests/in/001011.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
depends_on=['monitoring','backend'],
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='backend'),
2022-05-18 08:13:07 +02:00
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
),
])),
('tests/in/001100.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
ports=['8000:5000'],
),
Service(
name='monitoring',
image='awesome/monitoring',
depends_on=['backend'],
ports=['8000:5010'],
),
Service(
name='backend',
image='awesome/backend',
ports=['8000:5001'],
),
])),
('tests/in/001101.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
depends_on=['backend'],
ports=['8000:5010'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
),
])),
('tests/in/001110.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
ports=['8000:5000'],
),
Service(
name='monitoring',
image='awesome/monitoring',
depends_on=['backend'],
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 08:13:07 +02:00
ports=['8000:5010'],
),
Service(
name='backend',
image='awesome/backend',
ports=['8000:5001'],
),
])),
('tests/in/001111.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
depends_on=['backend'],
2022-05-18 10:03:33 +02:00
extends=Extends(service_name='frontend'),
2022-05-18 08:13:07 +02:00
ports=['8000:5010'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
2022-05-18 06:19:21 +02:00
),
])),
('tests/in/010000.yaml',Compose([
Service(
name='backend',
image='awesome/backend',
2022-05-18 08:13:07 +02:00
volumes=['db-data'],
),
2022-05-18 06:19:21 +02:00
])),
2022-05-18 08:21:51 +02:00
('tests/in/010001.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
volumes=['db-data'],
),
])),
2022-05-18 10:03:33 +02:00
('tests/in/010010.yaml',Compose([
Service(
name='common',
image='busybox',
volumes=['common-volume'],
),
Service(
name='cli',
extends=Extends(service_name='common'),
volumes=['cli-volume'],
),
])),
('tests/in/010011.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
volumes=['db-data'],
extends=Extends(service_name='monitoring'),
),
])),
('tests/in/010100.yaml',Compose([
Service(
name='backend',
image='awesome/backend',
volumes=['db-data'],
ports=["8000:5000"],
),
])),
2022-05-18 11:26:43 +02:00
('tests/in/010101.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
volumes=['db-data'],
ports=['8000:5000'],
),
])),
('tests/in/010110.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
),
Service(
name='monitoring',
image='awesome/monitoring',
),
Service(
name='backend',
image='awesome/backend',
volumes=['db-data'],
extends=Extends(service_name='monitoring'),
ports=['8000:5000'],
),
])),
2022-05-18 10:03:33 +02:00
('tests/in/010111.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
volumes=['db-data'],
extends=Extends(service_name='monitoring'),
ports=['8000:5000'],
),
])),
2022-05-18 11:26:43 +02:00
('tests/in/011000.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
),
Service(
name='monitoring',
image='awesome/monitoring',
depends_on=['backend'],
volumes=['db-data'],
),
Service(
name='backend',
image='awesome/backend',
),
])),
('tests/in/011001.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier','admin'],
volumes=['db-data'],
depends_on=['monitoring'],
),
])),
('tests/in/011010.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
),
Service(
name='monitoring',
image='awesome/monitoring',
),
Service(
name='backend',
image='awesome/backend',
volumes=['db-data'],
depends_on=['monitoring'],
extends=Extends(service_name='frontend'),
),
])),
('tests/in/011011.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier','back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier','admin'],
volumes=['db-data'],
depends_on=['monitoring'],
extends=Extends(service_name='frontend'),
),
])),
2022-05-18 10:03:33 +02:00
('tests/in/011100.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
),
Service(
name='monitoring',
image='awesome/monitoring',
),
Service(
name='backend',
image='awesome/backend',
volumes=['db-data'],
depends_on=['monitoring'],
extends=Extends(service_name='frontend'),
ports=['8000:5010'],
),
])),
2022-05-18 11:26:43 +02:00
('tests/in/011101.yaml',Compose([
Service(
name='vote',
depends_on=['redis'],
volumes=['app'],
ports=['5000:80'],
networks=['front-tier','back-tier'],
),
Service(
name='result',
depends_on=['db'],
volumes=['app'],
ports=['5001:80','5858:5858'],
networks=['front-tier','back-tier'],
),
Service(
name='worker',
depends_on=['redis','db'],
networks=['back-tier'],
),
Service(
name='redis',
image='redis:5.0-alpine3.10',
volumes=[''], #####
ports=['6379'],
networks=['back-tier'],
),
Service(
name='db',
image='postgres:9.4',
volumes=[''], #####
networks=['back-tier'],
),
])),
2022-05-18 08:13:07 +02:00
2022-05-18 10:03:33 +02:00
('tests/in/011110.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
),
Service(
name='monitoring',
image='awesome/monitoring',
),
Service(
name='backend',
image='awesome/backend',
volumes=['db-data'],
depends_on=['monitoring'],
extends=Extends(service_name='frontend'),
ports=['8000:5010'],
),
])),
('tests/in/011111.yaml',Compose([
Service(
name='frontend',
image='awesome/webapp',
networks=['front-tier', 'back-tier'],
),
Service(
name='monitoring',
image='awesome/monitoring',
networks=['admin'],
),
Service(
name='backend',
image='awesome/backend',
networks=['back-tier', 'admin'],
volumes=['db-data'],
depends_on=['monitoring'],
extends=Extends(service_name='monitoring'),
ports=['8000:5010'],
),
])),
2022-05-18 06:19:21 +02:00
('tests/in/100000.yaml',Compose([
Service(
name='web',
2022-05-18 08:13:07 +02:00
links=['db:database'],
2022-05-18 06:19:21 +02:00
),
Service(
name='db',
2022-05-18 08:13:07 +02:00
image='postgres',
),
2022-05-18 06:19:21 +02:00
])),
2022-05-18 08:13:07 +02:00
])
2022-05-18 06:19:21 +02:00
def test_parse_file(test_input, expected):
2022-05-07 18:42:14 +02:00
parser = Parser()
2022-05-18 06:19:21 +02:00
actual = parser.parse(test_input)
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
assert actual_service.ports == expected_service.ports
assert actual_service.networks == expected_service.networks
assert actual_service.volumes == expected_service.volumes
assert actual_service.depends_on == expected_service.depends_on
assert actual_service.links == expected_service.links
assert actual_service.extends == expected_service.extends