From 408f7757a996e83dd76a526e43d63b17653ff8f1 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Tue, 25 Jun 2024 14:53:00 +0200 Subject: [PATCH 1/4] Add: Coverage CMake targets and Codecov upload This adds new targets for measuring unit test coverage that can be enabled with the CMake option ENABLE_COVERAGE. The GitHub Actions workflow now also generates the coverage report and uploads it to Codecov. --- .github/workflows/build-and-test.yml | 11 +++++++++++ .github/workflows/build-docs.yml | 2 ++ CMakeLists.txt | 12 +++++++++++- src/CMakeLists.txt | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 175d4a263..135bc5798 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -48,6 +48,11 @@ jobs: runs-on: ubuntu-latest container: ${{ vars.IMAGE_REGISTRY }}/greenbone/gvmd-build:stable steps: + - name: Install git for Codecov uploader + run: | + apt update + apt install --no-install-recommends -y ca-certificates git + rm -rf /var/lib/apt/lists/* - name: Check out gvmd uses: actions/checkout@v4 - name: Build gvmd @@ -56,3 +61,9 @@ jobs: cmake --build build - name: Configure and run tests run: CTEST_OUTPUT_ON_FAILURE=1 cmake --build build -- tests test + - name: Upload test coverage to Codecov + uses: codecov/codecov-action@v4 + with: + file: build/coverage/coverage.xml + token: ${{ secrets.CODECOV_TOKEN }} + flags: unittests diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml index 6557c2ab6..d4a8bb68d 100644 --- a/.github/workflows/build-docs.yml +++ b/.github/workflows/build-docs.yml @@ -12,6 +12,8 @@ jobs: steps: - name: Run the c lang coverage action uses: greenbone/actions/doc-coverage-clang@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} build-gmp-doc: name: Build GMP documentation diff --git a/CMakeLists.txt b/CMakeLists.txt index 4831fd2ca..5a2beb689 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -259,6 +259,16 @@ configure_file (tools/greenbone-scapdata-sync.in tools/greenbone-scapdata-sync @ configure_file (tools/greenbone-certdata-sync.in tools/greenbone-certdata-sync @ONLY) configure_file (tools/gvm-manage-certs.in tools/gvm-manage-certs @ONLY) +## Code coverage + +OPTION (ENABLE_COVERAGE "Enable support for coverage analysis" OFF) +if (ENABLE_COVERAGE) + set (COVERAGE_FLAGS "--coverage -ftest-coverage -fprofile-arcs") + set (COVERAGE_DIR "${CMAKE_BINARY_DIR}/coverage") + file (MAKE_DIRECTORY ${COVERAGE_DIR}) + message ("-- Code Coverage enabled") +endif (ENABLE_COVERAGE) + ## Testing enable_testing () @@ -280,7 +290,7 @@ set (HARDENING_FLAGS "-Wformat -Wformat-security -D_FORTIFY_SOURCE=2 set (LINKER_HARDENING_FLAGS "-Wl,-z,relro -Wl,-z,now") # To find unused functions, add: -flto -fwhole-program -ffunction-sections -Wl,--gc-sections -Wl,--print-gc-sections -set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -D_BSD_SOURCE -D_ISOC99_SOURCE -D_SVID_SOURCE -D_DEFAULT_SOURCE -D_FILE_OFFSET_BITS=64 -DOPENVASD=${OPENVASD}") +set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -D_BSD_SOURCE -D_ISOC99_SOURCE -D_SVID_SOURCE -D_DEFAULT_SOURCE -D_FILE_OFFSET_BITS=64 -DOPENVASD=${OPENVASD} ${COVERAGE_FLAGS}") set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Werror -Wshadow ${COVERAGE_FLAGS} ${DEBUG_FUNCTION_NAMES_FLAGS}") set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${HARDENING_FLAGS} ${COVERAGE_FLAGS}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c075bdbdb..2f27baf66 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -262,6 +262,21 @@ add_custom_target (tests DEPENDS gmp-tickets-test manage-test manage-sql-test manage-utils-test utils-test) +if (ENABLE_COVERAGE) + add_custom_target (coverage-html + COMMAND gcovr --html-details ${COVERAGE_DIR}/coverage.html + -r ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) + add_custom_target (coverage-xml + COMMAND gcovr --xml ${COVERAGE_DIR}/coverage.xml + -r ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) + add_custom_target (coverage DEPENDS coverage-xml coverage-html) +endif (ENABLE_COVERAGE) + +add_custom_target (clean-coverage + COMMAND find . -name *.gcda -delete -or -name *.gcno -delete + COMMAND rm -f ${COVERAGE_DIR}/*) + + add_executable (gvmd main.c gvmd.c debug_utils.c From 1970e905183d0f7b2feb20712889abd22cacf9e3 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Wed, 26 Jun 2024 09:38:46 +0200 Subject: [PATCH 2/4] Set git safe.directory in unit test CI job --- .github/workflows/build-and-test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 135bc5798..470a23bb2 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -55,6 +55,8 @@ jobs: rm -rf /var/lib/apt/lists/* - name: Check out gvmd uses: actions/checkout@v4 + - name: Set git safe.directory + run: git config --global --add safe.directory '*' - name: Build gvmd run: | cmake -B build -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=1 From 719dfc6d730be92ae35b2bc9106091032f271b55 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Wed, 26 Jun 2024 10:06:54 +0200 Subject: [PATCH 3/4] Remove Codecov badge There are now two very different types of coverage (tests and documentation) and the badge does not appear to have an option for the flag that can differentiate the two. --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 2b48ad1c1..b64e6f90b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ # Greenbone Vulnerability Manager [![GitHub releases](https://img.shields.io/github/release/greenbone/gvmd.svg)](https://github.com/greenbone/gvmd/releases) -[![Code Documentation Coverage](https://img.shields.io/codecov/c/github/greenbone/gvmd.svg?label=Documentation%20Coverage&logo=codecov)](https://codecov.io/gh/greenbone/gvmd) [![Build and Test](https://github.com/greenbone/gvmd/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/greenbone/gvmd/actions/workflows/build-and-test.yml) [![Docker Pulls](https://img.shields.io/docker/pulls/greenbone/gvmd.svg)](https://hub.docker.com/r/greenbone/gvmd/) [![Docker Image Size](https://img.shields.io/docker/image-size/greenbone/gvmd.svg?maxAge=2592000)](https://hub.docker.com/r/greenbone/gvmd/) From 9d48da5a5816010565b118ce393146eedb229ea3 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Wed, 26 Jun 2024 11:47:49 +0200 Subject: [PATCH 4/4] Remove redundant setting of COVERAGE_FLAGS --- CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a2beb689..2c9561cb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -275,10 +275,6 @@ enable_testing () ## Program -if (ENABLE_COVERAGE) - set (COVERAGE_FLAGS "--coverage") -endif (ENABLE_COVERAGE) - if (DEBUG_FUNCTION_NAMES) # The excluded functions are for update_nvti_cache, which fills the log # quickly. Hopefully this internal NVTi cache is removed soon.