forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RabbitMQContainer.java
422 lines (361 loc) · 14.8 KB
/
RabbitMQContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package org.testcontainers.containers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.jetbrains.annotations.NotNull;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.lang.String.join;
import static java.util.Arrays.asList;
/**
* Testcontainer for RabbitMQ.
*
* @author Martin Greber
*/
public class RabbitMQContainer extends GenericContainer<RabbitMQContainer> {
/**
* The image defaults to the official RabbitmQ image: <a href="https://hub.docker.com/_/rabbitmq/">RabbitMQ</a>.
*/
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("rabbitmq");
private static final String DEFAULT_TAG = "3.7.25-management-alpine";
private static final int DEFAULT_AMQP_PORT = 5672;
private static final int DEFAULT_AMQPS_PORT = 5671;
private static final int DEFAULT_HTTPS_PORT = 15671;
private static final int DEFAULT_HTTP_PORT = 15672;
private String adminPassword = "guest";
private String adminUsername = "guest";
private final List<List<String>> values = new ArrayList<>();
/**
* Creates a RabbitMQ container using the official RabbitMQ docker image.
* @deprecated use {@link RabbitMQContainer(DockerImageName)} instead
*/
@Deprecated
public RabbitMQContainer() {
this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG));
}
/**
* Creates a RabbitMQ container using a specific docker image.
*
* @param dockerImageName The docker image to use.
*/
public RabbitMQContainer(String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}
public RabbitMQContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
addExposedPorts(DEFAULT_AMQP_PORT, DEFAULT_AMQPS_PORT, DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
this.waitStrategy = Wait.
forLogMessage(".*Server startup complete.*", 1)
.withStartupTimeout(Duration.ofSeconds(60));
}
@Override
protected void configure() {
if (adminPassword != null) {
addEnv("RABBITMQ_DEFAULT_PASS", adminPassword);
}
}
@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
values.forEach(command -> {
try {
ExecResult execResult = execInContainer(command.toArray(new String[0]));
if (execResult.getExitCode() != 0) {
logger().error("Could not execute command {}: {}", command, execResult.getStderr());
}
} catch (IOException | InterruptedException e) {
logger().error("Could not execute command {}: {}", command, e.getMessage());
}
});
}
/**
* @return The admin password for the <code>admin</code> account
*/
public String getAdminPassword() {
return adminPassword;
}
public String getAdminUsername() {
return adminUsername;
}
public Integer getAmqpPort() {
return getMappedPort(DEFAULT_AMQP_PORT);
}
public Integer getAmqpsPort() {
return getMappedPort(DEFAULT_AMQPS_PORT);
}
public Integer getHttpsPort() {
return getMappedPort(DEFAULT_HTTPS_PORT);
}
public Integer getHttpPort() {
return getMappedPort(DEFAULT_HTTP_PORT);
}
/**
* @return AMQP URL for use with AMQP clients.
*/
public String getAmqpUrl() {
return "amqp://" + getHost() + ":" + getAmqpPort();
}
/**
* @return AMQPS URL for use with AMQPS clients.
*/
public String getAmqpsUrl() {
return "amqps://" + getHost() + ":" + getAmqpsPort();
}
/**
* @return URL of the HTTP management endpoint.
*/
public String getHttpUrl() {
return "http://" + getHost() + ":" + getHttpPort();
}
/**
* @return URL of the HTTPS management endpoint.
*/
public String getHttpsUrl() {
return "https://" + getHost() + ":" + getHttpsPort();
}
/**
* Sets the password for the admin (default is <pre>guest</pre>)
*
* @param adminPassword The admin password.
* @return This container.
*/
public RabbitMQContainer withAdminPassword(final String adminPassword) {
this.adminPassword = adminPassword;
return this;
}
public enum SslVerification {
VERIFY_NONE("verify_none"), VERIFY_PEER("verify_peer");
SslVerification(String value) {
this.value = value;
}
private final String value;
}
public RabbitMQContainer withSSL(
final MountableFile keyFile,
final MountableFile certFile,
final MountableFile caFile,
final SslVerification verify,
boolean failIfNoCert,
int verificationDepth) {
return withSSL(keyFile, certFile, caFile, verify, failIfNoCert)
.withEnv("RABBITMQ_SSL_DEPTH", String.valueOf(verificationDepth));
}
public RabbitMQContainer withSSL(
final MountableFile keyFile,
final MountableFile certFile,
final MountableFile caFile,
final SslVerification verify,
boolean failIfNoCert) {
return withSSL(keyFile, certFile, caFile, verify)
.withEnv("RABBITMQ_SSL_FAIL_IF_NO_PEER_CERT", String.valueOf(failIfNoCert));
}
public RabbitMQContainer withSSL(
final MountableFile keyFile,
final MountableFile certFile,
final MountableFile caFile,
final SslVerification verify) {
return withEnv("RABBITMQ_SSL_CACERTFILE", "/etc/rabbitmq/ca_cert.pem")
.withEnv("RABBITMQ_SSL_CERTFILE", "/etc/rabbitmq/rabbitmq_cert.pem")
.withEnv("RABBITMQ_SSL_KEYFILE", "/etc/rabbitmq/rabbitmq_key.pem")
.withEnv("RABBITMQ_SSL_VERIFY", verify.value)
.withCopyFileToContainer(certFile, "/etc/rabbitmq/rabbitmq_cert.pem")
.withCopyFileToContainer(caFile, "/etc/rabbitmq/ca_cert.pem")
.withCopyFileToContainer(keyFile, "/etc/rabbitmq/rabbitmq_key.pem");
}
public RabbitMQContainer withPluginsEnabled(String... pluginNames) {
List<String> command = new ArrayList<>(asList("rabbitmq-plugins", "enable"));
command.addAll(asList(pluginNames));
values.add(command);
return self();
}
public RabbitMQContainer withBinding(String source, String destination) {
values.add(asList("rabbitmqadmin", "declare", "binding",
"source=" + source,
"destination=" + destination));
return self();
}
public RabbitMQContainer withBinding(String source, String destination, Map<String, Object> arguments, String routingKey, String destinationType) {
values.add(asList("rabbitmqadmin", "declare", "binding",
"source=" + source,
"destination=" + destination,
"routing_key=" + routingKey,
"destination_type=" + destinationType,
"arguments=" + toJson(arguments)));
return self();
}
public RabbitMQContainer withParameter(String component, String name, String value) {
values.add(asList("rabbitmqadmin", "declare", "parameter",
"component=" + component,
"name=" + name,
"value=" + value));
return self();
}
public RabbitMQContainer withPermission(String vhost, String user, String configure, String write, String read) {
values.add(asList("rabbitmqadmin", "declare", "permission",
"vhost=" + vhost,
"user=" + user,
"configure=" + configure,
"write=" + write,
"read=" + read));
return self();
}
public RabbitMQContainer withUser(String name, String password) {
values.add(asList("rabbitmqadmin", "declare", "user",
"name=" + name,
"password=" + password,
"tags="));
return self();
}
public RabbitMQContainer withUser(String name, String password, Set<String> tags) {
values.add(asList("rabbitmqadmin", "declare", "user",
"name=" + name,
"password=" + password,
"tags=" + join(",", tags)));
return self();
}
public RabbitMQContainer withPolicy(String name, String pattern, Map<String, Object> definition) {
values.add(asList("rabbitmqadmin", "declare", "policy",
"name=" + name,
"pattern=" + pattern,
"definition=" + toJson(definition)));
return self();
}
public RabbitMQContainer withPolicy(String vhost, String name, String pattern, Map<String, Object> definition) {
values.add(asList("rabbitmqadmin", "declare", "policy",
"--vhost=" + vhost,
"name=" + name,
"pattern=" + pattern,
"definition=" + toJson(definition)));
return self();
}
public RabbitMQContainer withPolicy(String name, String pattern, Map<String, Object> definition, int priority, String applyTo) {
values.add(asList("rabbitmqadmin", "declare", "policy",
"name=" + name,
"pattern=" + pattern,
"priority=" + priority,
"apply-to=" + applyTo,
"definition=" + toJson(definition)));
return self();
}
public RabbitMQContainer withOperatorPolicy(String name, String pattern, Map<String, Object> definition) {
values.add(new ArrayList<>(asList("rabbitmqadmin", "declare", "operator_policy",
"name=" + name,
"pattern=" + pattern,
"definition=" + toJson(definition))));
return self();
}
public RabbitMQContainer withOperatorPolicy(String name, String pattern, Map<String, Object> definition, int priority, String applyTo) {
values.add(asList("rabbitmqadmin", "declare", "operator_policy",
"name=" + name,
"pattern=" + pattern,
"priority=" + priority,
"apply-to=" + applyTo,
"definition=" + toJson(definition)));
return self();
}
public RabbitMQContainer withVhost(String name) {
values.add(asList("rabbitmqadmin", "declare", "vhost",
"name=" + name));
return self();
}
public RabbitMQContainer withVhost(String name, boolean tracing) {
values.add(asList("rabbitmqadmin", "declare", "vhost",
"name=" + name,
"tracing=" + tracing));
return self();
}
public RabbitMQContainer withVhostLimit(String vhost, String name, int value) {
values.add(asList("rabbitmqadmin", "declare", "vhost_limit",
"vhost=" + vhost,
"name=" + name,
"value=" + value));
return self();
}
public RabbitMQContainer withQueue(String name) {
values.add(asList("rabbitmqadmin", "declare", "queue",
"name=" + name));
return self();
}
public RabbitMQContainer withQueue(String name, boolean autoDelete, boolean durable, Map<String, Object> arguments) {
values.add(asList("rabbitmqadmin", "declare", "queue",
"name=" + name,
"auto_delete=" + autoDelete,
"durable=" + durable,
"arguments=" + toJson(arguments)));
return self();
}
public RabbitMQContainer withExchange(String name, String type) {
values.add(asList("rabbitmqadmin", "declare", "exchange",
"name=" + name,
"type=" + type));
return self();
}
public RabbitMQContainer withExchange(String name, String type, boolean autoDelete, boolean internal, boolean durable, Map<String, Object> arguments) {
values.add(asList("rabbitmqadmin", "declare", "exchange",
"name=" + name,
"type=" + type,
"auto_delete=" + autoDelete,
"internal=" + internal,
"durable=" + durable,
"arguments=" + toJson(arguments)));
return self();
}
public RabbitMQContainer withExchange(String vhost, String name, String type, boolean autoDelete, boolean internal, boolean durable, Map<String, Object> arguments) {
values.add(asList("rabbitmqadmin", "--vhost=" + vhost, "declare", "exchange",
"name=" + name,
"type=" + type,
"auto_delete=" + autoDelete,
"internal=" + internal,
"durable=" + durable,
"arguments=" + toJson(arguments)));
return self();
}
/**
* Overwrites the default RabbitMQ configuration file with the supplied one.
*
* @param rabbitMQConf The rabbitmq.conf file to use (in sysctl format, don't forget empty line in the end of file)
* @return This container.
*/
public RabbitMQContainer withRabbitMQConfig(MountableFile rabbitMQConf) {
return withRabbitMQConfigSysctl(rabbitMQConf);
}
/**
* Overwrites the default RabbitMQ configuration file with the supplied one.
*
* This function doesn't work with RabbitMQ < 3.7.
*
* This function and the Sysctl format is recommended for RabbitMQ >= 3.7
*
* @param rabbitMQConf The rabbitmq.config file to use (in sysctl format, don't forget empty line in the end of file)
* @return This container.
*/
public RabbitMQContainer withRabbitMQConfigSysctl(MountableFile rabbitMQConf) {
withEnv("RABBITMQ_CONFIG_FILE", "/etc/rabbitmq/rabbitmq-custom.conf");
return withCopyFileToContainer(rabbitMQConf, "/etc/rabbitmq/rabbitmq-custom.conf");
}
/**
* Overwrites the default RabbitMQ configuration file with the supplied one.
*
* @param rabbitMQConf The rabbitmq.config file to use (in erlang format)
* @return This container.
*/
public RabbitMQContainer withRabbitMQConfigErlang(MountableFile rabbitMQConf) {
withEnv("RABBITMQ_CONFIG_FILE", "/etc/rabbitmq/rabbitmq-custom.config");
return withCopyFileToContainer(rabbitMQConf, "/etc/rabbitmq/rabbitmq-custom.config");
}
@NotNull
private String toJson(Map<String, Object> arguments) {
try {
return new ObjectMapper().writeValueAsString(arguments);
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to convert arguments into json: " + e.getMessage(), e);
}
}
}