@@ -80,45 +80,13 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
8080
8181// Run creates an instance of the RabbitMQ container type
8282func Run (ctx context.Context , img string , opts ... testcontainers.ContainerCustomizer ) (* RabbitMQContainer , error ) {
83- req := testcontainers.ContainerRequest {
84- Image : img ,
85- Env : map [string ]string {
86- "RABBITMQ_DEFAULT_USER" : defaultUser ,
87- "RABBITMQ_DEFAULT_PASS" : defaultPassword ,
88- },
89- ExposedPorts : []string {
90- DefaultAMQPPort ,
91- DefaultAMQPSPort ,
92- DefaultHTTPSPort ,
93- DefaultHTTPPort ,
94- },
95- WaitingFor : wait .ForLog (".*Server startup complete.*" ).AsRegexp ().WithStartupTimeout (60 * time .Second ),
96- LifecycleHooks : []testcontainers.ContainerLifecycleHooks {
97- {
98- PostStarts : []testcontainers.ContainerHook {},
99- },
100- },
101- }
102-
103- genericContainerReq := testcontainers.GenericContainerRequest {
104- ContainerRequest : req ,
105- Started : true ,
106- }
107-
10883 // Gather all config options (defaults and then apply provided options)
10984 settings := defaultOptions ()
11085 for _ , opt := range opts {
11186 if apply , ok := opt .(Option ); ok {
112- apply (& settings )
113- }
114- if err := opt .Customize (& genericContainerReq ); err != nil {
115- return nil , err
116- }
117- }
118-
119- if settings .SSLSettings != nil {
120- if err := applySSLSettings (settings .SSLSettings )(& genericContainerReq ); err != nil {
121- return nil , err
87+ if err := apply (& settings ); err != nil {
88+ return nil , fmt .Errorf ("apply option: %w" , err )
89+ }
12290 }
12391 }
12492
@@ -133,38 +101,55 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
133101 return nil , err
134102 }
135103
136- if err := withConfig (tmpConfigFile )(& genericContainerReq ); err != nil {
137- return nil , err
104+ moduleOpts := []testcontainers.ContainerCustomizer {
105+ testcontainers .WithEnv (map [string ]string {
106+ "RABBITMQ_DEFAULT_USER" : settings .AdminUsername ,
107+ "RABBITMQ_DEFAULT_PASS" : settings .AdminPassword ,
108+ }),
109+ testcontainers .WithExposedPorts (
110+ DefaultAMQPPort ,
111+ DefaultAMQPSPort ,
112+ DefaultHTTPSPort ,
113+ DefaultHTTPPort ,
114+ ),
115+ testcontainers .WithWaitStrategy (wait .ForLog (".*Server startup complete.*" ).AsRegexp ().WithStartupTimeout (60 * time .Second )),
116+ withConfig (tmpConfigFile ),
138117 }
139118
140- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
119+ if settings .SSLSettings != nil {
120+ moduleOpts = append (moduleOpts , applySSLSettings (settings .SSLSettings ))
121+ }
122+
123+ moduleOpts = append (moduleOpts , opts ... )
124+
125+ ctr , err := testcontainers .Run (ctx , img , moduleOpts ... )
141126 var c * RabbitMQContainer
142- if container != nil {
127+ if ctr != nil {
143128 c = & RabbitMQContainer {
144- Container : container ,
129+ Container : ctr ,
145130 AdminUsername : settings .AdminUsername ,
146131 AdminPassword : settings .AdminPassword ,
147132 }
148133 }
149134
150135 if err != nil {
151- return c , fmt .Errorf ("generic container : %w" , err )
136+ return c , fmt .Errorf ("run rabbitmq : %w" , err )
152137 }
153138
154139 return c , nil
155140}
156141
157142func withConfig (hostPath string ) testcontainers.CustomizeRequestOption {
158143 return func (req * testcontainers.GenericContainerRequest ) error {
159- req .Env ["RABBITMQ_CONFIG_FILE" ] = defaultCustomConfPath
144+ if err := testcontainers .WithEnv (map [string ]string {"RABBITMQ_CONFIG_FILE" : defaultCustomConfPath })(req ); err != nil {
145+ return err
146+ }
160147
161- req . Files = append ( req . Files , testcontainers.ContainerFile {
148+ return testcontainers . WithFiles ( testcontainers.ContainerFile {
162149 HostFilePath : hostPath ,
163150 ContainerFilePath : defaultCustomConfPath ,
164151 FileMode : 0o644 ,
165- })
166-
167- return nil
152+ })(req )
168153 }
169154}
170155
@@ -177,27 +162,29 @@ func applySSLSettings(sslSettings *SSLSettings) testcontainers.CustomizeRequestO
177162 const defaultPermission = 0o644
178163
179164 return func (req * testcontainers.GenericContainerRequest ) error {
180- req .Files = append (req .Files , testcontainers.ContainerFile {
181- HostFilePath : sslSettings .CACertFile ,
182- ContainerFilePath : rabbitCaCertPath ,
183- FileMode : defaultPermission ,
184- })
185- req .Files = append (req .Files , testcontainers.ContainerFile {
186- HostFilePath : sslSettings .CertFile ,
187- ContainerFilePath : rabbitCertPath ,
188- FileMode : defaultPermission ,
189- })
190- req .Files = append (req .Files , testcontainers.ContainerFile {
191- HostFilePath : sslSettings .KeyFile ,
192- ContainerFilePath : rabbitKeyPath ,
193- FileMode : defaultPermission ,
194- })
165+ if err := testcontainers .WithFiles (
166+ testcontainers.ContainerFile {
167+ HostFilePath : sslSettings .CACertFile ,
168+ ContainerFilePath : rabbitCaCertPath ,
169+ FileMode : defaultPermission ,
170+ },
171+ testcontainers.ContainerFile {
172+ HostFilePath : sslSettings .CertFile ,
173+ ContainerFilePath : rabbitCertPath ,
174+ FileMode : defaultPermission ,
175+ },
176+ testcontainers.ContainerFile {
177+ HostFilePath : sslSettings .KeyFile ,
178+ ContainerFilePath : rabbitKeyPath ,
179+ FileMode : defaultPermission ,
180+ },
181+ )(req ); err != nil {
182+ return err
183+ }
195184
196185 // To verify that TLS has been enabled on the node, container logs should contain an entry about a TLS listener being enabled
197186 // See https://www.rabbitmq.com/ssl.html#enabling-tls-verify-configuration
198- req .WaitingFor = wait .ForAll (req .WaitingFor , wait .ForLog ("started TLS (SSL) listener on [::]:5671" ))
199-
200- return nil
187+ return testcontainers .WithAdditionalWaitStrategy (wait .ForLog ("started TLS (SSL) listener on [::]:5671" ))(req )
201188 }
202189}
203190
0 commit comments