Skip to content

Commit

Permalink
feat(compose): select services via profiles
Browse files Browse the repository at this point in the history
This commit allows users of the compose module to selectively enable services
by using Docker Compose profiles.

More about profiles: https://docs.docker.com/compose/profiles
  • Loading branch information
ngrash committed Sep 2, 2024
1 parent ab41ba5 commit debea8e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type composeStackOptions struct {
Paths []string
temporaryPaths map[string]bool
Logger testcontainers.Logging
Profiles []string
}

type ComposeStackOption interface {
Expand Down Expand Up @@ -116,6 +117,11 @@ func WithStackReaders(readers ...io.Reader) ComposeStackOption {
return ComposeStackReaders(readers)
}

// WithProfiles allows to enable/disable services based on the profiles defined in the compose file.
func WithProfiles(profiles ...string) ComposeStackOption {
return ComposeProfiles(profiles)
}

func NewDockerCompose(filePaths ...string) (*dockerCompose, error) {
return NewDockerComposeWith(WithStackFiles(filePaths...))
}
Expand All @@ -125,6 +131,7 @@ func NewDockerComposeWith(opts ...ComposeStackOption) (*dockerCompose, error) {
Identifier: uuid.New().String(),
temporaryPaths: make(map[string]bool),
Logger: testcontainers.Logger,
Profiles: nil,
}

for i := range opts {
Expand Down Expand Up @@ -168,6 +175,7 @@ func NewDockerComposeWith(opts ...ComposeStackOption) (*dockerCompose, error) {
configs: composeOptions.Paths,
temporaryConfigs: composeOptions.temporaryPaths,
logger: composeOptions.Logger,
projectProfiles: composeOptions.Profiles,
composeService: compose.NewComposeService(dockerCli),
dockerClient: dockerCli.Client(),
waitStrategies: make(map[string]wait.Strategy),
Expand Down
17 changes: 17 additions & 0 deletions modules/compose/compose_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ func (f ComposeStackFiles) applyToComposeStack(o *composeStackOptions) error {
return nil
}

type ComposeProfiles []string

func (p ComposeProfiles) applyToComposeStack(o *composeStackOptions) error {
o.Profiles = append(o.Profiles, p...)
return nil
}

type StackIdentifier string

func (f StackIdentifier) applyToComposeStack(o *composeStackOptions) error {
Expand Down Expand Up @@ -212,6 +219,9 @@ type dockerCompose struct {
// e.g. environment settings, ...
projectOptions []cli.ProjectOptionsFn

// profiles applied to the compose project after compilation.
projectProfiles []string

// compiled compose project
// can be nil if the stack wasn't started yet
project *types.Project
Expand Down Expand Up @@ -512,6 +522,13 @@ func (d *dockerCompose) compileProject(ctx context.Context) (*types.Project, err
return nil, fmt.Errorf("load project: %w", err)
}

if len(d.projectProfiles) > 0 {
proj, err = proj.WithProfiles(d.projectProfiles)
if err != nil {
return nil, fmt.Errorf("with profiles: %w", err)
}
}

for i, s := range proj.Services {
s.CustomLabels = map[string]string{
api.ProjectLabel: proj.Name,
Expand Down
53 changes: 53 additions & 0 deletions modules/compose/compose_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,59 @@ func TestDockerComposeAPIWithRunServices(t *testing.T) {
assert.Contains(t, serviceNames, "api-nginx")
}

func TestDockerComposeAPIWithProfiles(t *testing.T) {
path := RenderComposeProfiles(t)

testcases := map[string]struct {
withProfiles []string
wantServices []string
}{
"nil profile": {
withProfiles: nil,
wantServices: []string{"starts-always"},
},
"no profiles": {
withProfiles: []string{},
wantServices: []string{"starts-always"},
},
"dev profile": {
withProfiles: []string{"dev"},
wantServices: []string{"starts-always", "only-dev", "dev-or-test"},
},
"test profile": {
withProfiles: []string{"test"},
wantServices: []string{"starts-always", "dev-or-test"},
},
"wildcard profile": {
withProfiles: []string{"*"},
wantServices: []string{"starts-always", "only-dev", "dev-or-test", "only-prod"},
},
"undefined profile": {
withProfiles: []string{"undefined-profile"},
wantServices: []string{"starts-always"},
},
}

for name, test := range testcases {
t.Run(name, func(t *testing.T) {
compose, err := NewDockerComposeWith(WithStackFiles(path), WithProfiles(test.withProfiles...))
require.NoError(t, err, "NewDockerCompose()")

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

for _, service := range test.wantServices {
compose = compose.WaitForService(service, wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).(*dockerCompose)
}
err = compose.Up(ctx, Wait(true))
cleanup(t, compose)
require.NoError(t, err, "compose.Up()")

assert.ElementsMatch(t, test.wantServices, compose.Services())
})
}
}

func TestDockerComposeAPI_TestcontainersLabelsArePresent(t *testing.T) {
path, _ := RenderComposeComplex(t)
compose, err := NewDockerCompose(path)
Expand Down
6 changes: 6 additions & 0 deletions modules/compose/compose_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const (
testdataPackage = "testdata"
)

func RenderComposeProfiles(t *testing.T) string {
t.Helper()

return writeTemplate(t, "docker-compose-profiles.yml")
}

func RenderComposeComplex(t *testing.T) (string, []int) {
t.Helper()

Expand Down
25 changes: 25 additions & 0 deletions modules/compose/testdata/docker-compose-profiles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
starts-always:
image: docker.io/nginx:stable-alpine
ports:
- ":80"
# profiles: none defined, therefore always starts.
only-dev:
image: docker.io/nginx:stable-alpine
ports:
- ":80"
profiles:
- dev
dev-or-test:
image: docker.io/nginx:stable-alpine
ports:
- ":80"
profiles:
- dev
- test
only-prod:
image: docker.io/nginx:stable-alpine
ports:
- ":80"
profiles:
- prod

0 comments on commit debea8e

Please sign in to comment.