Skip to content

Commit

Permalink
include unit tests and integration tests in the cmake build
Browse files Browse the repository at this point in the history
Summary: This enable test targets to be built and ran

Reviewed By: lnicco

Differential Revision: D17408942

fbshipit-source-id: 144d223bc3830d07a0420e9569d3166a8646cd9a
  • Loading branch information
Udip Pant authored and facebook-github-bot committed Sep 18, 2019
1 parent e05a440 commit ea14236
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 93 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_subdirectory(katran)

if(BUILD_TESTS)
enable_testing()
include(KatranTest)
endif()

if(DEFINED ENV{CMAKE_BUILD_EXAMPLE_THRIFT})
Expand All @@ -49,7 +50,6 @@ endif()
if(DEFINED ENV{CMAKE_BUILD_EXAMPLE_GRPC})
add_subdirectory(example_grpc)
endif()
message("Building tools/utils for katran")

install(
EXPORT katran-exports
Expand Down
8 changes: 6 additions & 2 deletions build/fbcode_builder/manifests/katran
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ builder = nop
builder = cmake
subdir = .

[cmake.defines]
BUILD_TESTS = OFF
[cmake.defines.test=on]
BUILD_TESTS=ON

[cmake.defines.test=off]
BUILD_TESTS=OFF


[dependencies]
folly
Expand Down
57 changes: 57 additions & 0 deletions cmake/KatranTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

include(CTest)
if(BUILD_TESTS)
find_library(BOOST_SYSTEM libboost_system.a boost_system)
find_package(FOLLY CONFIG REQUIRED)
find_library(GTEST gtest)
find_path(GTEST_INCLUDE_DIR gtest)
find_library(PTHREAD pthread)
include(GoogleTest)
endif()

function(katran_add_test)
if(NOT BUILD_TESTS)
return()
endif()

set(options)
set(one_value_args TARGET WORKING_DIRECTORY PREFIX)
set(multi_value_args SOURCES DEPENDS INCLUDES EXTRA_ARGS)
cmake_parse_arguments(PARSE_ARGV 0 KATRAN_TEST "${options}" "${one_value_args}" "${multi_value_args}")

if(NOT KATRAN_TEST_TARGET)
message(FATAL_ERROR "The TARGET parameter is mandatory.")
endif()

if(NOT KATRAN_TEST_SOURCES)
set(KATRAN_TEST_SOURCES "${KATRAN_TEST_TARGET}.cpp")
endif()

add_executable(${KATRAN_TEST_TARGET}
"${KATRAN_TEST_SOURCES}"
# implementation of 'main()' that also calls folly::init
"${KATRAN_FBCODE_ROOT}/katran/lib/tests/common/TestMain.cpp"
)

target_link_libraries(${KATRAN_TEST_TARGET} PRIVATE
"${KATRAN_TEST_DEPENDS}"
${GTEST}
)

target_include_directories(${KATRAN_TEST_TARGET} PRIVATE
${BPF_INCLUDE_DIRS}
${GTEST_INCLUDE_DIR}
${FOLLY_INCLUDE_DIR}
${KATRAN_INCLUDE_DIR}
${KATRAN_EXTRA_INCLUDE_DIRECTORIES}
${KATRAN_TEST_INCLUDES}
)

gtest_discover_tests("${KATRAN_TEST_TARGET}"
EXTRA_ARGS "${KATRAN_TEST_EXTRA_ARGS}"
WORKING_DIRECTORY "${KATRAN_TEST_WORKING_DIRECTORY}"
TEST_PREFIX ${KATRAN_TEST_PREFIX}
TEST_LIST KATRAN_TEST_CASES)

set_tests_properties(${KATRAN_TEST_CASES} PROPERTIES TIMEOUT 120)
endfunction()
2 changes: 1 addition & 1 deletion katran/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ file(
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
*.h
)
list(FILTER KATRAN_HEADERS_TOINSTALL EXCLUDE REGEX test/)
list(FILTER KATRAN_HEADERS_TOINSTALL EXCLUDE REGEX tests/)
foreach(header ${KATRAN_HEADERS_TOINSTALL})
get_filename_component(header_dir ${header} DIRECTORY)
install(FILES ${header} DESTINATION include/katran/lib/${header_dir})
Expand Down
63 changes: 29 additions & 34 deletions katran/lib/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
if(NOT BUILD_TESTS)
return()
endif()

cmake_minimum_required (VERSION 3.0)
project(libkatran_testing)
find_library(GTEST gtest)

# find libs
find_package(folly CONFIG REQUIRED)
find_library(GTEST gtest)
find_path(GTEST_INCLUDE_DIR gtest)
find_library(BOOST_SYSTEM libboost_system.a boost_system)
include(KatranTest)

add_library(base64_helpers STATIC
Base64Helpers.h
Expand All @@ -25,6 +16,7 @@ target_link_libraries(base64_helpers
"${BOOST_SYSTEM}"
"katranlb"
)

target_include_directories(
base64_helpers PUBLIC
"${GTEST_INCLUDE_DIR}"
Expand Down Expand Up @@ -58,31 +50,34 @@ target_link_libraries(xdptester
bpfadapter
)

katran_add_test(TARGET base64helpers-tests
SOURCES
Base64Test.cpp
DEPENDS
base64_helpers
katranlb
${GTEST}
"glog::glog"
${GFLAGS}
"Folly::folly"
${LIBUNWIND}
INCLUDES
base64_helpers
)

# katran_tester is a 'standalone' integration test
add_executable(katran_tester katran_tester.cpp)

target_link_libraries(katran_tester
"${GFLAGS}"
xdptester
katranlb
katranlb
xdptester
${GFLAGS}
${GTEST}
)

enable_testing()

add_executable(base64helpers-tests Base64Test.cpp)
target_link_libraries(base64helpers-tests
"${GTEST}"
"glog::glog"
"${GFLAGS}"
"Folly::folly"
"${LIBUNWIND}"
base64_helpers
katranlb
)
target_include_directories(
base64helpers-tests PUBLIC
"${GTEST_INCLUDE_DIR}"
target_include_directories(katran_tester PRIVATE
${BPF_INCLUDE_DIRS}
${GTEST_INCLUDE_DIR}
${FOLLY_INCLUDE_DIR}
base64_helpers
${KATRAN_INCLUDE_DIR}
)

add_test(Base64Tests base64helpers-tests)
88 changes: 33 additions & 55 deletions katran/lib/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,65 +1,43 @@
cmake_minimum_required (VERSION 3.9)
project (libkatran_tests)
set(KATRAN_INCLUDE_DIR "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../>")

# find libs
# find libraries
find_library(CHHELPERS chhelpers)
find_path(CHHELPERS_INCLUDE_DIR chhelpers)
find_library(GTEST gtest)
find_path(GTEST_INCLUDE_DIR gtest)
find_package(FOLLY CONFIG REQUIRED)
find_library(PTHREAD pthread)

include(KatranTest)

add_executable(iphelpers-tests IpHelpersTest.cpp)
target_link_libraries(iphelpers-tests
"${GTEST}"
"${PTHREAD}"
"Folly::folly"
iphelpers
)
target_include_directories(
iphelpers-tests PUBLIC
"${GTEST_INCLUDE_DIR}"
${FOLLY_INCLUDE_DIR}
${KATRAN_INCLUDE_DIR}
katran_add_test(TARGET iphelpers-tests
SOURCES
IpHelpersTest.cpp
DEPENDS
iphelpers
${GTEST}
${PTHREAD}
"Folly::folly"
)

add_executable(chhelpers-tests CHHelpersTest.cpp)
target_link_libraries(chhelpers-tests
"${GTEST}"
"${PTHREAD}"
katranlb
)
target_include_directories(
chhelpers-tests PUBLIC
"${GTEST_INCLUDE_DIR}"
${KATRAN_INCLUDE_DIR}
katran_add_test(TARGET chhelpers-tests
SOURCES
CHHelpersTest.cpp
DEPENDS
katranlb
${GTEST}
${PTHREAD}
)

add_executable(libkatran-tests KatranLbTest.cpp)
target_link_libraries(libkatran-tests
"${GTEST}"
"${PTHREAD}"
"Folly::folly"
)
target_include_directories(
libkatran-tests PUBLIC
"${GTEST_INCLUDE_DIR}"
"${FOLLY_INCLUDE_DIR}"
katran_add_test(TARGET libkatran-tests
SOURCES
KatranLbTest.cpp
DEPENDS
katranlb
${GTEST}
${PTHREAD}
"Folly::folly"
)

add_executable(vip-tests VipTest.cpp)
target_link_libraries(vip-tests
"${GTEST}"
"${PTHREAD}"
)
target_include_directories(
vip-tests PUBLIC
"${GTEST_INCLUDE_DIR}"
katran_add_test(TARGET vip-tests
SOURCES
VipTest.cpp
DEPENDS
katranlb
${GTEST}
${PTHREAD}
"Folly::folly"
)

add_test(IpHelpersTests iphelpers-tests)
add_test(CHHelpersTests chhelpers-tests)
add_test(LibKatranTests libkatran-tests)
add_test(VipTests vip-tests)
41 changes: 41 additions & 0 deletions katran/lib/tests/common/TestMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright (C) 2018-present, Facebook, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <folly/init/Init.h>

#include <folly/Portability.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>

/*
* This is the recommended main function for all tests.
* The Makefile links it into all of the test programs so that tests do not need
* to - and indeed should typically not - define their own main() functions
*/
FOLLY_ATTR_WEAK int main(int argc, char** argv);

int main(int argc, char** argv) {
#if FOLLY_HAVE_LIBGFLAGS
// Enable glog logging to stderr by default.
gflags::SetCommandLineOptionWithMode(
"logtostderr", "1", gflags::SET_FLAGS_DEFAULT);
#endif

::testing::InitGoogleTest(&argc, argv);
folly::Init init(&argc, &argv);

return RUN_ALL_TESTS();
}

0 comments on commit ea14236

Please sign in to comment.