Extracting unit test results from a Docker multi-stage build (.NET Core 2.0)

I am creating a .NET Core 2.0 Web API and I am creating a Docker image. I'm new to Docker, so I apologize if the question was answered.

I have the following Docker file for creating an image. In particular, I run unit tests during the build process, and the results are output to ./test/test_results.xml (in a temporary container created during build, I think). My question is: how do I access these test results after the build is complete?

 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"] 

One approach I took is a comment on the line # COPY --from=build-env /app/test/test_results.xml . . This puts test_results.xml in my final image. Then I can extract these results and remove test_results.xml from the final image using the following powershell script command.

 $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 wonder if there is a cleaner way to do this.

I was hoping there was a way to set the volume during docker build , but it doesn't seem like it will be supported in the official Docker.

Now I look at creating a separate image, only for unit tests.

Not sure if there is a recommended way to achieve what I want.

+5
source share
1 answer

Thanks for your question - I had to solve the same thing.

I added a separate container step based on the build results. Testing and its output are processed there, so they never reach the final container. Therefore, build-env is used to build, and then the intermediate test container is based on this build-env file, and final is based on the run-time container in which the build-env results are copied.

 # ---- 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" 

I added a shell script as the next step, which then marks the image as a test project.

 #!bin/bash id=`docker images --filter "label=test=true" -q` docker tag $id projectname-test:latest 

After that, I basically do what you do using docker cp and extract the file. The difference is that my test results were never in the final image, so I do not touch the final image.

In general, I believe that the correct way to handle tests is likely to create a test image (based on the assembly image) and run it with the volume installed for the test results and run it when testing this unit. Having the proper image / container will also allow you to run integration tests, etc. This article is older, but has more details https://blogs.infosupport.com/build-deploy-test-aspnetcore-docker-linux-tfs2015/

0
source

Source: https://habr.com/ru/post/1272551/


All Articles