Skip to content

Commit

Permalink
Split WireMock Module and Generic Container demos
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-nenashev committed Sep 25, 2023
1 parent 26e3129 commit 9d85469
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 57 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Builds Demos with embedded tests
add_subdirectory(generic-container)
add_subdirectory(wiremock)
15 changes: 15 additions & 0 deletions demo/generic-container/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
19 changes: 19 additions & 0 deletions demo/generic-container/README.md
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
Binary file added demo/generic-container/sample_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions demo/generic-container/test_data/hello.json
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!"
}
}

16 changes: 5 additions & 11 deletions demo/wiremock/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
47 changes: 3 additions & 44 deletions demo/wiremock/README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,19 @@
# 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

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 <stdio.h>
#include <string.h>
#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;
}
```
File renamed without changes.

0 comments on commit 9d85469

Please sign in to comment.