From 36573d0adcf650207fb9b8c61072518a84a09674 Mon Sep 17 00:00:00 2001 From: Bo Kleynen Date: Thu, 20 Jun 2024 07:09:40 +0200 Subject: [PATCH] fix: allow compose files and readers to be used together (#2598) --- modules/compose/compose_api.go | 8 ++--- modules/compose/compose_api_test.go | 47 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/modules/compose/compose_api.go b/modules/compose/compose_api.go index 5bdffaf7c8..8af2d38e7f 100644 --- a/modules/compose/compose_api.go +++ b/modules/compose/compose_api.go @@ -22,8 +22,8 @@ import ( "github.com/docker/docker/client" "golang.org/x/sync/errgroup" - testcontainers "github.com/testcontainers/testcontainers-go" - wait "github.com/testcontainers/testcontainers-go/wait" + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/wait" ) type stackUpOptionFunc func(s *stackUpOptions) @@ -149,7 +149,7 @@ func (r ComposeStackReaders) applyToComposeStack(o *composeStackOptions) error { o.temporaryPaths[f[i]] = true } - o.Paths = f + o.Paths = append(o.Paths, f...) return nil } @@ -157,7 +157,7 @@ func (r ComposeStackReaders) applyToComposeStack(o *composeStackOptions) error { type ComposeStackFiles []string func (f ComposeStackFiles) applyToComposeStack(o *composeStackOptions) error { - o.Paths = f + o.Paths = append(o.Paths, f...) return nil } diff --git a/modules/compose/compose_api_test.go b/modules/compose/compose_api_test.go index bd895c3b3a..e4dff06cfd 100644 --- a/modules/compose/compose_api_test.go +++ b/modules/compose/compose_api_test.go @@ -472,6 +472,53 @@ services: require.Nil(t, f, "File should be removed") } +func TestDockerComposeAPIWithStackReaderAndComposeFile(t *testing.T) { + identifier := testNameHash(t.Name()) + simple, _ := RenderComposeSimple(t) + composeContent := `version: '3.7' +services: + api-postgres: + image: docker.io/postgres:14 + environment: + POSTGRES_PASSWORD: s3cr3t +` + + compose, err := NewDockerComposeWith( + identifier, + WithStackFiles(simple), + WithStackReaders(strings.NewReader(composeContent)), + ) + require.NoError(t, err, "NewDockerCompose()") + + t.Cleanup(func() { + require.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()") + }) + + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + + err = compose. + WithEnv(map[string]string{ + "bar": "BAR", + "foo": "FOO", + }). + Up(ctx, Wait(true)) + require.NoError(t, err, "compose.Up()") + + serviceNames := compose.Services() + + assert.Len(t, serviceNames, 2) + assert.Contains(t, serviceNames, "api-nginx") + assert.Contains(t, serviceNames, "api-postgres") + + present := map[string]string{ + "bar": "BAR", + "foo": "FOO", + } + absent := map[string]string{} + assertContainerEnvironmentVariables(t, identifier.String(), "api-nginx", present, absent) +} + func TestDockerComposeAPIWithEnvironment(t *testing.T) { identifier := testNameHash(t.Name())