Skip to content

Commit 260eac4

Browse files
Merge pull request #54 from seffradev/cpp-abstraction
Initial C++ library
2 parents 7b850d7 + 5e72b59 commit 260eac4

18 files changed

Lines changed: 624 additions & 182 deletions

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,4 +2,5 @@
22
add_subdirectory(generic-container)
33
add_subdirectory(wiremock)
44
add_subdirectory(google-test)
5+
add_subdirectory(google-test-cpp)
56
add_subdirectory(postgresql)
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Using Testcontainers C++ in Google Test
2+
3+
Demonstrates usage of Testcontainers C++ in [Google
4+
Test](https://github.com/google/googletest). See
5+
[test.cpp](./test.cpp) for the code.
6+
7+
## Run the demo
8+
9+
```bash
10+
cmake -S . -B /tmp/tc-native
11+
cmake --build /tmp/tc-native/
12+
ctest --output-on-failure -R Class
13+
```

demo/google-test-cpp/test.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <iostream>
4+
#include <string>
5+
6+
#include "testcontainers.hpp"
7+
8+
using namespace testcontainers;
9+
10+
class WireMockTestContainerClass : public ::testing::Test {
11+
const char* WIREMOCK_IMAGE = "wiremock/wiremock:3.0.1-1";
12+
const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings";
13+
14+
protected:
15+
void SetUp() override {
16+
using namespace std::literals;
17+
18+
builder.expose_port(TcpPort{8080}).wait_for_http(TcpPort{8080}, WIREMOCK_ADMIN_MAPPING_ENDPOINT);
19+
}
20+
21+
Container::Builder builder = Container::Builder{WIREMOCK_IMAGE};
22+
};
23+
24+
/// This test runs a "Hello World" example.
25+
TEST_F(WireMockTestContainerClass, HelloWorld) {
26+
auto container = builder.with_file("test_data/hello.json", "/home/wiremock/mappings/hello.json").build();
27+
ASSERT_TRUE(container.has_value()) << "Failed to run the container";
28+
29+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello");
30+
ASSERT_EQ(code, 200) << "Expected 200 OK. Received response: " << response;
31+
}
32+
33+
/// This test responds to an HTTP request with response from a
34+
/// prepared file.
35+
TEST_F(WireMockTestContainerClass, HelloWorldFromResource) {
36+
auto container = builder.with_file("test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json")
37+
.with_file("test_data/response.xml", "/home/wiremock/__files/response.xml")
38+
.build();
39+
ASSERT_TRUE(container.has_value()) << "Failed to run the container";
40+
41+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-resource");
42+
ASSERT_EQ(code, 200) << "Expected 200 OK. Received response: " << response;
43+
}
44+
45+
/// This test performs a request that is guaranteed to fail and
46+
/// return an HTTP-500.
47+
TEST_F(WireMockTestContainerClass, HelloWorldFromMissingResource) {
48+
auto container =
49+
builder.with_file("test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json").build();
50+
ASSERT_TRUE(container.has_value()) << "Failed to run the container";
51+
52+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-missing-resource");
53+
ASSERT_EQ(code, 500) << "Expected 500 Internal Server Error. Received response: " << response;
54+
}
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-bridge/go.mod

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
11
module github.com/testcontainers/testcontainers-native
22

3-
go 1.19
3+
go 1.25.0
44

55
require (
6-
github.com/docker/go-connections v0.4.0
7-
github.com/testcontainers/testcontainers-go v0.22.0
6+
github.com/docker/go-connections v0.6.0
7+
github.com/testcontainers/testcontainers-go v0.40.0
88
)
99

1010
require (
11-
dario.cat/mergo v1.0.0 // indirect
11+
dario.cat/mergo v1.0.2 // indirect
1212
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
13-
github.com/Microsoft/go-winio v0.6.1 // indirect
14-
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
15-
github.com/containerd/containerd v1.7.3 // indirect
16-
github.com/cpuguy83/dockercfg v0.3.1 // indirect
17-
github.com/docker/distribution v2.8.2+incompatible // indirect
18-
github.com/docker/docker v24.0.5+incompatible // indirect
19-
github.com/docker/go-connections v0.4.0 // indirect
13+
github.com/Microsoft/go-winio v0.6.2 // indirect
14+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
15+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
16+
github.com/containerd/errdefs v1.0.0 // indirect
17+
github.com/containerd/errdefs/pkg v0.3.0 // indirect
18+
github.com/containerd/log v0.1.0 // indirect
19+
github.com/containerd/platforms v0.2.1 // indirect
20+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
21+
github.com/davecgh/go-spew v1.1.1 // indirect
22+
github.com/distribution/reference v0.6.0 // indirect
23+
github.com/docker/docker v28.5.1+incompatible // indirect
2024
github.com/docker/go-units v0.5.0 // indirect
21-
github.com/gogo/protobuf v1.3.2 // indirect
22-
github.com/golang/protobuf v1.5.3 // indirect
23-
github.com/google/uuid v1.3.0 // indirect
24-
github.com/klauspost/compress v1.16.0 // indirect
25-
github.com/magiconair/properties v1.8.7 // indirect
26-
github.com/moby/patternmatcher v0.5.0 // indirect
27-
github.com/moby/sys/sequential v0.5.0 // indirect
25+
github.com/ebitengine/purego v0.8.4 // indirect
26+
github.com/felixge/httpsnoop v1.0.4 // indirect
27+
github.com/go-logr/logr v1.4.3 // indirect
28+
github.com/go-logr/stdr v1.2.2 // indirect
29+
github.com/go-ole/go-ole v1.2.6 // indirect
30+
github.com/google/uuid v1.6.0 // indirect
31+
github.com/klauspost/compress v1.18.0 // indirect
32+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
33+
github.com/magiconair/properties v1.8.10 // indirect
34+
github.com/moby/docker-image-spec v1.3.1 // indirect
35+
github.com/moby/go-archive v0.1.0 // indirect
36+
github.com/moby/patternmatcher v0.6.0 // indirect
37+
github.com/moby/sys/sequential v0.6.0 // indirect
38+
github.com/moby/sys/user v0.4.0 // indirect
39+
github.com/moby/sys/userns v0.1.0 // indirect
2840
github.com/moby/term v0.5.0 // indirect
2941
github.com/morikuni/aec v1.0.0 // indirect
3042
github.com/opencontainers/go-digest v1.0.0 // indirect
31-
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
32-
github.com/opencontainers/runc v1.1.5 // indirect
43+
github.com/opencontainers/image-spec v1.1.1 // indirect
3344
github.com/pkg/errors v0.9.1 // indirect
34-
github.com/sirupsen/logrus v1.9.0 // indirect
35-
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
36-
golang.org/x/mod v0.9.0 // indirect
37-
golang.org/x/net v0.9.0 // indirect
38-
golang.org/x/sys v0.9.0 // indirect
39-
golang.org/x/text v0.10.0 // indirect
40-
golang.org/x/tools v0.7.0 // indirect
41-
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
42-
google.golang.org/grpc v1.57.0 // indirect
43-
google.golang.org/protobuf v1.30.0 // indirect
45+
github.com/pmezard/go-difflib v1.0.0 // indirect
46+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
47+
github.com/shirou/gopsutil/v4 v4.25.6 // indirect
48+
github.com/sirupsen/logrus v1.9.3 // indirect
49+
github.com/stretchr/testify v1.11.1 // indirect
50+
github.com/tklauser/go-sysconf v0.3.12 // indirect
51+
github.com/tklauser/numcpus v0.6.1 // indirect
52+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
53+
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
54+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
55+
go.opentelemetry.io/otel v1.42.0 // indirect
56+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.42.0 // indirect
57+
go.opentelemetry.io/otel/metric v1.42.0 // indirect
58+
go.opentelemetry.io/otel/sdk v1.42.0 // indirect
59+
go.opentelemetry.io/otel/trace v1.42.0 // indirect
60+
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
61+
golang.org/x/crypto v0.43.0 // indirect
62+
golang.org/x/sys v0.41.0 // indirect
63+
google.golang.org/protobuf v1.36.11 // indirect
64+
gopkg.in/yaml.v3 v3.0.1 // indirect
4465
)

0 commit comments

Comments
 (0)