如何 运行 在无人机管道中的 docker 容器内进行集成测试

How to run integration test inside a docker container in drone pipeline

我为 mongodb 测试构建了一个 docker 图像。您可以从 zhaoyi0113/mongo-uat 找到。当从此映像启动 docker 容器时,它将创建几个 mongodb 实例,这将需要几分钟才能启动。现在我想 运行 通过无人机 CI 在这个容器中集成测试用例。下面是我的 .drone.yml 文件:

pipeline:
  build:
    image: node:latest
    commands:
      - npm install
      - npm test
      - npm run eslint
  integration:
    image: zhaoyi0113/mongo-uat
    commands:
      - npm install
      - npm run integration

这个管道有两个步骤,第一个是 运行 在 nodejs 项目中进行单元测试。第二个integration用于在mongodbdocker镜像中运行集成测试用例。

当我 运行 drone exec 时,它会得到一个错误 failed to connect to mongo instance。我认为这是因为 mongodb 实例需要几分钟才能启动。命令 npm installnpm run integration 应该是 运行 在 mongodb 实例启动后。如何延迟构建命令?

EDIT1

图像 zhaoyi0113/mongo-uat 具有 mongodb 环境。它将创建一些 mongodb 个实例。我可以 运行 这个命令 docker run -d zhaoyi0113/mongo-uat 来启动这个容器,然后我可以附加到这个容器来查看 mongodb 实例。我不确定无人机如何启动 docker 容器。

推荐的集成测试方法是将您的服务容器放在 Yaml [1][2]

的服务部分

因此,为了启动 Mongo 服务容器,我将创建以下 Yaml 文件。 Mongo 服务将在默认端口 127.0.0.1 上启动,并可从您的管道容器访问。

pipeline:
  test:
    image: node
    commands:
      - npm install
      - npm run test
  integration:
    image: node
    commands:
      - npm run integration

services:
  mongo:
    image: mongo:3.0

这是测试 MySQL、Postgres、Mongo 等服务的推荐方法。

[1] http://readme.drone.io/usage/getting-started/#services
[2] http://readme.drone.io/usage/services-guide/

作为 Brads 回答的简短补充:虽然 mongo 服务将 运行 在无人机主机上的 127.0.0.1 上 - 无法从该 IP 节点应用程序中。要访问该服务,您需要引用其服务名称(此处:mongo)。