From ab4582918a5f2512386f208dac1087e588ab855a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 9 Oct 2025 16:03:26 +0200 Subject: [PATCH] chore(valkey): use Run function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- modules/valkey/valkey.go | 115 ++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 68 deletions(-) diff --git a/modules/valkey/valkey.go b/modules/valkey/valkey.go index 674a4701df..ed1f1bf847 100644 --- a/modules/valkey/valkey.go +++ b/modules/valkey/valkey.go @@ -61,26 +61,19 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize // Run creates an instance of the Valkey container type func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*ValkeyContainer, error) { - req := testcontainers.ContainerRequest{ - Image: img, - ExposedPorts: []string{valkeyPort}, - } - - genericContainerReq := testcontainers.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - } - + // Process custom options first var settings options for _, opt := range opts { if opt, ok := opt.(Option); ok { if err := opt(&settings); err != nil { - return nil, err + return nil, fmt.Errorf("apply option: %w", err) } } } - tcOpts := []testcontainers.ContainerCustomizer{} + moduleOpts := []testcontainers.ContainerCustomizer{ + testcontainers.WithExposedPorts(valkeyPort), + } waitStrategies := []wait.Strategy{ wait.ForListeningPort(valkeyPort).WithStartupTimeout(time.Second * 10), @@ -109,23 +102,25 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom "--tls-auth-clients", "yes", } - tcOpts = append(tcOpts, testcontainers.WithCmdArgs(cmds...)) // Append the default CMD with the TLS certificates. - tcOpts = append(tcOpts, testcontainers.WithFiles( - testcontainers.ContainerFile{ - Reader: bytes.NewReader(caCert.Bytes), - ContainerFilePath: "/tls/ca.crt", - FileMode: 0o644, - }, - testcontainers.ContainerFile{ - Reader: bytes.NewReader(serverCert.Bytes), - ContainerFilePath: "/tls/server.crt", - FileMode: 0o644, - }, - testcontainers.ContainerFile{ - Reader: bytes.NewReader(serverCert.KeyBytes), - ContainerFilePath: "/tls/server.key", - FileMode: 0o644, - })) + moduleOpts = append(moduleOpts, + testcontainers.WithCmdArgs(cmds...), + testcontainers.WithFiles( + testcontainers.ContainerFile{ + Reader: bytes.NewReader(caCert.Bytes), + ContainerFilePath: "/tls/ca.crt", + FileMode: 0o644, + }, + testcontainers.ContainerFile{ + Reader: bytes.NewReader(serverCert.Bytes), + ContainerFilePath: "/tls/server.crt", + FileMode: 0o644, + }, + testcontainers.ContainerFile{ + Reader: bytes.NewReader(serverCert.KeyBytes), + ContainerFilePath: "/tls/server.key", + FileMode: 0o644, + }), + ) settings.tlsConfig = &tls.Config{ MinVersion: tls.VersionTLS12, @@ -135,26 +130,16 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom } } - tcOpts = append(tcOpts, testcontainers.WithWaitStrategy(waitStrategies...)) - - // Append the customizers passed to the Run function. - tcOpts = append(tcOpts, opts...) + moduleOpts = append(moduleOpts, testcontainers.WithWaitStrategy(waitStrategies...)) - // Apply the testcontainers customizers. - for _, opt := range tcOpts { - 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 *ValkeyContainer - if container != nil { - c = &ValkeyContainer{Container: container, settings: settings} + if ctr != nil { + c = &ValkeyContainer{Container: ctr, settings: settings} } if err != nil { - return c, fmt.Errorf("generic container: %w", err) + return c, fmt.Errorf("run valkey: %w", err) } return c, nil @@ -171,23 +156,23 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption { ContainerFilePath: defaultConfigFile, FileMode: 0o755, } - req.Files = append(req.Files, cf) + + if err := testcontainers.WithFiles(cf)(req); err != nil { + return err + } if len(req.Cmd) == 0 { - req.Cmd = []string{valkeyServerProcess, defaultConfigFile} - return nil + return testcontainers.WithCmd(valkeyServerProcess, defaultConfigFile)(req) } - // prepend the command to run the redis server with the config file, which must be the first argument of the redis server process + // prepend the command to run the valkey server with the config file, which must be the first argument of the valkey server process if req.Cmd[0] == valkeyServerProcess { // just insert the config file, then the rest of the args - req.Cmd = append([]string{valkeyServerProcess, defaultConfigFile}, req.Cmd[1:]...) - } else if req.Cmd[0] != valkeyServerProcess { - // prepend the redis server and the config file, then the rest of the args - req.Cmd = append([]string{valkeyServerProcess, defaultConfigFile}, req.Cmd...) + return testcontainers.WithCmd(append([]string{valkeyServerProcess, defaultConfigFile}, req.Cmd[1:]...)...)(req) } - return nil + // prepend the valkey server and the config file, then the rest of the args + return testcontainers.WithCmd(append([]string{valkeyServerProcess, defaultConfigFile}, req.Cmd...)...)(req) } } @@ -195,9 +180,7 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption { // See https://redis.io/docs/reference/modules/modules-api-ref/#redismodule_log for more information. func WithLogLevel(level LogLevel) testcontainers.CustomizeRequestOption { return func(req *testcontainers.GenericContainerRequest) error { - processValkeyServerArgs(req, []string{"--loglevel", string(level)}) - - return nil + return processValkeyServerArgs(req, []string{"--loglevel", string(level)}) } } @@ -214,24 +197,20 @@ func WithSnapshotting(seconds int, changedKeys int) testcontainers.CustomizeRequ } return func(req *testcontainers.GenericContainerRequest) error { - processValkeyServerArgs(req, []string{"--save", strconv.Itoa(seconds), strconv.Itoa(changedKeys)}) - return nil + return processValkeyServerArgs(req, []string{"--save", strconv.Itoa(seconds), strconv.Itoa(changedKeys)}) } } -func processValkeyServerArgs(req *testcontainers.GenericContainerRequest, args []string) { +func processValkeyServerArgs(req *testcontainers.GenericContainerRequest, args []string) error { if len(req.Cmd) == 0 { - req.Cmd = append([]string{valkeyServerProcess}, args...) - return + return testcontainers.WithCmd(append([]string{valkeyServerProcess}, args...)...)(req) } - // prepend the command to run the valkey server with the config file + // If valkey server is already set as the first argument, just append the args if req.Cmd[0] == valkeyServerProcess { - // valkey server is already set as the first argument, so just append the config file - req.Cmd = append(req.Cmd, args...) - } else if req.Cmd[0] != valkeyServerProcess { - // valkey server is not set as the first argument, so prepend it alongside the config file - req.Cmd = append([]string{valkeyServerProcess}, req.Cmd...) - req.Cmd = append(req.Cmd, args...) + return testcontainers.WithCmdArgs(args...)(req) } + + // valkey server is not set as the first argument, so prepend it alongside the existing command and args + return testcontainers.WithCmd(append(append([]string{valkeyServerProcess}, req.Cmd...), args...)...)(req) }