Skip to content

Commit a541886

Browse files
daschlrnorth
andauthored
[couchbase] Add support for Couchbase Analytics. (#4592)
Co-authored-by: Richard North <[email protected]>
1 parent e122628 commit a541886

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public class CouchbaseContainer extends GenericContainer<CouchbaseContainer> {
6868

6969
private static final int SEARCH_SSL_PORT = 18094;
7070

71+
private static final int ANALYTICS_PORT = 8095;
72+
73+
private static final int ANALYTICS_SSL_PORT = 18095;
74+
7175
private static final int KV_PORT = 11210;
7276

7377
private static final int KV_SSL_PORT = 11207;
@@ -84,7 +88,17 @@ public class CouchbaseContainer extends GenericContainer<CouchbaseContainer> {
8488

8589
private String password = "password";
8690

87-
private Set<CouchbaseService> enabledServices = EnumSet.allOf(CouchbaseService.class);
91+
/**
92+
* Enabled services does not include Analytics since most users likely do not need to test
93+
* with it and is also a little heavy on memory and runtime requirements. Also, it is only
94+
* available with the enterprise edition (EE).
95+
*/
96+
private Set<CouchbaseService> enabledServices = EnumSet.of(
97+
CouchbaseService.KV,
98+
CouchbaseService.QUERY,
99+
CouchbaseService.SEARCH,
100+
CouchbaseService.INDEX
101+
);
88102

89103
private final List<BucketDefinition> buckets = new ArrayList<>();
90104

@@ -144,6 +158,17 @@ public CouchbaseContainer withEnabledServices(final CouchbaseService... enabled)
144158
return this;
145159
}
146160

161+
/**
162+
* Enables the analytics service which is not enabled by default.
163+
*
164+
* @return this {@link CouchbaseContainer} for chaining purposes.
165+
*/
166+
public CouchbaseContainer withAnalyticsService() {
167+
checkNotRunning();
168+
this.enabledServices.add(CouchbaseService.ANALYTICS);
169+
return this;
170+
}
171+
147172
public final String getUsername() {
148173
return username;
149174
}
@@ -177,6 +202,8 @@ protected void configure() {
177202
QUERY_SSL_PORT,
178203
SEARCH_PORT,
179204
SEARCH_SSL_PORT,
205+
ANALYTICS_PORT,
206+
ANALYTICS_SSL_PORT,
180207
KV_PORT,
181208
KV_SSL_PORT
182209
);
@@ -214,6 +241,16 @@ protected void configure() {
214241
);
215242
}
216243

244+
if (enabledServices.contains(CouchbaseService.ANALYTICS)) {
245+
waitStrategy = waitStrategy.withStrategy(
246+
new HttpWaitStrategy()
247+
.forPath("/admin/ping")
248+
.forPort(ANALYTICS_PORT)
249+
.withBasicCredentials(username, password)
250+
.forStatusCode(200)
251+
);
252+
}
253+
217254
waitingFor(waitStrategy);
218255
}
219256

@@ -262,6 +299,10 @@ private void initializeIsEnterprise() {
262299
} catch (IOException e) {
263300
throw new IllegalStateException("Couchbase /pools did not return valid JSON");
264301
}
302+
303+
if (!isEnterprise && enabledServices.contains(CouchbaseService.ANALYTICS)) {
304+
throw new IllegalStateException("The Analytics Service is only supported with the Enterprise version");
305+
}
265306
}
266307

267308
/**
@@ -349,6 +390,11 @@ private void configureExternalPorts() {
349390
builder.add("ftsSSL", Integer.toString(getMappedPort(SEARCH_SSL_PORT)));
350391
}
351392

393+
if (enabledServices.contains(CouchbaseService.ANALYTICS)) {
394+
builder.add("cbas", Integer.toString(getMappedPort(ANALYTICS_PORT)));
395+
builder.add("cbasSSL", Integer.toString(getMappedPort(ANALYTICS_SSL_PORT)));
396+
}
397+
352398
@Cleanup Response response = doHttpRequest(
353399
MGMT_PORT,
354400
"/node/controller/setupAlternateAddresses/external",

modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ public enum CouchbaseService {
3939
/**
4040
* Indexing service (needed if QUERY is also used!).
4141
*/
42-
INDEX("index");
42+
INDEX("index"),
43+
44+
/**
45+
* Analytics service.
46+
*/
47+
ANALYTICS("cbas");
4348

4449
private final String identifier;
4550

modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.couchbase.client.java.Collection;
2222
import com.couchbase.client.java.json.JsonObject;
2323
import org.junit.Test;
24+
import org.testcontainers.containers.ContainerLaunchException;
2425
import org.testcontainers.utility.DockerImageName;
2526

2627
import java.time.Duration;
@@ -29,6 +30,7 @@
2930
import static org.awaitility.Awaitility.await;
3031
import static org.junit.Assert.assertEquals;
3132
import static org.junit.Assert.assertFalse;
33+
import static org.junit.Assert.assertThrows;
3234

3335
public class CouchbaseContainerTest {
3436

@@ -111,6 +113,20 @@ public void testBucketIsFlushableIfEnabled() {
111113
}
112114
}
113115

116+
/**
117+
* Make sure that the code fails fast if the Analytics service is enabled on the community
118+
* edition which is not supported.
119+
*/
120+
@Test
121+
public void testFailureIfCommunityUsedWithAnalytics() {
122+
try (
123+
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_COMMUNITY)
124+
.withEnabledServices(CouchbaseService.KV, CouchbaseService.ANALYTICS)
125+
) {
126+
assertThrows(ContainerLaunchException.class, () -> setUpClient(container, cluster -> {}));
127+
}
128+
}
129+
114130
private void setUpClient(CouchbaseContainer container, Consumer<Cluster> consumer) {
115131
container.start();
116132

0 commit comments

Comments
 (0)