Skip to content

Commit

Permalink
Option to build tests (#727)
Browse files Browse the repository at this point in the history
* Move building tests under option

Signed-off-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan-Jowett authored Oct 14, 2024
1 parent a476ecd commit 3427faa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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}/..")
Expand All @@ -139,14 +148,17 @@ 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 "$<$<CONFIG:DEBUG>:${DEBUG_FLAGS}>")
target_compile_options(ebpfverifier PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_FLAGS}>")
target_compile_options(ebpfverifier PUBLIC "$<$<CONFIG:SANITIZE>:${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
Expand All @@ -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 "$<$<CONFIG:DEBUG>:${DEBUG_FLAGS}>")
target_compile_options(check PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_FLAGS}>")
Expand All @@ -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()

0 comments on commit 3427faa

Please sign in to comment.