From 3427faabed7b69473d50aef2000c05b881cf64a2 Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Mon, 14 Oct 2024 11:05:50 -0700 Subject: [PATCH] Option to build tests (#727) * Move building tests under option Signed-off-by: Alan Jowett --- .github/workflows/build.yml | 17 +++++++++++++++-- CMakeLists.txt | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e063a9ee5..69579acea 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,6 +18,7 @@ jobs: strategy: matrix: configurations: [Debug, Release] + target: [tests, library] runs-on: ubuntu-latest env: # Configuration type to build. For documentation on how build matrices work, see @@ -37,13 +38,19 @@ jobs: - name: Build run: | mkdir build - cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_CONFIGURATION}} + if [ "${{matrix.target}}" = "library" ]; then + cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_CONFIGURATION}} -DVERIFIER_ENABLE_TESTS=OFF + else + cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_CONFIGURATION}} -DVERIFIER_ENABLE_TESTS=ON + fi cmake --build build -j $(nproc) - name: Run unit tests + if: ${{matrix.target == 'tests'}} run: ./tests -d yes - name: Test for memory leaks + if: ${{matrix.target == 'tests'}} # Any memory leaks will cause the test to fail # This BPF program was chosen because it is the largest one in the repo run: valgrind --leak-check=full --errors-for-leak-kinds=all --show-leak-kinds=all --error-exitcode=1 ./check ebpf-samples/cilium/bpf_xdp_snat_linux_v1.o 2/1 @@ -52,6 +59,7 @@ jobs: strategy: matrix: configurations: [Debug, Release] + target: [tests, library] runs-on: windows-2022 env: # Configuration type to build. For documentation on how build matrices work, see @@ -66,8 +74,13 @@ jobs: - name: Build run: | mkdir build - cmake -B build + if ("${{matrix.target}}" -eq "library") { + cmake -B build -DVERIFIER_ENABLE_TESTS=OFF + } else { + cmake -B build -DVERIFIER_ENABLE_TESTS=ON + } cmake --build build -j $(nproc) --config ${{env.BUILD_CONFIGURATION}} - name: Run unit tests + if: ${{matrix.target == 'tests'}} run: ./${{env.BUILD_CONFIGURATION}}/tests -d yes diff --git a/CMakeLists.txt b/CMakeLists.txt index 244656c03..1f3a497b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,8 @@ project(ebpf_verifier) include(FetchContent) +option(VERIFIER_ENABLE_TESTS "Build tests" ON) + FetchContent_Declare(GSL GIT_REPOSITORY "https://github.com/microsoft/GSL" GIT_TAG "v4.0.0" @@ -13,12 +15,14 @@ FetchContent_Declare(GSL FetchContent_MakeAvailable(GSL) get_target_property(GSL_INCLUDE_DIRS Microsoft.GSL::GSL INTERFACE_INCLUDE_DIRECTORIES) +if (VERIFIER_ENABLE_TESTS) FetchContent_Declare(Catch2 GIT_REPOSITORY "https://github.com/catchorg/Catch2.git" GIT_TAG "v3.7.1" GIT_SHALLOW ON ) FetchContent_MakeAvailable(Catch2) +endif() if (IS_DIRECTORY "${PROJECT_SOURCE_DIR}/.git") # Install Git pre-commit hook @@ -91,6 +95,7 @@ file(GLOB LIB_SRC "./src/linux/linux_platform.cpp" ) +if(VERIFIER_ENABLE_TESTS) file(GLOB ALL_TEST "./src/test/test_conformance.cpp" "./src/test/test_marshal.cpp" @@ -99,6 +104,7 @@ file(GLOB ALL_TEST "./src/test/test_wto.cpp" "./src/test/test_yaml.cpp" ) +endif() if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") @@ -115,13 +121,16 @@ endif () add_library(ebpfverifier ${LIB_SRC}) +if(VERIFIER_ENABLE_TESTS) add_executable(check src/main/check.cpp src/main/linux_verifier.cpp) add_executable(tests ${ALL_TEST}) add_executable(run_yaml src/main/run_yaml.cpp) add_executable(conformance_check src/test/conformance_check.cpp) +endif() target_include_directories(ebpfverifier PRIVATE ${GSL_INCLUDE_DIRS}) +if(VERIFIER_ENABLE_TESTS) set_target_properties(check PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/..") @@ -139,6 +148,7 @@ set_target_properties(conformance_check RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/..") add_dependencies(tests conformance_check) +endif() target_compile_options(ebpfverifier PRIVATE ${COMMON_FLAGS}) target_compile_options(ebpfverifier PUBLIC "$<$:${DEBUG_FLAGS}>") @@ -146,7 +156,9 @@ target_compile_options(ebpfverifier PUBLIC "$<$:${RELEASE_FLAGS} target_compile_options(ebpfverifier PUBLIC "$<$:${SANITIZE_FLAGS}>") add_subdirectory("external/bpf_conformance/external/elfio") +if(VERIFIER_ENABLE_TESTS) add_subdirectory("external/bpf_conformance/src") +endif() add_subdirectory("external/libbtf") # CMake derives a Visual Studio project GUID from the file path but can be overridden via a property @@ -164,6 +176,7 @@ target_link_libraries(ebpfverifier PRIVATE libbtf) target_link_libraries(ebpfverifier PRIVATE Microsoft.GSL::GSL) target_compile_options(ebpfverifier PRIVATE ${COMMON_FLAGS}) +if(VERIFIER_ENABLE_TESTS) target_compile_options(check PRIVATE ${COMMON_FLAGS}) target_compile_options(check PUBLIC "$<$:${DEBUG_FLAGS}>") target_compile_options(check PUBLIC "$<$:${RELEASE_FLAGS}>") @@ -184,3 +197,4 @@ target_link_libraries(tests PRIVATE Catch2::Catch2WithMain) target_link_libraries(run_yaml PRIVATE ebpfverifier) target_link_libraries(conformance_check PRIVATE ebpfverifier) +endif()