Skip to content

Commit 39a591e

Browse files
Merge pull request #53 from seffradev/c-abstraction
Extended C library abstraction
2 parents d6ec912 + 90304b2 commit 39a591e

File tree

13 files changed

+285
-130
lines changed

13 files changed

+285
-130
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ project (TESTCONTAINERS-C
88
include(GNUInstallDirs)
99
include(CTest)
1010

11+
add_subdirectory(testcontainers-bridge)
1112
add_subdirectory(testcontainers-c)
1213
add_subdirectory(modules)
1314
if(NOT DEFINED SKIP_DEMOS)
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
#include <stdio.h>
2-
#include "testcontainers-c.h"
2+
#include "testcontainers-c/container.h"
33

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

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

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

2121
printf("Sending HTTP request to the container\n");
22-
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello");
23-
if (response.r0 == -1) {
24-
printf("Failed to send HTTP request: %s\n", response.r2);
22+
char *response_body;
23+
int response_code = tc_container_send_http_get(containerId, 8080, "/hello", response_body, error);
24+
if (response_code == -1) {
25+
printf("Failed to send HTTP request: %s\n", error);
2526
return -1;
2627
}
27-
if (response.r0 != 200) {
28-
printf("Received wrong response code: %d instead of %d\n%s\n%s\n", response.r0, 200, response.r1, response.r2);
28+
if (response_code != 200) {
29+
printf("Received wrong response code: %d instead of %d\n%s\n%s\n", response_code, 200, response_body, error);
2930
return -1;
3031
}
31-
printf("Server Response: HTTP-%d\n%s\n\n", response.r0, response.r1);
32+
printf("Server Response: HTTP-%d\n%s\n\n", response_code, response_body);
3233
return 0;
3334
}
3435

demo/google-test/test.cpp

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include <iostream>
22
#include <string>
33
#include <gtest/gtest.h>
4-
#include "testcontainers-c.h"
4+
5+
extern "C" {
6+
#include "testcontainers-c/container.h"
7+
}
58

69
class WireMockTestContainer : public ::testing::Test {
710

@@ -11,23 +14,23 @@ const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings";
1114
protected:
1215
void SetUp() override {
1316
std::cout << "Creating new container: " << WIREMOCK_IMAGE << '\n';
14-
15-
int requestId = tc_new_container_request(WIREMOCK_IMAGE);
16-
tc_with_exposed_tcp_port(requestId, 8080);
17-
tc_with_wait_for_http(requestId, 8080, WIREMOCK_ADMIN_MAPPING_ENDPOINT);
18-
tc_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json");
19-
tc_with_file(requestId, "test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json");
20-
tc_with_file(requestId, "test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json");
21-
tc_with_file(requestId, "test_data/response.xml", "/home/wiremock/__files/response.xml");
22-
23-
struct tc_run_container_return ret = tc_run_container(requestId);
24-
containerId = ret.r0;
25-
26-
EXPECT_TRUE(ret.r1) << "Failed to run the container: " << ret.r2;
17+
18+
int requestId = tc_container_create(WIREMOCK_IMAGE);
19+
tc_container_with_exposed_tcp_port(requestId, 8080);
20+
tc_container_with_wait_for_http(requestId, 8080, WIREMOCK_ADMIN_MAPPING_ENDPOINT);
21+
tc_container_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello.json");
22+
tc_container_with_file(requestId, "test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json");
23+
tc_container_with_file(requestId, "test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json");
24+
tc_container_with_file(requestId, "test_data/response.xml", "/home/wiremock/__files/response.xml");
25+
26+
char* error;
27+
int containerId = tc_container_run(requestId, error);
28+
29+
EXPECT_TRUE(containerId != -1) << "Failed to run the container: " << error;
2730
};
2831

2932
void TearDown() override {
30-
char* error = tc_terminate_container(containerId);
33+
char* error = tc_container_terminate(containerId);
3134
ASSERT_EQ(error, nullptr) << "Failed to terminate the container after the test: " << error;
3235
};
3336

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

3740
TEST_F(WireMockTestContainer, HelloWorld) {
3841
std::cout << "Sending HTTP request to the container\n";
39-
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello");
40-
41-
ASSERT_NE(response.r0, -1) << "Failed to send HTTP request: " << response.r2;
42-
ASSERT_EQ(response.r0, 200) << "Received wrong response code: " << response.r1 << response.r2;
43-
44-
std::cout << "Server Response: HTTP-" << response.r0 << '\n' << response.r1 << '\n';
42+
char *response_body;
43+
char *error;
44+
int response_code = tc_container_send_http_get(containerId, 8080, "/hello", response_body, error);
45+
46+
ASSERT_NE(response_code, -1) << "Failed to send HTTP request: " << error;
47+
ASSERT_EQ(response_code, 200) << "Received wrong response code: " << response_body << error;
48+
49+
std::cout << "Server Response: HTTP-" << response_code << '\n' << response_body << '\n';
4550
}
4651

4752
TEST_F(WireMockTestContainer, HelloWorldFromResource) {
4853
std::cout << "Sending HTTP request to the container\n";
49-
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello-from-resource");
50-
51-
ASSERT_NE(response.r0, -1) << "Failed to send HTTP request: " << response.r2;
52-
ASSERT_EQ(response.r0, 200) << "Received wrong response code: " << response.r1 << response.r2;
53-
54-
std::cout << "Server Response: HTTP-" << response.r0 << '\n' << response.r1 << '\n';
54+
char *response_body;
55+
char *error;
56+
int response_code = tc_container_send_http_get(containerId, 8080, "/hello-from-resource", response_body, error);
57+
58+
ASSERT_NE(response_code, -1) << "Failed to send HTTP request: " << error;
59+
ASSERT_EQ(response_code, 200) << "Received wrong response code: " << response_body << error;
60+
61+
std::cout << "Server Response: HTTP-" << response_code << '\n' << response_body << '\n';
5562
}
5663

5764
TEST_F(WireMockTestContainer, HelloWorldFromMissingResource) {
5865
std::cout << "Sending HTTP request to the container\n";
59-
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello-from-missing-resource");
60-
61-
ASSERT_EQ(response.r0, 500) << "The request should have failed";
62-
}
66+
char *response_body;
67+
char *error;
68+
int response_code = tc_container_send_http_get(containerId, 8080, "/hello-from-missing-resource", response_body, error);
6369

64-
int main(int argc, char **argv) {
65-
::testing::InitGoogleTest(&argc, argv);
66-
return RUN_ALL_TESTS();
70+
ASSERT_EQ(response_code, 500) << "The request should have failed";
6771
}
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
#include <stdio.h>
22
#include <string.h>
33
#include "testcontainers-c-wiremock.h"
4+
#include "testcontainers-c/container.h"
45

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

89
printf("Creating new container: %s\n", DEFAULT_WIREMOCK_IMAGE);
910
int requestId = tc_wm_new_default_container();
1011
//FIXME: This method is bogus
11-
tc_wm_with_mapping(requestId, "test_data/hello.json", "hello");
12-
tc_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello2.json");
13-
struct tc_run_container_return ret = tc_run_container(requestId);
14-
int containerId = ret.r0;
15-
if (!ret.r1) {
16-
printf("Failed to run the container: %s\n", ret.r2);
12+
// tc_wm_with_mapping(requestId, "test_data/hello.json", "hello");
13+
tc_container_with_file(requestId, "test_data/hello.json", "/home/wiremock/mappings/hello2.json");
14+
char* error;
15+
int containerId = tc_container_run(requestId, error);
16+
if (containerId == -1) {
17+
printf("Failed to run the container: %s\n", error);
1718
if (containerId != -1) { // Print container log
18-
char* log = tc_get_container_log(containerId);
19+
char* log = tc_container_get_log(containerId);
1920
if (log != NULL) {
2021
printf("\n%s\n", log);
2122
}
@@ -33,16 +34,17 @@ int main() {
3334
}
3435

3536
printf("Sending HTTP request to the container\n");
36-
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/hello");
37-
if (response.r0 == -1) {
38-
printf("Failed to send HTTP request: %s\n", response.r2);
37+
char *response_body;
38+
int response_code = tc_container_send_http_get(containerId, 8080, "/hello", response_body, error);
39+
if (response_code == -1) {
40+
printf("Failed to send HTTP request: %s\n", error);
3941
return -1;
4042
}
41-
if (response.r0 != 200) {
42-
printf("Received wrong response code: %d instead of %d\n%s\n", response.r0, 200, response.r2);
43+
if (response_code != 200) {
44+
printf("Received wrong response code: %d instead of %d\n%s\n", response_code, 200, error);
4345
return -1;
4446
}
45-
printf("Server Response: HTTP-%d\n%s\n\n", response.r0, response.r1);
47+
printf("Server Response: HTTP-%d\n%s\n\n", response_code, response_body);
4648
return 0;
4749
}
4850

modules/wiremock/impl.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#include "testcontainers-c-wiremock.h"
2+
#include "testcontainers-c/container.h"
23
#include <string.h>
34
#include <stdio.h>
45

5-
GoInt tc_wm_new_default_container() {
6+
int tc_wm_new_default_container() {
67
return tc_wm_new_container(DEFAULT_WIREMOCK_IMAGE);
78
}
89

9-
GoInt tc_wm_new_container(char* image) {
10-
GoInt requestId = tc_new_container_request(image);
11-
tc_with_exposed_tcp_port(requestId, 8080);
12-
tc_with_wait_for_http(requestId, 8080, "/__admin/mappings");
10+
int tc_wm_new_container(char* image) {
11+
int requestId = tc_container_create(image);
12+
tc_container_with_exposed_tcp_port(requestId, 8080);
13+
tc_container_with_wait_for_http(requestId, 8080, "/__admin/mappings");
1314
return requestId;
1415
};
1516

16-
void tc_wm_with_mapping(GoInt requestID, char* filePath, char* destination) {
17+
void tc_wm_with_mapping(int requestID, char* filePath, char* destination) {
1718
char dest_file[128] = "";
1819
strcat(dest_file, WIREMOCK_MAPPINGS_DIR);
1920
strcat(dest_file, destination);
20-
21+
2122
// Append extension if missing
2223
if(strlen(destination) > 5 && !strcmp(destination + strlen(destination) - 5, ".json")) {
2324
strcat(dest_file, ".json");
@@ -27,21 +28,23 @@ void tc_wm_with_mapping(GoInt requestID, char* filePath, char* destination) {
2728
}
2829

2930
printf("DEBUG: %s to %s.\n", filePath, dest_file);
30-
tc_with_file(requestID, filePath, dest_file);
31+
tc_container_with_file(requestID, filePath, dest_file);
3132
};
3233

33-
struct WireMock_Mapping tc_wm_get_mappings(GoInt containerId) {
34-
struct tc_send_http_get_return response = tc_send_http_get(containerId, 8080, "/__admin/mappings");
35-
if (response.r0 == -1) {
34+
struct WireMock_Mapping tc_wm_get_mappings(int containerId) {
35+
char *response_body;
36+
char *error;
37+
int response_code = tc_container_send_http_get(containerId, 8080, "/__admin/mappings", response_body, error);
38+
if (response_code == -1) {
3639
char errorMsg[8000] = "";
37-
sprintf(errorMsg, "Failed to send HTTP request: %s\n", response.r2);
40+
sprintf(errorMsg, "Failed to send HTTP request: %s\n", error);
3841
return (struct WireMock_Mapping) { -1, NULL, errorMsg};
3942
}
40-
if (response.r0 != 200) {
43+
if (response_code != 200) {
4144
char errorMsg[8000] = "";
42-
sprintf(errorMsg, "Received wrong response code: %d instead of %d\n%s\n%s\n", response.r0, 200, response.r1, response.r2);
43-
return (struct WireMock_Mapping) { response.r0, NULL, errorMsg};
45+
sprintf(errorMsg, "Received wrong response code: %d instead of %d\n%s\n%s\n", response_code, 200, response_body, error);
46+
return (struct WireMock_Mapping) { response_code, NULL, errorMsg};
4447
}
4548

46-
return (struct WireMock_Mapping) {response.r0, response.r1, NULL};
49+
return (struct WireMock_Mapping) {response_code, response_body, NULL};
4750
};
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include "testcontainers-c.h"
2-
31
#ifndef TESTCONTAINERS_WIREMOCK_H
42
#define TESTCONTAINERS_WIREMOCK_H
53

@@ -13,29 +11,29 @@
1311

1412
struct WireMock_Mapping
1513
{
16-
GoInt responseCode;
14+
int responseCode;
1715
char* json;
1816
char* error;
1917
};
2018

2119
/// @brief Creates a container request with a default image, exposed port and init logic
2220
/// @return Container request ID
23-
GoInt tc_wm_new_default_container();
21+
int tc_wm_new_default_container();
2422

2523
/// @brief Creates a container request with a default image, exposed port and init logic
2624
/// @param image Full image name
2725
/// @return Container request ID
28-
GoInt tc_wm_new_container(char* image);
26+
int tc_wm_new_container(char* image);
2927

3028
/// @brief Adds WireMock mapping to the request
3129
/// @param requestID Container Request ID
3230
/// @param filePath Source file in the local filesystem
3331
/// @param destination Destination file, relative to the mappings dir. Extension is optional
34-
void tc_wm_with_mapping(GoInt requestID, char* filePath, char* destination);
32+
void tc_wm_with_mapping(int requestID, char* filePath, char* destination);
3533

3634
/// @brief Gets WireMock mappings using Admin API
3735
/// @param containerId Container ID
3836
/// @return Mapping information if response code is 200, error details otherwise
39-
struct WireMock_Mapping tc_wm_get_mappings(GoInt containerId);
37+
struct WireMock_Mapping tc_wm_get_mappings(int containerId);
4038

4139
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
set(SRCS testcontainers-bridge.go)
2+
3+
set(SHIM_TARGET testcontainers-bridge-shim)
4+
set(SHIM_TARGET_LIB testcontainers-bridge.so)
5+
set(SHIM_TARGET_HEADER testcontainers-bridge.h)
6+
7+
add_custom_command(OUTPUT ${SHIM_TARGET_LIB} ${SHIM_TARGET_HEADER}
8+
DEPENDS ${SRCS}
9+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
10+
COMMAND env GOPATH=${GOPATH} go build -buildmode=c-archive -linkshared
11+
-o "${CMAKE_CURRENT_BINARY_DIR}/${SHIM_TARGET_LIB}"
12+
${CMAKE_GO_FLAGS} ${SRCS}
13+
COMMENT "Building Testcontainers Go to C bridge library")
14+
15+
add_custom_target(${SHIM_TARGET} DEPENDS ${SHIM_TARGET_LIB} ${SHIM_TARGET_HEADER})
16+
17+
set(TARGET testcontainers-bridge)
18+
set(TARGET_NAME ${TARGET})
19+
set(TARGET_DESCRIPTION "Go to C bridge for Testcontainers functionality")
20+
set(TARGET_VERSION ${PROJECT_VERSION})
21+
22+
add_library(${TARGET} STATIC IMPORTED GLOBAL)
23+
add_dependencies(${TARGET} ${SHIM_TARGET})
24+
set_target_properties(${TARGET} PROPERTIES
25+
LINKER_LANGUAGE C
26+
VERSION ${TARGET_VERSION}
27+
PUBLIC_HEADER ${SHIM_TARGET_HEADER}
28+
IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${SHIM_TARGET_LIB}
29+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_BINARY_DIR})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/testcontainers/testcontainers-c
1+
module github.com/testcontainers/testcontainers-native
22

33
go 1.19
44

0 commit comments

Comments
 (0)