Tl; dr 我正在使用Docker运行我的Postman / Newman测试,并且在Travis-CI中运行时API测试挂起,而在本地运行时却未挂起。为什么我遇到无限运行的测试?
大家好!我最近开始学习Docker,Travis-CI和Newman的完整堆栈应用程序。我从开发API开始,正在采用TDD方法。因此,我首先要测试我的API。我设置了.travis.yml
文件以下载特定版本的Docker-Compose,然后使用Docker-Compose在名为api-test的容器中运行测试。容器中有一个图像dannydainton/htmlextra
,它是根据官方postman/newman:alpine
图像构建的,如下所示:
language: node_js
node_js:
- "14.3.0"
env:
global:
- DOCKER_COMPOSE_VERSION: 1.26.2
- PGHOST: db
- PGDATABASE: battle_academia
- secure: "xDZHJ9ZVe3WPXr6WetERMjFnTlMowyEoeckzLcRvWyEIe2qbnWrJRo7cIRxA0FsyJ7ao4QLVv4XhOIeqJupwW3nfnljo35WGcuRBLh76CW6JSuTIbpV1dndOpATW+aY3r6GSwpojnN4/yUVS53pvIeIn03PzQWmnbiJ0xfjStrJzYNpSVIHLn0arujDUMyze8+4ptS1qfekOy2KRifG5+viFarUbWUXaUiJfZCn14S4Wy5N/T+ycltNjX/qPAVZYV3fxY1ZyNX7wzJA+oV71MyApp5PgNW2SBlePkeZTnkbI7FW100MUnE4bvy00Jr/aCoWZYTySz86KT+8HSGzy6d+THO8zjOXKJV5Vn93+XWmxtp/yjBsg+dtFlZUWkN99EBkEjjwJc1Oy5zrOQNjsptNGpl1kid5+bAT4XcP4xn7X5pc7QB8ZE3igbfKTM11LABYN1adcIwgGIjUz1eQnFuibtkVM4oqE92JShUF/6gbwGJsWjQGBNBCOBBueYNB86sk0TiAfS08z2VW9L3pcljA2IwdXclw3f1ON6YelBTJmc88EmxI4TS0hRC5KgMCkegW1ndcTZwqIQGFm+NFbe1hKMmqTfgOg5M8OQZBtUkF60Lox09ECg59IrYj+BIa9J303+bo+IMgZ1JVYlL7FA2qc0bE8J/9A1C2wCRjDLLE="
- secure: "F/Ru7QZvA+zWjQ7K7vhA3M2ZrYKXVIlkIF1H7v2dPv/lsc18eWGpOQep4uAjX4IMyLY/6n7uYRLnSlbvOWulVUW8U52zWiQkYFF9OwosuTdIlVTAQGp3B0CAA+RCxMtDQay6fN9H6e2bL3KwjT//VUHd1E6BPu+O1/RyX+0+0KvTmExmMSuioSpDPcI20Mym2vRCgNPb1gfajr5QfWKPJlrPjfyNhDxWMhM94nwTuLYIVZwZPTZ0Ro5D6hhXFVZOFIlHr5VDbbFa+Xo0TIdP/ZudxZ7p3Mn7ncA8seLx2Q5/zH6tJ4DSUpEm67l5IqUrvd9qp0CNCjlTcl3kOJK4qIB1WtLm6oW2rBqDyvthhuprPpqEcs7C9z2604VLybdOmJ0+Y/7uIo6po388avGN4ZwZbWQ1xiiW+Ja8kkHZYEKo4m0AbKdX9pn8otcNO+1xlDtUU7CZey2QA8WrFlfHWqRapIgNfT5tTSTAul3yWAFCRw09PHYELuO7oQCqFZi7zu3HKWknbkzjf+Cz3TfIFTX/3saiqyquhieOPbnGC5xgTmTrA2ShfNxQ6nkDJPU0/qmaCNJt9CwpNS2ArqcK3xYijiNi+SHaKwEsYh0VqiUqSCWn05eYKNAe3MUQDsyKFEkykJW60yEkN7JsvO1WpI53VKmOnZlRHLzJyc5WkZw="
- PGPORT: 5432
services:
- docker
before_install:
- npm rebuild
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
jobs:
include:
- stage: api tests
script:
- docker --version
- docker-compose --version
- >
docker-compose run api-test
newman run battle-academia_placement-exam_api-test.postman-collection.json
-e battle-academia_placement-exam_docker.postman-environment.json
-r htmlextra,cli
而且,我的docker-compose.yml
文件具有 4 个容器:
在本地运行时,我在环境变量中进行了硬编码,但是文件如下:
version: '3.8'
services:
client:
build: ./client
ports:
- "80:80"
depends_on:
- api
api:
build: ./server
environment:
- PGHOST=${PGHOST}
- PGDATABASE=${PGDATABASE}
- PGUSER=${PGUSER}
- PGPASSWORD=${PGPASSWORD}
- PGPORT=${PGPORT}
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:12.3-alpine
restart: always
environment:
- POSTGRES_DB=${PGDATABASE}
- POSTGRES_USER=${PGUSER}
- POSTGRES_PASSWORD=${PGPASSWORD}
ports:
- "5432:5432"
volumes:
- ./server/db/scripts:/docker-entrypoint-initdb.d
api-test:
image: dannydainton/htmlextra
entrypoint: [""]
command: newman run -v
volumes:
- ./server/api/postman-collections:/etc/newman
depends_on:
- api
现在设置已经不可行了,我的问题是,当我剪下.travis.yml
并自己运行命令时,此配置可以在本地运行,但是,将Travis-CI混用会引发一个问题我的第一个测试只是...运行。
我感谢任何人提供的有关此问题的任何建议或见解。预先感谢!
答案 0 :(得分:0)
这个问题并非来自我的预期。调试之后,我认为问题最初来自权限错误,因为我发现/docker-entrypoint-initdb.d
目录在容器启动期间被忽略了。查看Postgres Dockerfile之后,我了解到文件被授予名为postgres
的用户权限。实际的问题源于我愚蠢地将数据库初始化脚本添加到我的.gitignore
中。
编辑 纽曼(Newman)测试也被挂起了,因为它们试图访问不存在的数据库表。