使用Dockerized postgres进行Spring Boot集成测试

时间:2019-12-17 07:49:20

标签: postgresql spring-boot docker integration-testing

尝试使用dockerzied postgres进行集成测试

12:49:19.647 [main] ERROR org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy - ping failed with configuration Environment variables, system properties and defaults. Resolved: 
    dockerHost=unix:///var/run/docker.sock
    apiVersion='{UNKNOWN_VERSION}'
    registryUrl='https://index.docker.io/v1/'
    registryUsername='aequalis'
    registryPassword='null'
    registryEmail='null'
    dockerConfig='DefaultDockerClientConfig[dockerHost=unix:///var/run/docker.sock,registryUsername=aequalis,registryPassword=<null>,registryEmail=<null>,registryUrl=https://index.docker.io/v1/,dockerConfigPath=/home/aequalis/.docker,sslConfig=<null>,apiVersion={UNKNOWN_VERSION},dockerConfig=<null>]'
 due to org.rnorth.ducttape.TimeoutException: Timeout waiting for result with exception
org.rnorth.ducttape.TimeoutException: Timeout waiting for result with exception
        at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:51)
        at org.testcontainers.dockerclient.DockerClientProviderStrategy.ping(DockerClientProviderStrategy.java:190)
        at org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy.test(EnvironmentAndSystemPropertyClientProviderStrategy.java:42)
        at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$2(DockerClientProviderStrategy.java:113)
        at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:267)
      org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:148)
        at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:105)
        at org.testcontainers.containers.GenericContainer.<init>(GenericContainer.java:142)
        at org.testcontainers.containers.JdbcDatabaseContainer.<init>(JdbcDatabaseContainer.java:45)
        at org.testcontainers.containers.PostgreSQLContainer.<init>(PostgreSQLContainer.java:30)
        at com.lava.configuration.management.activity.AbstractIntegrationTest.<clinit>(AbstractIntegrationTest.java:21)
        at sun.misc.Unsafe.ensureClassInitialized(Native Method)
        at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
        at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:156)
        at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
        at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
        at java.lang.reflect.Field.get(Field.java:393)
        at org.junit.runners.model.FrameworkField.get(FrameworkField.java:73)
        at org.junit.runners.model.TestClass.getAnnotatedFieldValues(TestClass.java:230)
        at org.junit.runners.ParentRunner.classRules(ParentRunner.java:255)
        at org.junit.runners.ParentRunner.withClassRules(ParentRunner.java:244)
        at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:194)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:362)
 org.rnorth.ducttape.unreliables.Unreliables.lambda$retryUntilSuccess$0(Unreliables.java:41)
        at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
        at java.util.concurrent.FutureTask.run(FutureTask.java)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: com.sun.jna.LastErrorException: [13] Permission denied
        at org.testcontainers.shaded.org.scalasbt.ipcsocket.UnixDomainSocketLibrary.connect(Native Method)
        at org.testcontainers.shaded.org.scalasbt.ipcsocket.UnixDomainSocket.<init>(UnixDomainSocket.java:57)
        ... 36 common frames omitted

在连接到dockerized的postgres进行集成测试时抛出上述错误。以下是要连接的配置代码。似乎是docker映像上的权限问题。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = LibraryConfigurationApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = AbstractIntegrationTest.Initializer.class)
public abstract class AbstractIntegrationTest {

    @ClassRule
    public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("kartoza/postgis:12.0")
        .withDatabaseName("integration-tests-db")
        .withUsername("docker")
        .withPassword("docker");

    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());
        }
    }

}

请提供帮助以解决此问题

1 个答案:

答案 0 :(得分:0)

您可以尝试一下,我认为它应该可以解决您的问题,因为它通常是缺少权限的 https://docs.docker.com/engine/install/linux-postinstall/