Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TestOnly][Build] Support to collect the test coverage in cobertura format #3012

Draft
wants to merge 43 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
7759c39
[Build] Support to collect the test coverage in cobertura format
xumia Jan 14, 2024
dac5b1a
Undefine SIMPLE_WAY
xumia Jan 14, 2024
3bc949d
Add debug info
xumia Jan 14, 2024
03e0d6b
Add debug option
xumia Jan 14, 2024
69a9a4b
Add debug option
xumia Jan 14, 2024
934bae3
Fix build option issue
xumia Jan 14, 2024
d3e71de
Fix build broken issue
xumia Jan 14, 2024
bb5830d
Fix the unsigned signed compare issue
xumia Jan 14, 2024
0203ee5
Add more test info
xumia Jan 14, 2024
cc30231
Disable debug
xumia Jan 14, 2024
88c69c7
Test
xumia Jan 15, 2024
5032800
Enable the debug option and fix assert issue
xumia Jan 15, 2024
9f3dc37
Fix variable not define issue
xumia Jan 15, 2024
8157dae
Fix not coverage file issue
xumia Jan 15, 2024
be2fd94
Support gcov coverage
xumia Jan 16, 2024
65a0fdb
Support to collect coverage
xumia Jan 16, 2024
60e2912
Stop services
xumia Jan 16, 2024
e2e71ac
Fix symbol not found issue
xumia Jan 16, 2024
ae51a85
Disable debug and remove test files from coverage
xumia Jan 17, 2024
f0fdd47
Change to use the agent with 16 cores to improve performance
xumia Jan 17, 2024
23a6e3b
Change build pool
xumia Jan 17, 2024
60f342c
Change build pool
xumia Jan 17, 2024
016511e
Fix python script issue
xumia Jan 17, 2024
27daae0
Add gcda files into logs
xumia Jan 17, 2024
fa8e825
Fix pipeline issue
xumia Jan 17, 2024
999071c
Fix python syntax issue
xumia Jan 18, 2024
6bd9a87
Merge coverage files
xumia Jan 18, 2024
6733da6
Add coverage.xml
xumia Jan 18, 2024
f7a15c7
Publish coverage.xml
xumia Jan 18, 2024
d8c4028
install lcov 2.0 to improve performance
xumia Jan 18, 2024
83bc032
Copy coverage.xml
xumia Jan 18, 2024
4de99d4
Fix coverage issue
xumia Jan 18, 2024
4f05c22
Fix coverage low issue
xumia Jan 18, 2024
c99ba5b
Fix coverage issue
xumia Jan 18, 2024
5a62bac
Fix coverage issue
xumia Jan 18, 2024
ba69f8d
Fix build issue
xumia Jan 18, 2024
1ed1b99
Update build pool
xumia Jan 18, 2024
9e45e5d
Fix adding test coverage issue
xumia Jan 19, 2024
1301a71
Support to retry the tests
xumia Jan 20, 2024
d69a547
Fix command not found issue
xumia Jan 20, 2024
91090c1
Fix not coverage issue
xumia Jan 21, 2024
854c424
change back to use gcovpreload.c
xumia Jan 21, 2024
dc625e5
Revert "change back to use gcovpreload.c"
xumia Jan 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .artifactignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
**/*
!*.deb
!coverage.info
!coverage.xml
!build.info
9 changes: 7 additions & 2 deletions .azure-pipelines/build-docker-sonic-vs-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,17 @@ jobs:

find $(Build.ArtifactStagingDirectory)/download/sairedis -name '*.deb' -exec cp "{}" .azure-pipelines/docker-sonic-vs/debs \;
cp -v $(Build.ArtifactStagingDirectory)/download/*.deb .azure-pipelines/docker-sonic-vs/debs
if [ -f $(Build.ArtifactStagingDirectory)/download/coverage.info ]; then
cp -v $(Build.ArtifactStagingDirectory)/download/coverage.info $(Build.ArtifactStagingDirectory)/
cp -v $(Build.ArtifactStagingDirectory)/download/coverage.xml $(Build.ArtifactStagingDirectory)/
fi

pushd .azure-pipelines

build_args=""
build_dir=$(grep BUILD_DIR $(Build.ArtifactStagingDirectory)/download/build.info | cut -d= -f2)
build_args="--build-arg build_dir=$build_dir"
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
3 changes: 3 additions & 0 deletions .azure-pipelines/build-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ jobs:
swig4.0 \
libdbus-1-dev \
libteam-dev
sudo pip3 install lcov_cobertura
displayName: "Install dependencies"
- task: DownloadPipelineArtifact@2
inputs:
Expand Down Expand Up @@ -193,11 +194,13 @@ jobs:
cp -r pytest.tgz $(Build.ArtifactStagingDirectory)/
if [ '${{ parameters.archive_gcov }}' == True ]; then
export ENABLE_GCOV=y
echo BUILD_DIR=$(pwd) > build.info
fi
if [ '${{ parameters.asan }}' == True ]; then
export ENABLE_ASAN=y
fi
./autogen.sh
#export DEB_BUILD_OPTIONS="debug nostrip noopt"
dpkg-buildpackage -us -uc -b -j$(nproc) && cp ../*.deb .
displayName: "Compile sonic swss"
- publish: $(System.DefaultWorkingDirectory)/
Expand Down
12 changes: 11 additions & 1 deletion .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
ARG build_dir
ENV BUILD_DIR=$build_dir

COPY ["debs", "/debs"]

Expand All @@ -24,4 +26,12 @@ 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 apt-get -y install software-properties-common libdatetime-perl libcapture-tiny-perl build-essential libcpanel-json-xs-perl git

RUN git clone -b v2.0 --single-branch --depth 1 https://github.com/linux-test-project/lcov && cd lcov && make install

RUN lcov --version

RUN pip3 install lcov_cobertura

RUN if [ -n "$BUILD_DIR" ]; then mkdir -p $BUILD_DIR && tar -xf /tmp/gcov/gcov-source.tar -C $BUILD_DIR; fi
89 changes: 45 additions & 44 deletions .azure-pipelines/test-docker-sonic-vs-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ jobs:
- job:
displayName: vstest
timeoutInMinutes: ${{ parameters.timeout }}
${{ if parameters.archive_gcov }}:
variables:
DIFF_COVER_CHECK_THRESHOLD: 80
DIFF_COVER_ENABLE: 'true'

pool: sonic-common
pool: sonic-common-test

steps:
- script: |
ip a show dev eth0 || true
ls -A1 | xargs -I{} sudo rm -rf {}
displayName: "Clean workspace"
- checkout: self
Expand Down Expand Up @@ -78,6 +83,17 @@ jobs:
path: $(Build.ArtifactStagingDirectory)/download
displayName: "Download sonic buildimage ubuntu20.04 deb packages"

- script: |
set -ex
# Install .NET CORE
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo apt-add-repository https://packages.microsoft.com/ubuntu/20.04/prod
sudo apt-get update
sudo apt-get install -y dotnet-sdk-7.0
sudo dotnet tool install dotnet-coverage --tool-path /usr/bin/
sudo dotnet tool install dotnet-reportgenerator-globaltool --tool-path /usr/bin
displayName: "Install .NET CORE"

- script: |
set -ex
sudo .azure-pipelines/build_and_install_module.sh
Expand All @@ -92,6 +108,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 @@ -106,7 +123,9 @@ jobs:

params=""
if [ '${{ parameters.archive_gcov }}' == True ]; then
params=" ${params} --keeptb "
cp $(Build.ArtifactStagingDirectory)/download/coverage.info ./
cp $(Build.ArtifactStagingDirectory)/download/coverage.xml ./
params=" ${params} --enable-coverage --force-recreate-dvs "
fi
if [ '${{ parameters.asan }}' == True ]; then
params=" ${params} --graceful-stop "
Expand All @@ -115,71 +134,53 @@ jobs:
params=" ${params} --num-ports=${{ parameters.num_ports }} "
fi

all_tests=$(ls test_*.py)
all_tests=$(ls test_*.py | xargs)
all_tests="${all_tests} p4rt"

if [ -n '${{ parameters.run_tests_pattern }}' ]; then
all_tests=" $(ls ${{ parameters.run_tests_pattern }}) "
fi

test_set=()
# Run 20 tests as a set.
for test in ${all_tests}; do
test_set+=("${test}")
if [ ${#test_set[@]} -ge 20 ]; 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)
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
all_tests=" $(ls ${{ parameters.run_tests_pattern }} | xargs) "
fi
# Run the tests in parallel and retry
retry=3
IMAGE_NAME=docker-sonic-vs:$(Build.DefinitionName).$(Build.BuildNumber).asan-${{ parameters.asan }}
echo $all_tests | xargs -n 1 | xargs -P 8 -I TEST_MODULE sudo ./run-tests.py "$IMAGE_NAME" "$params" "TEST_MODULE" 3

rm -rf $(Build.ArtifactStagingDirectory)/download
displayName: "Run vs tests"
continueOnError: ${{ parameters.asan }}

- script: |
set -ex
reportgenerator -reporttypes:Cobertura -reports:tests/*coverage.xml -targetdir:.
mkdir $(Build.ArtifactStagingDirectory)/gcov
cp Cobertura.xml tests/*coverage.xml $(Build.ArtifactStagingDirectory)/gcov/
cp tests/*coverage.info $(Build.ArtifactStagingDirectory)/gcov/
cp tests/*.gcda.tar $(Build.ArtifactStagingDirectory)/gcov/
condition: ${{ parameters.archive_gcov }}
displayName: "Generate coverage.xml"

- task: PublishCodeCoverageResults@1
condition: ${{ parameters.archive_gcov }}
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/Cobertura.xml'
displayName: 'Publish test coverage'

- task: PublishTestResults@2
inputs:
testResultsFiles: '**/*_tr.xml'
testRunTitle: vstest
condition: succeeded()
condition: always()

