/
CouchbaseContainer.java
284 lines (235 loc) · 9.73 KB
/
CouchbaseContainer.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
/*
* Copyright (c) 2016 Couchbase, Inc.
*
* Licensed 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 trycb.testcontainers;
import com.couchbase.client.core.message.config.RestApiResponse;
import com.couchbase.client.core.utils.Base64;
import com.couchbase.client.deps.io.netty.handler.codec.http.HttpHeaders;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.bucket.BucketType;
import com.couchbase.client.java.cluster.BucketSettings;
import com.couchbase.client.java.cluster.DefaultBucketSettings;
import com.couchbase.client.java.cluster.api.ClusterApiClient;
import com.couchbase.client.java.cluster.api.RestBuilder;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import com.couchbase.client.java.query.Index;
import com.couchbase.client.java.query.N1qlQuery;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.traits.LinkableContainer;
import org.testcontainers.containers.wait.HttpWaitStrategy;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* @author Laurent Doguin
*/
public class CouchbaseContainer<SELF extends CouchbaseContainer<SELF>> extends GenericContainer<SELF> {
private String memoryQuota = "400";
private String indexMemoryQuota = "400";
private String clusterUsername = "Administrator";
private String clusterPassword = "password";
private Boolean keyValue = true;
private Boolean query = true;
private Boolean index = true;
private Boolean fts = true;
private Boolean beerSample = false;
private Boolean travelSample = false;
private Boolean gamesIMSample = false;
private CouchbaseEnvironment couchbaseEnvironment;
private CouchbaseCluster couchbaseCluster;
private List<BucketSettings> newBuckets = new ArrayList<>();
private String urlBase;
public CouchbaseContainer() {
super("couchbase/server:latest");
}
public CouchbaseContainer(String containerName) {
super(containerName);
}
@Override
protected Integer getLivenessCheckPort() {
return getMappedPort(8091);
}
@Override
protected void configure() {
addFixedExposedPort(8091, 8091);
addFixedExposedPort(8092, 8092);
addFixedExposedPort(8093, 8093);
addFixedExposedPort(8094, 8094);
addFixedExposedPort(8095, 8095);
addFixedExposedPort(11207, 11207);
addFixedExposedPort(11210, 11210);
addFixedExposedPort(11211, 11211);
addFixedExposedPort(18091, 18091);
addFixedExposedPort(18092, 18092);
addFixedExposedPort(18093, 18093);
setWaitStrategy(new HttpWaitStrategy().forPath("/ui/index.html#/"));
}
public CouchbaseEnvironment getCouchbaseEnvironnement() {
if (couchbaseEnvironment == null) {
initCluster();
couchbaseEnvironment = DefaultCouchbaseEnvironment.builder()
.bootstrapCarrierDirectPort(getMappedPort(11210))
.bootstrapCarrierSslPort(getMappedPort(11207))
.bootstrapHttpDirectPort(getMappedPort(8091))
.bootstrapHttpSslPort(getMappedPort(18091))
.build();
}
return couchbaseEnvironment;
}
public CouchbaseCluster getCouchbaseCluster() {
if (couchbaseCluster == null) {
couchbaseCluster = CouchbaseCluster.create(getCouchbaseEnvironnement(), getContainerIpAddress());
}
return couchbaseCluster;
}
public SELF withClusterUsername(String username) {
this.clusterUsername = username;
return self();
}
public SELF withClusterPassword(String password) {
this.clusterPassword = password;
return self();
}
public SELF withMemoryQuota(String memoryQuota) {
this.memoryQuota = memoryQuota;
return self();
}
public SELF withIndexMemoryQuota(String indexMemoryQuota) {
this.indexMemoryQuota = indexMemoryQuota;
return self();
}
public SELF withKeyValue(Boolean withKV) {
this.keyValue = withKV;
return self();
}
public SELF withIndex(Boolean withIndex) {
this.index = withIndex;
return self();
}
public SELF withQuery(Boolean withQuery) {
this.query = withQuery;
return self();
}
public SELF withFTS(Boolean withFTS) {
this.fts = withFTS;
return self();
}
public SELF withTravelSample(Boolean withTravelSample) {
this.travelSample = withTravelSample;
return self();
}
public SELF withBeerSample(Boolean withBeerSample) {
this.beerSample = withBeerSample;
return self();
}
public SELF withGamesIMSample(Boolean withGamesIMSample) {
this.gamesIMSample = withGamesIMSample;
return self();
}
public SELF withNewBucket(BucketSettings bucketSettings) {
newBuckets.add(bucketSettings);
return self();
}
public void initCluster() {
urlBase = String.format("http://%s:%s", getContainerIpAddress(), getMappedPort(8091));
try {
String poolURL = "/pools/default";
String poolPayload = "memoryQuota=" + URLEncoder.encode(memoryQuota, "UTF-8") + "&indexMemoryQuota=" + URLEncoder.encode(indexMemoryQuota, "UTF-8");
String setupServicesURL = "/node/controller/setupServices";
StringBuilder servicePayloadBuilder = new StringBuilder();
if (keyValue) {
servicePayloadBuilder.append("kv,");
}
if (query) {
servicePayloadBuilder.append("n1ql,");
}
if (index) {
servicePayloadBuilder.append("index,");
}
if (fts) {
servicePayloadBuilder.append("fts,");
}
String setupServiceContent = "services=" + URLEncoder.encode(servicePayloadBuilder.toString(), "UTF-8");
String webSettingsURL = "/settings/web";
String webSettingsContent = "username=" + URLEncoder.encode(clusterUsername, "UTF-8") + "&password=" + URLEncoder.encode(clusterPassword, "UTF-8") + "&port=8091";
String bucketURL = "/sampleBuckets/install";
StringBuilder sampleBucketPayloadBuilder = new StringBuilder();
sampleBucketPayloadBuilder.append('[');
if (travelSample) {
sampleBucketPayloadBuilder.append("\"travel-sample\",");
}
if (beerSample) {
sampleBucketPayloadBuilder.append("\"beer-sample\",");
}
if (gamesIMSample) {
sampleBucketPayloadBuilder.append("\"gamesim-sample\",");
}
sampleBucketPayloadBuilder.append(']');
callCouchbaseRestAPI(poolURL, poolPayload);
callCouchbaseRestAPI(setupServicesURL, setupServiceContent);
callCouchbaseRestAPI(webSettingsURL, webSettingsContent);
callCouchbaseRestAPI(bucketURL, sampleBucketPayloadBuilder.toString());
CouchbaseWaitStrategy s = new CouchbaseWaitStrategy();
s.withBasicCredentials(clusterUsername, clusterPassword);
s.waitUntilReady(this);
callCouchbaseRestAPI("/settings/indexes", "indexerThreads=0&logLevel=info&maxRollbackPoints=5&storageMode=memory_optimized");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void createBucket(BucketSettings bucketSetting, Boolean createIndex){
BucketSettings bucketSettings = getCouchbaseCluster().clusterManager(clusterUsername, clusterPassword).insertBucket(bucketSetting);
// allow some time for the query service to come up
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (createIndex) {
getCouchbaseCluster().openBucket().query(Index.createPrimaryIndex().on(bucketSetting.name()));
}
}
public void callCouchbaseRestAPI(String url, String payload) throws IOException {
String fullUrl = urlBase + url;
HttpURLConnection httpConnection = (HttpURLConnection) ((new URL(fullUrl).openConnection()));
httpConnection.setDoOutput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
String encoded = Base64.encode((clusterUsername + ":" + clusterPassword).getBytes("UTF-8"));
httpConnection.setRequestProperty("Authorization", "Basic " + encoded);
DataOutputStream out = new DataOutputStream(httpConnection.getOutputStream());
out.writeBytes(payload);
out.flush();
out.close();
httpConnection.getResponseCode();
httpConnection.disconnect();
}
@Override
public void start() {
super.start();
if (!newBuckets.isEmpty()) {
for (BucketSettings bucketSetting : newBuckets) {
createBucket(bucketSetting, index);
}
}
}
}