Skip to content

Commit

Permalink
chore: move internal/testcontainersdocker package's files to internal…
Browse files Browse the repository at this point in the history
…/core (testcontainers#2083)

* chore: move testcontainersdocker package's files to core

* chore: rename file

* chore: expose the docker socket location

Used by the localstack module
  • Loading branch information
mdelapenya authored Jan 9, 2024
1 parent f051b0c commit 62d6214
Show file tree
Hide file tree
Showing 28 changed files with 91 additions and 76 deletions.
4 changes: 2 additions & 2 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/moby/patternmatcher/ignorefile"

tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -273,7 +273,7 @@ func (c *ContainerRequest) GetAuthConfigs() map[string]registry.AuthConfig {

// getAuthConfigsFromDockerfile returns the auth configs to be able to pull from an authenticated docker registry
func getAuthConfigsFromDockerfile(c *ContainerRequest) map[string]registry.AuthConfig {
images, err := testcontainersdocker.ExtractImagesFromDockerfile(filepath.Join(c.Context, c.GetDockerfile()), c.GetBuildArgs())
images, err := core.ExtractImagesFromDockerfile(filepath.Join(c.Context, c.GetDockerfile()), c.GetBuildArgs())
if err != nil {
return map[string]registry.AuthConfig{}
}
Expand Down
17 changes: 8 additions & 9 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/internal/config"
"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -934,7 +933,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
// the reaper does not need to start a reaper for itself
isReaperContainer := strings.HasSuffix(imageName, config.ReaperDefaultImage)
if !tcConfig.RyukDisabled && !isReaperContainer {
r, err := reuseOrCreateReaper(context.WithValue(ctx, testcontainersdocker.DockerHostContextKey, p.host), core.SessionID(), p)
r, err := reuseOrCreateReaper(context.WithValue(ctx, core.DockerHostContextKey, p.host), core.SessionID(), p)
if err != nil {
return nil, fmt.Errorf("%w: creating reaper failed", err)
}
Expand Down Expand Up @@ -1030,7 +1029,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque

if !isReaperContainer {
// add the labels that the reaper will use to terminate the container to the request
for k, v := range testcontainersdocker.DefaultLabels(core.SessionID()) {
for k, v := range core.DefaultLabels(core.SessionID()) {
req.Labels[k] = v
}
}
Expand Down Expand Up @@ -1190,7 +1189,7 @@ func (p *DockerProvider) ReuseOrCreateContainer(ctx context.Context, req Contain

var termSignal chan bool
if !tcConfig.RyukDisabled {
r, err := reuseOrCreateReaper(context.WithValue(ctx, testcontainersdocker.DockerHostContextKey, p.host), sessionID, p)
r, err := reuseOrCreateReaper(context.WithValue(ctx, core.DockerHostContextKey, p.host), sessionID, p)
if err != nil {
return nil, fmt.Errorf("%w: creating reaper failed", err)
}
Expand Down Expand Up @@ -1304,10 +1303,10 @@ func daemonHost(ctx context.Context, p *DockerProvider) (string, error) {
case "http", "https", "tcp":
p.hostCache = url.Hostname()
case "unix", "npipe":
if testcontainersdocker.InAContainer() {
if core.InAContainer() {
ip, err := p.GetGatewayIP(ctx)
if err != nil {
ip, err = testcontainersdocker.DefaultGatewayIP()
ip, err = core.DefaultGatewayIP()
if err != nil {
ip = "localhost"
}
Expand Down Expand Up @@ -1359,7 +1358,7 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)

var termSignal chan bool
if !tcConfig.RyukDisabled {
r, err := reuseOrCreateReaper(context.WithValue(ctx, testcontainersdocker.DockerHostContextKey, p.host), sessionID, p)
r, err := reuseOrCreateReaper(context.WithValue(ctx, core.DockerHostContextKey, p.host), sessionID, p)
if err != nil {
return nil, fmt.Errorf("%w: creating network reaper failed", err)
}
Expand All @@ -1370,7 +1369,7 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
}

// add the labels that the reaper will use to terminate the network to the request
for k, v := range testcontainersdocker.DefaultLabels(sessionID) {
for k, v := range core.DefaultLabels(sessionID) {
req.Labels[k] = v
}

Expand Down Expand Up @@ -1466,7 +1465,7 @@ func (p *DockerProvider) getDefaultNetwork(ctx context.Context, cli client.APICl
_, err = cli.NetworkCreate(ctx, reaperNetwork, types.NetworkCreate{
Driver: Bridge,
Attachable: true,
Labels: testcontainersdocker.DefaultLabels(core.SessionID()),
Labels: core.DefaultLabels(core.SessionID()),
})

if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions docker_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"github.com/cpuguy83/dockercfg"
"github.com/docker/docker/api/types/registry"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/core"
)

// DockerImageAuth returns the auth config for the given Docker image, extracting first its Docker registry.
// Finally, it will use the credential helpers to extract the information from the docker config file
// for that registry, if it exists.
func DockerImageAuth(ctx context.Context, image string) (string, registry.AuthConfig, error) {
defaultRegistry := defaultRegistry(ctx)
reg := testcontainersdocker.ExtractRegistry(image, defaultRegistry)
reg := core.ExtractRegistry(image, defaultRegistry)

cfgs, err := getDockerAuthConfigs()
if err != nil {
Expand Down Expand Up @@ -58,13 +58,13 @@ func getRegistryAuth(reg string, cfgs map[string]registry.AuthConfig) (registry.
func defaultRegistry(ctx context.Context) string {
client, err := NewDockerClientWithOpts(ctx)
if err != nil {
return testcontainersdocker.IndexDockerIO
return core.IndexDockerIO
}
defer client.Close()

info, err := client.Info(ctx)
if err != nil {
return testcontainersdocker.IndexDockerIO
return core.IndexDockerIO
}

return info.IndexServerAddress
Expand Down
4 changes: 2 additions & 2 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/wait"
)

const exampleAuth = "https://example-auth.com"

var testDockerConfigDirPath = filepath.Join("testdata", ".docker")

var indexDockerIO = testcontainersdocker.IndexDockerIO
var indexDockerIO = core.IndexDockerIO

func TestGetDockerConfig(t *testing.T) {
const expectedErrorMessage = "Expected to find %s in auth configs"
Expand Down
7 changes: 3 additions & 4 deletions docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/docker/docker/client"

"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
)

// DockerClient is a wrapper around the docker client that is used by testcontainers-go.
Expand Down Expand Up @@ -66,8 +65,8 @@ func (c *DockerClient) Info(ctx context.Context) (types.Info, error) {
Logger.Printf(infoMessage, packagePath,
dockerInfo.ServerVersion, c.Client.ClientVersion(),
dockerInfo.OperatingSystem, dockerInfo.MemTotal/1024/1024,
testcontainersdocker.ExtractDockerHost(ctx),
testcontainersdocker.ExtractDockerSocket(ctx),
core.ExtractDockerHost(ctx),
core.ExtractDockerSocket(ctx),
core.SessionID(),
core.ProcessID(),
)
Expand Down Expand Up @@ -101,7 +100,7 @@ func NewDockerClient() (*client.Client, error) {
}

func NewDockerClientWithOpts(ctx context.Context, opt ...client.Opt) (*DockerClient, error) {
dockerClient, err := testcontainersdocker.NewClient(ctx, opt...)
dockerClient, err := core.NewClient(ctx, opt...)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"

"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
)

var (
Expand Down Expand Up @@ -95,5 +94,5 @@ type GenericProvider interface {

// GenericLabels returns a map of labels that can be used to identify containers created by this library
func GenericLabels() map[string]string {
return testcontainersdocker.DefaultLabels(core.SessionID())
return core.DefaultLabels(core.SessionID())
}
6 changes: 3 additions & 3 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"path/filepath"
"testing"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/core"
)

func TestImageList(t *testing.T) {
t.Setenv("DOCKER_HOST", testcontainersdocker.ExtractDockerHost(context.Background()))
t.Setenv("DOCKER_HOST", core.ExtractDockerHost(context.Background()))

provider, err := ProviderDocker.GetProvider()
if err != nil {
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestImageList(t *testing.T) {
}

func TestSaveImages(t *testing.T) {
t.Setenv("DOCKER_HOST", testcontainersdocker.ExtractDockerHost(context.Background()))
t.Setenv("DOCKER_HOST", core.ExtractDockerHost(context.Background()))

provider, err := ProviderDocker.GetProvider()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"context"
Expand All @@ -8,7 +8,6 @@ import (

"github.com/testcontainers/testcontainers-go/internal"
"github.com/testcontainers/testcontainers-go/internal/config"
"github.com/testcontainers/testcontainers-go/internal/core"
)

// NewClient returns a new docker client extracting the docker host from the different alternatives
Expand All @@ -33,8 +32,8 @@ func NewClient(ctx context.Context, ops ...client.Opt) (*client.Client, error) {

opts = append(opts, client.WithHTTPHeaders(
map[string]string{
"x-tc-pp": core.ProjectPath(),
"x-tc-sid": core.SessionID(),
"x-tc-pp": ProjectPath(),
"x-tc-sid": SessionID(),
"User-Agent": "tc-go/" + internal.Version,
}),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"net/url"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"bufio"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"path/filepath"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testcontainersdocker
package core

import (
"github.com/testcontainers/testcontainers-go/internal"
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions modules/localstack/localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"golang.org/x/mod/semver"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/network"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -70,7 +69,7 @@ func WithNetwork(networkName string, alias string) testcontainers.CustomizeReque
// RunContainer creates an instance of the LocalStack container type, being possible to pass a custom request and options:
// - overrideReq: a function that can be used to override the default container request, usually used to set the image version, environment variables for localstack, etc.
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*LocalStackContainer, error) {
dockerHost := testcontainersdocker.ExtractDockerSocket(ctx)
dockerHost := testcontainers.ExtractDockerSocket()

req := testcontainers.ContainerRequest{
Image: fmt.Sprintf("localstack/localstack:%s", defaultVersion),
Expand Down
4 changes: 2 additions & 2 deletions network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/network"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestContainerIPs(t *testing.T) {
}

func TestContainerWithReaperNetwork(t *testing.T) {
if testcontainersdocker.IsWindows() {
if core.IsWindows() {
t.Skip("Skip for Windows. See https://stackoverflow.com/questions/43784916/docker-for-windows-networking-container-with-multiple-network-interfaces")
}

Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/docker/docker/api/types/network"

tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -102,7 +102,7 @@ func (p prependHubRegistry) Description() string {
// - if the image is a Docker Hub image where the hub registry is explicitly part of the name
// (i.e. anything with a docker.io or registry.hub.docker.com host part), the image is returned as is.
func (p prependHubRegistry) Substitute(image string) (string, error) {
registry := testcontainersdocker.ExtractRegistry(image, "")
registry := core.ExtractRegistry(image, "")

// add the exclusions in the right order
exclusions := []func() bool{
Expand Down
4 changes: 2 additions & 2 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"strings"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/core"
)

// possible provider types
Expand Down Expand Up @@ -146,7 +146,7 @@ func NewDockerProvider(provOpts ...DockerProviderOption) (*DockerProvider, error

tcConfig := ReadConfig()

dockerHost := testcontainersdocker.ExtractDockerHost(ctx)
dockerHost := core.ExtractDockerHost(ctx)

p := &DockerProvider{
DockerProviderOptions: o,
Expand Down
6 changes: 3 additions & 3 deletions provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"testing"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/core"
)

func TestProviderTypeGetProviderAutodetect(t *testing.T) {
dockerHost := testcontainersdocker.ExtractDockerHost(context.Background())
dockerHost := core.ExtractDockerHost(context.Background())
const podmanSocket = "unix://$XDG_RUNTIME_DIR/podman/podman.sock"

tests := []struct {
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestProviderTypeGetProviderAutodetect(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.tr == ProviderPodman && testcontainersdocker.IsWindows() {
if tt.tr == ProviderPodman && core.IsWindows() {
t.Skip("Podman provider is not implemented for Windows")
}

Expand Down
Loading

0 comments on commit 62d6214

Please sign in to comment.