Skip to content

Commit 81c3d26

Browse files
committed
WIP feat: Implement initial C++ library
1 parent 90304b2 commit 81c3d26

File tree

12 files changed

+252
-0
lines changed

12 files changed

+252
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ include(CTest)
1010

1111
add_subdirectory(testcontainers-bridge)
1212
add_subdirectory(testcontainers-c)
13+
add_subdirectory(testcontainers-cpp)
1314
add_subdirectory(modules)
1415
if(NOT DEFINED SKIP_DEMOS)
1516
add_subdirectory(demo)

demo/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
add_subdirectory(generic-container)
33
add_subdirectory(wiremock)
44
add_subdirectory(google-test)
5+
add_subdirectory(google-test-cpp)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Google Test demo
2+
# This is based on https://google.github.io/googletest/quickstart-cmake.html
3+
cmake_minimum_required (VERSION 3.26)
4+
project (google-test-cpp-demo
5+
VERSION 0.1.0
6+
DESCRIPTION "Demonstrates usage of Testcontainers C++ in Google Test"
7+
LANGUAGES CXX
8+
)
9+
10+
set(TARGET_OUT ${PROJECT_NAME}.out)
11+
12+
# GoogleTest requires at least C++14
13+
set(CMAKE_CXX_STANDARD 23)
14+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
15+
include(FetchContent)
16+
FetchContent_Declare(
17+
googletest
18+
GIT_REPOSITORY https://github.com/google/googletest.git
19+
GIT_TAG v1.17.0
20+
)
21+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
22+
FetchContent_MakeAvailable(googletest)
23+
24+
enable_testing()
25+
file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
26+
27+
add_executable(${TARGET_OUT} test.cpp)
28+
target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-cpp)
29+
target_link_libraries(${TARGET_OUT} PRIVATE GTest::gtest_main)
30+
31+
include(GoogleTest)
32+
gtest_discover_tests(${TARGET_OUT})

demo/google-test-cpp/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Using Testcontainers C in Google Test
2+
3+
Demonstrates usage of Testcontainers C in [Google Test](https://github.com/google/googletest).
4+
See [test.cpp](./test.cpp) for the code.
5+
6+
## Run the demo
7+
8+
```bash
9+
cmake .
10+
cmake --build .
11+
cd demo/google-test
12+
ctest --output-on-failure
13+
```
14+
15+
## Sample output
16+
17+
```shell
18+
onenashev:~/testcontainers-c/demo/google-test$ ctest --output-on-failure
19+
Test project /home/onenashev/testcontainers-c/demo/google-test
20+
Start 1: WireMockTestContainer.HelloWorld
21+
1/5 Test #1: WireMockTestContainer.HelloWorld ...................... Passed 3.31 sec
22+
Start 2: WireMockTestContainer.HelloWorldFromResource
23+
2/5 Test #2: WireMockTestContainer.HelloWorldFromResource .......... Passed 3.97 sec
24+
Start 3: WireMockTestContainer.HelloWorldFromMissingResource
25+
3/5 Test #3: WireMockTestContainer.HelloWorldFromMissingResource ... Passed 3.91 sec
26+
27+
100% tests passed, 0 tests failed out of 3
28+
```

demo/google-test-cpp/test.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <gtest/gtest.h>
4+
#include "testcontainers.hpp"
5+
6+
using namespace testcontainers;
7+
8+
class WireMockTestContainerClass : public ::testing::Test {
9+
10+
const char* WIREMOCK_IMAGE = "wiremock/wiremock:3.0.1-1";
11+
const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings";
12+
13+
protected:
14+
void SetUp() override {
15+
std::cout << "Creating new container: " << WIREMOCK_IMAGE << '\n';
16+
17+
container = std::make_unique<Container>(WIREMOCK_IMAGE);
18+
container->expose_port(TcpPort{8080});
19+
container->wait_for_http(TcpPort{8080}, WIREMOCK_ADMIN_MAPPING_ENDPOINT);
20+
container->with_file("test_data/hello.json", "/home/wiremock/mappings/hello.json");
21+
container->with_file("test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json");
22+
container->with_file("test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json");
23+
container->with_file("test_data/response.xml", "/home/wiremock/__files/response.xml");
24+
25+
auto result = container->run();
26+
27+
EXPECT_TRUE(!result.has_value()) << "Failed to run the container: " << result.error();
28+
};
29+
30+
std::unique_ptr<Container> container;
31+
};
32+
33+
TEST_F(WireMockTestContainerClass, HelloWorld) {
34+
std::cout << "Sending HTTP request to the container\n";
35+
36+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello");
37+
38+
ASSERT_TRUE(response.has_value()) << "Failed to send HTTP request: " << response.error();
39+
ASSERT_EQ(code, 200) << "Received wrong response code: " << response.error();
40+
41+
std::cout << "Server Response: HTTP-" << code << '\n' << response.value() << '\n';
42+
}
43+
44+
TEST_F(WireMockTestContainerClass, HelloWorldFromResource) {
45+
std::cout << "Sending HTTP request to the container\n";
46+
47+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-resource");
48+
49+
ASSERT_TRUE(response.has_value()) << "Failed to send HTTP request: " << response.error();
50+
ASSERT_EQ(code, 200) << "Received wrong response code: " << response.error();
51+
52+
std::cout << "Server Response: HTTP-" << code << '\n' << response.value() << '\n';
53+
}
54+
55+
TEST_F(WireMockTestContainerClass, HelloWorldFromMissingResource) {
56+
std::cout << "Sending HTTP request to the container\n";
57+
58+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-missing-resource");
59+
60+
ASSERT_EQ(code, 500) << "The request should have failed";
61+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"request": {
3+
"method": "GET",
4+
"url": "/hello"
5+
},
6+
7+
"response": {
8+
"status": 200,
9+
"body": "Hello, world!"
10+
}
11+
}
12+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"request": {
3+
"method": "GET",
4+
"url": "/hello-from-missing-resource"
5+
},
6+
"response": {
7+
"status": 200,
8+
"bodyFileName": "response_missing.xml"
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"request": {
3+
"method": "GET",
4+
"url": "/hello-from-resource"
5+
},
6+
"response": {
7+
"status": 200,
8+
"bodyFileName": "response.xml"
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<note>
2+
<to>you</to>
3+
<from>WireMock</from>
4+
<heading>Response</heading>
5+
<body>Hello, world!</body>
6+
</note>

testcontainers-cpp/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
set(TARGET testcontainers-cpp)
2+
set(TARGET_NAME ${TARGET})
3+
set(TARGET_DESCRIPTION "Testcontainer library for C++")
4+
set(TARGET_VERSION ${PROJECT_VERSION})
5+
6+
add_library(${TARGET} INTERFACE)
7+
8+
set(CMAKE_CXX_STANDARD 23)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
11+
target_sources(${TARGET}
12+
PUBLIC FILE_SET HEADERS
13+
BASE_DIRS .
14+
FILES testcontainers.hpp
15+
)
16+
17+
target_link_libraries(${TARGET} INTERFACE testcontainers-c)
18+
19+
configure_file(cmake.pc.in ${TARGET}.pc @ONLY)
20+
install(TARGETS ${TARGET}
21+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
22+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
23+
install(FILES ${CMAKE_BINARY_DIR}/${TARGET}.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

0 commit comments

Comments
 (0)