Skip to content

Commit 9fbc087

Browse files
mdelapenyaclaude
andcommitted
chore(valkey): use Run function
This commit migrates the valkey module to use the new testcontainers.Run() API. The main changes are: - Use testcontainers.Run() instead of testcontainers.GenericContainer() - Use entrypoint for valkey-server process and WithCmd/WithCmdArgs for arguments - Simplify WithConfigFile to prepend config file as first argument - Simplify WithLogLevel and WithSnapshotting to use WithCmdArgs directly - Process custom options before building module options for TLS support - Update option tests to reflect entrypoint-based approach Ref: testcontainers#3174 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent ab45829 commit 9fbc087

File tree

2 files changed

+20
-60
lines changed

2 files changed

+20
-60
lines changed

modules/valkey/options_test.go

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,12 @@ func TestWithConfigFile(t *testing.T) {
1717
{
1818
name: "no existing command",
1919
cmds: []string{},
20-
expectedCmds: []string{valkeyServerProcess, "/usr/local/valkey.conf"},
20+
expectedCmds: []string{"/usr/local/valkey.conf"},
2121
},
2222
{
23-
name: "existing redis-server command as first argument",
24-
cmds: []string{valkeyServerProcess, "a", "b", "c"},
25-
expectedCmds: []string{valkeyServerProcess, "/usr/local/valkey.conf", "a", "b", "c"},
26-
},
27-
{
28-
name: "non existing redis-server command",
23+
name: "existing commands",
2924
cmds: []string{"a", "b", "c"},
30-
expectedCmds: []string{valkeyServerProcess, "/usr/local/valkey.conf", "a", "b", "c"},
25+
expectedCmds: []string{"/usr/local/valkey.conf", "a", "b", "c"},
3126
},
3227
}
3328

@@ -39,7 +34,7 @@ func TestWithConfigFile(t *testing.T) {
3934
},
4035
}
4136

42-
err := WithConfigFile("redis.conf")(req)
37+
err := WithConfigFile("valkey.conf")(req)
4338
require.NoError(t, err)
4439

4540
require.Equal(t, tt.expectedCmds, req.Cmd)
@@ -56,17 +51,12 @@ func TestWithLogLevel(t *testing.T) {
5651
{
5752
name: "no existing command",
5853
cmds: []string{},
59-
expectedCmds: []string{valkeyServerProcess, "--loglevel", "debug"},
54+
expectedCmds: []string{"--loglevel", "debug"},
6055
},
6156
{
62-
name: "existing redis-server command as first argument",
63-
cmds: []string{valkeyServerProcess, "a", "b", "c"},
64-
expectedCmds: []string{valkeyServerProcess, "a", "b", "c", "--loglevel", "debug"},
65-
},
66-
{
67-
name: "non existing redis-server command",
57+
name: "existing commands",
6858
cmds: []string{"a", "b", "c"},
69-
expectedCmds: []string{valkeyServerProcess, "a", "b", "c", "--loglevel", "debug"},
59+
expectedCmds: []string{"a", "b", "c", "--loglevel", "debug"},
7060
},
7161
}
7262

@@ -99,28 +89,21 @@ func TestWithSnapshotting(t *testing.T) {
9989
cmds: []string{},
10090
seconds: 60,
10191
changedKeys: 100,
102-
expectedCmds: []string{valkeyServerProcess, "--save", "60", "100"},
92+
expectedCmds: []string{"--save", "60", "100"},
10393
},
10494
{
105-
name: "existing redis-server command as first argument",
106-
cmds: []string{valkeyServerProcess, "a", "b", "c"},
107-
seconds: 60,
108-
changedKeys: 100,
109-
expectedCmds: []string{valkeyServerProcess, "a", "b", "c", "--save", "60", "100"},
110-
},
111-
{
112-
name: "non existing redis-server command",
95+
name: "existing commands",
11396
cmds: []string{"a", "b", "c"},
11497
seconds: 60,
11598
changedKeys: 100,
116-
expectedCmds: []string{valkeyServerProcess, "a", "b", "c", "--save", "60", "100"},
99+
expectedCmds: []string{"a", "b", "c", "--save", "60", "100"},
117100
},
118101
{
119-
name: "existing redis-server command as first argument",
120-
cmds: []string{valkeyServerProcess, "a", "b", "c"},
102+
name: "zero values get normalized",
103+
cmds: []string{"a", "b", "c"},
121104
seconds: 0,
122105
changedKeys: 0,
123-
expectedCmds: []string{valkeyServerProcess, "a", "b", "c", "--save", "1", "1"},
106+
expectedCmds: []string{"a", "b", "c", "--save", "1", "1"},
124107
},
125108
}
126109

