我正在尝试将API测试添加到Web服务项目的gitlab-cicd管道中。为了运行API测试,我添加了一个管道阶段,其中构建和发布了Web服务的Docker映像,并且在每个测试阶段中拉并运行一个容器。
一旦Docker容器启动,管道就会运行使用frisby实现的API测试,该测试指向一个主机名,我认为这是正在运行的容器的主机。
问题出在这里:连接失败。
在测试中,我正在向我认为是服务主机的主机发送请求,该主机将在gitlab's docs section on how to access services之后出现,例如:
http://registry.gitlab.com__somegroup__someproject__somebranch:8080
不幸的是,发送到该URL的请求失败。
有人知道如何在Pipelien中运行API测试以与Docker容器中运行的服务连接吗?
这是我的.gitlab-ci.yml
的内容:
# as see on:
# https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
image: docker:stable
stages:
- build
- test
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
services:
- docker:dind
before_script:
- docker info
build:
stage: build
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build --pull -t $CONTAINER_TEST_IMAGE .
- docker push $CONTAINER_TEST_IMAGE
.tests:
stage: test
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker pull $CONTAINER_TEST_IMAGE
- docker run -d --name foo -p 80:80 $CONTAINER_TEST_IMAGE
- echo "Sleeping for 10 seconds..."
- sleep 10
- echo "Docker started."
after_script:
- docker stop foo
- echo "Docker stopped."
test foo:
extends: .tests
script:
- docker container ls
- apk add --update nodejs nodejs-npm
- cd tests
- npm install frisby jest
- npm test
- echo "Foo"
这是gitlab管道执行的输出:
$ npm test
> foo@1.0.0 test /builds/somegroup/someproject/tests
> jest
FAIL ./test.js
✕ should be a layers object (185ms)
✕ should be a workspace object (93ms)
● should be a layers object
FetchError: request to http://registry.gitlab.com__somegroup__someproject__somebranch:8080/ failed, reason: getaddrinfo ENOTFOUND registry.gitlab.com__somegroup__someproject__somebranch registry.gitlab.com__somegroup__someproject__somebranch:8080
at ClientRequest.<anonymous> (node_modules/node-fetch/lib/index.js:1444:11)
● should be a workspace object
FetchError: request to http://registry.gitlab.com__somegroup__someproject__somebranch:8080/ failed, reason: getaddrinfo ENOTFOUND registry.gitlab.com__somegroup__someproject__somebranch registry.gitlab.com__somegroup__someproject__somebranch:8080
at ClientRequest.<anonymous> (node_modules/node-fetch/lib/index.js:1444:11)
Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 total
Snapshots: 0 total
Time: 5.393s
Ran all test suites.
npm ERR! Test failed. See above for more details.
Running after script...