Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stop services in docker compose #918

Merged
merged 4 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var ErrNoStackConfigured = errors.New("no stack files configured")
type composeStackOptions struct {
Identifier string
Paths []string
Logger testcontainers.Logging
}

type ComposeStackOption interface {
Expand Down Expand Up @@ -102,6 +103,7 @@ func NewDockerCompose(filePaths ...string) (*dockerCompose, error) {
func NewDockerComposeWith(opts ...ComposeStackOption) (*dockerCompose, error) {
composeOptions := composeStackOptions{
Identifier: uuid.New().String(),
Logger: testcontainers.Logger,
}

for i := range opts {
Expand All @@ -124,6 +126,7 @@ func NewDockerComposeWith(opts ...ComposeStackOption) (*dockerCompose, error) {
composeAPI := &dockerCompose{
name: composeOptions.Identifier,
configs: composeOptions.Paths,
logger: composeOptions.Logger,
composeService: compose.NewComposeService(dockerCli),
dockerClient: dockerCli.Client(),
waitStrategies: make(map[string]wait.Strategy),
Expand Down
7 changes: 6 additions & 1 deletion modules/compose/compose_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ type dockerCompose struct {
// paths to stack files that will be considered when compiling the final compose project
configs []string

// used to set logger in DockerContainer
logger testcontainers.Logging

// wait strategies that are applied per service when starting the stack
// only one strategy can be added to a service, to use multiple use wait.ForAll(...)
waitStrategies map[string]wait.Strategy
Expand Down Expand Up @@ -301,8 +304,10 @@ func (d *dockerCompose) lookupContainer(ctx context.Context, svcName string) (*t

containerInstance := containers[0]
container := &testcontainers.DockerContainer{
ID: containerInstance.ID,
ID: containerInstance.ID,
Image: containerInstance.Image,
}
container.SetLogger(d.logger)

dockerProvider := &testcontainers.DockerProvider{}
dockerProvider.SetClient(d.dockerClient)
Expand Down
42 changes: 40 additions & 2 deletions modules/compose/compose_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package compose
import (
"context"
"fmt"
"github.com/docker/docker/api/types/filters"
"github.com/google/uuid"
"hash/fnv"
"path/filepath"
"testing"
"time"

"github.com/docker/docker/api/types/filters"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -115,6 +116,43 @@ func TestDockerComposeAPIWithRunServices(t *testing.T) {
assert.Contains(t, serviceNames, "nginx")
}

func TestDockerComposeAPIWithStopServices(t *testing.T) {
path := filepath.Join(testResourcesPackage, complexCompose)
compose, err := NewDockerComposeWith(
WithStackFiles(path),
WithLogger(testcontainers.TestLogger(t)))
assert.NoError(t, err, "NewDockerCompose()")

t.Cleanup(func() {
assert.NoError(t, compose.Down(context.Background(), RemoveOrphans(true), RemoveImagesLocal), "compose.Down()")
})

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

assert.NoError(t, compose.Up(ctx, Wait(true)), "compose.Up()")

serviceNames := compose.Services()

assert.Equal(t, 2, len(serviceNames))
assert.Contains(t, serviceNames, "nginx")
assert.Contains(t, serviceNames, "mysql")

// close mysql container in purpose
mysqlContainer, err := compose.ServiceContainer(context.Background(), "mysql")
assert.NoError(t, err, "Get mysql container")

stopTimeout := 10 * time.Second
err = mysqlContainer.Stop(ctx, &stopTimeout)
assert.NoError(t, err, "Stop mysql container")

// check container status
state, err := mysqlContainer.State(ctx)
assert.NoError(t, err)
assert.False(t, state.Running)
assert.Equal(t, "exited", state.Status)
}

func TestDockerComposeAPIWithWaitForService(t *testing.T) {
path := filepath.Join(testResourcesPackage, simpleCompose)
compose, err := NewDockerCompose(path)
Expand Down
4 changes: 4 additions & 0 deletions modules/compose/compose_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (o ComposeLoggerOption) ApplyToLocalCompose(opts *LocalDockerComposeOptions
opts.Logger = o.logger
}

func (o ComposeLoggerOption) applyToComposeStack(opts *composeStackOptions) {
opts.Logger = o.logger
}

func (f LocalDockerComposeOptionsFunc) ApplyToLocalCompose(opts *LocalDockerComposeOptions) {
f(opts)
}
Expand Down