我对使用测试容器非常陌生。我的测试失败,出现以下异常。
Running com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest
2020-04-08 14:27:08.441 INFO --- [ main] o.s.t.c.support.AbstractContextLoader
: Could not detect default resource locations for test class
resource found for suffixes {-context.xml, Context.groovy}.
2020-04-08 14:27:08.449 INFO --- [ main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest]: DemoControllerTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2020-04-08 14:27:08.611 INFO --- [ main] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration com.mastercard.example.testcontainers.testcontainersexampple.TestContainersExampleApplication for test class com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest
2020-04-08 14:27:08.701 INFO --- [ main] .b.t.c.SpringBootTestContextBootstrapper : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2020-04-08 14:27:08.725 INFO --- [ main] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@117159c0, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3e27ba32, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@7ef82753, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@3b0fe47a, org.springframework.test.context.support.DirtiesContextTestExecutionListener@202b0582, org.springframework.test.context.transaction.TransactionalTestExecutionListener@235ecd9f, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@1ca3b418, org.springframework.test.context.event.EventPublishingTestExecutionListener@58cbafc2, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@2034b64c, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@75d3a5e0, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@74d1dc36, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@7161d8d1, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@74e28667]
2020-04-08 14:27:08.781 ERROR --- [ main] o.t.d.DockerClientProviderStrategy : Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2020-04-08 14:27:08.782 ERROR --- [ main] o.t.d.DockerClientProviderStrategy : UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (ping failed). Root cause NoSuchFileException (/var/run/docker.sock)
2020-04-08 14:27:08.782 ERROR --- [ main] o.t.d.DockerClientProviderStrategy : As no valid configuration was found, execution cannot continue
我的问题是我是否需要在本地安装 docker 才能使用 testcontainers?如果是,那么像 Jenkins 这样的管道是如何工作的?
Caused by: java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration
at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$3(DockerClientProviderStrategy.java:158)
at java.util.Optional.orElseThrow(Optional.java:290)
at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:150)
at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:111)
at org.testcontainers.containers.GenericContainer.<init>(GenericContainer.java:175)
at org.testcontainers.containers.JdbcDatabaseContainer.<init>(JdbcDatabaseContainer.java:36)
at org.testcontainers.containers.PostgreSQLContainer.<init>(PostgreSQLContainer.java:32)
at com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest.<clinit>(DemoControllerTest.java:27)
... 25 more
更新 我已经在本地安装了 docker(尝试一下),但由于无法下载图像,我仍然无法运行测试用例。我的公司网络可能是一个问题。对于上述情况,有人有好的解决办法吗?
Caused by: com.github.dockerjava.api.exception.DockerClientException: Could not pull image: unexpected EOF
at com.github.dockerjava.core.command.PullImageResultCallback.checkDockerClientPullSuccessful(PullImageResultCallback.java:96)
at com.github.dockerjava.core.command.PullImageResultCallback.throwFirstError(PullImageResultCallback.java:111)
at com.github.dockerjava.core.async.ResultCallbackTemplate.awaitCompletion(ResultCallbackTemplate.java:91)
at com.github.dockerjava.core.command.PullImageResultCallback.awaitSuccess(PullImageResultCallback.java:124)
如果您使用的是 MAC,就我而言,为了解决问题,我必须添加一个链接。
按照以下命令操作:
sudo ln -s $HOME/.docker/run/docker.sock /var/run/docker.sock
就我而言,此错误是由 root 权限引起的。如果您使用 ubuntu,docker 需要创建容器的权限,而 testcontainers 无法在运行时向您请求此权限。所以你需要让 docker 可以在没有 sudo 的情况下运行。 尝试以下步骤创建 docker 组并添加连接的用户:
$ sudo groupadd docker
$ sudo gpasswd -a $USER docker
$ sudo service docker restart
如果您使用的是Ubuntu 14.04-15.10
,请在步骤 3 中尝试此操作:
$ sudo service docker.io restart
完成所有步骤后,在终端中尝试一些 docker 命令,而不使用 sudo,例如:
$ docker ps
如果您遇到权限错误,请重新启动计算机,然后必须修复此问题。
所以如果:
link:将我的用户添加到docker用户组
DOCKER_HOST
或文件
<user home>/.testcontainers.properties
自动检测如何连接到 docker
在我的例子中(WSL2 上的 Win10 + Docker 桌面),我必须在 tcp://localhost:2375 上公开 docker 守护进程,而不使用 TLS,然后:1)设置环境变量及其值(在 IDE 中)或系统)到 DOCKER_HOST=tcp://localhost:2375
或 2)创建文件
<user home>/.testcontainers.properties
并添加以下行:
docker.host=tcp\://localhost\:2375
# This second line might be not completely necessary:
docker.client.strategy=org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy
顺便说一句,如果您公开守护进程,最好使用 Windows 防火墙限制从计算机外部对其的访问(这不是讽刺;-))
docker.sock
不在
/var/run/docker.sock
中。所以我必须使用
docker.sock
找到
ss -x -a | grep docker.sock
:
u_str LISTEN 0 4096 /home/xxx/.docker/desktop/docker.sock 37055 * 0
u_str ESTAB 0 0 /home/xxx/.docker/desktop/docker.sock 5890598 * 5891857
u_str ESTAB 0 0 /home/xxx/.docker/desktop/docker.sock 44658 * 49293
我发现该文件位于$HOME/.docker/desktop/docker.sock
。所以为了让它工作,我必须运行
sudo ln -s $HOME/.docker/desktop/docker.sock /var/run/docker.sock
可以启用“首选项”->“高级”->“启用默认 Docker 套接字(需要密码)”
sudo ln -s ~/.rd/docker.sock /var/run/docker.sock