Skip to content

Commit

Permalink
rename some variables that shadowed imports
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jun 14, 2024
1 parent 5d68a52 commit 1f04d1a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 68 deletions.
66 changes: 33 additions & 33 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type DockerContainer struct {

isRunning bool
imageWasBuilt bool
// keepBuiltImage makes Terminate not remove the image if imageWasBuilt.
// keepBuiltImage makes Terminate not remove the sshdImage if imageWasBuilt.
keepBuiltImage bool
provider *DockerProvider
sessionID string
Expand Down Expand Up @@ -171,12 +171,12 @@ func (c *DockerContainer) Inspect(ctx context.Context) (*types.ContainerJSON, er
return c.raw, nil
}

json, err := c.inspectRawContainer(ctx)
jsonRaw, err := c.inspectRawContainer(ctx)
if err != nil {
return nil, err
}

return json, nil
return jsonRaw, nil
}

// MappedPort gets externally mapped port for a container port
Expand Down Expand Up @@ -1056,15 +1056,15 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
if req.AlwaysPullImage {
shouldPullImage = true // If requested always attempt to pull image
} else {
image, _, err := p.client.ImageInspectWithRaw(ctx, imageName)
img, _, err := p.client.ImageInspectWithRaw(ctx, imageName)
if err != nil {
if client.IsErrNotFound(err) {
shouldPullImage = true
} else {
return nil, err
}
}
if platform != nil && (image.Architecture != platform.Architecture || image.Os != platform.OS) {
if platform != nil && (img.Architecture != platform.Architecture || img.Os != platform.OS) {
shouldPullImage = true
}
}
Expand Down Expand Up @@ -1202,8 +1202,8 @@ func (p *DockerProvider) findContainerByName(ctx context.Context, name string) (
}

func (p *DockerProvider) waitContainerCreation(ctx context.Context, name string) (*types.Container, error) {
var container *types.Container
return container, backoff.Retry(func() error {
var ctr *types.Container
return ctr, backoff.Retry(func() error {
c, err := p.findContainerByName(ctx, name)
if err != nil {
if !errdefs.IsNotFound(err) && isPermanentClientError(err) {
Expand All @@ -1216,7 +1216,7 @@ func (p *DockerProvider) waitContainerCreation(ctx context.Context, name string)
return fmt.Errorf("container %s not found", name)
}

container = c
ctr = c
return nil
}, backoff.WithContext(backoff.NewExponentialBackOff(), ctx))
}
Expand Down Expand Up @@ -1289,8 +1289,8 @@ func (p *DockerProvider) ReuseOrCreateContainer(ctx context.Context, req Contain
return dc, nil
}

// attemptToPullImage tries to pull the image while respecting the ctx cancellations.
// Besides, if the image cannot be pulled due to ErrorNotFound then no need to retry but terminate immediately.
// attemptToPullImage tries to pull the sshdImage while respecting the ctx cancellations.
// Besides, if the sshdImage cannot be pulled due to ErrorNotFound then no need to retry but terminate immediately.
func (p *DockerProvider) attemptToPullImage(ctx context.Context, tag string, pullOpt types.ImagePullOptions) error {
registry, imageAuth, err := DockerImageAuth(ctx, tag)
if err != nil {
Expand Down Expand Up @@ -1377,15 +1377,15 @@ func daemonHost(ctx context.Context, p *DockerProvider) (string, error) {
}

// infer from Docker host
url, err := url.Parse(p.client.DaemonHost())
daemonURL, err := url.Parse(p.client.DaemonHost())
if err != nil {
return "", err
}
defer p.Close()

switch url.Scheme {
switch daemonURL.Scheme {
case "http", "https", "tcp":
p.hostCache = url.Hostname()
p.hostCache = daemonURL.Hostname()
case "unix", "npipe":
if core.InAContainer() {
ip, err := p.GetGatewayIP(ctx)
Expand Down Expand Up @@ -1510,9 +1510,9 @@ func (p *DockerProvider) GetGatewayIP(ctx context.Context) (string, error) {
}

var ip string
for _, config := range nw.IPAM.Config {
if config.Gateway != "" {
ip = config.Gateway
for _, cfg := range nw.IPAM.Config {
if cfg.Gateway != "" {
ip = cfg.Gateway
break
}
}
Expand Down Expand Up @@ -1566,38 +1566,38 @@ func containerFromDockerResponse(ctx context.Context, response types.Container)
return nil, err
}

container := DockerContainer{}
ctr := DockerContainer{}

container.ID = response.ID
container.WaitingFor = nil
container.Image = response.Image
container.imageWasBuilt = false
ctr.ID = response.ID
ctr.WaitingFor = nil
ctr.Image = response.Image
ctr.imageWasBuilt = false

container.logger = provider.Logger
container.lifecycleHooks = []ContainerLifecycleHooks{
DefaultLoggingHook(container.logger),
ctr.logger = provider.Logger
ctr.lifecycleHooks = []ContainerLifecycleHooks{
DefaultLoggingHook(ctr.logger),
}
container.provider = provider
ctr.provider = provider

container.sessionID = core.SessionID()
container.consumers = []LogConsumer{}
container.isRunning = response.State == "running"
ctr.sessionID = core.SessionID()
ctr.consumers = []LogConsumer{}
ctr.isRunning = response.State == "running"

// the termination signal should be obtained from the reaper
container.terminationSignal = nil
ctr.terminationSignal = nil

// populate the raw representation of the container
_, err = container.inspectRawContainer(ctx)
_, err = ctr.inspectRawContainer(ctx)
if err != nil {
return nil, err
}

// the health status of the container, if any
if health := container.raw.State.Health; health != nil {
container.healthStatus = health.Status
if health := ctr.raw.State.Health; health != nil {
ctr.healthStatus = health.Status
}

return &container, nil
return &ctr, nil
}

// ListImages list images from the provider. If an image has multiple Tags, each tag is reported
Expand Down
2 changes: 1 addition & 1 deletion docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestBuildContainerFromDockerfile(t *testing.T) {
terminateContainerOnEnd(t, ctx, redisC)
}

// removeImageFromLocalCache removes the image from the local cache
// removeImageFromLocalCache removes the sshdImage from the local cache
func removeImageFromLocalCache(t *testing.T, image string) {
ctx := context.Background()

Expand Down
62 changes: 31 additions & 31 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ func TestContainerStateAfterTermination(t *testing.T) {
func TestContainerTerminationRemovesDockerImage(t *testing.T) {
t.Run("if not built from Dockerfile", func(t *testing.T) {
ctx := context.Background()
client, err := NewDockerClientWithOpts(ctx)
dockerClient, err := NewDockerClientWithOpts(ctx)
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer dockerClient.Close()

container, err := GenericContainer(ctx, GenericContainerRequest{
ctr, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: nginxAlpineImage,
Expand All @@ -353,23 +353,23 @@ func TestContainerTerminationRemovesDockerImage(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = container.Terminate(ctx)
err = ctr.Terminate(ctx)
if err != nil {
t.Fatal(err)
}
_, _, err = client.ImageInspectWithRaw(ctx, nginxAlpineImage)
_, _, err = dockerClient.ImageInspectWithRaw(ctx, nginxAlpineImage)
if err != nil {
t.Fatal("nginx image should not have been removed")
}
})

t.Run("if built from Dockerfile", func(t *testing.T) {
ctx := context.Background()
client, err := NewDockerClientWithOpts(ctx)
dockerClient, err := NewDockerClientWithOpts(ctx)
if err != nil {
t.Fatal(err)
}
defer client.Close()
defer dockerClient.Close()

req := ContainerRequest{
FromDockerfile: FromDockerfile{
Expand All @@ -378,27 +378,27 @@ func TestContainerTerminationRemovesDockerImage(t *testing.T) {
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("Ready to accept connections"),
}
container, err := GenericContainer(ctx, GenericContainerRequest{
ctr, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
})
if err != nil {
t.Fatal(err)
}
containerID := container.GetContainerID()
resp, err := client.ContainerInspect(ctx, containerID)
containerID := ctr.GetContainerID()
resp, err := dockerClient.ContainerInspect(ctx, containerID)
if err != nil {
t.Fatal(err)
}
imageID := resp.Config.Image

err = container.Terminate(ctx)
err = ctr.Terminate(ctx)
if err != nil {
t.Fatal(err)
}

_, _, err = client.ImageInspectWithRaw(ctx, imageID)
_, _, err = dockerClient.ImageInspectWithRaw(ctx, imageID)
if err == nil {
t.Fatal("custom built image should have been removed", err)
}
Expand Down Expand Up @@ -1163,19 +1163,19 @@ func TestContainerWithTmpFs(t *testing.T) {
Tmpfs: map[string]string{"/testtmpfs": "rw"},
}

container, err := GenericContainer(ctx, GenericContainerRequest{
ctr, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
})

require.NoError(t, err)
terminateContainerOnEnd(t, ctx, container)
terminateContainerOnEnd(t, ctx, ctr)

path := "/testtmpfs/test.file"

// exec_reader_example {
c, reader, err := container.Exec(ctx, []string{"ls", path})
c, reader, err := ctr.Exec(ctx, []string{"ls", path})
if err != nil {
t.Fatal(err)
}
Expand All @@ -1194,7 +1194,7 @@ func TestContainerWithTmpFs(t *testing.T) {
// }

// exec_example {
c, _, err = container.Exec(ctx, []string{"touch", path})
c, _, err = ctr.Exec(ctx, []string{"touch", path})
if err != nil {
t.Fatal(err)
}
Expand All @@ -1203,7 +1203,7 @@ func TestContainerWithTmpFs(t *testing.T) {
}
// }

c, _, err = container.Exec(ctx, []string{"ls", path})
c, _, err = ctr.Exec(ctx, []string{"ls", path})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1308,39 +1308,39 @@ func TestContainerWithCustomHostname(t *testing.T) {
Image: nginxImage,
Hostname: hostname,
}
container, err := GenericContainer(ctx, GenericContainerRequest{
ctr, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
})

require.NoError(t, err)
terminateContainerOnEnd(t, ctx, container)
terminateContainerOnEnd(t, ctx, ctr)

if actualHostname := readHostname(t, container.GetContainerID()); actualHostname != hostname {
if actualHostname := readHostname(t, ctr.GetContainerID()); actualHostname != hostname {
t.Fatalf("expected hostname %s, got %s", hostname, actualHostname)
}
}

func TestContainerInspect_RawInspectIsCleanedOnStop(t *testing.T) {
container, err := GenericContainer(context.Background(), GenericContainerRequest{
ctr, err := GenericContainer(context.Background(), GenericContainerRequest{
ContainerRequest: ContainerRequest{
Image: nginxImage,
},
Started: true,
})
require.NoError(t, err)
terminateContainerOnEnd(t, context.Background(), container)
terminateContainerOnEnd(t, context.Background(), ctr)

inspect, err := container.Inspect(context.Background())
inspect, err := ctr.Inspect(context.Background())
require.NoError(t, err)

assert.NotEmpty(t, inspect.ID)

require.NoError(t, container.Stop(context.Background(), nil))
require.NoError(t, ctr.Stop(context.Background(), nil))

// type assertion to ensure that the container is a DockerContainer
dc := container.(*DockerContainer)
dc := ctr.(*DockerContainer)

assert.Nil(t, dc.raw)
}
Expand Down Expand Up @@ -1829,16 +1829,16 @@ func TestContainerWithUserID(t *testing.T) {
Cmd: []string{"sh", "-c", "id -u"},
WaitingFor: wait.ForExit(),
}
container, err := GenericContainer(ctx, GenericContainerRequest{
ctr, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
})

require.NoError(t, err)
terminateContainerOnEnd(t, ctx, container)
terminateContainerOnEnd(t, ctx, ctr)

r, err := container.Logs(ctx)
r, err := ctr.Logs(ctx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1858,16 +1858,16 @@ func TestContainerWithNoUserID(t *testing.T) {
Cmd: []string{"sh", "-c", "id -u"},
WaitingFor: wait.ForExit(),
}
container, err := GenericContainer(ctx, GenericContainerRequest{
ctr, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
})

require.NoError(t, err)
terminateContainerOnEnd(t, ctx, container)
terminateContainerOnEnd(t, ctx, ctr)

r, err := container.Logs(ctx)
r, err := ctr.Logs(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type ImageSubstitutor interface {

// }

// prependHubRegistry represents a way to prepend a custom Hub registry to the image name,
// prependHubRegistry represents a way to prepend a custom Hub registry to the sshdImage name,
// using the HubImageNamePrefix configuration value
type prependHubRegistry struct {
prefix string
Expand Down
Loading

0 comments on commit 1f04d1a

Please sign in to comment.