@@ -56,16 +56,7 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
5656
5757// Run creates an instance of the Redis container type
5858func Run (ctx context.Context , img string , opts ... testcontainers.ContainerCustomizer ) (* RedisContainer , error ) {
59- req := testcontainers.ContainerRequest {
60- Image : img ,
61- ExposedPorts : []string {redisPort },
62- }
63-
64- genericContainerReq := testcontainers.GenericContainerRequest {
65- ContainerRequest : req ,
66- Started : true ,
67- }
68-
59+ // Process custom options to extract settings
6960 var settings options
7061 for _ , opt := range opts {
7162 if opt , ok := opt .(Option ); ok {
@@ -75,17 +66,15 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
7566 }
7667 }
7768
78- tcOpts := []testcontainers.ContainerCustomizer {}
79-
80- waitStrategies := []wait.Strategy {
81- wait .ForListeningPort (redisPort ).WithStartupTimeout (time .Second * 10 ),
82- wait .ForLog ("* Ready to accept connections" ),
69+ moduleOpts := []testcontainers.ContainerCustomizer {
70+ testcontainers .WithExposedPorts (redisPort ),
71+ testcontainers .WithWaitStrategy (
72+ wait .ForListeningPort (redisPort ).WithStartupTimeout (time .Second * 10 ),
73+ wait .ForLog ("* Ready to accept connections" ),
74+ ),
8375 }
8476
8577 if settings .tlsEnabled {
86- // wait for the TLS port to be available
87- waitStrategies = append (waitStrategies , wait .ForListeningPort (redisPort ).WithStartupTimeout (time .Second * 10 ))
88-
8978 // Generate TLS certificates in the fly and add them to the container before it starts.
9079 // Update the CMD to use the TLS certificates.
9180 caCert , clientCert , serverCert , err := createTLSCerts ()
@@ -104,23 +93,26 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
10493 "--tls-auth-clients" , "yes" ,
10594 }
10695
107- tcOpts = append (tcOpts , testcontainers .WithCmdArgs (cmds ... )) // Append the default CMD with the TLS certificates.
108- tcOpts = append (tcOpts , testcontainers .WithFiles (
109- testcontainers.ContainerFile {
110- Reader : bytes .NewReader (caCert .Bytes ),
111- ContainerFilePath : "/tls/ca.crt" ,
112- FileMode : 0o644 ,
113- },
114- testcontainers.ContainerFile {
115- Reader : bytes .NewReader (serverCert .Bytes ),
116- ContainerFilePath : "/tls/server.crt" ,
117- FileMode : 0o644 ,
118- },
119- testcontainers.ContainerFile {
120- Reader : bytes .NewReader (serverCert .KeyBytes ),
121- ContainerFilePath : "/tls/server.key" ,
122- FileMode : 0o644 ,
123- }))
96+ moduleOpts = append (moduleOpts ,
97+ testcontainers .WithCmdArgs (cmds ... ),
98+ testcontainers .WithFiles (
99+ testcontainers.ContainerFile {
100+ Reader : bytes .NewReader (caCert .Bytes ),
101+ ContainerFilePath : "/tls/ca.crt" ,
102+ FileMode : 0o644 ,
103+ },
104+ testcontainers.ContainerFile {
105+ Reader : bytes .NewReader (serverCert .Bytes ),
106+ ContainerFilePath : "/tls/server.crt" ,
107+ FileMode : 0o644 ,
108+ },
109+ testcontainers.ContainerFile {
110+ Reader : bytes .NewReader (serverCert .KeyBytes ),
111+ ContainerFilePath : "/tls/server.key" ,
112+ FileMode : 0o644 ,
113+ },
114+ ),
115+ )
124116
125117 settings .tlsConfig = & tls.Config {
126118 MinVersion : tls .VersionTLS12 ,
@@ -130,26 +122,17 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
130122 }
131123 }
132124
133- tcOpts = append (tcOpts , testcontainers .WithWaitStrategy (waitStrategies ... ))
134-
135125 // Append the customizers passed to the Run function.
136- tcOpts = append (tcOpts , opts ... )
137-
138- // Apply the testcontainers customizers.
139- for _ , opt := range tcOpts {
140- if err := opt .Customize (& genericContainerReq ); err != nil {
141- return nil , err
142- }
143- }
126+ moduleOpts = append (moduleOpts , opts ... )
144127
145- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
128+ ctr , err := testcontainers .Run (ctx , img , moduleOpts ... )
146129 var c * RedisContainer
147- if container != nil {
148- c = & RedisContainer {Container : container , settings : settings }
130+ if ctr != nil {
131+ c = & RedisContainer {Container : ctr , settings : settings }
149132 }
150133
151134 if err != nil {
152- return c , fmt .Errorf ("generic container : %w" , err )
135+ return c , fmt .Errorf ("run redis : %w" , err )
153136 }
154137
155138 return c , nil
0 commit comments