diff --git a/README.md b/README.md index be14c61..05b1ab8 100644 --- a/README.md +++ b/README.md @@ -142,8 +142,10 @@ and linked to the consumer project. ### Available modules -- Generic container aka DYI - embedded -- [WireMock](./modules/wiremock/) +- Generic container for DYI containers (embedded) + - [Demo](./demo/generic-container/) +- [WireMock](./modules/wiremock/) - for API mocking and integration testing + - [Demo](./demo/wiremock/) ### Why modules? @@ -207,6 +209,7 @@ You are welcome to contribute more modules in this or a standalone repository! ## Examples and Demos +- [Using the generic Testcontainer C API](./demo/generic-container/) - [Using the WireMock module](./demo/wiremock/) ## Credits diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index 11770e7..bc124ad 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -1 +1,3 @@ +# Builds Demos with embedded tests +add_subdirectory(generic-container) add_subdirectory(wiremock) diff --git a/demo/generic-container/CMakeLists.txt b/demo/generic-container/CMakeLists.txt new file mode 100644 index 0000000..7559e5f --- /dev/null +++ b/demo/generic-container/CMakeLists.txt @@ -0,0 +1,15 @@ +project(testcontainers-c-generic-container-demo + VERSION 0.0.1 + DESCRIPTION "Demonstrates usage of the generic container API in a simple main app") + +set(TARGET_OUT demo_generic_container.out) + +include_directories(${testcontainers-c_SOURCE_DIR}) +# WORKING_DIRECTORY breaks shared lib loading +file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + +# Vanilla Demo for WireMock +add_executable(${TARGET_OUT} generic_container_demo.c) +add_dependencies(${TARGET_OUT} testcontainers-c-shim) +target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-c) +add_test(NAME generic_container_demo COMMAND ${TARGET_OUT}) diff --git a/demo/generic-container/README.md b/demo/generic-container/README.md new file mode 100644 index 0000000..7f8a59a --- /dev/null +++ b/demo/generic-container/README.md @@ -0,0 +1,19 @@ +# Demo - Generic Container on Testcontainers C + +Demonstrates usage of the generic container API provided by the Testcontainers C library. +In this demo, we do not use any module SDKs or test frameworks, just a simple `main()` function. + +## Running demo + +From the root of the repository: + +```bash +cmake . +cmake --build . +cd demo/generic-container +./demo_generic_container.out +``` + +## Sample output + +![Sample Output](./sample_output.png) diff --git a/demo/wiremock/wiremock_demo_vanilla.c b/demo/generic-container/generic_container_demo.c similarity index 100% rename from demo/wiremock/wiremock_demo_vanilla.c rename to demo/generic-container/generic_container_demo.c diff --git a/demo/generic-container/sample_output.png b/demo/generic-container/sample_output.png new file mode 100644 index 0000000..3c223c5 Binary files /dev/null and b/demo/generic-container/sample_output.png differ diff --git a/demo/generic-container/test_data/hello.json b/demo/generic-container/test_data/hello.json new file mode 100644 index 0000000..0525333 --- /dev/null +++ b/demo/generic-container/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/wiremock/CMakeLists.txt b/demo/wiremock/CMakeLists.txt index 07399ac..aa232ba 100644 --- a/demo/wiremock/CMakeLists.txt +++ b/demo/wiremock/CMakeLists.txt @@ -1,23 +1,17 @@ -project(testcontainers-c-wiremock-demo VERSION 0.0.1 +project(testcontainers-c-wiremock-demo + VERSION 0.0.1 DESCRIPTION "Demonstrates usage of the WireMock module for Testcontainers C in a simple main app") set(TARGET_OUT demo_wiremock_module.out) -set(VANILLA_OUT demo_vanilla.out) include_directories(${testcontainers-c_SOURCE_DIR}) # WORKING_DIRECTORY breaks shared lib loading file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) -# Vanilla Demo for WireMock -add_executable(${VANILLA_OUT} wiremock_demo_vanilla.c) -add_dependencies(${VANILLA_OUT} testcontainers-c-shim) -target_link_libraries(${VANILLA_OUT} PRIVATE testcontainers-c) -add_test(NAME vanilla_smoke COMMAND ${VANILLA_OUT}) - # WireMock Module demo -add_executable(${TARGET_OUT} wiremock_demo.c) -add_dependencies(${VANILLA_OUT} testcontainers-c-shim) +add_executable(${TARGET_OUT} wiremock_module_demo.c) +add_dependencies(${TARGET_OUT} testcontainers-c-shim) target_include_directories(${TARGET_OUT} PRIVATE ${testcontainers-c-wiremock_SOURCE_DIR}) target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-c) target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-c-wiremock) -add_test(NAME wiremock_module_smoke COMMAND ${TARGET_OUT}) +add_test(NAME wiremock_module_demo COMMAND ${TARGET_OUT}) diff --git a/demo/wiremock/README.md b/demo/wiremock/README.md index 729211a..c68698b 100644 --- a/demo/wiremock/README.md +++ b/demo/wiremock/README.md @@ -1,6 +1,6 @@ # Demo - WireMock on Testcontainers C -Demonstrates usage of the [WireMock](https://wiremock.org/) module in a simple main function. +Demonstrates usage of the [WireMock Module for Testcontainers C](../../modules/wiremock/) in a simple main function. No test framework is used here. ## Running demo @@ -8,53 +8,12 @@ No test framework is used here. From the root of the repository: ```bash +cmake . cmake --build . cd demo/wiremock -./demo.out +./demo_wiremock_module.out ``` ## Sample output ![Sample Output](./sample_output.png) - -## Wishlist - -The library implementation and api is sub-optimal at the moment. -This is how we want to see the code once everything is done, maybe: - -```c -#include -#include -#include "testcontainers-c.h" -#include "testcontainers-c-wiremock.h" - -#define DEFAULT_IMAGE "wiremock/wiremock:3.1.0-1" - -int main() { - printf("Using WireMock with the Testcontainers C binding:\n"); - - printf("Creating new container: %s\n", DEFAULT_IMAGE); - int requestId = NewContainerRequest(DEFAULT_IMAGE); - WithExposedTcpPort(requestId, 8080); - WithWaitForHttp(requestId, 8080, "/__admin/mappings"); - WithFile(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json"); - struct TestContainer container = RunContainer(requestId); - if (container.Id == -1) { - printf("Failed to run the container: %s\n", container.errorMsg); - return -1; - } - - printf("Sending HTTP request to the container\n"); - struct HttpGetResponse response = SendHttpGet(container.Id, 8080, "/hello"); - if (response.code == -1) { - printf("Failed to send HTTP request: %s\n", response.errorMsg); - return -1; - } - if (response.code != 200) { - printf("Request failed: HTTP-%d\n%s\n", response.code, response.errorMsg); - return -1; - } - printf("Server Response: HTTP-%d\n%s\n\n", response.code, response.msg); - return 0; -} -``` diff --git a/demo/wiremock/wiremock_demo.c b/demo/wiremock/wiremock_module_demo.c similarity index 100% rename from demo/wiremock/wiremock_demo.c rename to demo/wiremock/wiremock_module_demo.c