繁体   English   中英

如何在 Gitlab CI/CD 管道中使用 Docker-Compose 运行 API 端点测试

[英]How to run API endpoints tests with Docker-Compose in Gitlab CI/CD pipeline

我想使用 Gitlab CI/CD 管道和 docker-compose 为我的简单 API 自动化测试过程。 我有我想在构建应用程序容器时运行的测试,问题是在 http://app:80 地址上运行测试之前我不能等待应用程序服务。

项目结构:

project:
-- app
-- tests
-- docker-compose.yml
-- .gitlab-ci.yml

我拥有的:

docker-compose

version: "3.0"

services:
  app:
    build:
      context: ./app
      dockerfile: Dockerfile
    ports:
      - "81:80"
    volumes:
      - ./app:/app/app

  tests:
    build:
      context: ./tests
      dockerfile: Dockerfile

  postgres:
    image: postgres:12-alpine
    ports:
      - "5432"
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASS}
      - POSTGRES_DB=${POSTGRES_DB}
    volumes:
      - ./app/data:/var/lib/postgresql/data

tests/ dir with files:

def test_select():
    url = f"{HOST}/select/"
    response = requests.get(url)

    status_code = response.status_code
    result_len = len(response.json().get("result"))

    assert status_code == 200
    assert result_len != 0

.gitlab-ci.yml

stages:
  - build

build:
  stage: build
  script:
     - sudo mkdir -p /home/app/
     - sudo cp -r $PWD/* /home/app/
     - cd /home/app/
     - docker-compose up -d

最终目标是在 docker-compose 构建完成之前运行测试,如果某些测试失败,那么 docker-compose 将失败,管道也会失败。

这可能吗,如果有其他方法可以解决这个问题,我将不胜感激。

通常您不会从管道内启动docker-compose.yml docker-compose.yml对本地开发很有用,但在管道中您必须使用不同的方法,使用 GitLab 服务: https ://docs.gitlab.com/ee/ci/services/

但是,如果您想从 GitLab 管道进行 E2E 或负载测试您的 API,您可以使用它的服务来公开例如 postgress 数据库:

test:e2e:
   image: ubuntu:20.04
   stage: test
   services:
      - name: postgres:12-alpine
        alias: postgress
   script:
      - curl http://postgress:5432 # should work!

接下来的步骤是在分离模式下启动你的 api。 例如:

script:
   - python my-app.py &
   - sleep 30
   - # your app should be app now and should be exposed on let's say localhost:81 according your specs. You can safely run your API tests here

注意python不会是开箱即用的。 为此,您必须将其安装在管道中或创建在管道中使用的 docker 映像。 我个人总是在 GitLab 管道中使用自定义 docker 镜像来防止 Docker 速率限制。 我有一个个人项目的例子来创建自定义图像并将它们存储在 GitLab 中。

有一些解决方案具有不同的复杂程度:

  1. 为容器的启动添加足够长的等待时间
  2. 将重试逻辑(最好带有退避)添加到在容器内运行的代码中
  3. 依赖一个中间容器,该容器的逻辑负责确保其他依赖项完全可用且正常运行。

不过,我认为您的问题是您只是在depends_on - depends_on中缺少了depends_on声明。 还要确保您的app图像具有正确的EXPOSE声明或将它们添加到撰写文件中。

此外,由于您在 docker 网络中运行测试,因此不需要端口映射。 您可以直接在其暴露的端口上联系该服务。

  app:
    ports:
      - "80"
  tests:
    depends_on: # IMPORTANT! Waits for app to be available
      - app  # makes sure you can talk to app on the network
  # ...

那么你的测试应该能够到达http://app

作为使用公共项目的完整示例:

version: "3"
services:
  app:
    image: strm/helloworld-http
    ports:
      - "80"  # not strictly needed since this image has EXPOSE 80
  tests:
    depends_on:
      - app
    image: curlimages/curl
    command: "curl http://app"

如果你运行docker-compose up你会看到以下输出:

Creating testproj_app_1 ... done
Creating testproj_tests_1 ... done
Attaching to testproj_app_1, testproj_tests_1
app_1    | 172.18.0.3 - - [12/Nov/2021 03:03:46] "GET / HTTP/1.1" 200 -
tests_1  |   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
tests_1  |                                  Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0<html><head><title>HTTP Hello World</title></head><body><h1>Hello from d8
f6894ccd1e</h1></body></html
100   102  100   102    0     0   4706      0 --:--:-- --:--:-- --:--:--  4857
testproj_tests_1 exited with code 0

您也可以选择使用 gitlab 的services:但是,如果您已经使用 docker-compose 在本地进行了工作流测试,那么这不太理想,因为您在本地进行测试的方式与在 GitLab 中的测试方式不同,然后测试方法不是可移植到其他 CI 系统或其他开发人员的本地环境。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM