Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve org.testcontainers.containers.ContainerFetchException on Spring Boot?

I am working on developing a few integration tests for my spring boot application. I am using testcontainers in order to create a MongoDB docker image. My code so far:

ContainerListener.java class:

public class ContainerListener implements TestListener {

@ClassRule
public static Network sharedNetwork = Network.newNetwork();

@ClassRule
public static GenericContainer mongoDBContainer = new GenericContainer("mongo:3.2.4").withNetwork(sharedNetwork)
        .withNetworkAliases("mongo").withExposedPorts(27017);

public static MockServerContainer mockServerContainer = new MockServerContainer().withNetwork(sharedNetwork)
        .withNetworkAliases("mockserver").withExposedPorts(1080);

public static int getMockPort() {
    return mockServerContainer.getMappedPort(1080);
}

public static int getMongoPort() {
    return mongoDBContainer.getMappedPort(27017);
}

public static void runAll() {
    List.of(mongoDBContainer, mockServerContainer).forEach(e -> e.start());
}

public static void stopAll() {
    List.of(mongoDBContainer, mockServerContainer).forEach(e -> e.stop());
}

}

And SomeControllerIntegrationTest.java class:

import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

public class RatingControllerIntegrationTest {

    @BeforeAll
    public static void setuo() {
        ContainerListener.runAll();

    }

    @AfterAll
    public static void tearDown() {
        ContainerListener.stopAll();
    }

    @Test
    public void detailsTest() throws Exception {
        RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
        RestAssured.defaultParser = Parser.JSON;

        given().log().all().when().get("http://localhost:8080/actuator/info/").then().log().all()
                .statusCode(200);
    }

}

Despite that after running the tests I get the following error:

org.testcontainers.containers.ContainerLaunchException: Container startup failed

Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=mongo:3.2.4, imagePullPolicy=DefaultPullPolicy())
    at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1279)
    at org.testcontainers.containers.GenericContainer.logger(GenericContainer.java:613)
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:320)
    ... 58 more
Caused by: java.lang.IllegalStateException: Can not connect to Ryuk at localhost:32770
    at org.testcontainers.utility.ResourceReaper.start(ResourceReaper.java:176)
    at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:168)
    at org.testcontainers.LazyDockerClient.getDockerClient(LazyDockerClient.java:14)
    at org.testcontainers.LazyDockerClient.listImagesCmd(LazyDockerClient.java:12)
    at org.testcontainers.images.LocalImagesCache.maybeInitCache(LocalImagesCache.java:68)
    at org.testcontainers.images.LocalImagesCache.get(LocalImagesCache.java:32)
    at org.testcontainers.images.AbstractImagePullPolicy.shouldPull(AbstractImagePullPolicy.java:18)
    at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:59)
    at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:26)
    at org.testcontainers.utility.LazyFuture.getResolvedValue(LazyFuture.java:20)
    at org.testcontainers.utility.LazyFuture.get(LazyFuture.java:27)
    at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1277)
    ... 60 more
like image 472
hispanicprogrammer Avatar asked Aug 27 '20 11:08

hispanicprogrammer


People also ask

How do I disable RYUK Testcontainers?

Disabling Ryuk Ryuk must be started as a privileged container. If your environment already implements automatic cleanup of containers after the execution, but does not allow starting privileged containers, you can turn off the Ryuk container by setting TESTCONTAINERS_RYUK_DISABLED environment variable to true .

What is Testcontainers RYUK?

This project helps you to remove containers/networks/volumes/images by given filter after specified delay.

How do I set up a test container in Spring Boot?

The first prerequisite to using any test container is to have Docker installed on the machine where we are running the tests. Once we have Docker installed, we can start setting up our Spring Boot application. In this application, we'll set up a Redis hash, a repository, and a service that will use the repository to interact with Redis. 2.1.

What is testcontainers in Java?

Testcontainers is a Java library for creating temporary Docker containers for unit testing purposes. It's useful when we want to avoid testing with actual servers. In this tutorial, we'll learn how to use Testcontainers while testing a Spring Boot application that uses Redis. 2. Project Setup

What can you do with testcontainers?

We used Testcontainers to test our Spring Boot Application with a real database. The test is still self-contained, we don’t need to set up anything outside of the test case itself (besides having docker). This is huge! You can now test everything you do with the database.

What is testcontainers in Docker?

What is Testcontainers? Testcontainers is a Java library that provides functionality to handle a docker container. You can start any container by using the GenericContainer with any docker image, one of the specialized containers (e.g PostgreSqlContainer) provided by a module, or by programmatically creating your own image on the fly.


1 Answers

If you haven't seen yet: https://github.com/testcontainers/testcontainers-java/issues/3166

github user @bsideup outlined three options. This will of course only apply to you if you are a Mac User using Docker for Mac.

  • use Testcontainers 1.15.0-rc2 that includes the fix
  • Disable gRPC FUSE in Docker for Mac 2.4.0
  • Downgrade to Docker for Mac 2.3.x

This (disable gRPC) solved the issue for me so I leave this as a reference. Not using gRPC FUSE can have significant impact depending on your workflow. So you better check if that is fine or better downgrade to Docker for Mac 2.3.x. I did not pick the first option as it seems it does not stop containers which is the worst of all the options for me.

like image 164
kgalli Avatar answered Sep 29 '22 14:09

kgalli