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 22, 2024
1 parent 6aece92 commit 39fc242
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/features/common_functional_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ _Testcontainers for Go_ exposes an interface to perform this operations: `ImageS

Using the `WithImageSubstitutors` options, you could define your own substitutions to the container images. E.g. adding a prefix to the images so that they can be pulled from a Docker registry other than Docker Hub. This is the usual mechanism for using Docker image proxies, caches, etc.

#### WithEnv

- Since testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.29.0"><span class="tc-version">:material-tag: v0.29.0</span></a>

If you need to either pass additional environment variables to a container or override them, you can use `testcontainers.WithEnv` for example:

```golang
postgres, err = postgresModule.RunContainer(ctx, testcontainers.WithEnv(map[string]string{"POSTGRES_INITDB_ARGS", "--no-sync"}))
```

#### WithLogConsumers

- Since testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.28.0"><span class="tc-version">:material-tag: v0.28.0</span></a>
Expand Down
14 changes: 14 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ 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.
func WithEnv(envs map[string]string) CustomizeRequestOption {
return func(req *GenericContainerRequest) {
if req.Env == nil {
req.Env = map[string]string{}
}

for key, val := range envs {
req.Env[key] = val
}
}
}

// WithHostConfigModifier allows to override the default host config
func WithHostConfigModifier(modifier func(hostConfig *container.HostConfig)) CustomizeRequestOption {
return func(req *GenericContainerRequest) {
Expand Down
48 changes: 48 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,51 @@ 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 map[string]string
expect map[string]string
}{
"add": {
req: &testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{"KEY1": "VAL1"},
},
},
env: map[string]string{"KEY2": "VAL2"},
expect: map[string]string{
"KEY1": "VAL1",
"KEY2": "VAL2",
},
},
"add-nil": {
req: &testcontainers.GenericContainerRequest{},
env: map[string]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: map[string]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)
})
}
}

0 comments on commit 39fc242

Please sign in to comment.