CodeBuild to assess pass/fail from test and coverage report files?

0

We have a CodePipeline/CodeBuild playbook that builds, test and analyse code coverage in Docker build. The test and code coverage result files are exported back out to the CodeBuild host for ingestion and presentation in the CodeBuild web console.

The default scenario seems straightforward enough but how to implement conditional success or failure for the build based on the test or coverage percentage? The buildspec specification seems only concerned about file paths and formats and no parsing of the data reported within.

icelava
asked 3 months ago116 views
2 Answers
0
Accepted Answer

Some good and bad news.

Since CodeBuild does not natively have custom behaviour based on test/coverage results data (from the files), some custom scripting is required.

Install xq (since the result formats - Visual Studio TRX, Cobertura - are XML). After the files are exported out of Docker build, execute scripts during post_build phase to query the results and pass/fail (with exit code) according to conditions.

Failed tests

testfailcount=$(xq xunit.trx -x /TestRun/ResultSummary/Counters/@failed)
echo "$testfailcount tests failed."
if [ $testfailcount -gt 0 ]; then
	exit $testfailcount
fi

Code coverage percentage

coverrequired=90
coverdecimal=$(xq Cobertura.xml -x /coverage/@line-rate)
coverpercent=$(awk -v dec="$coverdecimal" 'BEGIN { printf "%.*f", 0, dec * 100 }')
echo "$coverpercent% code coverage."
if [ $coverpercent -lt $coverrequired ]; then
	echo "$coverrequired% coverage required."
	exit 1
fi

The scripts do their job for their intended scope. HOWEVER because they fail CodeBuild, the report files are not finally ingested for report presentation in the CodeBuild web console. So in order to view the visual reports, CodeBuild has to pass even though it ought to fail.

UPDATE

CodeBuild can ingest report files even on failure. The "problem" was the post_build phase was set to ABORT on failure. Both build and post_build must be defaulted to continue so that they can transition forward to upload_artifacts and finalizing phases where report ingestion happens.

Example buildspec

    build:
        commands:
            - docker buildx build --target export -o type=local,dest=buildreports .
    post_build:
        commands:
            - chmod +x buildscripts/*
            - cd buildreports 
            - ../buildscripts/testfailcount.sh
            - ../buildscripts/coverpercent.sh
reports:
    tests:
        files:
            - xunit.trx
        base-directory: buildreports
        file-format: VISUALSTUDIOTRX
    coverage:
        files:
            - Cobertura.xml
        base-directory: buildreports
        file-format: COBERTURAXML
icelava
answered 3 months ago
EXPERT
reviewed 3 months ago
0
  • His question was about how to make CodeBuild per se plain fail/stop, and not about how to peruse data from test/coverage results to determine if CodeBuild should pass/fail.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions