Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
cmake_minimum_required (VERSION 3.16.3)
project (TESTCONTAINERS-C)

include(CTest)

include_directories(${CMAKE_CURRENT_BINARY_DIR}/testcontainers-c)

add_subdirectory(testcontainers-c)
add_subdirectory(modules)
if(NOT DEFINED SKIP_DEMOS)
add_subdirectory(demo)
endif()
cmake_minimum_required (VERSION 3.26)
project (TESTCONTAINERS-C
VERSION 0.1.0
DESCRIPTION "Testcontainers for C and other native languages"
LANGUAGES C CXX
)

include(GNUInstallDirs)
include(CTest)

add_subdirectory(testcontainers-bridge)
add_subdirectory(testcontainers-c)
add_subdirectory(modules)
if(NOT DEFINED SKIP_DEMOS)
add_subdirectory(demo)
endif()
14 changes: 7 additions & 7 deletions demo/generic-container/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +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")
cmake_minimum_required (VERSION 3.26)
project (generic-container-demo
VERSION 0.1.0
DESCRIPTION "Demonstrates usage of the generic container API in a simple main app"
LANGUAGES C
)

set(TARGET_OUT demo_generic_container.out)
set(TARGET_OUT ${PROJECT_NAME}.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})
31 changes: 16 additions & 15 deletions demo/generic-container/generic_container_demo.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
#include <stdio.h>
#include "testcontainers-c.h"
#include "testcontainers-c/container.h"

#define DEFAULT_IMAGE "wiremock/wiremock:3.0.1-1"

int main() {
printf("Using WireMock with the Testcontainers C binding:\n");

printf("Creating new container: %s\n", DEFAULT_IMAGE);
int requestId = tc_new_container_request(DEFAULT_IMAGE);
tc_with_exposed_tcp_port(requestId, 8080);
tc_with_wait_for_http(requestId, 8080, "/__admin/mappings");
tc_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json");
struct tc_run_container_return ret = tc_run_container(requestId);
int containerId = ret.r0;
if (!ret.r1) {
printf("Failed to run the container: %s\n", ret.r2);
int requestId = tc_container_create(DEFAULT_IMAGE);
tc_container_with_exposed_tcp_port(requestId, 8080);
tc_container_with_wait_for_http(requestId, 8080, "/__admin/mappings");
tc_container_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json");
char* error;
int containerId = tc_container_run(requestId, error);
if (containerId == -1) {
printf("Failed to run the container: %s\n", error);
return -1;
}

printf("Sending HTTP request to the container\n");
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello");
if (response.r0 == -1) {
printf("Failed to send HTTP request: %s\n", response.r2);
char *response_body;
int response_code = tc_container_send_http_get(containerId, 8080, "/hello", response_body, error);
if (response_code == -1) {
printf("Failed to send HTTP request: %s\n", error);
return -1;
}
if (response.r0 != 200) {
printf("Received wrong response code: %d instead of %d\n%s\n%s\n", response.r0, 200, response.r1, response.r2);
if (response_code != 200) {
printf("Received wrong response code: %d instead of %d\n%s\n%s\n", response_code, 200, response_body, error);
return -1;
}
printf("Server Response: HTTP-%d\n%s\n\n", response.r0, response.r1);
printf("Server Response: HTTP-%d\n%s\n\n", response_code, response_body);
return 0;
}

13 changes: 8 additions & 5 deletions demo/google-test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# 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")
cmake_minimum_required (VERSION 3.26)
project (google-test-demo
VERSION 0.1.0
DESCRIPTION "Demonstrates usage of Testcontainers C in Google Test"
LANGUAGES CXX
)

set(TARGET_OUT ${PROJECT_NAME}.out)

Expand All @@ -12,7 +15,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.17.0
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
Expand All @@ -21,7 +25,6 @@ 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)

Expand Down
72 changes: 38 additions & 34 deletions demo/google-test/test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <iostream>
#include <string>
#include <gtest/gtest.h>
#include "testcontainers-c.h"

extern "C" {
#include "testcontainers-c/container.h"
}

class WireMockTestContainer : public ::testing::Test {

Expand All @@ -11,23 +14,23 @@ 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;

int requestId = tc_container_create(WIREMOCK_IMAGE);
tc_container_with_exposed_tcp_port(requestId, 8080);
tc_container_with_wait_for_http(requestId, 8080, WIREMOCK_ADMIN_MAPPING_ENDPOINT);
tc_container_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json");
tc_container_with_file(requestId, "test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json");
tc_container_with_file(requestId, "test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json");
tc_container_with_file(requestId, "test_data/response.xml", "/home/wiremock/__files/response.xml");

char* error;
int containerId = tc_container_run(requestId, error);

EXPECT_TRUE(containerId != -1) << "Failed to run the container: " << error;
};

void TearDown() override {
char* error = tc_terminate_container(containerId);
char* error = tc_container_terminate(containerId);
ASSERT_EQ(error, nullptr) << "Failed to terminate the container after the test: " << error;
};

Expand All @@ -36,32 +39,33 @@ const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings";

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';
char *response_body;
char *error;
int response_code = tc_container_send_http_get(containerId, 8080, "/hello", response_body, error);

ASSERT_NE(response_code, -1) << "Failed to send HTTP request: " << error;
ASSERT_EQ(response_code, 200) << "Received wrong response code: " << response_body << error;

std::cout << "Server Response: HTTP-" << response_code << '\n' << response_body << '\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';
char *response_body;
char *error;
int response_code = tc_container_send_http_get(containerId, 8080, "/hello-from-resource", response_body, error);

ASSERT_NE(response_code, -1) << "Failed to send HTTP request: " << error;
ASSERT_EQ(response_code, 200) << "Received wrong response code: " << response_body << error;

std::cout << "Server Response: HTTP-" << response_code << '\n' << response_body << '\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";
}
char *response_body;
char *error;
int response_code = tc_container_send_http_get(containerId, 8080, "/hello-from-missing-resource", response_body, error);

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
ASSERT_EQ(response_code, 500) << "The request should have failed";
}
33 changes: 16 additions & 17 deletions demo/wiremock/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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)

include_directories(${testcontainers-c_SOURCE_DIR})
# WORKING_DIRECTORY breaks shared lib loading
file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

# WireMock Module demo
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_demo COMMAND ${TARGET_OUT})
cmake_minimum_required (VERSION 3.26)
project (wiremock-demo
VERSION 0.1.0
DESCRIPTION "Demonstrates usage of the WireMock module for Testcontainers C in a simple main app"
LANGUAGES C
)

set(TARGET_OUT demo_wiremock_module.out)

file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

# WireMock Module demo
add_executable(${TARGET_OUT} wiremock_module_demo.c)
target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-c)
target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-c-wiremock)
add_test(NAME wiremock_module_demo COMMAND ${TARGET_OUT})
28 changes: 15 additions & 13 deletions demo/wiremock/wiremock_module_demo.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#include <stdio.h>
#include <string.h>
#include "testcontainers-c-wiremock.h"
#include "testcontainers-c/container.h"

int main() {
printf("Using WireMock with the Testcontainers C binding:\n");

printf("Creating new container: %s\n", DEFAULT_WIREMOCK_IMAGE);
int requestId = tc_wm_new_default_container();
//FIXME: This method is bogus
tc_wm_with_mapping(requestId, "test_data/hello.json", "hello");
tc_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello2.json");
struct tc_run_container_return ret = tc_run_container(requestId);
int containerId = ret.r0;
if (!ret.r1) {
printf("Failed to run the container: %s\n", ret.r2);
// tc_wm_with_mapping(requestId, "test_data/hello.json", "hello");
tc_container_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello2.json");
char* error;
int containerId = tc_container_run(requestId, error);
if (containerId == -1) {
printf("Failed to run the container: %s\n", error);
if (containerId != -1) { // Print container log
char* log = tc_get_container_log(containerId);
char* log = tc_container_get_log(containerId);
if (log != NULL) {
printf("\n%s\n", log);
}
Expand All @@ -33,16 +34,17 @@ int main() {
}

printf("Sending HTTP request to the container\n");
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello");
if (response.r0 == -1) {
printf("Failed to send HTTP request: %s\n", response.r2);
char *response_body;
int response_code = tc_container_send_http_get(containerId, 8080, "/hello", response_body, error);
if (response_code == -1) {
printf("Failed to send HTTP request: %s\n", error);
return -1;
}
if (response.r0 != 200) {
printf("Received wrong response code: %d instead of %d\n%s\n", response.r0, 200, response.r2);
if (response_code != 200) {
printf("Received wrong response code: %d instead of %d\n%s\n", response_code, 200, error);
return -1;
}
printf("Server Response: HTTP-%d\n%s\n\n", response.r0, response.r1);
printf("Server Response: HTTP-%d\n%s\n\n", response_code, response_body);
return 0;
}

1 change: 0 additions & 1 deletion docs/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ enable_testing()
file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

add_executable(${TARGET_OUT} mytest.cpp)
add_dependencies(${TARGET_OUT} testcontainers-c-shim)
target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-c)
add_test(NAME wiremock_module_demo COMMAND ${TARGET_OUT})
```
Expand Down
30 changes: 14 additions & 16 deletions modules/wiremock/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
set(TARGET testcontainers-c-wiremock)
set(TARGET_NAME ${TARGET})
set(TARGET_DESCRIPTION "Wiremock testcontainer abstractions for C")
set(TARGET_VERSION ${PROJECT_VERSION})

cmake_minimum_required(VERSION 3.9)
project(testcontainers-c-wiremock VERSION 0.0.1
DESCRIPTION "WireMock module for Testcontainers C")

include(GNUInstallDirs)

add_library(${PROJECT_NAME} SHARED
testcontainers-c-wiremock.h
add_library(${TARGET} SHARED
impl.c
)
add_dependencies(${PROJECT_NAME} testcontainers-c)

include_directories(${testcontainers-c_SOURCE_DIR})
target_sources(${TARGET}
PUBLIC FILE_SET HEADERS
BASE_DIRS .
FILES testcontainers-c-wiremock.h
)

set_target_properties(${PROJECT_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
PUBLIC_HEADER testcontainers-c-wiremock.h)
target_link_libraries(${TARGET} PRIVATE testcontainers-c)

configure_file(cmake.pc.in ${PROJECT_NAME}.pc @ONLY)
install(TARGETS ${PROJECT_NAME}
configure_file(cmake.pc.in ${TARGET}.pc @ONLY)
install(TARGETS ${TARGET}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
install(FILES ${CMAKE_BINARY_DIR}/${TARGET}.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
8 changes: 4 additions & 4 deletions modules/wiremock/cmake.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@
Name: @TARGET_NAME@
Description: @TARGET_DESCRIPTION@
Version: @TARGET_VERSION@

Requires:
Libs: -L${libdir} -lmylib
Libs: -L${libdir}
Cflags: -I${includedir}
Loading