Skip to content

Commit

Permalink
Fix exposing env variables and system properties under service name h…
Browse files Browse the repository at this point in the history
…aving dashes replaced with underscores
  • Loading branch information
Kirill Andreev authored and augi committed Feb 25, 2022
1 parent 7647f71 commit 0d97d26
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ abstract class ComposeSettings {
}

protected Map<String, Object> createEnvironmentVariables(String variableName, ContainerInfo ci) {
def serviceName = variableName.replaceAll('-', '_')
def serviceName = replaceV2Separator(variableName)
Map<String, Object> environmentVariables = [:]
environmentVariables.put("${serviceName}_HOST".toString(), ci.host)
environmentVariables.put("${serviceName}_CONTAINER_HOSTNAME".toString(), ci.containerHostname)
Expand All @@ -301,7 +301,7 @@ abstract class ComposeSettings {
}

protected Map<String, Object> createSystemProperties(String variableName, ContainerInfo ci) {
def serviceName = variableName.replaceAll('-', '_')
def serviceName = replaceV2Separator(variableName)
Map<String, Object> systemProperties = [:]
systemProperties.put("${serviceName}.host".toString(), ci.host)
systemProperties.put("${serviceName}.containerHostname".toString(), ci.containerHostname)
Expand All @@ -310,6 +310,10 @@ abstract class ComposeSettings {
systemProperties
}

static String replaceV2Separator(String serviceName) {
serviceName.replaceAll('-(\\d+)$', '_$1')
}

boolean removeOrphans() {
composeExecutor.version >= VersionNumber.parse('1.7.0') && this.removeOrphans.get()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,38 @@ class DockerComposePluginTest extends Specification {
''']
}

def "exposes environment variables and system properties for services having dash in service name"() {
def f = Fixture.custom(composeFileContent)
f.project.plugins.apply 'java'
f.project.tasks.composeUp.up()
Test test = f.project.tasks.test as Test
when:
f.project.dockerCompose.exposeAsEnvironment(test)
f.project.dockerCompose.exposeAsSystemProperties(test)
then:
test.environment.containsKey('WEB-SERVICE_HOST')
test.environment.containsKey('WEB-SERVICE_CONTAINER_HOSTNAME')
test.environment.containsKey('WEB-SERVICE_TCP_80')
test.environment.containsKey('WEB-SERVICE_UDP_81')
test.systemProperties.containsKey('web-service.host')
test.systemProperties.containsKey('web-service.containerHostname')
test.systemProperties.containsKey('web-service.tcp.80')
test.systemProperties.containsKey('web-service.udp.81')
cleanup:
f.project.tasks.composeDown.down()
f.close()
where:
composeFileContent << ['''
version: '2'
services:
web-service:
image: nginx:stable
ports:
- 80
- 81/udp
''']
}

private static boolean isRunningOnWindows() { System.properties['os.name'].toString().toLowerCase().startsWith('windows') }
private static boolean isRunningOnMac() { System.properties['os.name'].toString().toLowerCase().startsWith('macos') || System.properties['os.name'].toString().toLowerCase().startsWith('mac os') }

Expand Down

0 comments on commit 0d97d26

Please sign in to comment.