从多阶段Docker构建(.NET Core 2.0)中提取单元测试结果 [英] Extract unit test results from multi-stage Docker build (.NET Core 2.0)
问题描述
我正在构建.NET Core 2.0 Web API,并且正在创建Docker映像.我对Docker还是很陌生,所以如果之前已经回答过这个问题,我深表歉意.
I am building a .NET Core 2.0 web API and I am creating a Docker image. I am quite new to Docker so apologies if the question has been answered before.
我具有以下用于创建映像的Docker文件.特别是,我在构建过程中运行单元测试,结果输出到./test/test_results.xml
(我想是在构建过程中创建的临时容器中).我的问题是,构建完成后如何访问这些测试结果?
I have the following Docker file for creating the image. In particular, I run the unit tests during the build process and the results are output to ./test/test_results.xml
(in a temporary container created during the build, I guess). My question is, how do I access these test results after the build has finished?
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy main csproj file for DataService
COPY src/DataService.csproj ./src/
RUN dotnet restore ./src/DataService.csproj
# Copy test csproj file for DataService
COPY test/DataService.Tests.csproj ./test/
RUN dotnet restore ./test/DataService.Tests.csproj
# Copy everything else (excluding elements in dockerignore)
COPY . ./
# Run the unit tests
RUN dotnet test --results-directory ./ --logger "trx;LogFileName=test_results.xml" ./test/DataService.Tests.csproj
# Publish the app to the out directory
RUN dotnet publish ./src/DataService.csproj -c Release -o out
# Build the runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
EXPOSE 5001
COPY --from=build-env /app/src/out .
# Copy test results to the final image as well??
# COPY --from=build-env /app/test/test_results.xml .
ENTRYPOINT ["dotnet", "DataService.dll"]
我采用的一种方法是在# COPY --from=build-env /app/test/test_results.xml .
行中进行注释.这将test_results.xml
放入我的最终图像中.然后,我可以提取这些结果,并使用以下powershell脚本从最终图像中删除test_results.xml
.
One approach that I have taken is to comment in the line # COPY --from=build-env /app/test/test_results.xml .
. This puts the test_results.xml
in my final image. I can then extract these results and remove the test_results.xml
from the final image using the following powershell script.
$id=$(docker create dataservice)
docker cp ${id}:app/test_results.xml ./test/test_results.xml
docker start $id
docker exec $id rm -rf /app/test_results.xml
docker commit $id dataservice
docker rm -vf $id
但这看起来很难看,我想知道是否有一种更清洁的方法来做到这一点.
This however seems ugly and I am wondering is there a cleaner way to do it.
我希望有一种方法可以在docker build
期间挂载卷,但是官方Docker似乎不支持这种方式.
I was hoping that there was a way to mount a volume during docker build
but it does not appear that this is going to be supported in the official Docker.
我现在正在考虑创建一个单独的图像,仅用于单元测试.
I am looking now at creating a separate image, solely for the unit tests.
不确定是否有推荐的方式来实现我的目标.
Not sure if there is a recommended way of achieving what I want.
推荐答案
感谢您的问题-我需要解决同样的问题.
Thanks for your question - I needed to solve the same thing.
我根据构建结果添加了一个单独的容器阶段.测试及其输出均在此处进行处理,因此它们永远不会到达最终容器.因此,使用build-env进行构建,然后一个中间测试容器基于该build-env映像,而final基于运行时容器并复制了build-env的结果.
I added a separate container stage based on results of the build. The tests and its output are all handled in there so they never reach the final container. So build-env is used to build and then an intermediate test container is based on that build-env image and final is based on runtime container with results of build-env copied in.
# ---- Test ----
# run tests and capture results for later use. This use the results of the build stage
FROM build AS test
#Use label so we can later obtain this container from the multi-stage build
LABEL test=true
WORKDIR /
#Store test results in a file that we will later extract
RUN dotnet test --results-directory ../../TestResults/ --logger "trx;LogFileName=test_results.xml" "./src/ProjectNameTests/ProjectNameTests.csproj"
下一步,我添加了一个shell脚本,然后将该图像标记为project-test.
I added a shell script as a next step that then tags the image as project-test.
#!bin/bash
id=`docker images --filter "label=test=true" -q`
docker tag $id projectname-test:latest
在那之后,我基本上要做的就是使用docker cp并取出文件.区别在于我的测试结果永远不会出现在最终图像中,因此我不会触摸最终图像.
After that, I basically do what you do which is use docker cp and get the file out. The difference is my test results were never in the final image so I don't touch the final image.
总的来说,我认为处理测试的正确方法可能是创建一个测试映像(基于构建映像),并以已装载的卷来运行以获取测试结果,并在该容器启动时运行单元测试.拥有合适的映像/容器还可以使您运行集成测试等.本文较旧,但详细信息类似
Overall I think the correct way to handle tests is probably create a test image (based on the build image) and run it with a mounted volume for test results and have it run the unit tests when that container starts. Having a proper image/container would also allow you to run integration tests etc. This article is older but details similar https://blogs.infosupport.com/build-deploy-test-aspnetcore-docker-linux-tfs2015/
这篇关于从多阶段Docker构建(.NET Core 2.0)中提取单元测试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!