Skip to content

Commit d0b6154

Browse files
mdelapenyaclaude
andauthored
chore(vault): use Run function (#3443)
This commit migrates the vault module to use the new testcontainers.Run() API. The main changes are: - Use testcontainers.Run() instead of testcontainers.GenericContainer() - Convert to moduleOpts pattern with functional options - Use WithExposedPorts, WithHostConfigModifier, WithWaitStrategy, WithEnv - WithToken uses WithEnv for setting root token env vars - WithInitCommand uses WithAdditionalWaitStrategy to append exec wait Tests: 10 tests, 89.5% coverage Ref: #3174 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 225ad7f commit d0b6154

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

modules/vault/vault.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,25 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
2828

2929
// Run creates an instance of the Vault container type
3030
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*VaultContainer, error) {
31-
req := testcontainers.ContainerRequest{
32-
Image: img,
33-
ExposedPorts: []string{defaultPort + "/tcp"},
34-
HostConfigModifier: func(hc *container.HostConfig) {
31+
moduleOpts := []testcontainers.ContainerCustomizer{
32+
testcontainers.WithExposedPorts(defaultPort + "/tcp"),
33+
testcontainers.WithHostConfigModifier(func(hc *container.HostConfig) {
3534
hc.CapAdd = []string{"CAP_IPC_LOCK"}
36-
},
37-
WaitingFor: wait.ForHTTP("/v1/sys/health").WithPort(defaultPort),
38-
Env: map[string]string{
35+
}),
36+
testcontainers.WithWaitStrategy(wait.ForHTTP("/v1/sys/health").WithPort(defaultPort)),
37+
testcontainers.WithEnv(map[string]string{
3938
"VAULT_ADDR": "http://0.0.0.0:" + defaultPort,
40-
},
39+
}),
4140
}
4241

43-
genericContainerReq := testcontainers.GenericContainerRequest{
44-
ContainerRequest: req,
45-
Started: true,
46-
}
47-
48-
for _, opt := range opts {
49-
if err := opt.Customize(&genericContainerReq); err != nil {
50-
return nil, err
51-
}
52-
}
53-
54-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
42+
ctr, err := testcontainers.Run(ctx, img, append(moduleOpts, opts...)...)
5543
var c *VaultContainer
56-
if container != nil {
57-
c = &VaultContainer{Container: container}
44+
if ctr != nil {
45+
c = &VaultContainer{Container: ctr}
5846
}
5947

6048
if err != nil {
61-
return c, fmt.Errorf("generic container: %w", err)
49+
return c, fmt.Errorf("run vault: %w", err)
6250
}
6351

6452
return c, nil
@@ -67,10 +55,10 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
6755
// WithToken is a container option function that sets the root token for the Vault
6856
func WithToken(token string) testcontainers.CustomizeRequestOption {
6957
return func(req *testcontainers.GenericContainerRequest) error {
70-
req.Env["VAULT_DEV_ROOT_TOKEN_ID"] = token
71-
req.Env["VAULT_TOKEN"] = token
72-
73-
return nil
58+
return testcontainers.WithEnv(map[string]string{
59+
"VAULT_DEV_ROOT_TOKEN_ID": token,
60+
"VAULT_TOKEN": token,
61+
})(req)
7462
}
7563
}
7664

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

86-
req.WaitingFor = wait.ForAll(req.WaitingFor, wait.ForExec(cmd))
87-
88-
return nil
74+
return testcontainers.WithAdditionalWaitStrategy(wait.ForExec(cmd))(req)
8975
}
9076
}
9177

0 commit comments

Comments
 (0)