@@ -61,26 +61,19 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
6161
6262// Run creates an instance of the Valkey container type
6363func Run (ctx context.Context , img string , opts ... testcontainers.ContainerCustomizer ) (* ValkeyContainer , error ) {
64- req := testcontainers.ContainerRequest {
65- Image : img ,
66- ExposedPorts : []string {valkeyPort },
67- }
68-
69- genericContainerReq := testcontainers.GenericContainerRequest {
70- ContainerRequest : req ,
71- Started : true ,
72- }
73-
64+ // Process custom options first
7465 var settings options
7566 for _ , opt := range opts {
7667 if opt , ok := opt .(Option ); ok {
7768 if err := opt (& settings ); err != nil {
78- return nil , err
69+ return nil , fmt . Errorf ( "apply option: %w" , err )
7970 }
8071 }
8172 }
8273
83- tcOpts := []testcontainers.ContainerCustomizer {}
74+ moduleOpts := []testcontainers.ContainerCustomizer {
75+ testcontainers .WithExposedPorts (valkeyPort ),
76+ }
8477
8578 waitStrategies := []wait.Strategy {
8679 wait .ForListeningPort (valkeyPort ).WithStartupTimeout (time .Second * 10 ),
@@ -109,23 +102,25 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
109102 "--tls-auth-clients" , "yes" ,
110103 }
111104
112- tcOpts = append (tcOpts , testcontainers .WithCmdArgs (cmds ... )) // Append the default CMD with the TLS certificates.
113- tcOpts = append (tcOpts , testcontainers .WithFiles (
114- testcontainers.ContainerFile {
115- Reader : bytes .NewReader (caCert .Bytes ),
116- ContainerFilePath : "/tls/ca.crt" ,
117- FileMode : 0o644 ,
118- },
119- testcontainers.ContainerFile {
120- Reader : bytes .NewReader (serverCert .Bytes ),
121- ContainerFilePath : "/tls/server.crt" ,
122- FileMode : 0o644 ,
123- },
124- testcontainers.ContainerFile {
125- Reader : bytes .NewReader (serverCert .KeyBytes ),
126- ContainerFilePath : "/tls/server.key" ,
127- FileMode : 0o644 ,
128- }))
105+ moduleOpts = append (moduleOpts ,
106+ testcontainers .WithCmdArgs (cmds ... ),
107+ testcontainers .WithFiles (
108+ testcontainers.ContainerFile {
109+ Reader : bytes .NewReader (caCert .Bytes ),
110+ ContainerFilePath : "/tls/ca.crt" ,
111+ FileMode : 0o644 ,
112+ },
113+ testcontainers.ContainerFile {
114+ Reader : bytes .NewReader (serverCert .Bytes ),
115+ ContainerFilePath : "/tls/server.crt" ,
116+ FileMode : 0o644 ,
117+ },
118+ testcontainers.ContainerFile {
119+ Reader : bytes .NewReader (serverCert .KeyBytes ),
120+ ContainerFilePath : "/tls/server.key" ,
121+ FileMode : 0o644 ,
122+ }),
123+ )
129124
130125 settings .tlsConfig = & tls.Config {
131126 MinVersion : tls .VersionTLS12 ,
@@ -135,26 +130,16 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
135130 }
136131 }
137132
138- tcOpts = append (tcOpts , testcontainers .WithWaitStrategy (waitStrategies ... ))
139-
140- // Append the customizers passed to the Run function.
141- tcOpts = append (tcOpts , opts ... )
133+ moduleOpts = append (moduleOpts , testcontainers .WithWaitStrategy (waitStrategies ... ))
142134
143- // Apply the testcontainers customizers.
144- for _ , opt := range tcOpts {
145- if err := opt .Customize (& genericContainerReq ); err != nil {
146- return nil , err
147- }
148- }
149-
150- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
135+ ctr , err := testcontainers .Run (ctx , img , append (moduleOpts , opts ... )... )
151136 var c * ValkeyContainer
152- if container != nil {
153- c = & ValkeyContainer {Container : container , settings : settings }
137+ if ctr != nil {
138+ c = & ValkeyContainer {Container : ctr , settings : settings }
154139 }
155140
156141 if err != nil {
157- return c , fmt .Errorf ("generic container : %w" , err )
142+ return c , fmt .Errorf ("run valkey : %w" , err )
158143 }
159144
160145 return c , nil
@@ -171,33 +156,31 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption {
171156 ContainerFilePath : defaultConfigFile ,
172157 FileMode : 0o755 ,
173158 }
174- req .Files = append (req .Files , cf )
159+
160+ if err := testcontainers .WithFiles (cf )(req ); err != nil {
161+ return err
162+ }
175163
176164 if len (req .Cmd ) == 0 {
177- req .Cmd = []string {valkeyServerProcess , defaultConfigFile }
178- return nil
165+ return testcontainers .WithCmd (valkeyServerProcess , defaultConfigFile )(req )
179166 }
180167
181- // prepend the command to run the redis server with the config file, which must be the first argument of the redis server process
168+ // prepend the command to run the valkey server with the config file, which must be the first argument of the valkey server process
182169 if req .Cmd [0 ] == valkeyServerProcess {
183170 // just insert the config file, then the rest of the args
184- req .Cmd = append ([]string {valkeyServerProcess , defaultConfigFile }, req .Cmd [1 :]... )
185- } else if req .Cmd [0 ] != valkeyServerProcess {
186- // prepend the redis server and the config file, then the rest of the args
187- req .Cmd = append ([]string {valkeyServerProcess , defaultConfigFile }, req .Cmd ... )
171+ return testcontainers .WithCmd (append ([]string {valkeyServerProcess , defaultConfigFile }, req .Cmd [1 :]... )... )(req )
188172 }
189173
190- return nil
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 )
191176 }
192177}
193178
194179// WithLogLevel sets the log level for the valkey server process
195180// See https://redis.io/docs/reference/modules/modules-api-ref/#redismodule_log for more information.
196181func WithLogLevel (level LogLevel ) testcontainers.CustomizeRequestOption {
197182 return func (req * testcontainers.GenericContainerRequest ) error {
198- processValkeyServerArgs (req , []string {"--loglevel" , string (level )})
199-
200- return nil
183+ return processValkeyServerArgs (req , []string {"--loglevel" , string (level )})
201184 }
202185}
203186
@@ -214,24 +197,21 @@ func WithSnapshotting(seconds int, changedKeys int) testcontainers.CustomizeRequ
214197 }
215198
216199 return func (req * testcontainers.GenericContainerRequest ) error {
217- processValkeyServerArgs (req , []string {"--save" , strconv .Itoa (seconds ), strconv .Itoa (changedKeys )})
218- return nil
200+ return processValkeyServerArgs (req , []string {"--save" , strconv .Itoa (seconds ), strconv .Itoa (changedKeys )})
219201 }
220202}
221203
222- func processValkeyServerArgs (req * testcontainers.GenericContainerRequest , args []string ) {
204+ func processValkeyServerArgs (req * testcontainers.GenericContainerRequest , args []string ) error {
223205 if len (req .Cmd ) == 0 {
224- req .Cmd = append ([]string {valkeyServerProcess }, args ... )
225- return
206+ return testcontainers .WithCmd (append ([]string {valkeyServerProcess }, args ... )... )(req )
226207 }
227208
228209 // prepend the command to run the valkey server with the config file
229210 if req .Cmd [0 ] == valkeyServerProcess {
230- // valkey server is already set as the first argument, so just append the config file
231- req .Cmd = append (req .Cmd , args ... )
232- } else if req .Cmd [0 ] != valkeyServerProcess {
233- // valkey server is not set as the first argument, so prepend it alongside the config file
234- req .Cmd = append ([]string {valkeyServerProcess }, req .Cmd ... )
235- req .Cmd = append (req .Cmd , args ... )
211+ // valkey server is already set as the first argument, so just append the args
212+ return testcontainers .WithCmd (append (req .Cmd , args ... )... )(req )
236213 }
214+
215+ // valkey server is not set as the first argument, so prepend it alongside the args
216+ return testcontainers .WithCmd (append (append ([]string {valkeyServerProcess }, req .Cmd ... ), args ... )... )(req )
237217}
0 commit comments