Skip to content

Commit

Permalink
Test service with ctest too
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausKlein committed Aug 7, 2023
1 parent 5d09a5d commit 30faa64
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
48 changes: 18 additions & 30 deletions standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,36 @@ include(../cmake/CPM.cmake)
# TODO(CK): add_compile_definitions(BOOST_ASIO_NO_DEPRECATED)
add_compile_definitions(BOOST_ALL_NO_LIB)
set(Boost_USE_STATIC_LIBS YES)
# XXX set(Boost_DEBUG YES)

# Crow needs Boost 1.74 and does not provide CPM.cmake integration itself, so we have to get Boost
# first
# In the past, Crow needs Boost and does not provide CPM.cmake integration itself, so we have to get
# Boost first (installad on Ubuntu 20.04.6 LTS is v1.71)
find_package(
Boost 1.74
Boost 1.71
COMPONENTS system date_time
REQUIRED QUIET
REQUIRED # XXX for now! CK
QUIET
)
if(NOT Boost_FOUND)
# Use CPM.cmake to get Boost from the official repo if not provided as system lib
message(STATUS "GreeterStandalone: Boost system lib NOT found")
CPMAddPackage(
NAME Boost
GITHUB_REPOSITORY boostorg/boost
GIT_TAG boost-1.74.0
VERSION 1.74.0
)
# Ugly workaround: Boost cmake support is still experimental, the Boost::boost target is not
# provided if downloaded via FetchContent_declare / CPM.cmake. Crow uses it for asio, so we fake
# the Boost:boost target as asio
if(NOT TARGET Boost::boost)
add_library(Boost::boost INTERFACE IMPORTED)
target_link_libraries(Boost::boost INTERFACE Boost::asio)
endif()
# Use CPM.cmake to get Boost from the official repo if not found
message(WARNING "GreeterStandalone: Boost libs NOT found!")
option(BUILD_SHARED_LIBS "Build shared libraries" NO)
CPMAddPackage("gh:ClausKlein/boost-cmake@1.80.0")
else()
message(STATUS "GreeterStandalone: Boost system lib found")
message(STATUS "GreeterStandalone: Boost libs found")
endif()

# add Crow
CPMAddPackage(
NAME Crow
GITHUB_REPOSITORY CrowCpp/Crow
GIT_TAG v1.0+5
VERSION 1.0.0
OPTIONS "CROW_INSTALL ON"
)

CPMAddPackage(
GITHUB_REPOSITORY jarro2783/cxxopts
VERSION 3.1.1
SYSTEM ON # used in case of cmake v3.25
OPTIONS "CXXOPTS_BUILD_EXAMPLES NO" "CXXOPTS_BUILD_TESTS NO" "CXXOPTS_ENABLE_INSTALL YES"
# XXX OPTIONS "CROW_INSTALL ON"
)

# get the Greeter lib
# build the Greeter lib
CPMAddPackage(
NAME Greeter
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..
Expand All @@ -85,13 +71,15 @@ if(CMAKE_DEBUG_POSTFIX)
set_property(TARGET ${PROJECT_NAME} PROPERTY DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
endif()
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
target_link_libraries(${PROJECT_NAME} Greeter::Greeter cxxopts::cxxopts Crow::Crow)
target_link_libraries(${PROJECT_NAME} Greeter::Greeter Crow::Crow)
set_target_properties(${PROJECT_NAME} PROPERTIES UNITY_BUILD ${OPTION_ENABLE_UNITY})

# --- Test it ---

enable_testing()
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME} --help)
add_test(NAME ${PROJECT_NAME} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/service_test.sh
$<TARGET_FILE:${PROJECT_NAME}>
)

# --- Install it ---

Expand Down
35 changes: 35 additions & 0 deletions standalone/service_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# arg $1: service executable

set -e
set -x
set -u

# "pseudo" main function to provide top-down sequence, called from last line
main() {
# start the agent
${1} &
thePID=$!
interface=http://127.0.0.1:3080/hello

trap cleanup EXIT

sleep 1 # delay, be sure the service is ready

curl ${interface}?
curl ${interface}?language=
curl ${interface}?language='bla'
curl ${interface}?language='en'
curl ${interface}?language='de'
curl ${interface}?language='fr'

echo "test done, send SIGHUP"
echo cleanup OK
}

cleanup() {
kill -s HUP %% && wait
echo cleanup OK
}

main "$@"

0 comments on commit 30faa64

Please sign in to comment.