From 7b1a07eb597b0ffe0f1d49c39559c5300f7099f2 Mon Sep 17 00:00:00 2001 From: "Kasiewicz, Marek" Date: Fri, 9 Aug 2024 08:23:43 +0000 Subject: [PATCH 1/2] Add skeleton for unit tests Signed-off-by: Kasiewicz, Marek --- CMakeLists.txt | 9 +++++++-- media-proxy/CMakeLists.txt | 8 ++++++-- test.sh | 10 ++++++++++ tests/unit/CMakeLists.txt | 18 ++++++++++++++++++ tests/unit/proxy_context_tests.cc | 10 ++++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100755 test.sh create mode 100644 tests/unit/CMakeLists.txt create mode 100644 tests/unit/proxy_context_tests.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index bd1c06df..4d5b3353 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ set(CMAKE_SKIP_RPATH TRUE) set(MCM_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(MP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/media-proxy) set(SDK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk) +set(TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests/unit) set(SDK_INCLUDE_DIR ${SDK_DIR}/include) # Setup output folder @@ -29,8 +30,12 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +enable_testing() + add_subdirectory(${SDK_DIR}) add_subdirectory(${MP_DIR}) +add_subdirectory(${TESTS_DIR}) + +target_include_directories(media_proxy_lib PUBLIC ${SDK_INCLUDE_DIR}) +target_link_libraries(media_proxy_lib PUBLIC mcm_dp) -target_include_directories(media_proxy PUBLIC ${SDK_INCLUDE_DIR}) -target_link_libraries(media_proxy PUBLIC mcm_dp) diff --git a/media-proxy/CMakeLists.txt b/media-proxy/CMakeLists.txt index b3302111..4f309ec1 100644 --- a/media-proxy/CMakeLists.txt +++ b/media-proxy/CMakeLists.txt @@ -78,8 +78,12 @@ ADD_DEFINITIONS(-DIMTL_CONFIG_PATH=\"${CMAKE_INSTALL_PREFIX}/etc/imtl.json\") # Include generated *.pb.h files include_directories(${CMAKE_CURRENT_BINARY_DIR} ./include) -add_executable(media_proxy ${proto_srcs} ${grpc_srcs} ${proxy_srcs}) -target_link_libraries(media_proxy PRIVATE m ${MTL_LIB} ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF} ${MEMIF_LIB}) +add_library(media_proxy_lib ${proto_srcs} ${grpc_srcs} ${proxy_srcs}) +target_link_libraries(media_proxy_lib PUBLIC m ${MTL_LIB} ${_REFLECTION} + ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF} ${MEMIF_LIB}) + +add_executable(media_proxy ${proxy_srcs}) +target_link_libraries(media_proxy PRIVATE media_proxy_lib) install(TARGETS media_proxy DESTINATION ${CMAKE_INSTALL_PATH} COMPONENT media_proxy) install(FILES imtl.json DESTINATION ${CMAKE_CONFIG_PATH} COMPONENT config) diff --git a/test.sh b/test.sh new file mode 100755 index 00000000..875d202c --- /dev/null +++ b/test.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# SPDX-License-Identifier: BSD-3-Clause +# Copyright 2024 Intel Corporation + +set -eo pipefail +SCRIPT_DIR="$(readlink -f "$(dirname -- "${BASH_SOURCE[0]}")")" + +# Run all tests verbosely +ctest --test-dir ${SCRIPT_DIR}/out --verbose diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt new file mode 100644 index 00000000..d03b3ee6 --- /dev/null +++ b/tests/unit/CMakeLists.txt @@ -0,0 +1,18 @@ +# Find source files for tests +file(GLOB TEST_SOURCES "*.cc") + +set(MP_LIB media_proxy_lib) + +# Add an executable for tests +add_executable(MCM_unit_tests ${TEST_SOURCES}) + +# Link the executable with gtest and gtest_main +target_link_libraries(MCM_unit_tests PRIVATE gtest gtest_main ${MP_LIB}) +target_include_directories(MCM_unit_tests PUBLIC + ${CMAKE_SOURCE_DIR}/media-proxy/include + # Include generated *.pb.h files + ${CMAKE_BINARY_DIR}/media-proxy/ +) + +# Add tests to CTest +add_test(NAME MCM_unit_tests COMMAND MCM_unit_tests) diff --git a/tests/unit/proxy_context_tests.cc b/tests/unit/proxy_context_tests.cc new file mode 100644 index 00000000..8e9089a2 --- /dev/null +++ b/tests/unit/proxy_context_tests.cc @@ -0,0 +1,10 @@ +#include +#include "proxy_context.h" + +// TODO: Create real tests +TEST(ProxyContextTests, ProxyContextConstructor) { + ProxyContext ctx; + EXPECT_EQ(ctx.getTCPListenPort(), 8002); + EXPECT_EQ(ctx.getRPCListenAddress(), "0.0.0.0:8001"); + EXPECT_EQ(ctx.getTCPListenAddress(), "0.0.0.0:8002"); +} From 122b4714b869915c1152d54330cbe964a24b9aeb Mon Sep 17 00:00:00 2001 From: "Kasiewicz, Marek" Date: Wed, 14 Aug 2024 13:09:07 +0000 Subject: [PATCH 2/2] Add option to conditionally build unit tests To disable compilation of unit tests one can invoke "BUILD_UNIT_TESTS=OFF ./build.sh" MCM_unit_tests was renamed to mcm_unit_tests Signed-off-by: Kasiewicz, Marek --- CMakeLists.txt | 10 ++++++---- build.sh | 3 +++ tests/unit/CMakeLists.txt | 8 ++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d5b3353..82dda054 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,12 +30,14 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -enable_testing() - add_subdirectory(${SDK_DIR}) add_subdirectory(${MP_DIR}) -add_subdirectory(${TESTS_DIR}) + +option(BUILD_UNIT_TESTS "Build and enable unit tests" ON) +if (BUILD_UNIT_TESTS) + enable_testing() + add_subdirectory(${TESTS_DIR}) +endif() target_include_directories(media_proxy_lib PUBLIC ${SDK_INCLUDE_DIR}) target_link_libraries(media_proxy_lib PUBLIC mcm_dp) - diff --git a/build.sh b/build.sh index 51de5cac..52191d5b 100755 --- a/build.sh +++ b/build.sh @@ -9,11 +9,14 @@ SCRIPT_DIR="$(readlink -f "$(dirname -- "${BASH_SOURCE[0]}")")" # Set build type. ("Debug" or "Release") BUILD_TYPE="${BUILD_TYPE:-Release}" +# To disable the building of unit tests, set the value to "OFF". +BUILD_UNIT_TESTS="${BUILD_UNIT_TESTS:-ON}" INSTALL_PREFIX="${INSTALL_PREFIX:-/usr/local}" cmake -B "${SCRIPT_DIR}/out" \ -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \ -DCMAKE_INSTALL_PREFIX="${INSTALL_PREFIX}" \ + -DBUILD_UNIT_TESTS="${BUILD_UNIT_TESTS}" \ "${SCRIPT_DIR}" cmake --build "${SCRIPT_DIR}/out" -j diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index d03b3ee6..bc39dc72 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -4,15 +4,15 @@ file(GLOB TEST_SOURCES "*.cc") set(MP_LIB media_proxy_lib) # Add an executable for tests -add_executable(MCM_unit_tests ${TEST_SOURCES}) +add_executable(mcm_unit_tests ${TEST_SOURCES}) # Link the executable with gtest and gtest_main -target_link_libraries(MCM_unit_tests PRIVATE gtest gtest_main ${MP_LIB}) -target_include_directories(MCM_unit_tests PUBLIC +target_link_libraries(mcm_unit_tests PRIVATE gtest gtest_main ${MP_LIB}) +target_include_directories(mcm_unit_tests PUBLIC ${CMAKE_SOURCE_DIR}/media-proxy/include # Include generated *.pb.h files ${CMAKE_BINARY_DIR}/media-proxy/ ) # Add tests to CTest -add_test(NAME MCM_unit_tests COMMAND MCM_unit_tests) +add_test(NAME mcm_unit_tests COMMAND mcm_unit_tests)