From 56dbed4ca833a2cafc60c61057f45b04c63f8e7b Mon Sep 17 00:00:00 2001 From: Jan Boehm <6552713+jeloba@users.noreply.github.com> Date: Fri, 8 May 2020 12:11:45 +0200 Subject: [PATCH] Fixed ServiceInfo when no service instances exist --- .../gradle/dockercompose/ServiceInfo.groovy | 18 ++++++------- .../DockerComposePluginTest.groovy | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/groovy/com/avast/gradle/dockercompose/ServiceInfo.groovy b/src/main/groovy/com/avast/gradle/dockercompose/ServiceInfo.groovy index 2fabe98..a0b3e79 100644 --- a/src/main/groovy/com/avast/gradle/dockercompose/ServiceInfo.groovy +++ b/src/main/groovy/com/avast/gradle/dockercompose/ServiceInfo.groovy @@ -8,16 +8,16 @@ class ServiceInfo { /* Key is instance name, for example service_1 */ Map containerInfos = [:] - String getHost() { firstContainer.serviceHost.host } - Map getPorts() { firstContainer.tcpPorts } - Map getTcpPorts() { firstContainer.tcpPorts } - Map getUdpPorts() { firstContainer.udpPorts } - Integer getPort() { firstContainer.port } - Integer getTcpPort() { firstContainer.tcpPort } - Integer getUdpPort() { firstContainer.udpPort } - + String getHost() { firstContainer?.serviceHost.host } + Map getPorts() { tcpPorts } + Map getTcpPorts() { firstContainer?.tcpPorts ?: [:] } + Map getUdpPorts() { firstContainer?.udpPorts ?: [:] } + Integer getPort() { firstContainer?.port } + Integer getTcpPort() { firstContainer?.tcpPort } + Integer getUdpPort() { firstContainer?.udpPort } + ContainerInfo getFirstContainer() { - containerInfos.values().first() + containerInfos.values()?.find() } def propertyMissing(String name) { diff --git a/src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy b/src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy index fe07776..1874dcb 100644 --- a/src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy +++ b/src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy @@ -404,6 +404,32 @@ class DockerComposePluginTest extends Specification { f.close() } + @IgnoreIf({ parse(System.getenv('DOCKER_COMPOSE_VERSION')) < parse('1.13.0') }) + def "docker-compose scale to 0 does not cause exceptions because of missing first container"() { + def f = Fixture.custom(''' + web: + image: nginx:stable + ports: + - 80 + z: + image: nginx:stable + ports: [] + ''') + f.extension.scale = ['web': 0] + def integrationTestTask = f.project.tasks.create('integrationTest').doLast { + def webInfos = project.dockerCompose.servicesInfos.web.containerInfos + assert webInfos.size() == 0 + } + when: + f.project.tasks.composeUp.up() + integrationTestTask.actions.forEach { it.execute(integrationTestTask) } + then: + noExceptionThrown() + cleanup: + f.project.tasks.composeDown.down() + f.close() + } + def "exposes environment variables and system properties for container with custom name"() { def f = Fixture.custom(composeFileContent) f.project.plugins.apply 'java'