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



我为mongodb测试构建了一个docker映像。您可以从zhaoyi0113/mongo-uat中找到。当从这个映像启动一个docker容器时,它将创建几个mongodb实例,这将需要几分钟的启动时间。现在我想通过drone 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用于在mongodb docker映像中运行集成测试用例。

当我运行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实例。我不知道无人机是如何发射码头集装箱的。

集成测试的推荐方法是将服务容器放置在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)。

最新更新