Skip to content

Commit 0d61d53

Browse files
irozzo-1Apoiana
authored andcommitted
chore(libsinsp): add target to build library for sinsp integration tests
Signed-off-by: irozzo-1A <[email protected]>
1 parent eda8b90 commit 0d61d53

File tree

7 files changed

+55
-71
lines changed

7 files changed

+55
-71
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ jobs:
4242
4343
- name: Install a recent version of CMake ⛓️
4444
run: |
45-
curl -L -o /tmp/cmake.tar.gz https://github.com/Kitware/CMake/releases/download/v3.22.5/cmake-3.22.5-linux-$(uname -m).tar.gz
45+
CMAKE_VERSION=3.24.4
46+
curl -L -o /tmp/cmake.tar.gz https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-linux-$(uname -m).tar.gz
4647
gzip -d /tmp/cmake.tar.gz
4748
tar -xpf /tmp/cmake.tar --directory=/tmp
48-
cp -R /tmp/cmake-3.22.5-linux-$(uname -m)/* /usr
49-
rm -rf /tmp/cmake-3.22.5-linux-$(uname -m)/
49+
cp -R /tmp/cmake-${CMAKE_VERSION}-linux-$(uname -m)/* /usr
50+
rm -rf /tmp/cmake-${CMAKE_VERSION}-linux-$(uname -m)/
5051
5152
- name: Checkout Libs ⤵️
5253
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/CMakeLists.txt)
3030
)
3131
endif()
3232

33-
cmake_minimum_required(VERSION 3.12)
33+
cmake_minimum_required(VERSION 3.24)
3434

3535
# Enable MACOSX_RPATH (and keep CMake from complaining).
3636
if(POLICY CMP0042)

cmake/modules/gtest.cmake

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,18 @@ option(USE_BUNDLED_GTEST "Enable building of the bundled gtest" ${USE_BUNDLED_DE
1818
if(GTEST_INCLUDE_DIR)
1919
# we already have gtest
2020
elseif(NOT USE_BUNDLED_GTEST)
21-
find_path(
22-
GTEST_INCLUDE_DIR
23-
PATH_SUFFIXES gtest
24-
NAMES gtest.h
25-
)
26-
find_library(GTEST_LIB NAMES gtest)
27-
find_library(GTEST_MAIN_LIB NAMES gtest_main)
28-
if(GTEST_INCLUDE_DIR
29-
AND GTEST_LIB
30-
AND GTEST_MAIN_LIB
31-
)
32-
message(
33-
STATUS
34-
"Found gtest: include: ${GTEST_INCLUDE_DIR}, lib: ${GTEST_LIB}, main lib: ${GTEST_MAIN_LIB}"
35-
)
36-
else()
37-
message(FATAL_ERROR "Couldn't find system gtest")
38-
endif()
21+
find_package(GTest REQUIRED)
3922
else()
40-
# https://github.com/google/googletest/tree/main/googletest#incorporating-into-an-existing-cmake-project
41-
# Download and unpack googletest at configure time
42-
configure_file(
43-
CMakeListsGtestInclude.cmake ${PROJECT_BINARY_DIR}/googletest-download/CMakeLists.txt
44-
)
45-
execute_process(
46-
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
47-
RESULT_VARIABLE result
48-
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/googletest-download
49-
)
50-
if(result)
51-
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
52-
endif()
53-
execute_process(
54-
COMMAND ${CMAKE_COMMAND} --build .
55-
RESULT_VARIABLE result
56-
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/googletest-download
57-
)
58-
if(result)
59-
message(FATAL_ERROR "Build step for googletest failed: ${result}")
60-
endif()
61-
62-
# Add googletest directly to our build. This defines the gtest and gtest_main targets.
63-
add_subdirectory(
64-
${PROJECT_BINARY_DIR}/googletest-src ${PROJECT_BINARY_DIR}/googletest-build
65-
EXCLUDE_FROM_ALL
66-
)
67-
68-
set(GTEST_INCLUDE_DIR "${gtest_SOURCE_DIR}/include")
69-
set(GTEST_MAIN_LIB "gtest_main")
70-
install(
71-
DIRECTORY "${GTEST_INCLUDE_DIR}"
72-
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBS_PACKAGE_NAME}"
73-
COMPONENT "libs-deps"
23+
include(FetchContent)
24+
FetchContent_Declare(
25+
gtest
26+
URL https://github.com/google/googletest/archive/refs/tags/v1.16.0.tar.gz
27+
URL_HASH SHA256=78c676fc63881529bf97bf9d45948d905a66833fbfa5318ea2cd7478cb98f399
28+
OVERRIDE_FIND_PACKAGE
7429
)
30+
FetchContent_MakeAvailable(gtest)
7531
endif()
7632

7733
if(NOT TARGET gtest)
7834
add_custom_target(gtest)
7935
endif()
80-
81-
include_directories("${GTEST_INCLUDE_DIR}")

test/drivers/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
message(STATUS "Drivers tests build enabled")
22

3+
find_package(GTest REQUIRED)
4+
35
# Configure ia32 action test
46
option(ENABLE_IA32_TESTS "Enable ia32 tests. Require ia32 glibc and gcc." ON)
57
if(ENABLE_IA32_TESTS)
@@ -45,7 +47,7 @@ set(DRIVERS_TEST_SOURCES
4547

4648
set(DRIVERS_TEST_INCLUDE PRIVATE "${GTEST_INCLUDE}" "${LIBS_DIR}/driver/" "${LIBSCAP_INCLUDE_DIRS}")
4749

48-
set(DRIVERS_TEST_LINK_LIBRARIES scap "${GTEST_LIB}" "${GTEST_MAIN_LIB}")
50+
set(DRIVERS_TEST_LINK_LIBRARIES scap GTest::gtest GTest::gtest_main)
4951

5052
set(DRIVERS_TEST_DEPENDECIES scap gtest)
5153

test/libscap/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ set(LIBSCAP_TESTS_INCLUDE
3939
# Needed by gtest
4040
find_package(Threads)
4141

42-
set(LIBSCAP_TESTS_LIBRARIES "${GTEST_LIB}" "${GTEST_MAIN_LIB}" "${CMAKE_THREAD_LIBS_INIT}" scap)
42+
set(LIBSCAP_TESTS_LIBRARIES GTest::gtest GTest::gtest_main scap)
4343

4444
set(LIBSCAP_TESTS_DEPENDENCIES gtest scap)
4545

test/libsinsp_e2e/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
message(STATUS "Libsinsp unit e2e tests build enabled")
1717

18+
find_package(GTest REQUIRED)
1819
if(NOT DEFINED DRIVER_NAME)
1920
set(DRIVER_NAME "scap")
2021
endif()
@@ -57,7 +58,7 @@ else()
5758
add_dependencies(libsinsp_e2e_tests driver)
5859
endif()
5960

60-
target_link_libraries(libsinsp_e2e_tests sinsp "${GTEST_LIB}" "${GTEST_MAIN_LIB}" pthread)
61+
target_link_libraries(libsinsp_e2e_tests sinsp GTest::gtest GTest::gtest_main pthread)
6162

6263
target_include_directories(
6364
libsinsp_e2e_tests PRIVATE ${PROJECT_BINARY_DIR}/driver/src "${CMAKE_CURRENT_BINARY_DIR}"

userspace/libsinsp/test/CMakeLists.txt

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1212
# or implied. See the License for the specific language governing permissions and limitations under
1313
# the License.
14-
#
14+
find_package(GTest REQUIRED)
1515

1616
include(jsoncpp)
1717
if(NOT EMSCRIPTEN)
@@ -30,6 +30,26 @@ configure_file(
3030
"${CMAKE_CURRENT_BINARY_DIR}/libsinsp_test_var.h"
3131
)
3232

33+
file(GLOB_RECURSE TEST_HELPERS CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/helpers/*.cpp)
34+
35+
# sinsp_test_support library to be used to run integration tests in other projects (e.g., plugins)
36+
add_library(sinsp_test_support STATIC sinsp_with_test_input.cpp test_utils.cpp "${TEST_HELPERS}")
37+
38+
target_include_directories(
39+
sinsp_test_support
40+
PUBLIC ${LIBS_DIR} # needed for driver/event_stats.h
41+
${CMAKE_CURRENT_BINARY_DIR} # needed for libsinsp_test_var.h
42+
${CMAKE_CURRENT_SOURCE_DIR} ${UTHASH_INCLUDE} ${JSONCPP_INCLUDE}
43+
)
44+
45+
target_link_libraries(sinsp_test_support PUBLIC sinsp GTest::gtest "${JSONCPP_LIB}")
46+
47+
add_dependencies(sinsp_test_support uthash)
48+
49+
if(NOT EMSCRIPTEN)
50+
target_link_libraries(sinsp_test_support PUBLIC "${TBB_LIB}")
51+
endif()
52+
3353
file(GLOB_RECURSE TEST_PLUGINS CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/plugins/*.cpp)
3454

3555
if(NOT WIN32)
@@ -48,8 +68,11 @@ endif()
4868

4969
option(SCAP_FILES_SUITE_ENABLE "Enable scap-file tests in sinsp" "ON")
5070
# Scap-files on Big Endian systems are not supported
51-
if((NOT ${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "s390x") AND ${SCAP_FILES_SUITE_ENABLE})
52-
# Binary dir in which we will save all our Cmake files
71+
if((NOT ${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "s390x")
72+
AND ${SCAP_FILES_SUITE_ENABLE}
73+
AND TARGET unit-test-libsinsp
74+
)
75+
# Binary dir in which we will save all our scap files
5376
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/scap_files")
5477
# Add here the name for new scap-files
5578
set(SCAP_FILE_NAMES
@@ -66,22 +89,20 @@ if((NOT ${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "s390x") AND ${SCAP_FILES_SUITE_
6689
)
6790
message(STATUS "Download all scap-files from: ${SCAP_FILE_DOWNLOAD_PREFIX}")
6891
foreach(FILE_NAME ${SCAP_FILE_NAMES})
69-
message(STATUS "Downloading scap-file: ${SCAP_FILE_DOWNLOAD_PREFIX}/${FILE_NAME}")
7092
if(NOT EXISTS "${CMAKE_BINARY_DIR}/scap_files/${FILE_NAME}")
93+
message(STATUS "Downloading scap-file: ${FILE_NAME}")
7194
file(DOWNLOAD "${SCAP_FILE_DOWNLOAD_PREFIX}/${FILE_NAME}"
7295
"${CMAKE_BINARY_DIR}/scap_files/${FILE_NAME}" SHOW_PROGRESS
7396
)
7497
else()
75-
message(STATUS "Skipping download, file already present")
98+
message(STATUS "Skipping download, file already present: ${FILE_NAME}")
7699
endif()
77100
endforeach()
78101
file(GLOB_RECURSE SCAP_FILES_SUITE CONFIGURE_DEPENDS
79102
${CMAKE_CURRENT_SOURCE_DIR}/scap_files/*.cpp
80103
)
81104
endif()
82105

83-
file(GLOB_RECURSE TEST_HELPERS CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/helpers/*.cpp)
84-
85106
set(LIBSINSP_UNIT_TESTS_SOURCES
86107
ast_exprs.ut.cpp
87108
test_utils.cpp
@@ -196,9 +217,14 @@ target_include_directories(
196217
${CMAKE_CURRENT_SOURCE_DIR}
197218
)
198219

199-
target_link_libraries(
200-
unit-test-libsinsp sinsp "${GTEST_LIB}" "${GTEST_MAIN_LIB}" "${TBB_LIB}" "${JSONCPP_LIB}"
201-
)
220+
# Link against sinsp and gtest - using both old-style variables and new targets for compatibility
221+
if(NOT EMSCRIPTEN)
222+
target_link_libraries(
223+
unit-test-libsinsp sinsp GTest::gtest GTest::gtest_main "${TBB_LIB}" "${JSONCPP_LIB}"
224+
)
225+
else()
226+
target_link_libraries(unit-test-libsinsp sinsp GTest::gtest GTest::gtest_main "${JSONCPP_LIB}")
227+
endif()
202228

203229
# Add some additional include directories associated with `ADDITIONAL_SINSP_TESTS_SUITE`
204230
# `ADDITIONAL_SINSP_TESTS_INCLUDE_FOLDERS` is a list of include paths `;` separated

0 commit comments

Comments
 (0)