modules/valkey/valkey.go

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
7373

7474
moduleOpts := []testcontainers.ContainerCustomizer{
7575
testcontainers.WithExposedPorts(valkeyPort),
76+
testcontainers.WithEntrypoint(valkeyServerProcess),
7677
}
7778

7879
waitStrategies := []wait.Strategy{
@@ -145,8 +146,8 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
145146
return c, nil
146147
}
147148

148-
// WithConfigFile sets the config file to be used for the valkey container, and sets the command to run the valkey server
149-
// using the passed config file
149+
// WithConfigFile sets the config file to be used for the valkey container.
150+
// The config file must be the first argument to valkey-server.
150151
func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
151152
const defaultConfigFile = "/usr/local/valkey.conf"
152153

@@ -161,26 +162,16 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
161162
return err
162163
}
163164

164-
if len(req.Cmd) == 0 {
165-
return testcontainers.WithCmd(valkeyServerProcess, defaultConfigFile)(req)
166-
}
167-
168-
// prepend the command to run the valkey server with the config file, which must be the first argument of the valkey server process
169-
if req.Cmd[0] == valkeyServerProcess {
170-
// just insert the config file, then the rest of the args
171-
return testcontainers.WithCmd(append([]string{valkeyServerProcess, defaultConfigFile}, req.Cmd[1:]...)...)(req)
172-
}
173-
174-
// prepend the valkey server and the config file, then the rest of the args
175-
return testcontainers.WithCmd(append([]string{valkeyServerProcess, defaultConfigFile}, req.Cmd...)...)(req)
165+
// Prepend the config file as the first argument
166+
return testcontainers.WithCmd(append([]string{defaultConfigFile}, req.Cmd...)...)(req)
176167
}
177168
}
178169

179170
// WithLogLevel sets the log level for the valkey server process
180171
// See https://redis.io/docs/reference/modules/modules-api-ref/#redismodule_log for more information.
181172
func WithLogLevel(level LogLevel) testcontainers.CustomizeRequestOption {
182173
return func(req *testcontainers.GenericContainerRequest) error {
183-
return processValkeyServerArgs(req, []string{"--loglevel", string(level)})
174+
return testcontainers.WithCmdArgs("--loglevel", string(level))(req)
184175
}
185176
}
186177

@@ -197,20 +188,6 @@ func WithSnapshotting(seconds int, changedKeys int) testcontainers.CustomizeRequ
197188
}
198189

199190
return func(req *testcontainers.GenericContainerRequest) error {
200-
return processValkeyServerArgs(req, []string{"--save", strconv.Itoa(seconds), strconv.Itoa(changedKeys)})
201-
}
202-
}
203-
204-
func processValkeyServerArgs(req *testcontainers.GenericContainerRequest, args []string) error {
205-
if len(req.Cmd) == 0 {
206-
return testcontainers.WithCmd(append([]string{valkeyServerProcess}, args...)...)(req)
191+
return testcontainers.WithCmdArgs("--save", strconv.Itoa(seconds), strconv.Itoa(changedKeys))(req)
207192
}
208-
209-
// If valkey server is already set as the first argument, just append the args
210-
if req.Cmd[0] == valkeyServerProcess {
211-
return testcontainers.WithCmdArgs(args...)(req)
212-
}
213-
214-
// valkey server is not set as the first argument, so prepend it alongside the existing command and args
215-
return testcontainers.WithCmd(append(append([]string{valkeyServerProcess}, req.Cmd...), args...)...)(req)
216193
}

0 commit comments

Comments
 (0)