Skip to content

Commit

Permalink
feat: WithEnv customize request option
Browse files Browse the repository at this point in the history
Add WithEnv which set additional environment variables on a request
so that callers can customise a request.

This can be used to easily add environment variables to a container
created using one of the standard module e.g. postgres.
  • Loading branch information
stevenh committed Feb 21, 2024
1 parent 6aece92 commit e3b0ffa
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
5 changes: 2 additions & 3 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ func (c *DockerContainer) CopyFileFromContainer(ctx context.Context, filePath st

// CopyDirToContainer copies the contents of a directory to a parent path in the container. This parent path must exist in the container first
// as we cannot create it
func (c *DockerContainer) CopyDirToContainer(ctx context.Context, hostDirPath string, containerParentPath string, fileMode int64) error {
func (c *DockerContainer) CopyDirToContainer(ctx context.Context, hostDirPath, containerParentPath string, fileMode int64) error {
dir, err := isDir(hostDirPath)
if err != nil {
return err
Expand Down Expand Up @@ -589,7 +589,7 @@ func (c *DockerContainer) CopyDirToContainer(ctx context.Context, hostDirPath st
return nil
}

func (c *DockerContainer) CopyFileToContainer(ctx context.Context, hostFilePath string, containerFilePath string, fileMode int64) error {
func (c *DockerContainer) CopyFileToContainer(ctx context.Context, hostFilePath, containerFilePath string, fileMode int64) error {
dir, err := isDir(hostFilePath)
if err != nil {
return err
Expand Down Expand Up @@ -1446,7 +1446,6 @@ func (p *DockerProvider) getDefaultNetwork(ctx context.Context, cli client.APICl
Attachable: true,
Labels: core.DefaultLabels(core.SessionID()),
})

if err != nil {
return "", err
}
Expand Down
24 changes: 24 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ func WithEndpointSettingsModifier(modifier func(settings map[string]*network.End
}
}

// WithEnv sets the environment variables for a container.
// If the environment variable already exists, it will be overridden.
// panics if the number of arguments is odd.
func WithEnv(envs ...string) CustomizeRequestOption {
if len(envs)%2 != 0 {
panic("WithEnv requires an even number of arguments")
}

return func(req *GenericContainerRequest) {
if req.Env == nil {
req.Env = map[string]string{}
}

var key string
for i, v := range envs {
if i%2 == 0 {
key = v
} else {
req.Env[key] = v
}
}
}
}

// WithHostConfigModifier allows to override the default host config
func WithHostConfigModifier(modifier func(hostConfig *container.HostConfig)) CustomizeRequestOption {
return func(req *GenericContainerRequest) {
Expand Down
64 changes: 64 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,67 @@ func TestWithAfterReadyCommand(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "/tmp/.testcontainers\n", string(content))
}

func TestWithEnv(t *testing.T) {
tests := map[string]struct {
req *testcontainers.GenericContainerRequest
env []string
expect map[string]string
}{
"add": {
req: &testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"KEY1": "VAL1",
},
},
},
env: []string{
"KEY2", "VAL2",
},
expect: map[string]string{
"KEY1": "VAL1",
"KEY2": "VAL2",
},
},
"add-nil": {
req: &testcontainers.GenericContainerRequest{},
env: []string{
"KEY2", "VAL2",
},
expect: map[string]string{
"KEY2": "VAL2",
},
},
"override": {
req: &testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"KEY1": "VAL1",
"KEY2": "VAL2",
},
},
},
env: []string{
"KEY2", "VAL3",
},
expect: map[string]string{
"KEY1": "VAL1",
"KEY2": "VAL3",
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
opt := testcontainers.WithEnv(tc.env...)
opt.Customize(tc.req)
require.Equal(t, tc.expect, tc.req.Env)
})
}

t.Run("odd-args", func(t *testing.T) {
require.Panics(t, func() {
testcontainers.WithEnv("KEY1")
})
})
}
1 change: 0 additions & 1 deletion reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func lookUpReaperContainer(ctx context.Context, sessionID string) (*DockerContai

return nil
}, backoff.WithContext(exp, ctx))

if err != nil {
return nil, err
}
Expand Down

0 comments on commit e3b0ffa

Please sign in to comment.