Skip to content

Commit

Permalink
feat: add sessionID HTTP Header to the Docker client setup (#570)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mdelapenya committed Oct 18, 2022
1 parent d9bb488 commit db41dd1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
13 changes: 9 additions & 4 deletions docker.go
Expand Up @@ -745,6 +745,12 @@ func NewDockerClient() (cli *client.Client, host string, tcConfig TestContainers
host = "unix:///var/run/docker.sock"
}

opts = append(opts, client.WithHTTPHeaders(
map[string]string{
"x-tc-sid": sessionID().String(),
}),
)

cli, err = client.NewClientWithOpts(opts...)

if err != nil {
Expand Down Expand Up @@ -934,7 +940,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
req.Labels = make(map[string]string)
}

sessionID := uuid.New()
sessionID := sessionID()

var termSignal chan bool
if !req.SkipReaper {
Expand Down Expand Up @@ -1155,7 +1161,7 @@ func (p *DockerProvider) ReuseOrCreateContainer(ctx context.Context, req Contain
return p.CreateContainer(ctx, req)
}

sessionID := uuid.New()
sessionID := sessionID()
var termSignal chan bool
if !req.SkipReaper {
r, err := NewReaper(ctx, sessionID.String(), p, req.ReaperImage)
Expand Down Expand Up @@ -1310,10 +1316,9 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
IPAM: req.IPAM,
}

sessionID := uuid.New()

var termSignal chan bool
if !req.SkipReaper {
sessionID := sessionID()
r, err := NewReaper(context.WithValue(ctx, dockerHostContextKey, p.host), sessionID.String(), p, req.ReaperImage)
if err != nil {
return nil, fmt.Errorf("%w: creating network reaper failed", err)
Expand Down
6 changes: 5 additions & 1 deletion reaper.go
Expand Up @@ -63,7 +63,6 @@ func NewReaper(ctx context.Context, sessionID string, provider ReaperProvider, r
ExposedPorts: []string{string(listeningPort)},
NetworkMode: Bridge,
Labels: map[string]string{
TestcontainerLabel: "true",
TestcontainerLabelIsReaper: "true",
},
SkipReaper: true,
Expand All @@ -72,6 +71,11 @@ func NewReaper(ctx context.Context, sessionID string, provider ReaperProvider, r
WaitingFor: wait.ForListeningPort(listeningPort),
}

// include reaper-specific labels to the reaper container
for k, v := range reaper.Labels() {
req.Labels[k] = v
}

tcConfig := provider.Config()
req.Privileged = tcConfig.RyukPrivileged

Expand Down
5 changes: 3 additions & 2 deletions reaper_test.go
Expand Up @@ -35,8 +35,9 @@ func createContainerRequest(customize func(ContainerRequest) ContainerRequest) C
Image: "reaperImage",
ExposedPorts: []string{"8080/tcp"},
Labels: map[string]string{
TestcontainerLabel: "true",
TestcontainerLabelIsReaper: "true",
TestcontainerLabel: "true",
TestcontainerLabelIsReaper: "true",
TestcontainerLabelSessionID: "sessionId",
},
SkipReaper: true,
Mounts: Mounts(BindMount("/var/run/docker.sock", "/var/run/docker.sock")),
Expand Down
18 changes: 18 additions & 0 deletions session.go
@@ -0,0 +1,18 @@
package testcontainers

import (
"sync"

"github.com/google/uuid"
)

var tcSessionID uuid.UUID
var tcSessionIDOnce sync.Once

func sessionID() uuid.UUID {
tcSessionIDOnce.Do(func() {
tcSessionID = uuid.New()
})

return tcSessionID
}

0 comments on commit db41dd1

Please sign in to comment.