Skip to content

Commit

Permalink
fix: pass docker context key when reusing a container (#550)
Browse files Browse the repository at this point in the history
* fix: pass docker context key when reusing a container

* feat: add sessionID HTTP Header to the Docker client setup (#570)

* chore: create network's session ID for reaper, only

* chore: include reaper-specific labels to the reaper container

* feat: initialise sessionID just once

* chore: do not expose the sessionID function

* fix: update reaper tests

* chore: add unit tests for extracting the docker host for reaper

* chore: add tests for the docker host when it's passed as part of the context
  • Loading branch information
mdelapenya committed Oct 19, 2022
1 parent db41dd1 commit 322aa4e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ func (p *DockerProvider) ReuseOrCreateContainer(ctx context.Context, req Contain
sessionID := sessionID()
var termSignal chan bool
if !req.SkipReaper {
r, err := NewReaper(ctx, sessionID.String(), p, req.ReaperImage)
r, err := NewReaper(context.WithValue(ctx, dockerHostContextKey, p.host), sessionID.String(), p, req.ReaperImage)
if err != nil {
return nil, fmt.Errorf("%w: creating reaper failed", err)
}
Expand Down
55 changes: 54 additions & 1 deletion reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func Test_NewReaper(t *testing.T) {
name string
req ContainerRequest
config TestContainersConfig
ctx context.Context
}

tests := []cases{
Expand All @@ -76,6 +77,15 @@ func Test_NewReaper(t *testing.T) {
RyukPrivileged: true,
},
},
{
name: "docker-host in context",
req: createContainerRequest(func(req ContainerRequest) ContainerRequest {
req.Mounts = Mounts(BindMount("/value/in/context.sock", "/var/run/docker.sock"))
return req
}),
config: TestContainersConfig{},
ctx: context.WithValue(context.TODO(), dockerHostContextKey, "unix:///value/in/context.sock"),
},
}

for _, test := range tests {
Expand All @@ -86,11 +96,54 @@ func Test_NewReaper(t *testing.T) {
config: test.config,
}

_, err := NewReaper(context.TODO(), "sessionId", provider, "reaperImage")
if test.ctx == nil {
test.ctx = context.TODO()
}

_, err := NewReaper(test.ctx, "sessionId", provider, "reaperImage")
// we should have errored out see mockReaperProvider.RunContainer
assert.EqualError(t, err, "expected")

assert.Equal(t, test.req, provider.req, "expected ContainerRequest doesn't match the submitted request")
})
}
}

func Test_ExtractDockerHost(t *testing.T) {
t.Run("Docker Host as environment variable", func(t *testing.T) {
t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock")
host := extractDockerHost(context.Background())

assert.Equal(t, "/path/to/docker.sock", host)
})

t.Run("Default Docker Host", func(t *testing.T) {
host := extractDockerHost(context.Background())

assert.Equal(t, "/var/run/docker.sock", host)
})

t.Run("Malformed Docker Host is passed in context", func(t *testing.T) {
ctx := context.Background()

host := extractDockerHost(context.WithValue(ctx, dockerHostContextKey, "path-to-docker-sock"))

assert.Equal(t, "/var/run/docker.sock", host)
})

t.Run("Malformed Schema Docker Host is passed in context", func(t *testing.T) {
ctx := context.Background()

host := extractDockerHost(context.WithValue(ctx, dockerHostContextKey, "http://path to docker sock"))

assert.Equal(t, "/var/run/docker.sock", host)
})

t.Run("Unix Docker Host is passed in context", func(t *testing.T) {
ctx := context.Background()

host := extractDockerHost(context.WithValue(ctx, dockerHostContextKey, "unix:///this/is/a/sample.sock"))

assert.Equal(t, "/this/is/a/sample.sock", host)
})
}

0 comments on commit 322aa4e

Please sign in to comment.