-1

我有两个命令要在 Dockerfile 中运行。一种用于运行测试和生成日志。第二个用于在执行测试后生成 html 报告。我的 Dockerfile 看起来像这样:

FROM golang:1.13   
ADD . /app

WORKDIR /app

RUN go mod download

RUN go get -u "github.com/ains/go-test-html"

CMD ["make", "test", "$URL=", "$INTEGRATION=", "$TESTTYPE=", "$TAGS="]

CMD ["make", "html", "$HTML="]

我的 docker-compose.yml 看起来像这样:

version: '3'

services:
  tests:
    image: int-testing-framework:latest
    environment:
      - URL=http://localhost:3000/
      - INTEGRATION=kapten
      - TESTTYPE=contract
      - TAGS=quotes bookings getTrip cancelTrip

  html:
    image: int-testing-framework:latest
    command: make html
    environment:
      - HTML=html
    links:
      - tests
    depends_on:
      - tests  

我的日志看起来像这样:

sudo docker-compose up
Creating network "integration_default" with the default driver
Starting integration_tests_1  ... done
Creating integration_html_1 ... done
Attaching to integration_tests_1, integration_html_1
html_1   | Generating HTML report
html_1   | go-test-html logs/[gotest_stdout_file] logs/[gotest_stderr_file] logs/output_file.html
html_1   | Test results written to '/app/logs/output_file.html'
integration_html_1 exited with code 0
tests_1  | Generating HTML report
tests_1  | go-test- logs/[gotest_stdout_file] logs/[gotest_stderr_file] logs/output_file.html
tests_1  | /bin/bash: go-test-: command not found
tests_1  | make: *** [Makefile:14: html] Error 127
integration_tests_1 exited with code 2

它没有完全执行tests:服务。应该有测试日志。关于如何首先执行tests:并生成日志的任何想法。然后生成html报告?

4

1 回答 1

1

为此,您只需要一个容器。让它的主要命令是一个 shell 脚本,它首先运行测试,然后生成 HTML 报告。

#!/bin/sh
make test
RC=$?
make html
exit "$RC"
CMD ["./run_tests_and_report.sh"]

你也可以通过同时调用两个 Makefile 目标来做类似的事情

CMD ["make", "test", "html"]

(尽管如果测试报告非零退出代码,则不会生成报告)。

在您当前的方法中,有两个问题。第一个是一个 Docker 容器只有一个入口点和一个命令,所以你的示例 Dockerfile 有两CMD行,第二个是生效的,两个容器都在运行make html。第二个是 Docker Compose 几乎没有同步选项,特别是没有办法让报告生成等待测试执行完成(除非您以某种方式将其写入容器中的脚本)。

于 2020-01-28T11:05:13.923 回答