我有一组依赖postgres数据库的集成测试。为了使测试独立,我使用this项目在测试之前启动postgres docker容器:
@Rule
DockerRule postgresDockerRule = DockerRule
.builder()
.imageName("postgres:9")
.expose(databaseConfig.port().toString(), "5432")
.env("POSTGRES_PASSWORD", databaseConfig.password())
.waitForMessage("PostgreSQL init process complete; ready for start up.", 60)
.keepContainer(false)
.build();
这在本地工作正常。规则启动容器,运行测试,测试后,容器被删除。
但是,我在gitlab.com
上运行这些测试时遇到了麻烦。
测试总是失败,出现以下异常(这是较长堆栈跟踪的结束):
Caused by: java.io.IOException: No such file or directory
at jnr.unixsocket.UnixSocketChannel.doConnect(UnixSocketChannel.java:94)
at jnr.unixsocket.UnixSocketChannel.connect(UnixSocketChannel.java:102)
at com.spotify.docker.client.ApacheUnixSocket.connect(ApacheUnixSocket.java:73)
at com.spotify.docker.client.UnixConnectionSocketFactory.connectSocket(UnixConnectionSocketFactory.java:74)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:435)
... 21 more
提供DockerRule
的项目使用spotify docker client连接到docker守护程序的远程API。 (这就是为什么它抛出一个IOException,说明“没有这样的文件或目录” - 它找不到套接字。)
我的.gitlab-ci.yml文件如下所示:
stages:
- build
- deploy
build_rest-api:
image: openjdk:8
stage: build
script:
- ./gradlew clean build -Dorg.gradle.parallel=true
artifacts:
when: always
paths:
- 'rest-api/build/distributions/*.zip'
- '*/build/reports/*'
deploy_on_development:
image: governmentpaas/cf-cli
stage: deploy
before_script:
- cf api ...
- cf auth ...
- cf target -o XXX -s development
script:
- cf push ....
only:
- master
我想要达到的目标是:
我考虑过使用services
的{{1}}部分在CI过程中将postgres数据库作为服务提供。但这意味着我必须手动启动postgres数据库才能在本地运行集成测试。我对junit规则方法的喜欢之处在于,我可以通过在后台运行docker来简单地运行我的集成测试,就像任何其他测试一样。
如果有人可以提出一个允许我在CI过程中连接到docker实例的解决方案,我会很高兴但我也很高兴有关如何更改我的集成测试的整体设置的想法,以便工作