forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolrContainer.java
164 lines (137 loc) · 4.94 KB
/
SolrContainer.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
package org.testcontainers.containers;
import com.github.dockerjava.api.command.InspectContainerResponse;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.utility.DockerImageName;
import java.net.URL;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;
/**
* Testcontainers implementation for Solr.
* <p>
* Supported image: {@code solr}
* <p>
* Exposed ports:
* <ul>
* <li>Solr: 8983</li>
* <li>Zookeeper: 9983</li>
* </ul>
*/
public class SolrContainer extends GenericContainer<SolrContainer> {
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("solr");
@Deprecated
public static final String IMAGE = DEFAULT_IMAGE_NAME.getUnversionedPart();
@Deprecated
public static final String DEFAULT_TAG = "8.3.0";
public static final Integer ZOOKEEPER_PORT = 9983;
public static final Integer SOLR_PORT = 8983;
private SolrContainerConfiguration configuration;
/**
* @deprecated use {@link #SolrContainer(DockerImageName)} instead
*/
@Deprecated
public SolrContainer() {
this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG));
}
/**
* @deprecated use {@link #SolrContainer(DockerImageName)} instead
*/
public SolrContainer(final String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}
public SolrContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
this.waitStrategy =
new LogMessageWaitStrategy()
.withRegEx(".*o\\.e\\.j\\.s\\.Server Started.*")
.withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS));
this.configuration = new SolrContainerConfiguration();
}
public SolrContainer withZookeeper(boolean zookeeper) {
configuration.setZookeeper(zookeeper);
return self();
}
public SolrContainer withCollection(String collection) {
if (StringUtils.isEmpty(collection)) {
throw new IllegalArgumentException("Collection name must not be empty");
}
configuration.setCollectionName(collection);
return self();
}
public SolrContainer withConfiguration(String name, URL solrConfig) {
if (StringUtils.isEmpty(name) || solrConfig == null) {
throw new IllegalArgumentException();
}
configuration.setConfigurationName(name);
configuration.setSolrConfiguration(solrConfig);
return self();
}
public SolrContainer withSchema(URL schema) {
configuration.setSolrSchema(schema);
return self();
}
public int getSolrPort() {
return getMappedPort(SOLR_PORT);
}
public int getZookeeperPort() {
return getMappedPort(ZOOKEEPER_PORT);
}
@Override
@SneakyThrows
protected void configure() {
if (configuration.getSolrSchema() != null && configuration.getSolrConfiguration() == null) {
throw new IllegalStateException("Solr needs to have a configuration is you want to use a schema");
}
// Generate Command Builder
String command = "solr -f";
// Add Default Ports
this.addExposedPort(SOLR_PORT);
// Configure Zookeeper
if (configuration.isZookeeper()) {
this.addExposedPort(ZOOKEEPER_PORT);
command = "-DzkRun -h localhost";
}
// Apply generated Command
this.setCommand(command);
}
@Override
public Set<Integer> getLivenessCheckPortNumbers() {
return new HashSet<>(getSolrPort());
}
@Override
protected void waitUntilContainerStarted() {
getWaitStrategy().waitUntilReady(this);
}
@Override
@SneakyThrows
protected void containerIsStarted(InspectContainerResponse containerInfo) {
if (!configuration.isZookeeper()) {
ExecResult result = execInContainer("solr", "create_core", "-c", configuration.getCollectionName());
if (result.getExitCode() != 0) {
throw new IllegalStateException(
"Unable to create solr core:\nStdout: " + result.getStdout() + "\nStderr:" + result.getStderr()
);
}
return;
}
if (StringUtils.isNotEmpty(configuration.getConfigurationName())) {
SolrClientUtils.uploadConfiguration(
getHost(),
getSolrPort(),
configuration.getConfigurationName(),
configuration.getSolrConfiguration(),
configuration.getSolrSchema()
);
}
SolrClientUtils.createCollection(
getHost(),
getSolrPort(),
configuration.getCollectionName(),
configuration.getConfigurationName()
);
}
}