From fe4c8c3016d42e6cf462bb9958bd08a241b81f4b Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 22 Jul 2024 13:50:52 +0200 Subject: [PATCH] Save progress --- testcontainers-c/testcontainers-c-compose.go | 33 ++++++++++++++------ 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/testcontainers-c/testcontainers-c-compose.go b/testcontainers-c/testcontainers-c-compose.go index 87f9820..9ed2df5 100644 --- a/testcontainers-c/testcontainers-c-compose.go +++ b/testcontainers-c/testcontainers-c-compose.go @@ -6,23 +6,28 @@ import "C" import ( "context" "fmt" - "io" - "net/http" - "strconv" - "strings" + "sync" - "github.com/docker/go-connections/nat" "github.com/testcontainers/testcontainers-go" - uuid "github.com/google/uuid" + "github.com/compose-spec/compose-go/v2/cli" + "github.com/compose-spec/compose-go/v2/types" + "github.com/docker/compose/v2/pkg/api" + "github.com/docker/docker/client" compose "github.com/testcontainers/testcontainers-go/modules/compose" "github.com/testcontainers/testcontainers-go/wait" ) -var composeGroups []compose.ComposeStack +var composeGroups []*DockerComposeRequest + +// FIXME: Copy of compose_api: dockerCompose. Export to public +type DockerComposeContext struct { + stack ComposeStack * + terminator func() error +} //export tc_new_docker_compose -func tc_new_docker_compose(path *C.cchar_t) (id int, ok bool, errstr *C.char) { +func tc_run_docker_compose(path *C.cchar_t) (id int, ok bool, errstr *C.char) { composePath := C.GoString(path) composeInstance, err := compose.NewDockerComposeWith(compose.WithStackFiles(composePath)) if err != nil { @@ -30,11 +35,21 @@ func tc_new_docker_compose(path *C.cchar_t) (id int, ok bool, errstr *C.char) { } composeId := -1 + request := DockerComposeContext() if composeInstance != nil { + terminator := func() error { + return composeInstance.Down(context.Background(), compose.RemoveOrphans(true), compose.RemoveImagesLocal) + } + + request.terminator = terminator + stack := composeInstance.Up(ctx, tc.WithRecreate(api.RecreateNever), tc.Wait(true)) + composeInstance.stack = &stack; + // We register the container even if the run failed - composeGroups = append(composeGroups, &composeInstance) + composeGroups = append(composeGroups, request) composeId = len(containers) - 1 } + return composeId, true, nil }