-
Notifications
You must be signed in to change notification settings - Fork 462
/
DockerClusterRabbitMQExtension.java
120 lines (98 loc) · 4.73 KB
/
DockerClusterRabbitMQExtension.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
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.queue.rabbitmq;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.testcontainers.containers.Network;
import com.google.common.collect.ImmutableList;
import com.rabbitmq.client.Address;
public class DockerClusterRabbitMQExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
public static final String RABBIT_1 = "rabbit1";
public static final String RABBIT_2 = "rabbit2";
public static final String RABBIT_3 = "rabbit3";
private DockerRabbitMQCluster cluster;
private Network network;
@Override
public void beforeEach(ExtensionContext context) throws Exception {
String cookie = DigestUtils.sha1Hex("secret cookie here");
network = Network.NetworkImpl.builder()
.enableIpv6(false)
.createNetworkCmdModifiers(ImmutableList.of())
.build();
DockerRabbitMQ rabbitMQ1 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_1, cookie, "rabbit@rabbit1", network);
DockerRabbitMQ rabbitMQ2 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_2, cookie, "rabbit@rabbit2", network);
DockerRabbitMQ rabbitMQ3 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_3, cookie, "rabbit@rabbit3", network);
rabbitMQ1.start();
rabbitMQ2.start();
rabbitMQ3.start();
rabbitMQ2.join(rabbitMQ1);
rabbitMQ3.join(rabbitMQ1);
rabbitMQ2.startApp();
rabbitMQ3.startApp();
cluster = new DockerRabbitMQCluster(rabbitMQ1, rabbitMQ2, rabbitMQ3);
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
cluster.stop();
network.close();
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return (parameterContext.getParameter().getType() == DockerRabbitMQCluster.class);
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return cluster;
}
public static class DockerRabbitMQCluster {
private final DockerRabbitMQ rabbitMQ1;
private final DockerRabbitMQ rabbitMQ2;
private final DockerRabbitMQ rabbitMQ3;
public DockerRabbitMQCluster(DockerRabbitMQ rabbitMQ1, DockerRabbitMQ rabbitMQ2, DockerRabbitMQ rabbitMQ3) {
this.rabbitMQ1 = rabbitMQ1;
this.rabbitMQ2 = rabbitMQ2;
this.rabbitMQ3 = rabbitMQ3;
}
public void stop() {
rabbitMQ1.stop();
rabbitMQ2.stop();
rabbitMQ3.stop();
}
public DockerRabbitMQ getRabbitMQ1() {
return rabbitMQ1;
}
public DockerRabbitMQ getRabbitMQ2() {
return rabbitMQ2;
}
public DockerRabbitMQ getRabbitMQ3() {
return rabbitMQ3;
}
public ImmutableList<Address> getAddresses() {
return ImmutableList.of(
new Address(rabbitMQ1.getHostIp(), rabbitMQ1.getPort()),
new Address(rabbitMQ2.getHostIp(), rabbitMQ2.getPort()),
new Address(rabbitMQ3.getHostIp(), rabbitMQ3.getPort()));
}
}
}