Skip to content

Commit b890b0c

Browse files
mdelapenyaclaude
andauthored
chore(redis): use Run function (#3429)
* chore(redis): use Run function 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * fix(redis): always wait for listening port - Move wait.ForListeningPort to base moduleOpts instead of conditional TLS block - Port 6379 is always listening (regular or TLS traffic) so wait should be unconditional - Removed duplicate wait strategy that was only added for TLS case 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent da9df18 commit b890b0c

File tree

2 files changed

+36
-54
lines changed

2 files changed

+36
-54
lines changed

modules/redis/options.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
4343
const defaultConfigFile = "/usr/local/redis.conf"
4444

4545
return func(req *testcontainers.GenericContainerRequest) error {
46-
cf := testcontainers.ContainerFile{
46+
if err := testcontainers.WithFiles(testcontainers.ContainerFile{
4747
HostFilePath: configFile,
4848
ContainerFilePath: defaultConfigFile,
4949
FileMode: 0o755,
50+
})(req); err != nil {
51+
return err
5052
}
51-
req.Files = append(req.Files, cf)
5253

5354
// prepend the command to run the redis server with the config file, which must be the first argument of the redis server process
54-
req.Cmd = append([]string{defaultConfigFile}, req.Cmd...)
55-
56-
return nil
55+
return testcontainers.WithCmd(append([]string{defaultConfigFile}, req.Cmd...)...)(req)
5756
}
5857
}
5958

modules/redis/redis.go

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,7 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
5656

5757
// Run creates an instance of the Redis container type
5858
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*RedisContainer, error) {
59-
req := testcontainers.ContainerRequest{
60-
Image: img,
61-
ExposedPorts: []string{redisPort},
62-
}
63-
64-
genericContainerReq := testcontainers.GenericContainerRequest{
65-
ContainerRequest: req,
66-
Started: true,
67-
}
68-
59+
// Process custom options to extract settings
6960
var settings options
7061
for _, opt := range opts {
7162
if opt, ok := opt.(Option); ok {
@@ -75,17 +66,15 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
7566
}
7667
}
7768

78-
tcOpts := []testcontainers.ContainerCustomizer{}
79-
80-
waitStrategies := []wait.Strategy{
81-
wait.ForListeningPort(redisPort).WithStartupTimeout(time.Second * 10),
82-
wait.ForLog("* Ready to accept connections"),
69+
moduleOpts := []testcontainers.ContainerCustomizer{
70+
testcontainers.WithExposedPorts(redisPort),
71+
testcontainers.WithWaitStrategy(
72+
wait.ForListeningPort(redisPort).WithStartupTimeout(time.Second*10),
73+
wait.ForLog("* Ready to accept connections"),
74+
),
8375
}
8476

8577
if settings.tlsEnabled {
86-
// wait for the TLS port to be available
87-
waitStrategies = append(waitStrategies, wait.ForListeningPort(redisPort).WithStartupTimeout(time.Second*10))
88-
8978
// Generate TLS certificates in the fly and add them to the container before it starts.
9079
// Update the CMD to use the TLS certificates.
9180
caCert, clientCert, serverCert, err := createTLSCerts()
@@ -104,23 +93,26 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
10493
"--tls-auth-clients", "yes",
10594
}
10695

107-
tcOpts = append(tcOpts, testcontainers.WithCmdArgs(cmds...)) // Append the default CMD with the TLS certificates.
108-
tcOpts = append(tcOpts, testcontainers.WithFiles(
109-
testcontainers.ContainerFile{
110-
Reader: bytes.NewReader(caCert.Bytes),
111-
ContainerFilePath: "/tls/ca.crt",
112-
FileMode: 0o644,
113-
},
114-
testcontainers.ContainerFile{
115-
Reader: bytes.NewReader(serverCert.Bytes),
116-
ContainerFilePath: "/tls/server.crt",
117-
FileMode: 0o644,
118-
},
119-
testcontainers.ContainerFile{
120-
Reader: bytes.NewReader(serverCert.KeyBytes),
121-
ContainerFilePath: "/tls/server.key",
122-
FileMode: 0o644,
123-
}))
96+
moduleOpts = append(moduleOpts,
97+
testcontainers.WithCmdArgs(cmds...),
98+
testcontainers.WithFiles(
99+
testcontainers.ContainerFile{
100+
Reader: bytes.NewReader(caCert.Bytes),
101+
ContainerFilePath: "/tls/ca.crt",
102+
FileMode: 0o644,
103+
},
104+
testcontainers.ContainerFile{
105+
Reader: bytes.NewReader(serverCert.Bytes),
106+
ContainerFilePath: "/tls/server.crt",
107+
FileMode: 0o644,
108+
},
109+
testcontainers.ContainerFile{
110+
Reader: bytes.NewReader(serverCert.KeyBytes),
111+
ContainerFilePath: "/tls/server.key",
112+
FileMode: 0o644,
113+
},
114+
),
115+
)
124116

125117
settings.tlsConfig = &tls.Config{
126118
MinVersion: tls.VersionTLS12,
@@ -130,26 +122,17 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
130122
}
131123
}
132124

133-
tcOpts = append(tcOpts, testcontainers.WithWaitStrategy(waitStrategies...))
134-
135125
// Append the customizers passed to the Run function.
136-
tcOpts = append(tcOpts, opts...)
137-
138-
// Apply the testcontainers customizers.
139-
for _, opt := range tcOpts {
140-
if err := opt.Customize(&genericContainerReq); err != nil {
141-
return nil, err
142-
}
143-
}
126+
moduleOpts = append(moduleOpts, opts...)
144127

145-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
128+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
146129
var c *RedisContainer
147-
if container != nil {
148-
c = &RedisContainer{Container: container, settings: settings}
130+
if ctr != nil {
131+
c = &RedisContainer{Container: ctr, settings: settings}
149132
}
150133

151134
if err != nil {
152-
return c, fmt.Errorf("generic container: %w", err)
135+
return c, fmt.Errorf("run redis: %w", err)
153136
}
154137

155138
return c, nil

0 commit comments

Comments
 (0)