- script: |
cp -r tests/log $(Build.ArtifactStagingDirectory)/

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))

- publish: $(Build.ArtifactStagingDirectory)/
artifact: ${{ parameters.log_artifact_name }}@$(System.JobAttempt)
displayName: "Publish logs"
Expand Down
4 changes: 3 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ stages:
- template: .azure-pipelines/build-template.yml
parameters:
arch: amd64
pool: sonicbld
sonic_slave: sonic-slave-bullseye
common_lib_artifact_name: common-lib
swss_common_artifact_name: sonic-swss-common
Expand All @@ -55,6 +56,7 @@ stages:
- template: .azure-pipelines/build-template.yml
parameters:
arch: amd64
pool: sonicbld
sonic_slave: sonic-slave-bullseye
common_lib_artifact_name: common-lib
swss_common_artifact_name: sonic-swss-common
Expand Down Expand Up @@ -137,8 +139,8 @@ stages:
asan: true

- stage: Gcov
condition: false
dependsOn: Test
condition: in(dependencies.Test.result, 'Succeeded', 'SucceededWithIssues')
jobs:
- template: .azure-pipelines/gcov.yml
parameters:
Expand Down
28 changes: 14 additions & 14 deletions cfgmgr/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@ macsecmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI) $(
macsecmgrd_LDADD = $(LDFLAGS_ASAN) $(COMMON_LIBS) $(SAIMETA_LIBS)

if GCOV_ENABLED
vlanmgrd_LDADD += -lgcovpreload
teammgrd_LDADD += -lgcovpreload
portmgrd_LDADD += -lgcovpreload
intfmgrd_LDADD+= -lgcovpreload
buffermgrd_LDADD += -lgcovpreload
vrfmgrd_LDADD += -lgcovpreload
nbrmgrd_LDADD += -lgcovpreload
vxlanmgrd_LDADD += -lgcovpreload
sflowmgrd_LDADD += -lgcovpreload
natmgrd_LDADD += -lgcovpreload
coppmgrd_LDADD += -lgcovpreload
tunnelmgrd_LDADD += -lgcovpreload
macsecmgrd_LDADD += -lgcovpreload
fabricmgrd_LDADD += -lgcovpreload
vlanmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
teammgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
portmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
fabricmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
intfmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
buffermgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
vrfmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
nbrmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
vxlanmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
sflowmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
natmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
coppmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
tunnelmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
macsecmgrd_SOURCES += ../gcovpreload/gcovpreload.cpp
endif

if ASAN_ENABLED
Expand Down
8 changes: 5 additions & 3 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ifeq ($(ENABLE_ASAN), y)
endif

ifeq ($(ENABLE_GCOV), y)
configure_opts += --enable-gcov CFLAGS="-g -O0" CXXFLAGS="-g -O0"
configure_opts += --enable-gcov --enable-code-coverage CFLAGS="-g -O0" CXXFLAGS="-g -O0"
endif

override_dh_auto_configure:
Expand All @@ -43,9 +43,11 @@ 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 --exclude "$(shell pwd)/tests/*" --exclude "$(shell pwd)/**/tests/*" --output-file coverage.info
lcov_cobertura coverage.info -o coverage.xml
find ./ -type f -regex '.*\.\(h\|cpp\|gcno\|info\)' | tar -cf debian/swss/tmp/gcov/gcov-source.tar -T -
#sh ./tests/gcov_support.sh collect swss
endif

override_dh_strip:
dh_strip --dbg-package=swss-dbg

2 changes: 1 addition & 1 deletion fdbsyncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fdbsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(COV_CFLAGS) $(CF
fdbsyncd_LDADD = $(LDFLAGS_ASAN) -lnl-3 -lnl-route-3 -lswsscommon $(COV_LDFLAGS)

if GCOV_ENABLED
fdbsyncd_LDADD += -lgcovpreload
fdbsyncd_SOURCES += ../gcovpreload/gcovpreload.cpp
endif

if ASAN_ENABLED
Expand Down
2 changes: 1 addition & 1 deletion fpmsyncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fpmsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_ASAN)
fpmsyncd_LDADD = $(LDFLAGS_ASAN) -lnl-3 -lnl-route-3 -lswsscommon

if GCOV_ENABLED
fpmsyncd_LDADD += -lgcovpreload
fpmsyncd_SOURCES += ../gcovpreload/gcovpreload.cpp
endif

if ASAN_ENABLED
Expand Down
2 changes: 1 addition & 1 deletion gcovpreload/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CXX:=$(shell sh -c 'type $${CXX%% *} >/dev/null 2>/dev/null && echo $(CXX) || ec

DYLIBSUFFIX=so
DYLIBNAME=$(LIBNAME).$(DYLIBSUFFIX)
DYLIB_MAKE_CMD=$(CC) -shared -fpic gcovpreload.c -o ${DYLIBNAME}
DYLIB_MAKE_CMD=$(CC) -shared -fpic gcovpreload.cpp -o ${DYLIBNAME}

all:
$(DYLIB_MAKE_CMD)
Expand Down
12 changes: 6 additions & 6 deletions gcovpreload/gcovpreload.c → gcovpreload/gcovpreload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#include <stdlib.h>
#include <signal.h>
#include <signal.h>
#define SIMPLE_WAY

extern "C" void __gcov_dump();

void sighandler(int signo)
{
#ifdef SIMPLE_WAY
exit(signo);
#else
extern void __gcov_flush();
__gcov_flush(); /* flush out gcov stats data */
__gcov_dump();
raise(signo); /* raise the signal again to crash process */
#endif
}
Expand All @@ -27,15 +27,15 @@ void ctor()
int sigs[] = {
SIGILL, SIGFPE, SIGABRT, SIGBUS,
SIGSEGV, SIGHUP, SIGINT, SIGQUIT,
SIGTERM
SIGTERM, SIGKILL, SIGUSR1
};
int i;
struct sigaction sa;
sa.sa_handler = sighandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;
sa.sa_flags = (int)SA_RESETHAND;

for(i = 0; i < sizeof(sigs)/sizeof(sigs[0]); ++i) {
for(i = 0; i < (int)(sizeof(sigs)/sizeof(sigs[0])); ++i) {
if (sigaction(sigs[i], &sa, NULL) == -1) {
perror("Could not set signal handler");
}
Expand Down
2 changes: 1 addition & 1 deletion mclagsyncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mclagsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_ASAN)
mclagsyncd_LDADD = $(LDFLAGS_ASAN) -lnl-3 -lnl-route-3 -lswsscommon

if GCOV_ENABLED
mclagsyncd_LDADD += -lgcovpreload
mclagsyncd_SOURCES += ../gcovpreload/gcovpreload.cpp
endif

if ASAN_ENABLED
Expand Down
2 changes: 1 addition & 1 deletion natsyncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ natsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_ASAN)
natsyncd_LDADD = $(LDFLAGS_ASAN) -lnl-3 -lnl-route-3 -lnl-nf-3 -lswsscommon

if GCOV_ENABLED
natsyncd_LDADD += -lgcovpreload
natsyncd_SOURCES += ../gcovpreload/gcovpreload.cpp
endif

if ASAN_ENABLED
Expand Down
2 changes: 1 addition & 1 deletion neighsyncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ neighsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_ASAN)
neighsyncd_LDADD = $(LDFLAGS_ASAN) -lnl-3 -lnl-route-3 -lswsscommon

if GCOV_ENABLED
neighsyncd_LDADD += -lgcovpreload
neighsyncd_SOURCES += ../gcovpreload/gcovpreload.cpp
endif

if ASAN_ENABLED
Expand Down