Skip to content

Commit 014500b

Browse files
authored
Add support for community edition of couchbase server (#4221)
1 parent a4a475f commit 014500b

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public class CouchbaseContainer extends GenericContainer<CouchbaseContainer> {
8888

8989
private final List<BucketDefinition> buckets = new ArrayList<>();
9090

91+
private boolean isEnterprise = false;
92+
9193
/**
9294
* Creates a new couchbase container with the default image and version.
9395
* @deprecated use {@link CouchbaseContainer(DockerImageName)} instead
@@ -220,6 +222,7 @@ protected void containerIsStarting(final InspectContainerResponse containerInfo)
220222
logger().debug("Couchbase container is starting, performing configuration.");
221223

222224
waitUntilNodeIsOnline();
225+
initializeIsEnterprise();
223226
renameNode();
224227
initializeServices();
225228
configureAdminUser();
@@ -246,6 +249,19 @@ private void waitUntilNodeIsOnline() {
246249
.waitUntilReady(this);
247250
}
248251

252+
/**
253+
* Fetches edition (enterprise or community) of started container.
254+
*/
255+
private void initializeIsEnterprise() {
256+
@Cleanup Response response = doHttpRequest(MGMT_PORT, "/pools", "GET", null, true);
257+
258+
try {
259+
isEnterprise = MAPPER.readTree(response.body().string()).get("isEnterprise").asBoolean();
260+
} catch (IOException e) {
261+
throw new IllegalStateException("Couchbase /pools did not return valid JSON");
262+
}
263+
}
264+
249265
/**
250266
* Rebinds/renames the internal hostname.
251267
* <p>
@@ -349,7 +365,7 @@ private void configureIndexer() {
349365
logger().debug("Configuring the indexer service");
350366

351367
@Cleanup Response response = doHttpRequest(MGMT_PORT, "/settings/indexes", "POST", new FormBody.Builder()
352-
.add("storageMode", "memory_optimized")
368+
.add("storageMode", isEnterprise ? "memory_optimized" : "forestdb")
353369
.build(), true
354370
);
355371

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,20 @@
3232

3333
public class CouchbaseContainerTest {
3434

35-
private static final DockerImageName COUCHBASE_IMAGE = DockerImageName.parse("couchbase/server:6.5.1");
35+
private static final DockerImageName COUCHBASE_IMAGE_ENTERPRISE =
36+
DockerImageName.parse("couchbase/server:enterprise-6.6.2");
37+
private static final DockerImageName COUCHBASE_IMAGE_COMMUNITY =
38+
DockerImageName.parse("couchbase/server:community-6.6.0");
3639

3740
@Test
38-
public void testBasicContainerUsage() {
41+
public void testBasicContainerUsageForEnterpriseContainer() {
3942
// bucket_definition {
4043
BucketDefinition bucketDefinition = new BucketDefinition("mybucket");
4144
// }
4245

4346
try (
4447
// container_definition {
45-
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE)
48+
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_ENTERPRISE)
4649
.withBucket(bucketDefinition)
4750
// }
4851
) {
@@ -61,13 +64,36 @@ public void testBasicContainerUsage() {
6164
}
6265
}
6366

67+
@Test
68+
public void testBasicContainerUsageForCommunityContainer() {
69+
BucketDefinition bucketDefinition = new BucketDefinition("mybucket");
70+
71+
try (
72+
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_COMMUNITY)
73+
.withBucket(bucketDefinition)
74+
) {
75+
setUpClient(container, cluster -> {
76+
Bucket bucket = cluster.bucket(bucketDefinition.getName());
77+
bucket.waitUntilReady(Duration.ofSeconds(10L));
78+
79+
Collection collection = bucket.defaultCollection();
80+
81+
collection.upsert("foo", JsonObject.create().put("key", "value"));
82+
83+
JsonObject fooObject = collection.get("foo").contentAsObject();
84+
85+
assertEquals("value", fooObject.getString("key"));
86+
});
87+
}
88+
}
89+
6490
@Test
6591
public void testBucketIsFlushableIfEnabled() {
6692
BucketDefinition bucketDefinition = new BucketDefinition("mybucket")
6793
.withFlushEnabled(true);
6894

6995
try (
70-
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE)
96+
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_ENTERPRISE)
7197
.withBucket(bucketDefinition)
7298
) {
7399
setUpClient(container, cluster -> {

0 commit comments

Comments
 (0)