Skip to content

Commit e730674

Browse files
authored
Allow spock tests to be skipped when Docker is unavailable (#10180)
Fixes #10178
1 parent 80ae5d7 commit e730674

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.testcontainers.spock
2+
3+
import org.testcontainers.DockerClientFactory
4+
5+
class DockerAvailableDetector {
6+
7+
boolean isDockerAvailable() {
8+
try {
9+
DockerClientFactory.instance().client();
10+
return true;
11+
} catch (Throwable ex) {
12+
return false;
13+
}
14+
}
15+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,11 @@ import java.lang.annotation.Target
5454
@Target([ElementType.TYPE, ElementType.METHOD])
5555
@ExtensionAnnotation(TestcontainersExtension)
5656
@interface Testcontainers {
57+
58+
/**
59+
* Whether tests should be disabled (rather than failing) when Docker is not available. Defaults to
60+
* {@code false}.
61+
* @return if the tests should be disabled when Docker is not available
62+
*/
63+
boolean disabledWithoutDocker() default false;
5764
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,23 @@ import org.spockframework.runtime.model.SpecInfo
77

88
class TestcontainersExtension extends AbstractAnnotationDrivenExtension<Testcontainers> {
99

10+
private final DockerAvailableDetector dockerDetector
11+
12+
TestcontainersExtension() {
13+
this(new DockerAvailableDetector())
14+
}
15+
16+
TestcontainersExtension(DockerAvailableDetector dockerDetector) {
17+
this.dockerDetector = dockerDetector
18+
}
19+
1020
@Override
1121
void visitSpecAnnotation(Testcontainers annotation, SpecInfo spec) {
22+
if (annotation.disabledWithoutDocker()) {
23+
if (!dockerDetector.isDockerAvailable()) {
24+
spec.skip("disabledWithoutDocker is true and Docker is not available")
25+
}
26+
}
1227
def listener = new ErrorListener()
1328
def interceptor = new TestcontainersMethodInterceptor(spec, listener)
1429
spec.addSetupSpecInterceptor(interceptor)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.testcontainers.spock
2+
3+
import org.spockframework.runtime.model.SpecInfo
4+
import spock.lang.Specification
5+
import spock.lang.Unroll
6+
7+
class TestcontainersExtensionTest extends Specification {
8+
9+
@Unroll
10+
def "should handle disabledWithoutDocker=#disabledWithoutDocker and dockerAvailable=#dockerAvailable correctly"() {
11+
given:
12+
def dockerDetector = Mock(DockerAvailableDetector)
13+
dockerDetector.isDockerAvailable() >> dockerAvailable
14+
def extension = new TestcontainersExtension(dockerDetector)
15+
def specInfo = Mock(SpecInfo)
16+
def annotation = disabledWithoutDocker ?
17+
TestDisabledWithoutDocker.getAnnotation(Testcontainers) :
18+
TestEnabledWithoutDocker.getAnnotation(Testcontainers)
19+
20+
when:
21+
extension.visitSpecAnnotation(annotation, specInfo)
22+
23+
then:
24+
skipCalls * specInfo.skip("disabledWithoutDocker is true and Docker is not available")
25+
26+
where:
27+
disabledWithoutDocker | dockerAvailable | skipCalls
28+
true | true | 0
29+
true | false | 1
30+
false | true | 0
31+
false | false | 0
32+
}
33+
34+
@Testcontainers(disabledWithoutDocker = true)
35+
static class TestDisabledWithoutDocker {}
36+
37+
@Testcontainers
38+
static class TestEnabledWithoutDocker {}
39+
}

0 commit comments

Comments
 (0)