Skip to content

Commit

Permalink
Add a new target to generate coverage report without Azure pipeline
Browse files Browse the repository at this point in the history
Signed-off-by: Yuanzhe, Liu <[email protected]>
  • Loading branch information
yuazhe committed Jan 15, 2024
1 parent 2e27b11 commit 4382cd2
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rules/sonic-cpp-coverage.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generate code coverage report for cpp project. Currently support:
# 1. sonic-swss-common
# 2. sonic-sairedis
# 3. sonic-swss

sonic-coverage-tools:
/bin/bash $(BUILD_WORKDIR)/src/sonic-cpp-coverage/install-tools.sh

sonic-swss-common-coverage: sonic-coverage-tools
/bin/bash $(BUILD_WORKDIR)/src/sonic-cpp-coverage/sonic-swss-common.sh

sonic-sairedis-coverage: sonic-coverage-tools
/bin/bash $(BUILD_WORKDIR)/src/sonic-cpp-coverage/sonic-sairedis.sh

sonic-swss-coverage: sonic-coverage-tools
/bin/bash $(BUILD_WORKDIR)/src/sonic-cpp-coverage/sonic-swss.sh
54 changes: 54 additions & 0 deletions src/sonic-cpp-coverage/install-tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
set -ex
cd /sonic/src/sonic-cpp-coverage

# Install gcovr
git clone https://github.com/gcovr/gcovr.git
cd gcovr/
git checkout 5.2
sudo pip3 install setuptools
sudo python3 setup.py install
cd ..
sudo rm -rf gcovr

# Install redis
sudo apt-get install -y redis-server
sudo sed -i 's/notify-keyspace-events ""/notify-keyspace-events AKE/' /etc/redis/redis.conf
sudo sed -ri 's/^# unixsocket/unixsocket/' /etc/redis/redis.conf
sudo sed -ri 's/^unixsocketperm .../unixsocketperm 777/' /etc/redis/redis.conf
sudo sed -ri 's/redis-server.sock/redis.sock/' /etc/redis/redis.conf
sudo service redis-server restart
sudo rm -rf /var/run/sswsyncd
sudo mkdir -m 755 /var/run/sswsyncd

# Install rsyslog
sudo apt-get install -y rsyslog
sudo service rsyslog start

# Install libyang
mkdir dep-libyang
cp /sonic/target/debs/bullseye/libyang-*.deb ./dep-libyang
cp /sonic/target/debs/bullseye/libyang_*.deb ./dep-libyang
sudo dpkg -i $(find ./dep-libyang -name "*.deb")
sudo rm -rf dep-libyang

# Create yang model folder
sudo rm -rf /usr/local/yang-models
sudo mkdir /usr/local/yang-models

# Install Pympler
sudo pip3 install Pympler==0.8

# Install .NET CORE
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo apt-add-repository https://packages.microsoft.com/debian/11/prod
sudo apt-get update
sudo apt-get install -y dotnet-sdk-6.0

# Install report tool
already_installed=`sudo dotnet tool list -g | grep dotnet-reportgenerator-globaltool || true`
if [[ $already_installed == "" ]]; then
sudo dotnet tool install -g dotnet-reportgenerator-globaltool
fi

# Install diff cover tool
sudo pip3 install diff_cover
45 changes: 45 additions & 0 deletions src/sonic-cpp-coverage/sonic-sairedis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Build target to make it auto install dependencies
set -ex
cd /sonic
#rm -f target/debs/bullseye/syncd_1.0.0_amd64.deb
make -f slave.mk target/debs/bullseye/syncd_1.0.0_amd64.deb-install
cd /sonic/src/sonic-sairedis

# Install sonic-swss-common
sudo dpkg -i /sonic/target/debs/bullseye/libswsscommon_1.0.0_amd64.deb
sudo dpkg -i /sonic/target/debs/bullseye/libswsscommon-dev_1.0.0_amd64.deb

# Build target
rm ../*.deb || true
./autogen.sh
extraflags='--enable-code-coverage'
DEB_BUILD_OPTIONS=nocheck DEB_CONFIGURE_EXTRA_FLAGS=$extraflags dpkg-buildpackage -us -uc -b -Psyncd,vs,nopython2 -j$(nproc)
mv ../*.deb .

# Update rsyslog.conf
sudo cp azsyslog.conf /etc/rsyslog.conf
sudo service rsyslog restart

# Generate coverage.xml
sudo setcap "cap_sys_time=eip" syncd/.libs/syncd_tests
sudo setcap "cap_dac_override,cap_ipc_lock,cap_ipc_owner,cap_sys_time=eip" unittest/syncd/.libs/tests
redis-cli FLUSHALL
make check
gcovr --version
find SAI/meta -name "*.gc*" | xargs rm -vf
gcov_dirs=$(find . -path "*.libs*gcda" | xargs dirname | sort -u | cut -c"3-")
for dir in ${gcov_dirs}; do
source_dir=$(dirname $dir)
output_file="coverage-$source_dir.json"
gcovr --exclude-unreachable-branches --json-pretty -o $output_file --object-directory $source_dir $dir
done
gcovr -r ./ -e ".*/SAI/.*" -e ".+/json.hpp" -e "swss/.+" -e ".*/.libs/.*" -e ".*/debian/.*" --exclude-unreachable-branches --json-pretty -o coverage-all.json
gcovr -a "coverage-*.json" -x --xml-pretty -o coverage.xml

