diff --git a/.gitignore b/.gitignore index a11254b..c78ed6d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ CMakeCache.txt **/Testing/Temporary/** DartConfiguration.tcl build/ +/_deps +/lib # Generated files testcontainers-c/testcontainers-c.h diff --git a/README.md b/README.md index 90dd7a6..78aa240 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,11 @@ Coming soon: guidelines, specs and code documentation. Check out the examples fo ## Usage in C/C++ -See [the examples and demos](./demo/) +- [Using the generic Testcontainer C API](./demo/generic-container/) +- [Using Testcontainers C in Google Test (C++)](./demo/google-test/) +- [Using the WireMock module](./demo/wiremock/) + +See [the examples and demos](./demo/) for more examples. ## Usage in other languages @@ -206,11 +210,6 @@ You are welcome to contribute more modules in this or a standalone repository! > If you develop new modules, once `vcpkg` or `Conan` packaging is implemented for Testcontainers C, > you might want to develop your module in a standalone repository instead. -## Examples and Demos - -- [Using the generic Testcontainer C API](./demo/generic-container/) -- [Using the WireMock module](./demo/wiremock/) - ## Credits Using a complex Golang framework from C/C++ is not trivial. diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index bc124ad..3b46c2c 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -1,3 +1,4 @@ # Builds Demos with embedded tests add_subdirectory(generic-container) add_subdirectory(wiremock) +add_subdirectory(google-test) diff --git a/demo/generic-container/generic_container_demo.c b/demo/generic-container/generic_container_demo.c index 2a44110..ff600f4 100644 --- a/demo/generic-container/generic_container_demo.c +++ b/demo/generic-container/generic_container_demo.c @@ -1,5 +1,4 @@ #include -#include #include "testcontainers-c.h" #define DEFAULT_IMAGE "wiremock/wiremock:3.0.1-1" diff --git a/demo/google-test/CMakeLists.txt b/demo/google-test/CMakeLists.txt new file mode 100644 index 0000000..ede0901 --- /dev/null +++ b/demo/google-test/CMakeLists.txt @@ -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}) diff --git a/demo/google-test/README.md b/demo/google-test/README.md new file mode 100644 index 0000000..55b9706 --- /dev/null +++ b/demo/google-test/README.md @@ -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 +``` diff --git a/demo/google-test/test.cpp b/demo/google-test/test.cpp new file mode 100644 index 0000000..cb57c27 --- /dev/null +++ b/demo/google-test/test.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#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(); +} diff --git a/demo/google-test/test_data/hello.json b/demo/google-test/test_data/hello.json new file mode 100644 index 0000000..0525333 --- /dev/null +++ b/demo/google-test/test_data/hello.json @@ -0,0 +1,12 @@ +{ + "request": { + "method": "GET", + "url": "/hello" + }, + + "response": { + "status": 200, + "body": "Hello, world!" + } + } + \ No newline at end of file diff --git a/demo/google-test/test_data/hello_with_missing_resource.json b/demo/google-test/test_data/hello_with_missing_resource.json new file mode 100644 index 0000000..cf6abd2 --- /dev/null +++ b/demo/google-test/test_data/hello_with_missing_resource.json @@ -0,0 +1,10 @@ +{ + "request": { + "method": "GET", + "url": "/hello-from-missing-resource" + }, + "response": { + "status": 200, + "bodyFileName": "response_missing.xml" + } +} diff --git a/demo/google-test/test_data/hello_with_resource.json b/demo/google-test/test_data/hello_with_resource.json new file mode 100644 index 0000000..afe6f7e --- /dev/null +++ b/demo/google-test/test_data/hello_with_resource.json @@ -0,0 +1,10 @@ +{ + "request": { + "method": "GET", + "url": "/hello-from-resource" + }, + "response": { + "status": 200, + "bodyFileName": "response.xml" + } +} diff --git a/demo/google-test/test_data/response.xml b/demo/google-test/test_data/response.xml new file mode 100644 index 0000000..33c5aef --- /dev/null +++ b/demo/google-test/test_data/response.xml @@ -0,0 +1,6 @@ + + you + WireMock + Response + Hello, world! +