-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d01a073
commit d7dc97b
Showing
11 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Builds Demos with embedded tests | ||
add_subdirectory(generic-container) | ||
add_subdirectory(wiremock) | ||
add_subdirectory(google-test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Google Test demo | ||
# This is based on https://google.github.io/googletest/quickstart-cmake.html | ||
project(google-test-demo | ||
VERSION 0.0.1 | ||
DESCRIPTION "Demonstrates usage of Testcontainers C in Google Test") | ||
|
||
set(TARGET_OUT ${PROJECT_NAME}.out) | ||
|
||
# GoogleTest requires at least C++14 | ||
set(CMAKE_CXX_STANDARD 14) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
include(FetchContent) | ||
FetchContent_Declare( | ||
googletest | ||
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip | ||
) | ||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
FetchContent_MakeAvailable(googletest) | ||
|
||
enable_testing() | ||
file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
||
add_executable(${TARGET_OUT} test.cpp) | ||
add_dependencies(${TARGET_OUT} testcontainers-c-shim) | ||
target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-c) | ||
target_link_libraries(${TARGET_OUT} PRIVATE GTest::gtest_main) | ||
|
||
include(GoogleTest) | ||
gtest_discover_tests(${TARGET_OUT}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Using Testcontainers C in Google Test | ||
|
||
Demonstrates usage of Testcontainers C in [Google Test](https://github.com/google/googletest). | ||
See [test.cpp](./test.cpp) for the code. | ||
|
||
## Run the demo | ||
|
||
```bash | ||
cmake . | ||
cmake --build . | ||
cd demo/google-test | ||
ctest --output-on-failure | ||
``` | ||
|
||
## Sample output | ||
|
||
```shell | ||
onenashev:~/testcontainers-c/demo/google-test$ ctest --output-on-failure | ||
Test project /home/onenashev/testcontainers-c/demo/google-test | ||
Start 1: WireMockTestContainer.HelloWorld | ||
1/5 Test #1: WireMockTestContainer.HelloWorld ...................... Passed 3.31 sec | ||
Start 2: WireMockTestContainer.HelloWorldFromResource | ||
2/5 Test #2: WireMockTestContainer.HelloWorldFromResource .......... Passed 3.97 sec | ||
Start 3: WireMockTestContainer.HelloWorldFromMissingResource | ||
3/5 Test #3: WireMockTestContainer.HelloWorldFromMissingResource ... Passed 3.91 sec | ||
|
||
100% tests passed, 0 tests failed out of 3 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <gtest/gtest.h> | ||
#include "testcontainers-c.h" | ||
|
||
class WireMockTestContainer : public ::testing::Test { | ||
|
||
const char* WIREMOCK_IMAGE = "wiremock/wiremock:3.0.1-1"; | ||
const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings"; | ||
|
||
protected: | ||
void SetUp() override { | ||
std::cout << "Creating new container: " << WIREMOCK_IMAGE << '\n'; | ||
|
||
int requestId = tc_new_container_request(WIREMOCK_IMAGE); | ||
tc_with_exposed_tcp_port(requestId, 8080); | ||
tc_with_wait_for_http(requestId, 8080, WIREMOCK_ADMIN_MAPPING_ENDPOINT); | ||
tc_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json"); | ||
tc_with_file(requestId, "test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json"); | ||
tc_with_file(requestId, "test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json"); | ||
tc_with_file(requestId, "test_data/response.xml", "/home/wiremock/__files/response.xml"); | ||
|
||
struct tc_run_container_return ret = tc_run_container(requestId); | ||
containerId = ret.r0; | ||
|
||
EXPECT_TRUE(ret.r1) << "Failed to run the container: " << ret.r2; | ||
}; | ||
|
||
void TearDown() override { | ||
// Code here will be called immediately after each test (right | ||
// before the destructor). | ||
}; | ||
|
||
int containerId; | ||
}; | ||
|
||
TEST_F(WireMockTestContainer, HelloWorld) { | ||
std::cout << "Sending HTTP request to the container\n"; | ||
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello"); | ||
|
||
ASSERT_NE(response.r0, -1) << "Failed to send HTTP request: " << response.r2; | ||
ASSERT_EQ(response.r0, 200) << "Received wrong response code: " << response.r1 << response.r2; | ||
|
||
std::cout << "Server Response: HTTP-" << response.r0 << '\n' << response.r1 << '\n'; | ||
} | ||
|
||
TEST_F(WireMockTestContainer, HelloWorldFromResource) { | ||
std::cout << "Sending HTTP request to the container\n"; | ||
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello-from-resource"); | ||
|
||
ASSERT_NE(response.r0, -1) << "Failed to send HTTP request: " << response.r2; | ||
ASSERT_EQ(response.r0, 200) << "Received wrong response code: " << response.r1 << response.r2; | ||
|
||
std::cout << "Server Response: HTTP-" << response.r0 << '\n' << response.r1 << '\n'; | ||
} | ||
|
||
TEST_F(WireMockTestContainer, HelloWorldFromMissingResource) { | ||
std::cout << "Sending HTTP request to the container\n"; | ||
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello-from-missing-resource"); | ||
|
||
ASSERT_EQ(response.r0, 500) << "The request should have failed"; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"url": "/hello" | ||
}, | ||
|
||
"response": { | ||
"status": 200, | ||
"body": "Hello, world!" | ||
} | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
demo/google-test/test_data/hello_with_missing_resource.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"url": "/hello-from-missing-resource" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"bodyFileName": "response_missing.xml" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"url": "/hello-from-resource" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"bodyFileName": "response.xml" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<note> | ||
<to>you</to> | ||
<from>WireMock</from> | ||
<heading>Response</heading> | ||
<body>Hello, world!</body> | ||
</note> |