From a497066f0c61f7ce43d4883e5ee4d8643ab5ce6b Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 25 Sep 2023 12:27:53 +0200 Subject: [PATCH] Add API for terminating containers --- demo/google-test/test.cpp | 4 ++-- testcontainers-c/testcontainers-c.go | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/demo/google-test/test.cpp b/demo/google-test/test.cpp index cb57c27..ae87581 100644 --- a/demo/google-test/test.cpp +++ b/demo/google-test/test.cpp @@ -27,8 +27,8 @@ const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings"; }; void TearDown() override { - // Code here will be called immediately after each test (right - // before the destructor). + char* error = tc_terminate_container(containerId); + ASSERT_EQ(error, nullptr) << "Failed to terminate the container after the test: " << error; }; int containerId; diff --git a/testcontainers-c/testcontainers-c.go b/testcontainers-c/testcontainers-c.go index fca9744..4a1db48 100644 --- a/testcontainers-c/testcontainers-c.go +++ b/testcontainers-c/testcontainers-c.go @@ -69,6 +69,13 @@ func _RunContainer(requestID int) (id int, ok bool, err error) { return containerId, true, nil } +//export tc_terminate_container +func tc_terminate_container(containerID int) *C.char { + ctx := context.Background() + container := *containers[containerID] + return ToCString(container.Terminate(ctx)) +} + //export tc_get_container_log func tc_get_container_log(containerID int) (log *C.char) { ctx := context.Background() @@ -187,7 +194,10 @@ func SendHttpRequest(httpMethod string, container testcontainers.Container, port } func ToCString(err error) *C.char { - return C.CString(fmt.Sprintf("%v", err)) + if err != nil { + return C.CString(fmt.Sprintf("%v", err)) + } + return nil } func main() {}