Skip to content

Commit

Permalink
Add dktesting.Cleanup() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dhui committed Apr 15, 2024
1 parent e913336 commit 4bc6777
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions dktesting/dktesting.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package dktesting

import (
"context"
"fmt"
"testing"
)

import (
"github.com/dhui/dktest"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)

// ContainerSpec holds Docker testing setup specifications
Expand All @@ -14,6 +18,26 @@ type ContainerSpec struct {
Options dktest.Options
}

// Cleanup cleanups the ContainerSpec after a test run by removing the ContainerSpec's image
func (s *ContainerSpec) Cleanup() (retErr error) {
// copied from dktest.RunContext()
dc, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.41"))
if err != nil {
return err
}
defer func() {
if err := dc.Close(); err != nil && retErr == nil {
retErr = fmt.Errorf("error closing Docker client: %w", err)
}
}()
ctx, timeoutCancelFunc := context.WithTimeout(context.Background(), s.Options.CleanupTimeout)
defer timeoutCancelFunc()
if _, err := dc.ImageRemove(ctx, s.ImageName, types.ImageRemoveOptions{Force: true, PruneChildren: true}); err != nil {
return err
}
return nil
}

// ParallelTest runs Docker tests in parallel
func ParallelTest(t *testing.T, specs []ContainerSpec,
testFunc func(*testing.T, dktest.ContainerInfo)) {
Expand Down

0 comments on commit 4bc6777

Please sign in to comment.