1
1
package org .testcontainers .containers ;
2
2
3
+ import static org .assertj .core .api .Assertions .assertThat ;
4
+ import static org .assertj .core .api .Assertions .assertThatCode ;
5
+ import static org .testcontainers .containers .RabbitMQContainer .SslVerification .VERIFY_PEER ;
6
+ import static org .testcontainers .utility .MountableFile .forClasspathResource ;
7
+
3
8
import com .google .common .collect .ImmutableMap ;
4
9
import com .google .common .collect .ImmutableSet ;
5
10
import com .rabbitmq .client .Channel ;
6
11
import com .rabbitmq .client .Connection ;
7
12
import com .rabbitmq .client .ConnectionFactory ;
8
- import org .junit .Test ;
9
- import org .testcontainers .utility .MountableFile ;
10
-
11
- import javax .net .ssl .KeyManagerFactory ;
12
- import javax .net .ssl .SSLContext ;
13
- import javax .net .ssl .TrustManagerFactory ;
14
13
import java .io .File ;
15
14
import java .io .FileInputStream ;
16
15
import java .io .IOException ;
17
- import java .security .*;
16
+ import java .security .KeyManagementException ;
17
+ import java .security .KeyStore ;
18
+ import java .security .KeyStoreException ;
19
+ import java .security .NoSuchAlgorithmException ;
20
+ import java .security .UnrecoverableKeyException ;
18
21
import java .security .cert .CertificateException ;
19
-
20
- import static org . assertj . core . api . Assertions . assertThat ;
21
- import static org . assertj . core . api . Assertions . assertThatCode ;
22
- import static org .testcontainers . containers . RabbitMQContainer . SslVerification . VERIFY_PEER ;
23
- import static org .testcontainers .utility .MountableFile . forClasspathResource ;
22
+ import javax . net . ssl . KeyManagerFactory ;
23
+ import javax . net . ssl . SSLContext ;
24
+ import javax . net . ssl . TrustManagerFactory ;
25
+ import org .junit . Test ;
26
+ import org .testcontainers .utility .MountableFile ;
24
27
25
28
/**
26
29
* @author Martin Greber
@@ -34,7 +37,8 @@ public class RabbitMQContainerTest {
34
37
public static final int DEFAULT_HTTP_PORT = 15672 ;
35
38
36
39
@ Test
37
- public void shouldCreateRabbitMQContainer () {
40
+ public void shouldCreateRabbitMQContainer ()
41
+ {
38
42
try (RabbitMQContainer container = new RabbitMQContainer ()) {
39
43
40
44
assertThat (container .getDockerImageName ()).isEqualTo (DEFAULT_IMAGE );
@@ -44,163 +48,199 @@ public void shouldCreateRabbitMQContainer() {
44
48
container .start ();
45
49
46
50
assertThat (container .getAmqpsUrl ()).isEqualTo (
47
- String .format ("amqps://%s:%d" , container .getContainerIpAddress (), container .getMappedPort (DEFAULT_AMQPS_PORT )));
51
+ String .format ("amqps://%s:%d" , container .getContainerIpAddress (), container .getMappedPort (DEFAULT_AMQPS_PORT )));
48
52
assertThat (container .getAmqpUrl ()).isEqualTo (
49
- String .format ("amqp://%s:%d" , container .getContainerIpAddress (), container .getMappedPort (DEFAULT_AMQP_PORT )));
53
+ String .format ("amqp://%s:%d" , container .getContainerIpAddress (), container .getMappedPort (DEFAULT_AMQP_PORT )));
50
54
assertThat (container .getHttpsUrl ()).isEqualTo (
51
- String .format ("https://%s:%d" , container .getContainerIpAddress (), container .getMappedPort (DEFAULT_HTTPS_PORT )));
55
+ String .format ("https://%s:%d" , container .getContainerIpAddress (), container .getMappedPort (DEFAULT_HTTPS_PORT )));
52
56
assertThat (container .getHttpUrl ()).isEqualTo (
53
- String .format ("http://%s:%d" , container .getContainerIpAddress (), container .getMappedPort (DEFAULT_HTTP_PORT )));
57
+ String .format ("http://%s:%d" , container .getContainerIpAddress (), container .getMappedPort (DEFAULT_HTTP_PORT )));
54
58
55
59
assertThat (container .getHttpsPort ()).isEqualTo (container .getMappedPort (DEFAULT_HTTPS_PORT ));
56
60
assertThat (container .getHttpPort ()).isEqualTo (container .getMappedPort (DEFAULT_HTTP_PORT ));
57
61
assertThat (container .getAmqpsPort ()).isEqualTo (container .getMappedPort (DEFAULT_AMQPS_PORT ));
58
62
assertThat (container .getAmqpPort ()).isEqualTo (container .getMappedPort (DEFAULT_AMQP_PORT ));
59
63
60
64
assertThat (container .getLivenessCheckPortNumbers ()).containsExactlyInAnyOrder (
61
- container .getMappedPort (DEFAULT_AMQP_PORT ),
62
- container .getMappedPort (DEFAULT_AMQPS_PORT ),
63
- container .getMappedPort (DEFAULT_HTTP_PORT ),
64
- container .getMappedPort (DEFAULT_HTTPS_PORT )
65
+ container .getMappedPort (DEFAULT_AMQP_PORT ),
66
+ container .getMappedPort (DEFAULT_AMQPS_PORT ),
67
+ container .getMappedPort (DEFAULT_HTTP_PORT ),
68
+ container .getMappedPort (DEFAULT_HTTPS_PORT )
65
69
);
66
70
}
67
71
}
68
72
69
73
@ Test
70
- public void shouldCreateRabbitMQContainerWithTag () {
74
+ public void shouldCreateRabbitMQContainerWithTag ()
75
+ {
71
76
try (RabbitMQContainer container = new RabbitMQContainer (DEFAULT_IMAGE )) {
72
77
assertThat (container .getDockerImageName ()).isEqualTo (DEFAULT_IMAGE );
73
78
}
74
79
}
75
80
76
81
@ Test
77
- public void shouldCreateRabbitMQContainerWithExchange () throws IOException , InterruptedException {
82
+ public void shouldCreateRabbitMQContainerWithExchange () throws IOException , InterruptedException
83
+ {
78
84
try (RabbitMQContainer container = new RabbitMQContainer ()) {
79
85
container .withExchange ("test-exchange" , "direct" );
80
86
81
87
container .start ();
82
88
83
89
assertThat (container .execInContainer ("rabbitmqctl" , "list_exchanges" ).getStdout ())
84
- .containsPattern ("test-exchange\\ s+direct" );
90
+ .containsPattern ("test-exchange\\ s+direct" );
85
91
}
86
92
}
87
93
88
94
@ Test
89
- public void shouldCreateRabbitMQContainerWithQueues () throws IOException , InterruptedException {
95
+ public void shouldCreateRabbitMQContainerWithQueues () throws IOException , InterruptedException
96
+ {
90
97
try (RabbitMQContainer container = new RabbitMQContainer ()) {
91
98
92
99
container .withQueue ("queue-one" )
93
- .withQueue ("queue-two" , false , true , ImmutableMap .of ("x-message-ttl" , 1000 ));
100
+ .withQueue ("queue-two" , false , true , ImmutableMap .of ("x-message-ttl" , 1000 ));
94
101
95
102
container .start ();
96
103
97
104
assertThat (container .execInContainer ("rabbitmqctl" , "list_queues" , "name" , "arguments" ).getStdout ())
98
- .containsPattern ("queue-one" );
105
+ .containsPattern ("queue-one" );
99
106
assertThat (container .execInContainer ("rabbitmqctl" , "list_queues" , "name" , "arguments" ).getStdout ())
100
- .containsPattern ("queue-two\\ s.*x-message-ttl" );
107
+ .containsPattern ("queue-two\\ s.*x-message-ttl" );
108
+ }
109
+ }
110
+
111
+ @ Test
112
+ public void shouldMountConfigurationFile ()
113
+ {
114
+ try (RabbitMQContainer container = new RabbitMQContainer ()) {
115
+
116
+ container .withRabbitMQConfig (MountableFile .forClasspathResource ("/rabbitmq-custom.conf" ));
117
+ container .start ();
118
+
119
+ assertThat (container .getLogs ()).contains ("config file(s) : /etc/rabbitmq/rabbitmq-custom.conf" );
120
+ assertThat (container .getLogs ()).doesNotContain (" (not found)" );
101
121
}
102
122
}
103
123
124
+
125
+ @ Test
126
+ public void shouldMountConfigurationFileErlang ()
127
+ {
128
+ try (RabbitMQContainer container = new RabbitMQContainer ()) {
129
+
130
+ container .withRabbitMQConfigErlang (MountableFile .forClasspathResource ("/rabbitmq-custom.config" ));
131
+ container .start ();
132
+
133
+ assertThat (container .getLogs ()).contains ("config file(s) : /etc/rabbitmq/rabbitmq-custom.config" );
134
+ assertThat (container .getLogs ()).doesNotContain (" (not found)" );
135
+ }
136
+ }
137
+
138
+
104
139
@ Test
105
- public void shouldMountConfigurationFile () {
140
+ public void shouldMountConfigurationFileSysctl ()
141
+ {
106
142
try (RabbitMQContainer container = new RabbitMQContainer ()) {
107
143
108
144
container .withRabbitMQConfig (MountableFile .forClasspathResource ("/rabbitmq-custom.conf" ));
109
145
container .start ();
110
146
111
- assertThat (container .getLogs ().contains ("/etc/rabbitmq/rabbitmq-custom.conf" )).isTrue ();
147
+ assertThat (container .getLogs ()).contains ("config file(s) : /etc/rabbitmq/rabbitmq-custom.conf" );
148
+ assertThat (container .getLogs ()).doesNotContain (" (not found)" );
112
149
}
113
150
}
114
151
115
152
@ Test
116
- public void shouldStartTheWholeEnchilada () throws IOException , InterruptedException {
153
+ public void shouldStartTheWholeEnchilada () throws IOException , InterruptedException
154
+ {
117
155
try (RabbitMQContainer container = new RabbitMQContainer ()) {
118
156
container
119
- .withVhost ("vhost1" )
120
- .withVhostLimit ("vhost1" , "max-connections" , 1 )
121
- .withVhost ("vhost2" , true )
122
- .withExchange ("direct-exchange" , "direct" )
123
- .withExchange ("topic-exchange" , "topic" )
124
- .withQueue ("queue1" )
125
- .withQueue ("queue2" , true , false , ImmutableMap .of ("x-message-ttl" , 1000 ))
126
- .withBinding ("direct-exchange" , "queue1" )
127
- .withUser ("user1" , "password1" )
128
- .withUser ("user2" , "password2" , ImmutableSet .of ("administrator" ))
129
- .withPermission ("vhost1" , "user1" , ".*" , ".*" , ".*" )
130
- .withPolicy ("max length policy" , "^dog" , ImmutableMap .of ("max-length" , 1 ), 1 , "queues" )
131
- .withPolicy ("alternate exchange policy" , "^direct-exchange" , ImmutableMap .of ("alternate-exchange" , "amq.direct" ))
132
- .withOperatorPolicy ("operator policy 1" , "^queue1" , ImmutableMap .of ("message-ttl" , 1000 ), 1 , "queues" )
133
- .withPluginsEnabled ("rabbitmq_shovel" , "rabbitmq_random_exchange" );
157
+ .withVhost ("vhost1" )
158
+ .withVhostLimit ("vhost1" , "max-connections" , 1 )
159
+ .withVhost ("vhost2" , true )
160
+ .withExchange ("direct-exchange" , "direct" )
161
+ .withExchange ("topic-exchange" , "topic" )
162
+ .withQueue ("queue1" )
163
+ .withQueue ("queue2" , true , false , ImmutableMap .of ("x-message-ttl" , 1000 ))
164
+ .withBinding ("direct-exchange" , "queue1" )
165
+ .withUser ("user1" , "password1" )
166
+ .withUser ("user2" , "password2" , ImmutableSet .of ("administrator" ))
167
+ .withPermission ("vhost1" , "user1" , ".*" , ".*" , ".*" )
168
+ .withPolicy ("max length policy" , "^dog" , ImmutableMap .of ("max-length" , 1 ), 1 , "queues" )
169
+ .withPolicy ("alternate exchange policy" , "^direct-exchange" , ImmutableMap .of ("alternate-exchange" , "amq.direct" ))
170
+ .withOperatorPolicy ("operator policy 1" , "^queue1" , ImmutableMap .of ("message-ttl" , 1000 ), 1 , "queues" )
171
+ .withPluginsEnabled ("rabbitmq_shovel" , "rabbitmq_random_exchange" );
134
172
135
173
container .start ();
136
174
137
175
assertThat (container .execInContainer ("rabbitmqadmin" , "list" , "queues" )
138
- .getStdout ())
139
- .contains ("queue1" , "queue2" );
176
+ .getStdout ())
177
+ .contains ("queue1" , "queue2" );
140
178
141
179
assertThat (container .execInContainer ("rabbitmqadmin" , "list" , "exchanges" )
142
- .getStdout ())
143
- .contains ("direct-exchange" , "topic-exchange" );
180
+ .getStdout ())
181
+ .contains ("direct-exchange" , "topic-exchange" );
144
182
145
183
assertThat (container .execInContainer ("rabbitmqadmin" , "list" , "bindings" )
146
- .getStdout ())
147
- .contains ("direct-exchange" );
184
+ .getStdout ())
185
+ .contains ("direct-exchange" );
148
186
149
187
assertThat (container .execInContainer ("rabbitmqadmin" , "list" , "users" )
150
- .getStdout ())
151
- .contains ("user1" , "user2" );
188
+ .getStdout ())
189
+ .contains ("user1" , "user2" );
152
190
153
191
assertThat (container .execInContainer ("rabbitmqadmin" , "list" , "policies" )
154
- .getStdout ())
155
- .contains ("max length policy" , "alternate exchange policy" );
192
+ .getStdout ())
193
+ .contains ("max length policy" , "alternate exchange policy" );
156
194
157
195
assertThat (container .execInContainer ("rabbitmqadmin" , "list" , "operator_policies" )
158
- .getStdout ())
159
- .contains ("operator policy 1" );
196
+ .getStdout ())
197
+ .contains ("operator policy 1" );
160
198
161
199
assertThat (container .execInContainer ("rabbitmq-plugins" , "is_enabled" , "rabbitmq_shovel" , "--quiet" )
162
- .getStdout ())
163
- .contains ("rabbitmq_shovel is enabled" );
200
+ .getStdout ())
201
+ .contains ("rabbitmq_shovel is enabled" );
164
202
165
203
assertThat (container .execInContainer ("rabbitmq-plugins" , "is_enabled" , "rabbitmq_random_exchange" , "--quiet" )
166
- .getStdout ())
167
- .contains ("rabbitmq_random_exchange is enabled" );
204
+ .getStdout ())
205
+ .contains ("rabbitmq_random_exchange is enabled" );
168
206
}
169
207
}
170
208
171
209
@ Test
172
- public void shouldThrowExceptionForDodgyJson () {
210
+ public void shouldThrowExceptionForDodgyJson ()
211
+ {
173
212
try (RabbitMQContainer container = new RabbitMQContainer ()) {
174
213
175
214
assertThatCode (() ->
176
- container .withQueue (
177
- "queue2" ,
178
- true ,
179
- false ,
180
- ImmutableMap .of ("x-message-ttl" , container ))
215
+ container .withQueue (
216
+ "queue2" ,
217
+ true ,
218
+ false ,
219
+ ImmutableMap .of ("x-message-ttl" , container ))
181
220
).hasMessageStartingWith ("Failed to convert arguments into json" );
182
221
183
222
}
184
223
}
185
224
186
225
@ Test
187
- public void shouldWorkWithSSL () {
226
+ public void shouldWorkWithSSL ()
227
+ {
188
228
try (RabbitMQContainer container = new RabbitMQContainer ()) {
189
229
container .withSSL (
190
- forClasspathResource ("/certs/server_key.pem" , 0644 ),
191
- forClasspathResource ("/certs/server_certificate.pem" , 0644 ),
192
- forClasspathResource ("/certs/ca_certificate.pem" , 0644 ),
193
- VERIFY_PEER ,
194
- true
230
+ forClasspathResource ("/certs/server_key.pem" , 0644 ),
231
+ forClasspathResource ("/certs/server_certificate.pem" , 0644 ),
232
+ forClasspathResource ("/certs/ca_certificate.pem" , 0644 ),
233
+ VERIFY_PEER ,
234
+ true
195
235
);
196
236
197
237
container .start ();
198
238
199
239
assertThatCode (() -> {
200
240
ConnectionFactory connectionFactory = new ConnectionFactory ();
201
241
connectionFactory .useSslProtocol (createSslContext (
202
- "certs/client_key.p12" , "password" ,
203
- "certs/truststore.jks" , "password" ));
242
+ "certs/client_key.p12" , "password" ,
243
+ "certs/truststore.jks" , "password" ));
204
244
connectionFactory .enableHostnameVerification ();
205
245
connectionFactory .setUri (container .getAmqpsUrl ());
206
246
connectionFactory .setPassword (container .getAdminPassword ());
@@ -213,7 +253,8 @@ public void shouldWorkWithSSL() {
213
253
}
214
254
215
255
private SSLContext createSslContext (String keystoreFile , String keystorePassword , String truststoreFile , String truststorePassword )
216
- throws KeyStoreException , IOException , NoSuchAlgorithmException , CertificateException , UnrecoverableKeyException , KeyManagementException {
256
+ throws KeyStoreException , IOException , NoSuchAlgorithmException , CertificateException , UnrecoverableKeyException , KeyManagementException
257
+ {
217
258
ClassLoader classLoader = getClass ().getClassLoader ();
218
259
219
260
KeyStore ks = KeyStore .getInstance ("PKCS12" );
0 commit comments