@@ -174,60 +174,50 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
174174 password : defaultPassword ,
175175 },
176176 }
177- req := testcontainers.GenericContainerRequest {
178- ContainerRequest : testcontainers.ContainerRequest {
179- Image : img ,
180- ExposedPorts : []string {
181- defaultSQLPort ,
182- defaultAdminPort ,
183- },
184- Env : map [string ]string {
185- "COCKROACH_DATABASE" : defaultDatabase ,
186- "COCKROACH_USER" : defaultUser ,
187- "COCKROACH_PASSWORD" : defaultPassword ,
188- },
189- Files : []testcontainers.ContainerFile {{
190- Reader : newDefaultsReader (clusterDefaults ),
191- ContainerFilePath : clusterDefaultsContainerFile ,
192- FileMode : 0o644 ,
193- }},
194- Cmd : []string {
195- "start-single-node" ,
196- memStorageFlag + defaultStoreSize ,
197- },
198- WaitingFor : wait .ForAll (
199- wait .ForFile (cockroachDir + "/init_success" ),
200- wait .ForHTTP ("/health" ).WithPort (defaultAdminPort ),
201- wait .ForTLSCert (
202- certsDir + "/client." + defaultUser + ".crt" ,
203- certsDir + "/client." + defaultUser + ".key" ,
204- ).WithRootCAs (fileCACert ).WithServerName ("127.0.0.1" ),
205- wait .ForSQL (defaultSQLPort , "pgx/v5" , func (host string , port nat.Port ) string {
206- connStr , err := ctr .connString (host , port )
207- if err != nil {
208- panic (err )
209- }
210- return connStr
211- }),
212- ),
213- },
214- Started : true ,
215- }
216177
217- for _ , opt := range opts {
218- if err := opt .Customize (& req ); err != nil {
219- return nil , fmt .Errorf ("customize request: %w" , err )
220- }
178+ moduleOpts := []testcontainers.ContainerCustomizer {
179+ testcontainers .WithCmd (
180+ "start-single-node" ,
181+ memStorageFlag + defaultStoreSize ,
182+ ),
183+ testcontainers .WithExposedPorts (defaultSQLPort , defaultAdminPort ),
184+ testcontainers .WithEnv (map [string ]string {
185+ "COCKROACH_DATABASE" : defaultDatabase ,
186+ "COCKROACH_USER" : defaultUser ,
187+ "COCKROACH_PASSWORD" : defaultPassword ,
188+ }),
189+ testcontainers .WithFiles (testcontainers.ContainerFile {
190+ Reader : newDefaultsReader (clusterDefaults ),
191+ ContainerFilePath : clusterDefaultsContainerFile ,
192+ FileMode : 0o644 ,
193+ }),
194+ testcontainers .WithWaitStrategy (wait .ForAll (
195+ wait .ForFile (cockroachDir + "/init_success" ),
196+ wait .ForHTTP ("/health" ).WithPort (defaultAdminPort ),
197+ wait .ForTLSCert (
198+ certsDir + "/client." + defaultUser + ".crt" ,
199+ certsDir + "/client." + defaultUser + ".key" ,
200+ ).WithRootCAs (fileCACert ).WithServerName ("127.0.0.1" ),
201+ wait .ForSQL (defaultSQLPort , "pgx/v5" , func (host string , port nat.Port ) string {
202+ connStr , err := ctr .connString (host , port )
203+ if err != nil {
204+ panic (err )
205+ }
206+ return connStr
207+ }),
208+ )),
221209 }
222210
223- if err := ctr .configure (& req ); err != nil {
224- return nil , fmt .Errorf ("set options: %w" , err )
225- }
211+ moduleOpts = append (moduleOpts , opts ... )
212+
213+ // configure the wait strategy after all the options have been applied
214+ // It extracts the TLS strategy from the wait strategy and sets it on the container.
215+ moduleOpts = append (moduleOpts , ctr .configure ())
226216
227217 var err error
228- ctr .Container , err = testcontainers .GenericContainer (ctx , req )
218+ ctr .Container , err = testcontainers .Run (ctx , img , moduleOpts ... )
229219 if err != nil {
230- return ctr , fmt .Errorf ("generic container : %w" , err )
220+ return ctr , fmt .Errorf ("run cockroachdb : %w" , err )
231221 }
232222
233223 return ctr , nil
@@ -253,7 +243,12 @@ func (c *CockroachDBContainer) connConfig(host string, port nat.Port) (*pgx.Conn
253243 }
254244
255245 sslMode := "disable"
256- tlsConfig := c .tlsStrategy .TLSConfig ()
246+
247+ var tlsConfig * tls.Config
248+ if c .tlsStrategy != nil {
249+ tlsConfig = c .tlsStrategy .TLSConfig ()
250+ }
251+
257252 if tlsConfig != nil {
258253 sslMode = "verify-full"
259254 }
@@ -278,44 +273,3 @@ func (c *CockroachDBContainer) connConfig(host string, port nat.Port) (*pgx.Conn
278273
279274 return cfg , nil
280275}
281-
282- // configure sets the CockroachDBContainer options from the given request and updates the request
283- // wait strategies to match the options.
284- func (c * CockroachDBContainer ) configure (req * testcontainers.GenericContainerRequest ) error {
285- c .database = req .Env [envDatabase ]
286- c .user = req .Env [envUser ]
287- c .password = req .Env [envPassword ]
288-
289- var insecure bool
290- for _ , arg := range req .Cmd {
291- if arg == insecureFlag {
292- insecure = true
293- break
294- }
295- }
296-
297- // Walk the wait strategies to find the TLS strategy and either remove it or
298- // update the client certificate files to match the user and configure the
299- // container to use the TLS strategy.
300- if err := wait .Walk (& req .WaitingFor , func (strategy wait.Strategy ) error {
301- if cert , ok := strategy .(* wait.TLSStrategy ); ok {
302- if insecure {
303- // If insecure mode is enabled, the certificate strategy is removed.
304- return errors .Join (wait .ErrVisitRemove , wait .ErrVisitStop )
305- }
306-
307- // Update the client certificate files to match the user which may have changed.
308- cert .WithCert (certsDir + "/client." + c .user + ".crt" , certsDir + "/client." + c .user + ".key" )
309-
310- c .tlsStrategy = cert
311-
312- // Stop the walk as the certificate strategy has been found.
313- return wait .ErrVisitStop
314- }
315- return nil
316- }); err != nil {
317- return fmt .Errorf ("walk strategies: %w" , err )
318- }
319-
320- return nil
321- }
0 commit comments