Skip to content

Commit

Permalink
fix: allow compose files and readers to be used together (#2598)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoKleynen committed Jun 20, 2024
1 parent 1561ad9 commit 36573d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
8 changes: 4 additions & 4 deletions modules/compose/compose_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -149,15 +149,15 @@ func (r ComposeStackReaders) applyToComposeStack(o *composeStackOptions) error {
o.temporaryPaths[f[i]] = true
}

o.Paths = f
o.Paths = append(o.Paths, f...)

return nil
}

type ComposeStackFiles []string

func (f ComposeStackFiles) applyToComposeStack(o *composeStackOptions) error {
o.Paths = f
o.Paths = append(o.Paths, f...)
return nil
}

Expand Down
47 changes: 47 additions & 0 deletions modules/compose/compose_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 36573d0

Please sign in to comment.