I’m working on a web service project which already has a CICD pipeline that builds and publishes Docker images. Now I’d like to add an API testing stage where the Docker image is spun up and a batch of API tests are performed on the service. Unfortunately I’m yet to find a source that provides an example on how to pull this off. Does anyone know if GitLab offers any way to perform this sort of task (i.e., setup the pipeline to run a container and run API tests over the network) or is this sort of task impossible?
Just to provide some context, here’s the minimum working example of the .gitlab-ci.yml
I’m using to try to get my API tests to work:
# as see on:
# https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
image: docker:stable
stages:
- 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
.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 swg -p 8080:8080 $CONTAINER_TEST_IMAGE
- echo "Sleeping for 30 seconds..."
- sleep 30
- echo "Docker started."
after_script:
- docker stop swg
- echo "Docker stopped."
test foo:
extends: .tests
script:
- docker container ls
- apk add --update nodejs nodejs-npm
- cd tests
- npm install
- npm test