Skip to content

Commit 3852045

Browse files
mdelapenyaclaude
andauthored
chore(yugabytedb): use Run function (#3444)
This commit migrates the yugabytedb 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 WithCmd, WithWaitStrategy, WithExposedPorts, WithEnv - Use Inspect after Run to retrieve actual env var values after user customizations - Multiple wait strategies for logs and listening ports Tests: 9 tests, 93.0% coverage Ref: #3174 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 88f1499 commit 3852045

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

modules/yugabytedb/yugabytedb.go

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,61 @@ type Container struct {
4949
// [*Container.YSQLConnectionString] and [*Container.YCQLConfigureClusterConfig]
5050
// methods to use the container in their respective clients.
5151
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
52-
req := testcontainers.ContainerRequest{
53-
Image: img,
54-
Cmd: []string{"bin/yugabyted", "start", "--background=false"},
55-
WaitingFor: wait.ForAll(
52+
moduleOpts := []testcontainers.ContainerCustomizer{
53+
testcontainers.WithCmd("bin/yugabyted", "start", "--background=false"),
54+
testcontainers.WithWaitStrategy(
5655
wait.ForLog("YugabyteDB Started").WithOccurrence(1),
5756
wait.ForLog("Data placement constraint successfully verified").WithOccurrence(1),
5857
wait.ForListeningPort(ysqlPort),
5958
wait.ForListeningPort(ycqlPort),
6059
),
61-
ExposedPorts: []string{ycqlPort, ysqlPort},
62-
Env: map[string]string{
60+
testcontainers.WithExposedPorts(ycqlPort, ysqlPort),
61+
testcontainers.WithEnv(map[string]string{
6362
ycqlKeyspaceEnv: ycqlKeyspace,
6463
ycqlUserNameEnv: ycqlUserName,
6564
ycqlPasswordEnv: ycqlPassword,
6665
ysqlDatabaseNameEnv: ysqlDatabaseName,
6766
ysqlDatabaseUserEnv: ysqlDatabaseUser,
6867
ysqlDatabasePasswordEnv: ysqlDatabasePassword,
69-
},
68+
}),
7069
}
7170

72-
genericContainerReq := testcontainers.GenericContainerRequest{
73-
ContainerRequest: req,
74-
Started: true,
75-
}
76-
77-
for _, opt := range opts {
78-
if err := opt.Customize(&genericContainerReq); err != nil {
79-
return nil, fmt.Errorf("customize: %w", err)
80-
}
81-
}
82-
83-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
71+
ctr, err := testcontainers.Run(ctx, img, append(moduleOpts, opts...)...)
8472
var c *Container
85-
if container != nil {
73+
if ctr != nil {
8674
c = &Container{
87-
Container: container,
88-
ysqlDatabaseName: req.Env[ysqlDatabaseNameEnv],
89-
ysqlDatabaseUser: req.Env[ysqlDatabaseUserEnv],
90-
ysqlDatabasePassword: req.Env[ysqlDatabasePasswordEnv],
75+
Container: ctr,
76+
ysqlDatabaseName: ysqlDatabaseName,
77+
ysqlDatabaseUser: ysqlDatabaseUser,
78+
ysqlDatabasePassword: ysqlDatabasePassword,
9179
}
9280
}
9381

9482
if err != nil {
95-
return c, fmt.Errorf("generic container: %w", err)
83+
return c, fmt.Errorf("run yugabytedb: %w", err)
84+
}
85+
86+
// Inspect the container to get the actual env var values after user customizations
87+
inspect, err := ctr.Inspect(ctx)
88+
if err != nil {
89+
return c, fmt.Errorf("inspect yugabytedb: %w", err)
90+
}
91+
92+
var foundName, foundUser, foundPassword bool
93+
for _, env := range inspect.Config.Env {
94+
if v, ok := strings.CutPrefix(env, ysqlDatabaseNameEnv+"="); ok {
95+
c.ysqlDatabaseName, foundName = v, true
96+
}
97+
if v, ok := strings.CutPrefix(env, ysqlDatabaseUserEnv+"="); ok {
98+
c.ysqlDatabaseUser, foundUser = v, true
99+
}
100+
if v, ok := strings.CutPrefix(env, ysqlDatabasePasswordEnv+"="); ok {
101+
c.ysqlDatabasePassword, foundPassword = v, true
102+
}
103+
104+
if foundName && foundUser && foundPassword {
105+
break
106+
}
96107
}
97108

98109
return c, nil

0 commit comments

Comments
 (0)