问题
I am getting an error while running tests on gitlab CI using the command:
./gradlew clean test
I am using test containers to run my tests: https://www.testcontainers.org/modules/docker_compose/
Here is the code I am using to load the docker compose file which is at src/test/resources.
@ClassRule
public static DockerComposeContainer container = new DockerComposeContainer(new File(BaseServiceTest.class.getClassLoader().getResource("docker-compose.yml").getFile()));
It runs fine when I run locally, buy when running ci on gitlab, I get the following error:
{"timestamp":"2019-06-17T18:47:44.903Z","level":"ERROR","thread":"Test worker","logger":"docker[docker/compose:1.8.0]","message":"Log output from the failed container:\n.IOError: [Errno 2] No such file or directory: '/builds/xxxx/xxxx/xxxx-xxxx/src/test/resources/docker-compose.yml'\n","context":"default"}
Following is my gitlab-ci.yml file:
include:
- project: 'xxxx/xxxx/sdlc'
file: '/sdlc.yml'
variables:
CONTAINER_NAME: xxxx-xxxx
test:
stage: test
image: registry.xxxx.com/xxxx-alpine-jdk8:v1_8_181
script:
- ls -la src/test/resources
- ./gradlew clean test -i
In my script, I have ls -la src/test/resources
and I can see the docker-compose.yml
file when that script is run. Not sure why it is not available when running the code.
回答1:
Based on the Testcontainers docs here. DinD service is required for Testcontainers on Gitlab CI
Here an example:
# DinD service is required for Testcontainers
services:
- docker:dind
variables:
# Instruct Testcontainers to use the daemon of DinD.
DOCKER_HOST: "tcp://docker:2375"
# Improve performance with overlayfs.
DOCKER_DRIVER: overlay2
test:
image: gradle:5.0
stage: test
script: ./gradlew test
来源:https://stackoverflow.com/questions/56674613/reading-a-file-from-resources-not-working-in-gitlab-ci