Skip to content

Commit

Permalink
[Build] Support to collect the test coverage in cobertura format
Browse files Browse the repository at this point in the history
  • Loading branch information
xumia committed Jan 10, 2024
1 parent 7702b8a commit f5e8e49
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .azure-pipelines/build-docker-sonic-vs-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ jobs:
pushd .azure-pipelines
build_args=""
build_args="--build-arg build_dir=$(pwd)"
if [ '${{ parameters.asan }}' == True ]; then
build_args="--build-arg need_dbg=y"
build_args="$build_args --build-arg need_dbg=y"
fi
docker build $build_args --no-cache -t docker-sonic-vs:$(Build.DefinitionName).$(Build.BuildNumber).asan-${{ parameters.asan }} docker-sonic-vs
Expand Down
8 changes: 8 additions & 0 deletions .azure-pipelines/docker-sonic-vs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM docker-sonic-vs

ARG docker_container_name
ARG need_dbg
ENV build_dir
ENV BUILD_DIR=$build_dir

COPY ["debs", "/debs"]

Expand All @@ -25,3 +27,9 @@ RUN if [ "$need_dbg" = "y" ] ; then dpkg -i /debs/swss-dbg_1.0.0_amd64.deb ; fi
RUN apt-get update

RUN apt-get -y install lcov

RUN pip install gcovr

RUN mkdir -p $build_dir

RUN tar -xf /tmp/gcov/gcov-source.tar -C $build_dir
45 changes: 22 additions & 23 deletions .azure-pipelines/test-docker-sonic-vs-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ jobs:
- job:
displayName: vstest
timeoutInMinutes: ${{ parameters.timeout }}
variables:
DIFF_COVER_CHECK_THRESHOLD: 80
DIFF_COVER_ENABLE: 'true'

pool: sonic-common

Expand Down Expand Up @@ -92,6 +95,7 @@ jobs:
sudo apt-get install -y net-tools bridge-utils vlan
sudo apt-get install -y python3-pip
sudo pip3 install pytest==4.6.2 attrs==19.1.0 exabgp==4.0.10 distro==1.5.0 docker>=4.4.1 redis==3.3.4 flaky==3.7.0
sudo pip3 install lcov_cobertura
displayName: "Install dependencies"
- script: |
Expand All @@ -115,6 +119,8 @@ jobs:
params=" ${params} --num-ports=${{ parameters.num_ports }} "
fi
cp $(Build.ArtifactStagingDirectory)/download/coverage.info ./
all_tests=$(ls test_*.py)
all_tests="${all_tests} p4rt"
Expand All @@ -124,30 +130,28 @@ jobs:
test_set=()
# Run 20 tests as a set.
last_test=${all_tests[-1]}
for test in ${all_tests}; do
test_set+=("${test}")
if [ ${#test_set[@]} -ge 20 ]; then
if [ ${#test_set[@]} -ge 20 ] || [ $test == $last_test ]; then
test_name=$(echo "${test_set[0]}" | cut -d "." -f 1)
echo "${test_set[*]}" | xargs sudo py.test -v --force-flaky --junitxml="${test_name}_tr.xml" $params --imgname=docker-sonic-vs:$(Build.DefinitionName).$(Build.BuildNumber).asan-${{ parameters.asan }}
container_count=$(docker ps -q -a | wc -l)
if [ '${{ parameters.archive_gcov }}' == True ] && [ ${container_count} -gt 0 ]; then
./gcov_support.sh set_environment $(Build.ArtifactStagingDirectory)
for container in $(docker ps | grep docker-sonic-vs | awk '{print $1}'); do
docker exec $container bash -c 'cd $BUILD_DIR; lcov -c --directory . --no-external --output-file /tmp/coverage.info'
docker cp $container:/tmp/coverage.info $container.coverage.info
done
docker stop $(docker ps -q -a)
docker rm $(docker ps -q -a)
fi
test_set=()
fi
done
if [ ${#test_set[@]} -gt 0 ]; then
test_name=$(echo "${test_set[0]}" | cut -d "." -f 1)
echo "${test_set[*]}" | xargs sudo py.test -v $params --force-flaky --junitxml="${test_name}_tr.xml" $params --imgname=docker-sonic-vs:$(Build.DefinitionName).$(Build.BuildNumber).asan-${{ parameters.asan }}
container_count=$(docker ps -q -a | wc -l)
if [ '${{ parameters.archive_gcov }}' == True ] && [ ${container_count} -gt 0 ]; then
./gcov_support.sh set_environment $(Build.ArtifactStagingDirectory)
docker stop $(docker ps -q -a)
docker rm $(docker ps -q -a)
fi
fi
cat *coverage.info > all_coverage.info
lcov_cobertura all_coverage.info -o coverage.xml
ls *coverage.info coverage.xml -l
rm -rf $(Build.ArtifactStagingDirectory)/download
displayName: "Run vs tests"
Expand All @@ -165,20 +169,15 @@ jobs:
if [ '${{ parameters.asan }}' == True ]; then
cp -vr tests/log/*/log/asan $(Build.ArtifactStagingDirectory)/
fi
if [ '${{ parameters.archive_gcov }}' == True ]; then
sudo apt-get install -y lcov
cd $(Build.ArtifactStagingDirectory)/gcov_tmp/
tar -zcvf sonic-gcov.tar.gz sonic-gcov/
rm -rf sonic-gcov
fi
displayName: "Collect logs"
condition: always()
- publish: $(Build.ArtifactStagingDirectory)/gcov_tmp
artifact: ${{ parameters.gcov_artifact_name }}
displayName: "Publish gcov output"
condition: and(succeeded(), eq('${{ parameters.archive_gcov }}', true))
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/coverage.xml'
reportDirectory: '$(System.DefaultWorkingDirectory)/htmlcov/'
displayName: 'Publish test coverage'

- publish: $(Build.ArtifactStagingDirectory)/
artifact: ${{ parameters.log_artifact_name }}@$(System.JobAttempt)
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ stages:
asan: true

- stage: Gcov
condition: false
dependsOn: Test
condition: in(dependencies.Test.result, 'Succeeded', 'SucceededWithIssues')
jobs:
Expand Down
5 changes: 4 additions & 1 deletion debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ override_dh_auto_install:
dh_auto_install --destdir=debian/swss
ifeq ($(ENABLE_GCOV), y)
mkdir -p debian/swss/tmp/gcov
sh ./tests/gcov_support.sh collect swss
lcov -c --directory . --no-external --output-file coverage.info
tar -cf my_archive.tar $$(shell )
find ./ -type f -regex '.*\.\(h\|ccp\|gcno|info\)' | tar -cf debian/swss/tmp/gcov/gcov-source.tar -T -
#sh ./tests/gcov_support.sh collect swss
endif

override_dh_strip:
Expand Down

0 comments on commit f5e8e49

Please sign in to comment.