I have the following integration test setup for Kafka producer consumer application.
@Testcontainer
@DirtiesContext
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class KafkaIntegrationTest {
@Container
private static final KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:latest"))
.withFileSystemBind("/var/run/docker.sock", "/var/run/docker.sock", BindMode.READ_WRITE);
@DynamicPropertySource
private static void kafkaProperties(DynamicPropertyRegistry registry) {
registry.add("spring.kafka.bootstrap-servers", KafkaContainer::getBootstrapServers);
}
@Test
public void givenKafkaContainerWhenSendingWithProducerThenMessageReceived() {
// Consumer integration test here
}
}
Moreover, I have added these additional configurations in Dockerfile
and Jenkinsfile
to mount the host's Docker socket into the Docker container.
Dockerfile
...
FROM docker:latest
...
VOLUME /var/run/docker.sock:/var/run/docker.sock
...
RUN ./gradlew --no-daemon testClasses processIntegrationTests
...
Jenkinsfile
...
agent {
docker {
image 'docker:latest'
args '-v /var/run/docker.sock:/var/run/docker.sock --privileged'
}
}
...
stage('Test') {
steps {
sh "docker build --target test-result ."
}
...
}
The integration tests are working fine when running the Gradle task in IDE, but still not pulling the Kafka image programmatically using Testcontainers
in the Docker based CI/CD pipeline and failing with the following error.
environment initializationError FAILED
java.lang.IllegalStateException at DockerClientProviderStrategy.java