/
DockerElasticSearchRule.java
103 lines (83 loc) · 4.34 KB
/
DockerElasticSearchRule.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
/****************************************************************
* 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;
import java.util.Arrays;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.james.modules.mailbox.ElasticSearchConfiguration;
import org.apache.james.util.streams.SwarmGenericContainer;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.testcontainers.shaded.com.github.dockerjava.api.model.ExposedPort;
import org.testcontainers.shaded.com.github.dockerjava.api.model.Ports.Binding;
import com.google.inject.Module;
public class DockerElasticSearchRule implements GuiceModuleTestRule {
private static final int ELASTIC_SEARCH_PORT = 9300;
public static final int ELASTIC_SEARCH_HTTP_PORT = 9200;
private static boolean isBindingToEveryThing(Binding binding) {
String bindingIp = binding.getHostIp();
return bindingIp == null || bindingIp.equals("0.0.0.0");
}
public PropertiesConfiguration getElasticSearchConfigurationForDocker() {
PropertiesConfiguration configuration = new PropertiesConfiguration();
configuration.addProperty("elasticsearch.masterHost", getIp());
configuration.addProperty("elasticsearch.port", ELASTIC_SEARCH_PORT);
configuration.addProperty("elasticsearch.nb.shards", 1);
configuration.addProperty("elasticsearch.nb.replica", 0);
configuration.addProperty("elasticsearch.retryConnection.maxRetries", 7);
configuration.addProperty("elasticsearch.retryConnection.minDelay", 3000);
configuration.addProperty("elasticsearch.indexAttachments", false);
configuration.addProperty("elasticsearch.http.host", getIp());
configuration.addProperty("elasticsearch.http.port", ELASTIC_SEARCH_HTTP_PORT);
configuration.addProperty("elasticsearch.metrics.reports.enabled", true);
configuration.addProperty("elasticsearch.metrics.reports.period", 30);
configuration.addProperty("elasticsearch.metrics.reports.index", "james-metrics");
return configuration;
}
private SwarmGenericContainer elasticSearchContainer = new SwarmGenericContainer("elasticsearch:2.2.2");
@Override
public Statement apply(Statement base, Description description) {
return elasticSearchContainer.apply(base, description);
}
@Override
public void await() {
}
@Override
public Module getModule() {
return (binder) -> binder.bind(ElasticSearchConfiguration.class).toInstance(this::getElasticSearchConfigurationForDocker);
}
public String getIp() {
return elasticSearchContainer.getIp();
}
public int getBindingPort() {
Binding[] bindings = elasticSearchContainer
.getContainerInfo()
.getNetworkSettings()
.getPorts()
.getBindings()
.get(ExposedPort.tcp(ELASTIC_SEARCH_PORT));
return Integer.valueOf(
Arrays.stream(bindings)
.filter(DockerElasticSearchRule::isBindingToEveryThing)
.map(binding -> binding.getHostPortSpec())
.findFirst().get());
}
public SwarmGenericContainer getElasticSearchContainer() {
return elasticSearchContainer;
}
}