Skip to content

Commit cd495e3

Browse files
authored
Fix integration tests (#1836)
Adds back some stuff that was removed when removing unit tests, but are still needed for integration tests which do not run as PR checks.
1 parent b534183 commit cd495e3

File tree

10 files changed

+411
-13
lines changed

10 files changed

+411
-13
lines changed

CMakeLists.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ set(3RDPARTY_DIR "${MODULES_DIR}/3rdparty" CACHE INTERNAL "3rdparty libraries ro
3232
include( "demos/logging-stack/logging.cmake" )
3333

3434
# Configure options to always show in CMake GUI.
35+
option( BUILD_TESTS
36+
"Set this to ON to build test executables."
37+
OFF )
3538
option( BUILD_DEMOS
3639
"Set this to ON to build demo executables."
3740
ON )
@@ -76,6 +79,12 @@ if(DEFINED ENV{PWD})
7679
endif()
7780
endif()
7881

82+
# Build the tests if flag enabled.
83+
if(BUILD_TESTS)
84+
enable_testing()
85+
include(${ROOT_DIR}/tools/cmock/cmock_dependencies.cmake)
86+
endif()
87+
7988
include(GNUInstallDirs)
8089

8190
# Try installing to /opt if the directory exists, or use the home directory otherwise.
@@ -119,12 +128,22 @@ include( tools/cmake/utility.cmake )
119128
# Make THING_NAME an alias for CLIENT_IDENTIFIER
120129
set_alias( "CLIENT_IDENTIFIER" ALIASES "THING_NAME" )
121130

131+
if(BUILD_TESTS)
132+
# Add build configuration for integration tests.
133+
add_subdirectory( integration-test )
134+
endif()
122135
if(BUILD_DEMOS)
123136
# Add build configuration for demos.
124137
add_subdirectory( demos )
125138
endif()
126139

127140
if(DOWNLOAD_CERTS)
141+
if(BUILD_TESTS)
142+
set(CERT_DOWNLOAD_DIR ${DEMOS_DIR}/certificates)
143+
set(CERT_DOWNLOAD_DIR ${SYSTEM_TEST_DIR}/certificates)
144+
else()
145+
set(CERT_DOWNLOAD_DIR ${DEMOS_DIR}/certificates)
146+
endif()
128147
set(CERT_DOWNLOAD_DIR ${DEMOS_DIR}/certificates)
129148
file(MAKE_DIRECTORY ${CERT_DOWNLOAD_DIR})
130149
# Download the Amazon Root CA certificate.
@@ -135,5 +154,9 @@ if(DOWNLOAD_CERTS)
135154
)
136155
# Copy certificates to the build directory.
137156
file(COPY "${CERT_DOWNLOAD_DIR}"
138-
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
157+
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
158+
if(BUILD_TESTS)
159+
file(COPY "${CERT_DOWNLOAD_DIR}"
160+
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests)
161+
endif()
139162
endif()

platform/posix/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,5 @@ if(INSTALL_PLATFORM_ABSTRACTIONS)
1818
)
1919
endif()
2020

21-
if(BUILD_TESTS)
22-
add_subdirectory(utest)
23-
endif()
24-
2521
# Add the transport targets
2622
add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/transport )

platform/posix/ota_pal/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@ target_include_directories( ota_pal
1313
target_link_libraries( ota_pal
1414
INTERFACE ${OPENSSL_CRYPTO_LIBRARY}
1515
)
16-
17-
if(${BUILD_TESTS})
18-
add_subdirectory(utest)
19-
endif()

platform/posix/transport/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,3 @@ if(INSTALL_PLATFORM_ABSTRACTIONS)
8686
LIBRARY DESTINATION "${CSDK_LIB_INSTALL_PATH}"
8787
ARCHIVE DESTINATION "${CSDK_LIB_INSTALL_PATH}")
8888
endif()
89-
90-
if( BUILD_TESTS )
91-
add_subdirectory( utest )
92-
endif()

tools/cmock/cmock_dependencies.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Check if the CMock source directory exists.
2+
if( NOT EXISTS ${3RDPARTY_DIR}/CMock/src )
3+
# Attempt to clone CMock.
4+
if( ${BUILD_CLONE_SUBMODULES} )
5+
find_package( Git REQUIRED )
6+
7+
message( "Cloning submodule CMock." )
8+
execute_process( COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive libraries/3rdparty/CMock
9+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
10+
RESULT_VARIABLE CMOCK_CLONE_RESULT )
11+
12+
if( NOT ${CMOCK_CLONE_RESULT} STREQUAL "0" )
13+
message( FATAL_ERROR "Failed to clone CMock submodule." )
14+
endif()
15+
else()
16+
message( FATAL_ERROR "The required submodule CMock does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." )
17+
endif()
18+
endif()
19+
20+
include("${ROOT_DIR}/tools/cmock/create_test.cmake")
21+
22+
include_directories("${3RDPARTY_DIR}/CMock/vendor/unity/src/"
23+
"${3RDPARTY_DIR}/CMock/vendor/unity/extras/fixture/src"
24+
"${3RDPARTY_DIR}/CMock/vendor/unity/extras/memory/src"
25+
"${3RDPARTY_DIR}/CMock/src"
26+
)
27+
link_directories("${CMAKE_BINARY_DIR}/lib"
28+
)

tools/cmock/coverage.cmake

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Taken from amazon-freertos repository
2+
cmake_minimum_required(VERSION 3.2.0)
3+
set(BINARY_DIR ${CMAKE_BINARY_DIR})
4+
# reset coverage counters
5+
execute_process(
6+
COMMAND lcov --directory ${CMAKE_BINARY_DIR}
7+
--base-directory ${CMAKE_BINARY_DIR}
8+
--zerocounters
9+
10+
COMMAND mkdir -p ${CMAKE_BINARY_DIR}/coverage
11+
)
12+
# make the initial/baseline capture a zeroed out files
13+
execute_process( COMMAND lcov --directory ${CMAKE_BINARY_DIR}
14+
--base-directory ${CMAKE_BINARY_DIR}
15+
--initial
16+
--capture
17+
--rc lcov_branch_coverage=1
18+
--rc genhtml_branch_coverage=1
19+
--output-file=${CMAKE_BINARY_DIR}/base_coverage.info
20+
)
21+
file(GLOB files "${CMAKE_BINARY_DIR}/bin/tests/*")
22+
23+
set(REPORT_FILE ${CMAKE_BINARY_DIR}/utest_report.txt)
24+
file(WRITE ${REPORT_FILE} "")
25+
# execute all files in bin directory, gathering the output to show it in CI
26+
foreach(testname ${files})
27+
get_filename_component(test
28+
${testname}
29+
NAME_WE
30+
)
31+
message("Running ${testname}")
32+
execute_process(COMMAND ${testname} OUTPUT_FILE ${CMAKE_BINARY_DIR}/${test}_out.txt)
33+
34+
file(READ ${CMAKE_BINARY_DIR}/${test}_out.txt CONTENTS)
35+
file(APPEND ${REPORT_FILE} "${CONTENTS}")
36+
endforeach()
37+
38+
# generate Junit style xml output
39+
execute_process(COMMAND ruby
40+
${CMAKE_SOURCE_DIR}/../libraries/3rdparty/CMock/vendor/unity/auto/parse_output.rb
41+
-xml ${REPORT_FILE}
42+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
43+
)
44+
45+
# capture data after running the tests
46+
execute_process(
47+
COMMAND lcov --capture
48+
--rc lcov_branch_coverage=1
49+
--rc genhtml_branch_coverage=1
50+
--base-directory ${CMAKE_BINARY_DIR}
51+
--directory ${CMAKE_BINARY_DIR}
52+
--output-file ${CMAKE_BINARY_DIR}/second_coverage.info
53+
)
54+
55+
# combile baseline results (zeros) with the one after running the tests
56+
execute_process(
57+
COMMAND lcov --base-directory ${CMAKE_BINARY_DIR}
58+
--directory ${CMAKE_BINARY_DIR}
59+
--add-tracefile ${CMAKE_BINARY_DIR}/base_coverage.info
60+
--add-tracefile ${CMAKE_BINARY_DIR}/second_coverage.info
61+
--output-file ${CMAKE_BINARY_DIR}/coverage.info
62+
--no-external
63+
--rc lcov_branch_coverage=1
64+
)
65+
execute_process(
66+
COMMAND genhtml --rc lcov_branch_coverage=1
67+
--prefix ${ROOT_DIR}
68+
--branch-coverage
69+
--output-directory ${CMAKE_BINARY_DIR}/coverage
70+
${CMAKE_BINARY_DIR}/coverage.info
71+
)

tools/cmock/create_test.cmake

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Taken from amazon-freertos repository
2+
3+
#function to create the test executable
4+
function(create_test test_name
5+
test_src
6+
link_list
7+
dep_list
8+
include_list)
9+
set(mocks_dir "${CMAKE_CURRENT_BINARY_DIR}/mocks")
10+
include (CTest)
11+
get_filename_component(test_src_absolute ${test_src} ABSOLUTE)
12+
add_custom_command(OUTPUT ${test_name}_runner.c
13+
COMMAND ruby
14+
${CMAKE_SOURCE_DIR}/libraries/3rdparty/CMock/vendor/unity/auto/generate_test_runner.rb
15+
${CMAKE_SOURCE_DIR}/tools/cmock/project.yml
16+
${test_src_absolute}
17+
${test_name}_runner.c
18+
DEPENDS ${test_src}
19+
)
20+
link_directories(${CMAKE_CURRENT_BINARY_DIR}
21+
${CMAKE_CURRENT_BINARY_DIR}/lib
22+
)
23+
24+
set(singleValueArgs "USE_CUSTOM_RUNNER")
25+
cmake_parse_arguments(VARGS "" "${singleValueArgs}" "" ${ARGN})
26+
if((DEFINED VARGS_USE_CUSTOM_RUNNER) AND (${VARGS_USE_CUSTOM_RUNNER}))
27+
add_executable(${test_name}
28+
${test_src}
29+
${CMAKE_SOURCE_DIR}/integration-test/custom_test_runner/custom_unity_runner.c)
30+
target_include_directories(${test_name}
31+
PUBLIC
32+
${CMAKE_SOURCE_DIR}/integration-test/custom_test_runner)
33+
target_compile_definitions(${test_name} PUBLIC -DUSE_CUSTOM_RUNNER=1)
34+
else()
35+
add_executable(${test_name} ${test_src} ${test_name}_runner.c)
36+
endif()
37+
38+
set_target_properties(${test_name} PROPERTIES
39+
COMPILE_FLAG "-O0 -ggdb"
40+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/tests"
41+
INSTALL_RPATH_USE_LINK_PATH TRUE
42+
LINK_FLAGS " \
43+
-Wl,-rpath,${CMAKE_BINARY_DIR}/lib \
44+
-Wl,-rpath,${CMAKE_CURRENT_BINARY_DIR}/lib"
45+
)
46+
target_include_directories(${test_name} PUBLIC
47+
${mocks_dir}
48+
${include_list}
49+
)
50+
51+
# link all libraries sent through parameters
52+
foreach(link IN LISTS link_list)
53+
target_link_libraries(${test_name} ${link})
54+
endforeach()
55+
56+
# add dependency to all the dep_list parameter
57+
foreach(dependency IN LISTS dep_list)
58+
add_dependencies(${test_name} ${dependency})
59+
target_link_libraries(${test_name} ${dependency})
60+
endforeach()
61+
target_link_libraries(${test_name} -lgcov unity)
62+
add_test(NAME ${test_name}
63+
COMMAND ${CMAKE_BINARY_DIR}/bin/tests/${test_name}
64+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
65+
)
66+
endfunction()
67+
68+
# Run the C preprocessor on target files.
69+
# Takes a CMAKE list of arguments to pass to the C compiler
70+
function(preprocess_mock_list mock_name file_list compiler_args)
71+
set_property(GLOBAL PROPERTY ${mock_name}_processed TRUE)
72+
foreach (target_file IN LISTS file_list)
73+
# Has to be TARGET ALL so the file is pre-processed before CMOCK
74+
# is executed on the file.
75+
add_custom_command(OUTPUT ${target_file}.backup
76+
COMMAND scp ${target_file} ${target_file}.backup
77+
VERBATIM COMMAND ${CMAKE_C_COMPILER} -E ${compiler_args} ${target_file} > ${target_file}.out
78+
)
79+
add_custom_target(pre_${mock_name}
80+
COMMAND mv ${target_file}.out ${target_file}
81+
DEPENDS ${target_file}.backup
82+
)
83+
endforeach()
84+
85+
# Clean up temporary files that were created.
86+
# First we test to see if the backup file still exists. If it does we revert
87+
# the change made to the original file.
88+
foreach (target_file IN LISTS file_list)
89+
add_custom_command(TARGET ${mock_name}
90+
POST_BUILD
91+
COMMAND test ! -e ${target_file}.backup || mv ${target_file}.backup ${target_file}
92+
)
93+
endforeach()
94+
endfunction()
95+
96+
# Generates a mock library based on a module's header file
97+
# places the generated source file in the build directory
98+
# @param mock_name: name of the target name
99+
# @param mock_list list of header files to mock
100+
# @param cmock_config configuration file of the cmock framework
101+
# @param mock_include_list include list for the target
102+
# @param mock_define_list special definitions to control compilation
103+
function(create_mock_list mock_name
104+
mock_list
105+
cmock_config
106+
mock_include_list
107+
mock_define_list)
108+
set(mocks_dir "${CMAKE_CURRENT_BINARY_DIR}/mocks")
109+
add_library(${mock_name} SHARED)
110+
foreach (mock_file IN LISTS mock_list)
111+
get_filename_component(mock_file_abs
112+
${mock_file}
113+
ABSOLUTE
114+
)
115+
get_filename_component(mock_file_name
116+
${mock_file}
117+
NAME_WE
118+
)
119+
get_filename_component(mock_file_dir
120+
${mock_file}
121+
DIRECTORY
122+
)
123+
add_custom_command (
124+
OUTPUT ${mocks_dir}/mock_${mock_file_name}.c
125+
COMMAND ruby
126+
${CMAKE_SOURCE_DIR}/libraries/3rdparty/CMock/lib/cmock.rb
127+
-o${cmock_config} ${mock_file_abs}
128+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
129+
)
130+
target_sources(${mock_name} PUBLIC
131+
${mocks_dir}/mock_${mock_file_name}.c
132+
)
133+
134+
target_include_directories(${mock_name} PUBLIC
135+
${mock_file_dir}
136+
)
137+
endforeach()
138+
target_include_directories(${mock_name} PUBLIC
139+
${mocks_dir}
140+
${mock_include_list}
141+
)
142+
set_target_properties(${mock_name} PROPERTIES
143+
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib
144+
POSITION_INDEPENDENT_CODE ON
145+
)
146+
target_compile_definitions(${mock_name} PUBLIC
147+
${mock_define_list}
148+
)
149+
target_link_libraries(${mock_name} cmock unity)
150+
endfunction()
151+
152+
153+
function(create_real_library target
154+
src_file
155+
real_include_list
156+
mock_name)
157+
add_library(${target} STATIC
158+
${src_file}
159+
)
160+
target_include_directories(${target} PUBLIC
161+
${real_include_list}
162+
)
163+
set_target_properties(${target} PROPERTIES
164+
COMPILE_FLAGS "-Wextra -Wpedantic \
165+
-fprofile-arcs -ftest-coverage -fprofile-generate \
166+
-Wno-unused-but-set-variable"
167+
LINK_FLAGS "-fprofile-arcs -ftest-coverage \
168+
-fprofile-generate "
169+
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib
170+
)
171+
if(NOT(mock_name STREQUAL ""))
172+
add_dependencies(${target} ${mock_name})
173+
target_link_libraries(${target}
174+
-l${mock_name}
175+
-lgcov
176+
)
177+
endif()
178+
endfunction()

tools/cmock/ota.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
:cmock:
2+
:mock_prefix: mock_
3+
:when_no_prototypes: :warn
4+
:enforce_strict_ordering: true
5+
# fclose is called after program terminates, so this will avoid a segfault.
6+
:fail_on_unexpected_calls: false
7+
:plugins:
8+
- :ignore
9+
- :ignore_arg
10+
- :expect_any_args
11+
- :array
12+
- :callback
13+
- :return_thru_ptr
14+
:callback_include_count: true # include a count arg when calling the callback
15+
:callback_after_arg_check: false # check arguments before calling the callback
16+
:treat_as:
17+
uint8: HEX8
18+
uint16: HEX16
19+
uint32: UINT32
20+
int8: INT8
21+
bool: UINT8
22+
:includes: # This will add these includes to each mock.
23+
- <stdbool.h>
24+
- <stdint.h>
25+
:treat_externs: :include # Now the extern-ed functions will be mocked.
26+
:weak: __attribute__((weak))
27+
:verbosity: 3
28+
:strippables:
29+
# These keywords are found in many POSIX APIs but is stripped as it cannot be parsed by CMock.
30+
- __restrict
31+
- \s__THROW
32+
- __owur
33+
- __attribute__((__warn_unused_result__))

0 commit comments

Comments
 (0)