Skip to content

Commit

Permalink
revert TerminateOption to terminateOptions, seperate from DockerConta…
Browse files Browse the repository at this point in the history
…iner and introduce configuration test
  • Loading branch information
moogacs committed Dec 21, 2024
1 parent 4c3c5b5 commit 4fab35b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
14 changes: 7 additions & 7 deletions cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ type terminateOptions struct {
}

// TerminateOption is a type that represents an option for terminating a container.
type TerminateOption func(*DockerContainer)
type TerminateOption func(*terminateOptions)

// StopContext returns a TerminateOption that sets the context.
// Default: context.Background().
func StopContext(ctx context.Context) TerminateOption {
return func(c *DockerContainer) {
c.terminationOptions.ctx = ctx
return func(c *terminateOptions) {
c.ctx = ctx
}
}

// StopTimeout returns a TerminateOption that sets the timeout.
// Default: See [Container.Stop].
func StopTimeout(timeout time.Duration) TerminateOption {
return func(c *DockerContainer) {
c.terminationOptions.timeout = &timeout
return func(c *terminateOptions) {
c.timeout = &timeout
}
}

Expand All @@ -38,8 +38,8 @@ func StopTimeout(timeout time.Duration) TerminateOption {
// which are not removed by default.
// Default: nil.
func RemoveVolumes(volumes ...string) TerminateOption {
return func(c *DockerContainer) {
c.terminationOptions.volumes = volumes
return func(c *terminateOptions) {
c.volumes = volumes
}
}

Expand Down
26 changes: 16 additions & 10 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ type DockerContainer struct {
logProductionCtx context.Context

logProductionTimeout *time.Duration
terminationOptions terminateOptions
logger Logging
lifecycleHooks []ContainerLifecycleHooks

Expand Down Expand Up @@ -299,8 +298,15 @@ func (c *DockerContainer) Stop(ctx context.Context, timeout *time.Duration) erro

// WithTerminateTimeout is a functional option that sets the timeout for the container termination.
func WithTerminateTimeout(timeout time.Duration) TerminateOption {
return func(c *DockerContainer) {
c.terminationOptions.timeout = &timeout
return func(c *terminateOptions) {
c.timeout = &timeout
}
}

// WithTerminateVolumes is a functional option that sets the volumes for the container termination.
func WithTerminateVolumes(volumes ...string) TerminateOption {
return func(opts *terminateOptions) {
opts.volumes = volumes
}
}

Expand All @@ -314,16 +320,16 @@ func WithTerminateTimeout(timeout time.Duration) TerminateOption {
//
// Default: timeout is 10 seconds.
func (c *DockerContainer) Terminate(ctx context.Context, opts ...TerminateOption) error {
if len(opts) == 0 {
d := 10 * time.Second
c.terminationOptions.timeout = &d
defaultTimeout := 10 * time.Second
options := &terminateOptions{
timeout: &defaultTimeout,
}

for _, opt := range opts {
opt(c)
opt(options)
}

err := c.Stop(ctx, c.terminationOptions.timeout)
err := c.Stop(ctx, options.timeout)
if err != nil && !isCleanupSafe(err) {
return fmt.Errorf("stop: %w", err)
}
Expand Down Expand Up @@ -359,7 +365,7 @@ func (c *DockerContainer) Terminate(ctx context.Context, opts ...TerminateOption
c.isRunning = false

// Remove additional volumes if any.
if len(c.terminationOptions.volumes) == 0 {
if len(options.volumes) == 0 {
return nil
}

Expand All @@ -371,7 +377,7 @@ func (c *DockerContainer) Terminate(ctx context.Context, opts ...TerminateOption
defer client.Close()

// Best effort to remove all volumes.
for _, volume := range c.terminationOptions.volumes {
for _, volume := range options.volumes {
if errRemove := client.VolumeRemove(ctx, volume, true); errRemove != nil {
errs = append(errs, fmt.Errorf("volume remove %q: %w", volume, errRemove))
}
Expand Down
50 changes: 50 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,56 @@ func TestContainerCreationWithVolumeAndFileWritingToIt(t *testing.T) {
require.NoError(t, err)
}

func TestContainerCreationWithVolumeCleaning(t *testing.T) {
absPath, err := filepath.Abs(filepath.Join(".", "testdata", "hello.sh"))
require.NoError(t, err)
ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second)
defer cnl()

// Create the volume.
volumeName := "volumeName"

// Create the container that writes into the mounted volume.
bashC, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: "bash:5.2.26",
Files: []ContainerFile{
{
HostFilePath: absPath,
ContainerFilePath: "/hello.sh",
},
},
Mounts: Mounts(VolumeMount(volumeName, "/data")),
Cmd: []string{"bash", "/hello.sh"},
WaitingFor: wait.ForLog("done"),
},
Started: true,
})
require.NoError(t, err)
err = bashC.Terminate(ctx, WithTerminateVolumes(volumeName))
require.NoError(t, err)
}

func TestContainerTerminationOptions(t *testing.T) {
volumeName := "volumeName"
definedVolumeOpt := &terminateOptions{}
volumeOpt := WithTerminateVolumes(volumeName)
volumeOpt(definedVolumeOpt)
require.Equal(t, definedVolumeOpt.volumes, []string{volumeName})

defaultTimeout := 10 * time.Second
definedTimeoutOpt := &terminateOptions{
timeout: &defaultTimeout,
}

configuredTimeout := 1 * time.Second

timeoutOpt := WithTerminateTimeout(1 * time.Second)
timeoutOpt(definedTimeoutOpt)
require.Equal(t, *definedTimeoutOpt.timeout, configuredTimeout)
}

func TestContainerWithTmpFs(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Expand Down

0 comments on commit 4fab35b

Please sign in to comment.