testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
8.02k stars 1.65k forks source link

[Bug]: Error in running test on kube-jenkins #5602

Open XiaosongWen opened 2 years ago

XiaosongWen commented 2 years ago

Module

PostgreSQL

Testcontainers version

1.16.3

Using the latest Testcontainers version?

Yes

Host OS

linux

Host Arch

x86

Docker version

Container Runtime Version:  docker://20.10.7

What happened?

I am using testcontainers for my unit test for repository, it passes locally but failed in the jenkins, and it only have one line log trace:

EntityRepositoryTest > initializationError FAILED
  com.github.dockerjava.api.exception.InternalServerErrorException at DefaultInvocationBuilder.java:247

Here are my code, I retaged and pushed postgres:13.2 to company's private jfrog

public class MyRepositoryPostgresqlContainer extends PostgreSQLContainer<MyRepositoryPostgresqlContainer> {

    static private DockerImageName myImage = DockerImageName.parse("comany./postgres:13.2").asCompatibleSubstituteFor("postgres");

    private static MyRepositoryPostgresqlContainer container;

    private MyRepositoryPostgresqlContainer(DockerImageName myImage) {
        super(myImage);
    }

    public static MyRepositoryPostgresqlContainer getInstance() {
        if (container == null) {
            container = new MyRepositoryPostgresqlContainer(myImage)
                    .withDatabaseName("XXX")
                    .withUsername("XXX")
                    .withPassword("XXX);
        }
        return container;
    }
   ...
}
@ContextConfiguration(initializers = {MyTest.Initializer.class})
public class MyTest {

    @ClassRule
    public static PostgreSQLContainer postgreSQLContainer = MyRepositoryPostgresqlContainer.getInstance();

    static class Initializer
            implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            TestPropertyValues.of(
                    "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
                    "spring.datasource.username=" + postgreSQLContainer.getUsername(),
                    "spring.datasource.password=" + postgreSQLContainer.getPassword()
            ).applyTo(configurableApplicationContext.getEnvironment());
        }
    }
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class EntityRepositoryTest extends MyTest{
    @Autowired
    private MyRepository repo;
   @Test
    public void test() {
         MyEntity myEntity = MyEntity.builder().
                ....
                .build();
       repo.saveAndFlush(myEntity);
        assertEquals(1, repo.findAll().size());
    }
}

Any suggestion helps, Thanks

Relevant log output

EntityRepositoryTest > initializationError FAILED
  com.github.dockerjava.api.exception.InternalServerErrorException at DefaultInvocationBuilder.java:247


### Additional Information

_No response_
kiview commented 2 years ago

Hi @XiaosongWen, please configure logging according to our recommended configuration and share the logs with us: https://www.testcontainers.org/supported_docker_environment/logging_config/

Also, 1.16.3 is not the latest version of Tescontainers, 1.17.3 is.

Your Jenkins build node has access to the Docker daemon?

XiaosongWen commented 2 years ago

Hi Kiview, thank for the reply. I have added the logs configure and updated to version 1.17.3. And from this issue: https://github.com/testcontainers/testcontainers-java/issues/3455 Also setup a ImageNameSubstitutor. I can confirm that from the local log it is using: comany.com/xxx/testcontainers/ryuk:0.3.3 and comany.com/xxx/postgres:13.2. Here is what I got from jekeins:

MyEntityRepositoryAuditTest > initializationError FAILED
17:54:33      java.lang.ExceptionInInitializerError at Unsafe.java:-2
17:54:33          Caused by: org.testcontainers.containers.ContainerFetchException at GenericContainer.java:1371
17:54:33              Caused by: org.testcontainers.containers.ContainerLaunchException at GenericContainer.java:349
17:54:33                  Caused by: org.testcontainers.containers.ContainerFetchException at GenericContainer.java:1371
17:54:33                      Caused by: org.testcontainers.containers.ContainerFetchException at RemoteDockerImage.java:119
17:54:33                          Caused by: com.github.dockerjava.api.exception.InternalServerErrorException at DefaultInvocationBuilder.java:247
17:54:33  
17:54:33  MyEntityTest > shouldBeAbleToCreateEntity FAILED
17:54:33      java.lang.NoClassDefFoundError at MyEntityTest.java:12

And it somehow cause an NoClassDefFoundError, but MyEntityTest has nothing to do with testcontainers. I am wondering if I need to add something to jenkins file, currently it is:

stage('Tests') {
            steps {
                dir("XXXi") {
                    withCredentials([[
                        ...
                    ]]){
                        sh """
                            gradle clean test -Partifactory_user=$USERNAME -Partifactory_password=$PASSWORD
                        """
                    }
                }
            }
        }
kiview commented 2 years ago

Can you please run Gradle with -i, so we see the actual logs of the tests?