def _generate_pydock_service_file(app_name, depends_on): dependencies = _service_dependency_def(depends_on) app = _app_service_def(app_name=app_name, service_dependencies=dependencies) code = service_template.generate_service_file([app] + dependencies) with open('pydock_service_def.py', 'w') as f: print(code, file=f)
def test_load_and_exec(): """ it returns new namespace with service file exec result classes """ services = [ structures.ServiceDef( name='pydock', build={ 'context': '.', 'args': { 'app_name': 'pydock' } }, volumes=['.:/pydock'], deps=['redis'] ), structures.ServiceDef( name='redis', image='redis:alpine', ) ] code = service_template.generate_service_file(services) ns = docker_compose._load_and_exec(code) assert inspect.isclass(ns['Pydock']) == True assert inspect.isclass(ns['Redis']) == True
def test_validate_dependencies_raises_on_missing_dependencies(): """ it detects missing dependencies and raises """ services = [ structures.ServiceDef( name='pydock', build={ 'context': '.', 'args': { 'app_name': 'pydock' } }, volumes=['.:/pydock'], deps=['redis'] ), ] source = service_template.generate_service_file(services) ns = docker_compose._load_and_exec(source) with pytest.raises(exceptions.PydockException) as execinfo: docker_compose._validate_dependencies(ns) assert str(execinfo.value) == "missing dependencies {'redis'} for service Pydock"
def test_generates_docker_compose(): """ given valid service definitions, it generates correct docker compose file """ services = [ structures.ServiceDef( name='pydock', build={ 'context': '.', 'args': { 'app_name': 'pydock' } }, volumes=['.:/pydock'], deps=['redis'] ), structures.ServiceDef( name='redis', image='redis:alpine', ) ] code = service_template.generate_service_file(services) compose_yaml = docker_compose.generate_docker_compose(code) assert compose_yaml == { 'version': '3', 'services': { 'pydock': { 'build': { 'context': '.', 'args': { 'app_name': 'pydock' } }, 'volumes': ['.:/pydock'], 'depends_on': ['redis'] }, 'redis': { 'image': 'redis:alpine' } } }
def test_service_template(): """ it generates the correct service file """ services = [ structures.ServiceDef(name='pydock', build={ 'context': '.', 'args': { 'app_name': 'pydock' } }, volumes=['.:/pydock'], deps=['redis']), structures.ServiceDef( name='redis', image='redis:alpine', ) ] code = service_template.generate_service_file(services) namespace = {} exec(code, namespace) assert namespace['Pydock'].BUILD == { 'context': '.', 'args': { 'app_name': 'pydock' } } assert namespace['Pydock'].IMAGE == None assert namespace['Pydock'].VOLUMES == ['.:/pydock'] assert namespace['Pydock'].DEPENDS_ON == ['redis'] assert namespace['Redis'].BUILD == None assert namespace['Redis'].IMAGE == 'redis:alpine' assert namespace['Redis'].VOLUMES == None assert namespace['Redis'].DEPENDS_ON == []