简体   繁体   English

如何使用多个 docker 设置 Gitlab CI E2E 测试

[英]How to setup Gitlab CI E2E tests using Multiple dockers

I am a bit lost with the automated testing using Gitlab CI.我对使用 Gitlab CI 的自动化测试有点迷失了。 I hope I can explain my problem so somebody can help me.我希望我能解释我的问题,以便有人可以帮助我。 I'll try to explain the situation first, after which I'll try to ask a question (which is harder than it sounds)我会先试着解释一下情况,然后我会试着问一个问题(这比听起来更难)

Situation情况

Architecture建筑学

  1. React frontend with Jest unit tests and Cypress e2e tests使用 Jest 单元测试和 Cypress e2e 测试反应前端
  2. Django API server 1 including a Postgres database and tests Django API 服务器 1,包括 Postgres 数据库和测试
  3. Django API server 2 with a MongoDB database (which communicates with the other API Django API 服务器 2 与 MongoDB 数据库(与其他 ZDB974238714CA8DE634A7CE1D08

Gitlab Gitlab

For the 2 API's, there is a Docker and a docker-compose file.对于 2 个 API,有一个Docker和一个docker-compose文件。 These work fine and are set up correctly.这些工作正常并且设置正确。

We are using GitLab for the CI/CD, there we have the following stages in this order:我们将 GitLab 用于 CI/CD,我们按此顺序有以下阶段:

  1. build: where dockers for 1, 2 & 3 are build separate and pushed to private-registry build:其中 1、2 和 3 的 docker 单独构建并推送到 private-registry
  2. Test: Where the unit testing and e2e test (should) run测试:单元测试和 e2e 测试(应该)运行的地方
  3. Release: where the docker images are released发布: docker 镜像发布的地方
  4. Deploy: Where the docker images are deployed部署:部署docker 映像的位置

Goal目标

I want to set up the GitLab CI such that it runs the cypress tests.我想设置 GitLab CI 以便它运行柏树测试。 But for this, all build dockers are needed.但为此,需要所有构建 docker。 Currently, I am not able to use all dockers together when performing the end-to-end tests.目前,在执行端到端测试时,我无法同时使用所有 docker。

Problem问题

I don't really get how I would achieve this.我真的不明白我将如何实现这一目标。

  • Can I use the dockers that are built in the build stage for my e2e tests and can somebody give me an example of how this would be achieved?我可以使用构建阶段构建的 docker 进行 e2e 测试吗?有人可以举个例子说明如何实现吗? (By running the build docker containers as a service?) (通过将构建 docker 容器作为服务运行?)
  • Do I need one Docker-compose file including all dockers and databases?我需要一个包含所有 docker 和数据库的 Docker-compose 文件吗?
  • Do I even need a dind ?我什至需要一个dind吗?

I hope somebody can give me some advice on how to achieve this.我希望有人能给我一些关于如何实现这一目标的建议。 An example would be even better but I don't know if somebody would want to do that.一个例子会更好,但我不知道是否有人愿意这样做。

Thanks for taking the time to read!感谢您花时间阅读!

(if needed) Example of the API server 1 (如果需要) API 服务器示例 1

build-api:
  image: docker:19
  stage: build
  services:
    - docker:19-dind
  script:
    cd api
    docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    docker pull $IMAGE_TAG_API:latest || true
    docker build -f ./Dockerfile --cache-from $IMAGE_TAG_API:latest --tag $IMAGE_TAG_API:$CI_COMMIT_SHA .
    docker push $IMAGE_TAG_API:$CI_COMMIT_SHA

test-api:
  image: docker:19
  stage: test
  services:
    - postgres:12.2-alpine
    - docker:19-dind
  variables:
    DB_NAME: project_ci_test
    POSTGRES_HOST_AUTH_METHOD: trust
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker pull $IMAGE_TAG_API:$CI_COMMIT_SHA
    - docker run $IMAGE_TAG_API:$CI_COMMIT_SHA sh -c "python manage.py test"
  after_script:
    - echo "Pytest tests complete"
  coverage: "/TOTAL.+ ([0-9]{1,3}%)/"

release-api-staging:
  image: docker:19
  stage: release
  services:
    - docker:19-dind
  only:
    refs: [ master ]
    changes: [ ".gitlab-ci.yml", "api/**/*" ]
  environment:
    name: staging
  script:
    - docker pull $IMAGE_TAG_API:$CI_COMMIT_SHA
    - docker tag $IMAGE_TAG_API:$CI_COMMIT_SHA $IMAGE_TAG_API:latest
    - docker push $IMAGE_TAG_API:latest

This article might shed some light.这篇文章可能会有所启发。 https://jessie.codes/article/running-cypress-gitlab-ci/ Essentially, you make two docker composers, one for your Cypress test and one for your items that are to be tested. https://jessie.codes/article/running-cypress-gitlab-ci/基本上,您制作了两个 docker 作曲家,一个用于 Cypress 测试,一个用于您要测试的项目。 This gets around the issues with images being able to access node and docker.这解决了图像能够访问节点和 docker 的问题。

The answer is a bit late, but still i'll try to explain the approach briefly.答案有点晚了,但我还是会尝试简要解释一下这种方法。

When e2e test full-stack application you have to either:当 e2e 测试全栈应用程序时,您必须:

  • test against a deployed environment针对已部署的环境进行测试
  • spin-up the environment temporary in the pipeline在管道中临时启动环境

As you noted, you want to spin-up the environment temporary in the pipeline.如您所述,您希望在管道中临时启动环境。 The following steps should be taken:应采取以下步骤:

  1. Deploy all backends as docker images in GitLab's private registry;在 GitLab 的私有注册表中将所有后端部署为 docker 映像;
  2. Mimic your docker-compose.yml services in 1 job in the pipeline;在管道中的 1 个作业中模拟您的docker-compose.yml服务;
  3. connect the dots together.将这些点连接在一起。

Deploy backends as docker images in GitLab private registry在 GitLab 私有注册表中将后端部署为 docker 映像

First you have to publish your docker images in the private registry of GitLab.首先,您必须在 GitLab 的私有注册表中发布您的 docker 映像。 You do this, because you now can reuse those images in another job.您这样做是因为您现在可以在另一项工作中重复使用这些图像。 For this approach you need docker:dind .对于这种方法,您需要docker:dind A simple example job to publish to a private registry on gitlab looks like:发布到 gitlab 上的私有注册表的简单示例作业如下所示:

before_script:
  - echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY

publish:image:docker:
  stage: publish
  image: docker
  services:
    - name: docker:dind
      alias: docker
  variables:
    CI_DOCKER_NAME: ${CI_REGISTRY_IMAGE}/my-docker-image
  script:
    - docker pull $CI_REGISTRY_IMAGE || true
    - docker build --pull --cache-from $CI_REGISTRY_IMAGE --tag $CI_DOCKER_NAME --file Dockerfile .
    - docker push $CI_DOCKER_NAME
  only:
    - master

To see a real-world example, I have an example project that is public available.要查看真实示例,我有一个公开的示例项目

Mimic your docker-compose.yml services in 1 job in the pipeline在管道中的 1 个作业中模拟您的docker-compose.yml服务

Once you dockerized all backends and published the images on private registry, you can start to mimic your docker-compose.yml with a GitLab job.将所有后端 docker 化并在私有注册表上发布图像后,您可以开始使用 GitLab 作业来模仿您的docker-compose.yml A basic example:一个基本的例子:

test:e2e:
   image: ubuntu:20.04
   stage: test
   services:
      - name: postgres:12-alpine
        alias: postgress
      - name: mongo
        alias: mongo
      # my backend image
      - name: registry.gitlab.com/[MY_GROUP]/my-docker-image
        alias: server
   script:
      - curl http://server:3000 # expecting server exposes on port 3000, this should work
      - curl http://mongo:270117 # should work
      - curl http://postgress:5432 # should work!

Run the tests运行测试

Now everything is running in a single job in GitLab, you can simply start your front-end in detached mode and run cypress to test it.现在一切都在 GitLab 中的单个作业中运行,您可以简单地以分离模式启动前端并运行 cypress 来测试它。 Example:例子:

 script:
   - npm run start & # start in detached mode
   - wait-on http://localhost:8080 # see: https://www.npmjs.com/package/wait-on
   - cypress run # make sure cypress is available as well

Conclusion结论

Your docker-compose.yml is not meant to run in a pipeline.您的docker-compose.yml并不意味着在管道中运行。 Mimic it instead using GitLab services .使用GitLab services来模仿它。 Dockerize all backends and store them in GitLab's private registry . Dockerize 所有后端并将它们存储在GitLab 的私有注册表中。 Spin up all services in your pipeline and run your tests.启动管道中的所有服务并运行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 GitLab CI 设置 Selenium E2E 测试? - How to setup Selenium E2E testing with GitLab CI? Gitlab CI:如何使用多个服务器实例配置 cypress e2e 测试? - Gitlab CI: How to configure cypress e2e tests with multiple server instances? GitLab CI 赛普拉斯 E2E 挂在“验证赛普拉斯”上 - GitLab CI Cypress E2E Hangs on “Verifying Cypress” 为什么 e2e 数据库测试在 CI 内失败,而不是在本地? - Why e2e database tests failing within CI but not locally? 如何为使用 Puppeteer 编写的 e2e 测试制作 Dockerfile - How to make Dockerfile for e2e tests written with Puppeteer Dockerized Angular E2E测试通过本地计算机,但是Travis CI中超时 - Dockerized Angular E2E tests pass on local machine, but timeout in Travis CI E2E 测试未在 Jenkins 作业上运行 - E2E tests are not running on Jenkins job 带位桶流水线的量角器e2e测试 - Protractor e2e tests with bitbucket pipelines VueJS,使用 Cypress 将 e2e 测试运行到使用 docker 映像的 Jenkins 管道中 - VueJS, run e2e tests using Cypress into a Jenkins pipeline using a docker image 如何在Docker容器中为Laravel运行和安装Protractor E2E测试? - How to run and install Protractor E2E tests in Docker container for Laravel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM