Skip to content

Commit d89a26a

Browse files
authored
feat: add unit tests in ci (#201)
1 parent 0e99247 commit d89a26a

File tree

8 files changed

+78
-40
lines changed

8 files changed

+78
-40
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Checks:
44
-modernize-use-trailing-return-type,-bugprone-easily-swappable-parameters,-readability-identifier-length'
55
WarningsAsErrors: '*'
66
HeaderFilterRegex: 'include/aws/.*\.h$'
7+
ExcludeHeaderFilter: 'build/_deps/gtest-src.*'
78
FormatStyle: 'none'
89
CheckOptions:
910
- key: modernize-pass-by-value.ValuesOnly

.github/workflows/workflow.yml

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,24 @@ env:
1212

1313
jobs:
1414
build:
15-
# The CMake configure and build commands are platform agnostic and should work equally
16-
# well on Windows or Mac. You can convert this to a matrix build if you need
17-
# cross-platform coverage.
18-
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
19-
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
arch: [ubuntu-latest, ubuntu-24.04-arm]
18+
runs-on: ${{ matrix.arch }}
2019

2120
steps:
2221
- uses: actions/checkout@v3
23-
2422
- name: Install Dependencies
2523
run: sudo apt-get update && sudo apt-get install -y clang-tidy libcurl4-openssl-dev
2624

2725
- name: Configure CMake
28-
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
29-
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
30-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_CLANG_TIDY=clang-tidy
26+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_CLANG_TIDY=clang-tidy -DENABLE_TESTS=ON
3127

3228
- name: Build It
33-
# Build your program with the given configuration
3429
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
3530

36-
build-on-arm-too:
37-
runs-on: ubuntu-latest
38-
steps:
39-
- uses: actions/checkout@v3
40-
- uses: uraimo/run-on-arch-action@v2
41-
with:
42-
arch: aarch64
43-
distro: ubuntu20.04
44-
run: |
45-
apt-get update && apt-get install -y cmake g++ clang-tidy libcurl4-openssl-dev
46-
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_CLANG_TIDY=clang-tidy
47-
cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
31+
- name: Test It
32+
run: cd build && make && ctest
4833

4934
build-demo:
5035
runs-on: ubuntu-latest
@@ -68,15 +53,11 @@ jobs:
6853
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=~/lambda-install
6954
make
7055
make aws-lambda-package-demo
71-
72-
56+
7357
format:
7458
runs-on: ubuntu-latest
75-
7659
steps:
7760
- uses: actions/checkout@v3
7861

7962
- name: Check Formatting
8063
run: ./ci/codebuild/format-check.sh
81-
82-

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ $ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/lambda-install
3939
$ make && make install
4040
```
4141

42+
### Running Unit Tests Locally
43+
44+
To run the unit tests locally, follow these steps to build:
45+
46+
```bash
47+
$ cd aws-lambda-cpp
48+
$ mkdir build
49+
$ cd build
50+
$ cmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_TESTS=ON
51+
$ make
52+
```
53+
54+
Run unit tests:
55+
```bash
56+
$ ctest
57+
```
58+
4259
To consume this library in a project that is also using CMake, you would do:
4360

4461
```cmake

tests/CMakeLists.txt

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
1+
cmake_minimum_required(VERSION 3.11)
12
project(aws-lambda-runtime-tests LANGUAGES CXX)
23

3-
find_package(AWSSDK COMPONENTS lambda iam)
4+
if(DEFINED ENV{GITHUB_ACTIONS})
5+
# Fetch Google Test for unit tests
6+
include(FetchContent)
7+
FetchContent_Declare(gtest
8+
URL https://github.com/google/googletest/archive/v1.12.0.tar.gz
9+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
10+
)
11+
# Configure build of googletest
12+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
13+
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
14+
set(INSTALL_GTEST OFF)
15+
FetchContent_MakeAvailable(gtest)
416

5-
add_executable(${PROJECT_NAME}
6-
main.cpp
7-
runtime_tests.cpp
8-
version_tests.cpp
9-
gtest/gtest-all.cc)
17+
add_executable(unit_tests
18+
unit/no_op_test.cpp)
19+
target_link_libraries(unit_tests PRIVATE gtest_main aws-lambda-runtime)
1020

11-
target_link_libraries(${PROJECT_NAME} PRIVATE ${AWSSDK_LINK_LIBRARIES} aws-lambda-runtime)
21+
# Register unit tests
22+
include(GoogleTest)
23+
gtest_discover_tests(unit_tests
24+
PROPERTIES
25+
LABELS "unit"
26+
DISCOVERY_TIMEOUT 10)
27+
else()
28+
message(STATUS "Unit tests skipped: Not in GitHub Actions environment")
29+
endif()
1230

13-
include(GoogleTest)
14-
gtest_discover_tests(${PROJECT_NAME} EXTRA_ARGS "--aws_prefix=${TEST_RESOURCE_PREFIX}") # requires CMake 3.10 or later
1531

16-
add_subdirectory(resources)
32+
find_package(AWSSDK COMPONENTS lambda iam QUIET)
33+
34+
if(AWSSDK_FOUND)
35+
add_executable(${PROJECT_NAME}
36+
integration/main.cpp
37+
integration/runtime_tests.cpp
38+
integration/version_tests.cpp
39+
gtest/gtest-all.cc)
40+
41+
target_link_libraries(${PROJECT_NAME} PRIVATE ${AWSSDK_LINK_LIBRARIES} aws-lambda-runtime)
42+
43+
include(GoogleTest)
44+
gtest_discover_tests(${PROJECT_NAME} EXTRA_ARGS "--aws_prefix=${TEST_RESOURCE_PREFIX}")
45+
46+
add_subdirectory(resources)
47+
else()
48+
message(STATUS "Integration tests skipped: AWS SDK not found or not in GitHub Actions environment")
49+
endif()
1750

tests/main.cpp renamed to tests/integration/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <aws/core/Aws.h>
22
#include <aws/core/utils/logging/ConsoleLogSystem.h>
3-
#include "gtest/gtest.h"
3+
#include "../gtest/gtest.h"
44

55
std::function<std::shared_ptr<Aws::Utils::Logging::LogSystemInterface>()> get_console_logger_factory()
66
{

tests/runtime_tests.cpp renamed to tests/integration/runtime_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <aws/lambda/model/CreateFunctionRequest.h>
1414
#include <aws/lambda/model/InvokeRequest.h>
1515
#include <aws/core/utils/base64/Base64.h>
16-
#include "gtest/gtest.h"
16+
#include "../gtest/gtest.h"
1717
#include <aws/lambda/model/LogType.h>
1818
#include <cstdio>
1919
#include <iostream>

tests/version_tests.cpp renamed to tests/integration/version_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <aws/lambda-runtime/version.h>
2-
#include "gtest/gtest.h"
2+
#include "../gtest/gtest.h"
33

44
using namespace aws::lambda_runtime;
55

tests/unit/no_op_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <gtest/gtest.h>
2+
3+
TEST(noop, dummy_test)
4+
{
5+
ASSERT_EQ(0, 0);
6+
}

0 commit comments

Comments
 (0)