Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 16 additions & 30 deletions modules/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,25 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize

// Run creates an instance of the Vault container type
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*VaultContainer, error) {
req := testcontainers.ContainerRequest{
Image: img,
ExposedPorts: []string{defaultPort + "/tcp"},
HostConfigModifier: func(hc *container.HostConfig) {
moduleOpts := []testcontainers.ContainerCustomizer{
testcontainers.WithExposedPorts(defaultPort + "/tcp"),
testcontainers.WithHostConfigModifier(func(hc *container.HostConfig) {
hc.CapAdd = []string{"CAP_IPC_LOCK"}
},
WaitingFor: wait.ForHTTP("/v1/sys/health").WithPort(defaultPort),
Env: map[string]string{
}),
testcontainers.WithWaitStrategy(wait.ForHTTP("/v1/sys/health").WithPort(defaultPort)),
testcontainers.WithEnv(map[string]string{
"VAULT_ADDR": "http://0.0.0.0:" + defaultPort,
},
}),
}

genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}

for _, opt := range opts {
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
}
}

container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
ctr, err := testcontainers.Run(ctx, img, append(moduleOpts, opts...)...)
var c *VaultContainer
if container != nil {
c = &VaultContainer{Container: container}
if ctr != nil {
c = &VaultContainer{Container: ctr}
}

if err != nil {
return c, fmt.Errorf("generic container: %w", err)
return c, fmt.Errorf("run vault: %w", err)
}

return c, nil
Expand All @@ -67,10 +55,10 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
// WithToken is a container option function that sets the root token for the Vault
func WithToken(token string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Env["VAULT_DEV_ROOT_TOKEN_ID"] = token
req.Env["VAULT_TOKEN"] = token

return nil
return testcontainers.WithEnv(map[string]string{
"VAULT_DEV_ROOT_TOKEN_ID": token,
"VAULT_TOKEN": token,
})(req)
}
}

Expand All @@ -83,9 +71,7 @@ func WithInitCommand(commands ...string) testcontainers.CustomizeRequestOption {
}
cmd := []string{"/bin/sh", "-c", strings.Join(commandsList, " && ")}

req.WaitingFor = wait.ForAll(req.WaitingFor, wait.ForExec(cmd))

return nil
return testcontainers.WithAdditionalWaitStrategy(wait.ForExec(cmd))(req)
}
}

Expand Down
Loading