Skip to content

Commit 3e56fb9

Browse files
thaJeztahaauschclaudemdelapenya
authored
chore!: migrate to moby modules (#3591)
--------- Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Co-authored-by: Alex Ausch <alex.ausch@going.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: mdelapenya <mdelapenya@gmail.com>
1 parent 66bb0e5 commit 3e56fb9

File tree

239 files changed

+2805
-4312
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+2805
-4312
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ formatters:
88
- standard
99
- default
1010
- prefix(github.com/testcontainers)
11+
1112
linters:
1213
enable:
1314
- errorlint
@@ -115,4 +116,5 @@ output:
115116
path: stdout
116117
run:
117118
relative-path-mode: gitroot
119+
118120
version: "2"

cleanup.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"reflect"
88
"time"
9+
10+
"github.com/moby/moby/client"
911
)
1012

1113
// TerminateOptions is a type that holds the options for terminating a container.
@@ -48,15 +50,15 @@ func (o *TerminateOptions) Cleanup() error {
4850
if len(o.volumes) == 0 {
4951
return nil
5052
}
51-
client, err := NewDockerClientWithOpts(o.ctx)
53+
apiClient, err := NewDockerClientWithOpts(o.ctx)
5254
if err != nil {
5355
return fmt.Errorf("docker client: %w", err)
5456
}
55-
defer client.Close()
57+
defer apiClient.Close()
5658
// Best effort to remove all volumes.
5759
var errs []error
5860
for _, volume := range o.volumes {
59-
if errRemove := client.VolumeRemove(o.ctx, volume, true); errRemove != nil {
61+
if _, errRemove := apiClient.VolumeRemove(o.ctx, volume, client.VolumeRemoveOptions{Force: true}); errRemove != nil {
6062
errs = append(errs, fmt.Errorf("volume remove %q: %w", volume, errRemove))
6163
}
6264
}

commons-test.mk

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ $(GOBIN)/gotestsum:
1414
$(GOBIN)/mockery:
1515
$(call go_install,github.com/vektra/mockery/v2@v2.53.4)
1616

17+
$(GOBIN)/gci:
18+
$(call go_install,github.com/daixiang0/gci@v0.13.5)
19+
1720
.PHONY: install
1821
install: $(GOBIN)/golangci-lint $(GOBIN)/gotestsum $(GOBIN)/mockery
1922

@@ -30,9 +33,10 @@ dependencies-scan:
3033

3134
.PHONY: lint
3235
lint: $(GOBIN)/golangci-lint
33-
golangci-lint run --verbose -c $(ROOT_DIR)/.golangci.yml --fix
36+
golangci-lint run -c $(ROOT_DIR)/.golangci.yml --fix
3437

3538
.PHONY: generate
39+
generate: $(GOBIN)/gci
3640
generate: $(GOBIN)/mockery
3741
go generate ./...
3842

container.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ import (
1313
"time"
1414

1515
"github.com/cpuguy83/dockercfg"
16-
"github.com/docker/docker/api/types/build"
17-
"github.com/docker/docker/api/types/container"
18-
"github.com/docker/docker/api/types/network"
19-
"github.com/docker/docker/api/types/registry"
20-
"github.com/docker/go-connections/nat"
2116
"github.com/google/uuid"
2217
"github.com/moby/go-archive"
18+
"github.com/moby/moby/api/types/container"
19+
"github.com/moby/moby/api/types/network"
20+
"github.com/moby/moby/api/types/registry"
21+
"github.com/moby/moby/client"
2322
"github.com/moby/patternmatcher/ignorefile"
2423

2524
tcexec "github.com/testcontainers/testcontainers-go/exec"
@@ -34,23 +33,23 @@ import (
3433
type DeprecatedContainer interface {
3534
GetHostEndpoint(ctx context.Context, port string) (string, string, error)
3635
GetIPAddress(ctx context.Context) (string, error)
37-
LivenessCheckPorts(ctx context.Context) (nat.PortSet, error)
36+
LivenessCheckPorts(ctx context.Context) (network.PortSet, error)
3837
Terminate(ctx context.Context) error
3938
}
4039

4140
// Container allows getting info about and controlling a single container instance
4241
type Container interface {
43-
GetContainerID() string // get the container id from the provider
44-
Endpoint(context.Context, string) (string, error) // get proto://ip:port string for the lowest exposed port
45-
PortEndpoint(ctx context.Context, port nat.Port, proto string) (string, error) // get proto://ip:port string for the given exposed port
46-
Host(context.Context) (string, error) // get host where the container port is exposed
47-
Inspect(context.Context) (*container.InspectResponse, error) // get container info
48-
MappedPort(context.Context, nat.Port) (nat.Port, error) // get externally mapped port for a container port
49-
Ports(context.Context) (nat.PortMap, error) // Deprecated: Use c.Inspect(ctx).NetworkSettings.Ports instead
50-
SessionID() string // get session id
51-
IsRunning() bool // IsRunning returns true if the container is running, false otherwise.
52-
Start(context.Context) error // start the container
53-
Stop(context.Context, *time.Duration) error // stop the container
42+
GetContainerID() string // get the container id from the provider
43+
Endpoint(context.Context, string) (string, error) // get proto://ip:port string for the lowest exposed port
44+
PortEndpoint(ctx context.Context, port string, proto string) (string, error) // get proto://ip:port string for the given exposed port
45+
Host(context.Context) (string, error) // get host where the container port is exposed
46+
Inspect(context.Context) (*container.InspectResponse, error) // get container info
47+
MappedPort(context.Context, string) (network.Port, error) // get externally mapped port for a container port
48+
Ports(context.Context) (network.PortMap, error) // Deprecated: Use c.Inspect(ctx).NetworkSettings.Ports instead
49+
SessionID() string // get session id
50+
IsRunning() bool // IsRunning returns true if the container is running, false otherwise.
51+
Start(context.Context) error // start the container
52+
Stop(context.Context, *time.Duration) error // stop the container
5453

5554
// Terminate stops and removes the container and its image if it was built and not flagged as kept.
5655
Terminate(ctx context.Context, opts ...TerminateOption) error
@@ -75,15 +74,15 @@ type Container interface {
7574

7675
// ImageBuildInfo defines what is needed to build an image
7776
type ImageBuildInfo interface {
78-
BuildOptions() (build.ImageBuildOptions, error) // converts the ImageBuildInfo to a build.ImageBuildOptions
79-
GetContext() (io.Reader, error) // the path to the build context
80-
GetDockerfile() string // the relative path to the Dockerfile, including the file itself
81-
GetRepo() string // get repo label for image
82-
GetTag() string // get tag label for image
83-
BuildLogWriter() io.Writer // for output of build log, use io.Discard to disable the output
84-
ShouldBuildImage() bool // return true if the image needs to be built
85-
GetBuildArgs() map[string]*string // return the environment args used to build the Dockerfile
86-
GetAuthConfigs() map[string]registry.AuthConfig // Deprecated. Testcontainers will detect registry credentials automatically. Return the auth configs to be able to pull from an authenticated docker registry
77+
BuildOptions() (client.ImageBuildOptions, error) // converts the ImageBuildInfo to a build.ImageBuildOptions
78+
GetContext() (io.Reader, error) // the path to the build context
79+
GetDockerfile() string // the relative path to the Dockerfile, including the file itself
80+
GetRepo() string // get repo label for image
81+
GetTag() string // get tag label for image
82+
BuildLogWriter() io.Writer // for output of build log, use io.Discard to disable the output
83+
ShouldBuildImage() bool // return true if the image needs to be built
84+
GetBuildArgs() map[string]*string // return the environment args used to build the Dockerfile
85+
GetAuthConfigs() map[string]registry.AuthConfig // Deprecated. Testcontainers will detect registry credentials automatically. Return the auth configs to be able to pull from an authenticated docker registry
8786
}
8887

8988
// FromDockerfile represents the parameters needed to build an image from a Dockerfile
@@ -105,7 +104,7 @@ type FromDockerfile struct {
105104
// BuildOptionsModifier Modifier for the build options before image build. Use it for
106105
// advanced configurations while building the image. Please consider that the modifier
107106
// is called after the default build options are set.
108-
BuildOptionsModifier func(*build.ImageBuildOptions)
107+
BuildOptionsModifier func(*client.ImageBuildOptions)
109108
}
110109

111110
type ContainerFile struct {
@@ -435,8 +434,8 @@ func (c *ContainerRequest) BuildLogWriter() io.Writer {
435434
// BuildOptions returns the image build options when building a Docker image from a Dockerfile.
436435
// It will apply some defaults and finally call the BuildOptionsModifier from the FromDockerfile struct,
437436
// if set.
438-
func (c *ContainerRequest) BuildOptions() (build.ImageBuildOptions, error) {
439-
buildOptions := build.ImageBuildOptions{
437+
func (c *ContainerRequest) BuildOptions() (client.ImageBuildOptions, error) {
438+
buildOptions := client.ImageBuildOptions{
440439
Remove: true,
441440
ForceRemove: true,
442441
}
@@ -452,7 +451,7 @@ func (c *ContainerRequest) BuildOptions() (build.ImageBuildOptions, error) {
452451
// Make sure the auth configs from the Dockerfile are set right after the user-defined build options.
453452
authsFromDockerfile, err := getAuthConfigsFromDockerfile(c)
454453
if err != nil {
455-
return build.ImageBuildOptions{}, fmt.Errorf("auth configs from Dockerfile: %w", err)
454+
return client.ImageBuildOptions{}, fmt.Errorf("auth configs from Dockerfile: %w", err)
456455
}
457456

458457
if buildOptions.AuthConfigs == nil {
@@ -468,7 +467,7 @@ func (c *ContainerRequest) BuildOptions() (build.ImageBuildOptions, error) {
468467
for _, is := range c.ImageSubstitutors {
469468
modifiedTag, err := is.Substitute(tag)
470469
if err != nil {
471-
return build.ImageBuildOptions{}, fmt.Errorf("failed to substitute image %s with %s: %w", tag, is.Description(), err)
470+
return client.ImageBuildOptions{}, fmt.Errorf("failed to substitute image %s with %s: %w", tag, is.Description(), err)
472471
}
473472

474473
if modifiedTag != tag {
@@ -487,18 +486,18 @@ func (c *ContainerRequest) BuildOptions() (build.ImageBuildOptions, error) {
487486
if !c.ShouldKeepBuiltImage() {
488487
dst := GenericLabels()
489488
if err = core.MergeCustomLabels(dst, c.Labels); err != nil {
490-
return build.ImageBuildOptions{}, err
489+
return client.ImageBuildOptions{}, err
491490
}
492491
if err = core.MergeCustomLabels(dst, buildOptions.Labels); err != nil {
493-
return build.ImageBuildOptions{}, err
492+
return client.ImageBuildOptions{}, err
494493
}
495494
buildOptions.Labels = dst
496495
}
497496

498497
// Do this as late as possible to ensure we don't leak the context on error/panic.
499498
buildContext, err := c.GetContext()
500499
if err != nil {
501-
return build.ImageBuildOptions{}, err
500+
return client.ImageBuildOptions{}, err
502501
}
503502

504503
buildOptions.Context = buildContext

container_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"testing"
1111
"time"
1212

13-
"github.com/docker/docker/api/types/build"
14-
"github.com/docker/docker/api/types/container"
13+
"github.com/moby/moby/api/types/container"
14+
"github.com/moby/moby/client"
1515
"github.com/stretchr/testify/require"
1616

1717
"github.com/testcontainers/testcontainers-go"
@@ -342,7 +342,7 @@ func TestCustomLabelsBuildOptionsModifier(t *testing.T) {
342342
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
343343
Context: "./testdata",
344344
Dockerfile: "Dockerfile",
345-
BuildOptionsModifier: func(opts *build.ImageBuildOptions) {
345+
BuildOptionsModifier: func(opts *client.ImageBuildOptions) {
346346
opts.Labels = map[string]string{
347347
myBuildOptionLabel: myBuildOptionValue,
348348
}
@@ -377,8 +377,8 @@ func Test_GetLogsFromFailedContainer(t *testing.T) {
377377
b, err := io.ReadAll(logs)
378378
require.NoError(t, err)
379379

380-
log := string(b)
381-
require.Contains(t, log, "I was not expecting this")
380+
out := string(b)
381+
require.Contains(t, out, "I was not expecting this")
382382
}
383383

384384
// dockerImageSubstitutor {
@@ -497,7 +497,7 @@ func TestShouldStartContainersInParallel(t *testing.T) {
497497
// }
498498
require.NoError(t, err)
499499

500-
t.Logf("Parallel container [iteration_%d] listening on %d\n", i, port.Int())
500+
t.Logf("Parallel container [iteration_%d] listening on %d\n", i, port.Num())
501501
})
502502
}
503503
}

0 commit comments

Comments
 (0)