Skip to content

Commit a62518b

Browse files
authored
Add support for ComposeContainer with Spock (#11109)
1 parent a3783a3 commit a62518b

File tree

5 files changed

+124
-15
lines changed

5 files changed

+124
-15
lines changed

modules/spock/src/main/groovy/org/testcontainers/spock/TestcontainersMethodInterceptor.groovy

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.spockframework.runtime.extension.AbstractMethodInterceptor
44
import org.spockframework.runtime.extension.IMethodInvocation
55
import org.spockframework.runtime.model.FieldInfo
66
import org.spockframework.runtime.model.SpecInfo
7+
import org.testcontainers.containers.ComposeContainer
78
import org.testcontainers.containers.DockerComposeContainer
89
import org.testcontainers.containers.GenericContainer
910
import org.testcontainers.lifecycle.TestLifecycleAware
@@ -24,6 +25,9 @@ class TestcontainersMethodInterceptor extends AbstractMethodInterceptor {
2425
def containers = findAllContainers(true)
2526
startContainers(containers, invocation)
2627

28+
def dockerCompose = findAllDockerComposeContainers(true)
29+
startDockerComposeContainers(dockerCompose, invocation)
30+
2731
def compose = findAllComposeContainers(true)
2832
startComposeContainers(compose, invocation)
2933

@@ -35,6 +39,9 @@ class TestcontainersMethodInterceptor extends AbstractMethodInterceptor {
3539
def containers = findAllContainers(true)
3640
stopContainers(containers, invocation)
3741

42+
def dockerCompose = findAllDockerComposeContainers(true)
43+
stopDockerComposeContainers(dockerCompose, invocation)
44+
3845
def compose = findAllComposeContainers(true)
3946
stopComposeContainers(compose, invocation)
4047

@@ -46,6 +53,9 @@ class TestcontainersMethodInterceptor extends AbstractMethodInterceptor {
4653
def containers = findAllContainers(false)
4754
startContainers(containers, invocation)
4855

56+
def dockerCompose = findAllDockerComposeContainers(false)
57+
startDockerComposeContainers(dockerCompose, invocation)
58+
4959
def compose = findAllComposeContainers(false)
5060
startComposeContainers(compose, invocation)
5161

@@ -58,6 +68,9 @@ class TestcontainersMethodInterceptor extends AbstractMethodInterceptor {
5868
def containers = findAllContainers(false)
5969
stopContainers(containers, invocation)
6070

71+
def dockerCompose = findAllDockerComposeContainers(false)
72+
stopDockerComposeContainers(dockerCompose, invocation)
73+
6174
def compose = findAllComposeContainers(false)
6275
stopComposeContainers(compose, invocation)
6376

@@ -70,12 +83,18 @@ class TestcontainersMethodInterceptor extends AbstractMethodInterceptor {
7083
}
7184
}
7285

73-
private List<FieldInfo> findAllComposeContainers(boolean shared) {
86+
private List<FieldInfo> findAllDockerComposeContainers(boolean shared) {
7487
spec.allFields.findAll { FieldInfo f ->
7588
DockerComposeContainer.isAssignableFrom(f.type) && f.shared == shared
7689
}
7790
}
7891

92+
private List<FieldInfo> findAllComposeContainers(boolean shared) {
93+
spec.allFields.findAll { FieldInfo f ->
94+
ComposeContainer.isAssignableFrom(f.type) && f.shared == shared
95+
}
96+
}
97+
7998
private static void startContainers(List<FieldInfo> containers, IMethodInvocation invocation) {
8099
containers.each { FieldInfo f ->
81100
GenericContainer container = readContainerFromField(f, invocation)
@@ -105,20 +124,34 @@ class TestcontainersMethodInterceptor extends AbstractMethodInterceptor {
105124
}
106125
}
107126

108-
private static void startComposeContainers(List<FieldInfo> compose, IMethodInvocation invocation) {
127+
private static void startDockerComposeContainers(List<FieldInfo> compose, IMethodInvocation invocation) {
109128
compose.each { FieldInfo f ->
110129
DockerComposeContainer c = f.readValue(invocation.instance) as DockerComposeContainer
111130
c.start()
112131
}
113132
}
114133

115-
private static void stopComposeContainers(List<FieldInfo> compose, IMethodInvocation invocation) {
134+
private static void startComposeContainers(List<FieldInfo> compose, IMethodInvocation invocation) {
135+
compose.each { FieldInfo f ->
136+
ComposeContainer c = f.readValue(invocation.instance) as ComposeContainer
137+
c.start()
138+
}
139+
}
140+
141+
private static void stopDockerComposeContainers(List<FieldInfo> compose, IMethodInvocation invocation) {
116142
compose.each { FieldInfo f ->
117143
DockerComposeContainer c = f.readValue(invocation.instance) as DockerComposeContainer
118144
c.stop()
119145
}
120146
}
121147

148+
private static void stopComposeContainers(List<FieldInfo> compose, IMethodInvocation invocation) {
149+
compose.each { FieldInfo f ->
150+
ComposeContainer c = f.readValue(invocation.instance) as ComposeContainer
151+
c.stop()
152+
}
153+
}
154+
122155

123156
private static GenericContainer readContainerFromField(FieldInfo f, IMethodInvocation invocation) {
124157
f.readValue(invocation.instance) as GenericContainer

modules/spock/src/test/groovy/org/testcontainers/spock/ComposeContainerIT.groovy

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ package org.testcontainers.spock
22

33
import org.apache.http.client.methods.HttpGet
44
import org.apache.http.impl.client.HttpClientBuilder
5-
import org.testcontainers.containers.DockerComposeContainer
5+
import org.testcontainers.containers.ComposeContainer
66
import org.testcontainers.containers.wait.strategy.Wait
77
import org.testcontainers.utility.DockerImageName
88
import spock.lang.Specification
99

1010
@Testcontainers
1111
class ComposeContainerIT extends Specification {
1212

13-
DockerComposeContainer composeContainer = new DockerComposeContainer(
14-
DockerImageName.parse("docker/compose:debian-1.29.2"),
13+
ComposeContainer composeContainer = new ComposeContainer(
14+
DockerImageName.parse("docker:24.0.2"),
1515
new File("src/test/resources/docker-compose.yml"))
16-
.withExposedService("whoami_1", 80, Wait.forHttp("/"))
16+
.withExposedService("whoami-1", 80, Wait.forHttp("/"))
1717

1818
String host
1919

2020
int port
2121

2222
def setup() {
23-
host = composeContainer.getServiceHost("whoami_1", 80)
24-
port = composeContainer.getServicePort("whoami_1", 80)
23+
host = composeContainer.getServiceHost("whoami-1", 80)
24+
port = composeContainer.getServicePort("whoami-1", 80)
2525
}
2626

2727
def "running compose defined container is accessible on configured port"() {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.testcontainers.spock
2+
3+
import org.apache.http.client.methods.HttpGet
4+
import org.apache.http.impl.client.HttpClientBuilder
5+
import org.testcontainers.containers.DockerComposeContainer
6+
import org.testcontainers.containers.wait.strategy.Wait
7+
import org.testcontainers.utility.DockerImageName
8+
import spock.lang.Specification
9+
10+
@Testcontainers
11+
class DockerComposeContainerIT extends Specification {
12+
13+
DockerComposeContainer composeContainer = new DockerComposeContainer(
14+
DockerImageName.parse("docker/compose:debian-1.29.2"),
15+
new File("src/test/resources/docker-compose.yml"))
16+
.withExposedService("whoami_1", 80, Wait.forHttp("/"))
17+
18+
String host
19+
20+
int port
21+
22+
def setup() {
23+
host = composeContainer.getServiceHost("whoami_1", 80)
24+
port = composeContainer.getServicePort("whoami_1", 80)
25+
}
26+
27+
def "running compose defined container is accessible on configured port"() {
28+
given: "a http client"
29+
def client = HttpClientBuilder.create().build()
30+
31+
when: "accessing web server"
32+
def response = client.execute(new HttpGet("http://$host:$port"))
33+
34+
then: "docker container is running and returns http status code 200"
35+
response.statusLine.statusCode == 200
36+
}
37+
}

modules/spock/src/test/groovy/org/testcontainers/spock/SharedComposeContainerIT.groovy

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.testcontainers.spock
22

33
import org.apache.http.client.methods.HttpGet
44
import org.apache.http.impl.client.HttpClientBuilder
5-
import org.testcontainers.containers.DockerComposeContainer
5+
import org.testcontainers.containers.ComposeContainer
66
import org.testcontainers.containers.wait.strategy.Wait
77
import org.testcontainers.utility.DockerImageName
88
import spock.lang.Shared
@@ -12,18 +12,18 @@ import spock.lang.Specification
1212
class SharedComposeContainerIT extends Specification {
1313

1414
@Shared
15-
DockerComposeContainer composeContainer = new DockerComposeContainer(
16-
DockerImageName.parse("docker/compose:debian-1.29.2"),
15+
ComposeContainer composeContainer = new ComposeContainer(
16+
DockerImageName.parse("docker:24.0.2"),
1717
new File("src/test/resources/docker-compose.yml"))
18-
.withExposedService("whoami_1", 80, Wait.forHttp("/"))
18+
.withExposedService("whoami-1", 80, Wait.forHttp("/"))
1919

2020
String host
2121

2222
int port
2323

2424
def setup() {
25-
host = composeContainer.getServiceHost("whoami_1", 80)
26-
port = composeContainer.getServicePort("whoami_1", 80)
25+
host = composeContainer.getServiceHost("whoami-1", 80)
26+
port = composeContainer.getServicePort("whoami-1", 80)
2727
}
2828

2929
def "running compose defined container is accessible on configured port"() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.testcontainers.spock
2+
3+
import org.apache.http.client.methods.HttpGet
4+
import org.apache.http.impl.client.HttpClientBuilder
5+
import org.testcontainers.containers.DockerComposeContainer
6+
import org.testcontainers.containers.wait.strategy.Wait
7+
import org.testcontainers.utility.DockerImageName
8+
import spock.lang.Shared
9+
import spock.lang.Specification
10+
11+
@Testcontainers
12+
class SharedDockerComposeContainerIT extends Specification {
13+
14+
@Shared
15+
DockerComposeContainer composeContainer = new DockerComposeContainer(
16+
DockerImageName.parse("docker/compose:debian-1.29.2"),
17+
new File("src/test/resources/docker-compose.yml"))
18+
.withExposedService("whoami_1", 80, Wait.forHttp("/"))
19+
20+
String host
21+
22+
int port
23+
24+
def setup() {
25+
host = composeContainer.getServiceHost("whoami_1", 80)
26+
port = composeContainer.getServicePort("whoami_1", 80)
27+
}
28+
29+
def "running compose defined container is accessible on configured port"() {
30+
given: "a http client"
31+
def client = HttpClientBuilder.create().build()
32+
33+
when: "accessing web server"
34+
def response = client.execute(new HttpGet("http://$host:$port"))
35+
36+
then: "docker container is running and returns http status code 200"
37+
response.statusLine.statusCode == 200
38+
}
39+
}

0 commit comments

Comments
 (0)