# Generate HTML full report
sudo /root/.dotnet/tools/reportgenerator -reports:"./coverage.xml" -targetdir:"htmlcov" -reporttypes:Html
echo "sonic-sairedis full coverage report is at src/sonic-sairedis/htmlcov"

# Generate diff report
diff-cover coverage.xml --html-report report.html --compare-branch=origin/master --fail-under=80
echo "sonic-sairedis diff coverage report is at src/sonic-sairedis/report.html"
42 changes: 42 additions & 0 deletions src/sonic-cpp-coverage/sonic-swss-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Build target to make it auto install dependencies
set -ex
cd /sonic
rm -f target/debs/bullseye/libswsscommon_1.0.0_amd64.deb
make -f slave.mk target/debs/bullseye/libswsscommon_1.0.0_amd64.deb-install
cd /sonic/src/sonic-swss-common

# Clean previous coverage data if any
sudo rm -rf $(find . -name "*.gcda")
sudo rm -rf $(find . -name "*.gcno")
sudo git clean -d -f

# Build target
rm ../*.deb || true
./autogen.sh
make clean || true
fakeroot debian/rules DEB_CONFIGURE_EXTRA_FLAGS='--enable-code-coverage' DEB_BUILD_PROFILES=nopython2 CFLAGS="" CXXFLAGS="--coverage -fprofile-abs-path" LDFLAGS="--coverage -fprofile-abs-path" binary
mv ../*.deb .

# Install target debs
sudo dpkg -i libswsscommon_*.deb
sudo dpkg -i libswsscommon-dev_*.deb
sudo dpkg -i python3-swsscommon_*.deb

# Run tests and collect coverage data
redis-cli FLUSHALL
./tests/tests
redis-cli FLUSHALL
pytest-3 --cov=. --cov-report=xml
[ -f coverage.xml ] && mv coverage.xml tests/coverage.xml
gcovr -r ./ -e ".*/swsscommon_wrap.cpp" -e=tests --exclude-unreachable-branches --exclude-throw-branches --gcov-ignore-parse-errors -x --xml-pretty -o coverage.xml
make -C goext
redis-cli FLUSHALL
make -C goext check

# Generate HTML full report
sudo /root/.dotnet/tools/reportgenerator -reports:"./coverage.xml" -targetdir:"htmlcov" -reporttypes:Html
echo "sonic-swss-common full coverage report is at src/sonic-swss-common/htmlcov"

# Generate diff report
diff-cover coverage.xml --html-report report.html --compare-branch=origin/master --fail-under=80
echo "sonic-swss-common diff coverage report is at src/sonic-swss-common/report.html"
25 changes: 25 additions & 0 deletions src/sonic-cpp-coverage/sonic-swss.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Build target to make it auto install dependencies
cd /sonic
make -f slave.mk target/debs/bullseye/swss_1.0.0_amd64.deb

# Build with gcov flag
cd /sonic/src/sonic-swss
rm ../*.deb || true
export ENABLE_GCOV=y
./autogen.sh
dpkg-buildpackage -us -uc -b -j$(nproc) && cp ../*.deb .

make check
[ -f coverage.xml ] && mv coverage.xml tests/coverage.xml

# Run tests and collect coverage data
gcovr -r ./ -e=tests --exclude-unreachable-branches --exclude-throw-branches --gcov-ignore-parse-errors -x --xml-pretty -o coverage.xml
echo "sonic-swss-common full coverage report is at src/sonic-swss-common/htmlcov"

# # Generate HTML full report
sudo /root/.dotnet/tools/reportgenerator -reports:"./coverage.xml" -targetdir:"htmlcov" -reporttypes:Html
echo "sonic-swss full coverage report is at src/sonic-swss/htmlcov"

# Generate diff report
diff-cover coverage.xml --html-report report.html --compare-branch=origin/master --fail-under=80
echo "sonic-swss diff coverage report is at src/sonic-swss/report.html"

0 comments on commit 4382cd2

Please sign in to comment.