Skip to content

Commit 9b39d09

Browse files
authored
chore(cockroachDB): use Run function (#3326)
* chore(cockroachDB): use Run function * chore: avoid nil tlsconfig
1 parent 65a9c9c commit 9b39d09

File tree

2 files changed

+91
-90
lines changed

2 files changed

+91
-90
lines changed

modules/cockroachdb/cockroachdb.go

Lines changed: 44 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
}

modules/cockroachdb/options.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package cockroachdb
22

33
import (
44
"errors"
5+
"fmt"
56
"path/filepath"
7+
"slices"
68
"strings"
79

810
"github.com/testcontainers/testcontainers-go"
11+
"github.com/testcontainers/testcontainers-go/wait"
912
)
1013

1114
// errInsecureWithPassword is returned when trying to use insecure mode with a password.
@@ -117,3 +120,47 @@ func WithInsecure() testcontainers.CustomizeRequestOption {
117120
return nil
118121
}
119122
}
123+
124+
// configure sets the CockroachDBContainer options from the given request and updates the request
125+
// wait strategies to match the options.
126+
// This option must be called after all the options have been applied, in order to extract
127+
// the credentials from the environment variables and the TLS strategy from the wait strategy.
128+
func (c *CockroachDBContainer) configure() testcontainers.CustomizeRequestOption {
129+
return func(req *testcontainers.GenericContainerRequest) error {
130+
// refresh the credentials from the environment variables
131+
c.user = req.Env[envUser]
132+
c.password = req.Env[envPassword]
133+
c.database = req.Env[envDatabase]
134+
135+
var insecure bool
136+
if slices.Contains(req.Cmd, insecureFlag) {
137+
insecure = true
138+
}
139+
140+
// Walk the wait strategies to find the TLS strategy and either remove it or
141+
// update the client certificate files to match the user and configure the
142+
// container to use the TLS strategy.
143+
if err := wait.Walk(&req.WaitingFor, func(strategy wait.Strategy) error {
144+
if cert, ok := strategy.(*wait.TLSStrategy); ok {
145+
if insecure {
146+
// If insecure mode is enabled, the certificate strategy is removed.
147+
return errors.Join(wait.ErrVisitRemove, wait.ErrVisitStop)
148+
}
149+
150+
// Update the client certificate files to match the user which may have changed.
151+
cert.WithCert(certsDir+"/client."+c.user+".crt", certsDir+"/client."+c.user+".key")
152+
153+
c.tlsStrategy = cert
154+
155+
// Stop the walk as the certificate strategy has been found.
156+
return wait.ErrVisitStop
157+
}
158+
159+
return nil
160+
}); err != nil {
161+
return fmt.Errorf("walk strategies: %w", err)
162+
}
163+
164+
return nil
165+
}
166+
}

0 commit comments

Comments
 (0)