diff --git a/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java b/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java index e9247b8c84a..db8d8ec9666 100644 --- a/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java +++ b/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java @@ -68,6 +68,10 @@ public class CouchbaseContainer extends GenericContainer { private static final int SEARCH_SSL_PORT = 18094; + private static final int ANALYTICS_PORT = 8095; + + private static final int ANALYTICS_SSL_PORT = 18095; + private static final int KV_PORT = 11210; private static final int KV_SSL_PORT = 11207; @@ -84,7 +88,17 @@ public class CouchbaseContainer extends GenericContainer { private String password = "password"; - private Set enabledServices = EnumSet.allOf(CouchbaseService.class); + /** + * Enabled services does not include Analytics since most users likely do not need to test + * with it and is also a little heavy on memory and runtime requirements. Also, it is only + * available with the enterprise edition (EE). + */ + private Set enabledServices = EnumSet.of( + CouchbaseService.KV, + CouchbaseService.QUERY, + CouchbaseService.SEARCH, + CouchbaseService.INDEX + ); private final List buckets = new ArrayList<>(); @@ -144,6 +158,17 @@ public CouchbaseContainer withEnabledServices(final CouchbaseService... enabled) return this; } + /** + * Enables the analytics service which is not enabled by default. + * + * @return this {@link CouchbaseContainer} for chaining purposes. + */ + public CouchbaseContainer withAnalyticsService() { + checkNotRunning(); + this.enabledServices.add(CouchbaseService.ANALYTICS); + return this; + } + public final String getUsername() { return username; } @@ -177,6 +202,8 @@ protected void configure() { QUERY_SSL_PORT, SEARCH_PORT, SEARCH_SSL_PORT, + ANALYTICS_PORT, + ANALYTICS_SSL_PORT, KV_PORT, KV_SSL_PORT ); @@ -214,6 +241,16 @@ protected void configure() { ); } + if (enabledServices.contains(CouchbaseService.ANALYTICS)) { + waitStrategy = waitStrategy.withStrategy( + new HttpWaitStrategy() + .forPath("/admin/ping") + .forPort(ANALYTICS_PORT) + .withBasicCredentials(username, password) + .forStatusCode(200) + ); + } + waitingFor(waitStrategy); } @@ -262,6 +299,10 @@ private void initializeIsEnterprise() { } catch (IOException e) { throw new IllegalStateException("Couchbase /pools did not return valid JSON"); } + + if (!isEnterprise && enabledServices.contains(CouchbaseService.ANALYTICS)) { + throw new IllegalStateException("The Analytics Service is only supported with the Enterprise version"); + } } /** @@ -349,6 +390,11 @@ private void configureExternalPorts() { builder.add("ftsSSL", Integer.toString(getMappedPort(SEARCH_SSL_PORT))); } + if (enabledServices.contains(CouchbaseService.ANALYTICS)) { + builder.add("cbas", Integer.toString(getMappedPort(ANALYTICS_PORT))); + builder.add("cbasSSL", Integer.toString(getMappedPort(ANALYTICS_SSL_PORT))); + } + @Cleanup Response response = doHttpRequest( MGMT_PORT, "/node/controller/setupAlternateAddresses/external", diff --git a/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseService.java b/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseService.java index c60fc6b71d0..57d3e37fcf2 100644 --- a/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseService.java +++ b/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseService.java @@ -39,7 +39,12 @@ public enum CouchbaseService { /** * Indexing service (needed if QUERY is also used!). */ - INDEX("index"); + INDEX("index"), + + /** + * Analytics service. + */ + ANALYTICS("cbas"); private final String identifier; diff --git a/modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java b/modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java index fcebbe2bb62..5d2d7d9402a 100644 --- a/modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java +++ b/modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java @@ -21,6 +21,7 @@ import com.couchbase.client.java.Collection; import com.couchbase.client.java.json.JsonObject; import org.junit.Test; +import org.testcontainers.containers.ContainerLaunchException; import org.testcontainers.utility.DockerImageName; import java.time.Duration; @@ -29,6 +30,7 @@ import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; public class CouchbaseContainerTest { @@ -111,6 +113,20 @@ public void testBucketIsFlushableIfEnabled() { } } + /** + * Make sure that the code fails fast if the Analytics service is enabled on the community + * edition which is not supported. + */ + @Test + public void testFailureIfCommunityUsedWithAnalytics() { + try ( + CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_COMMUNITY) + .withEnabledServices(CouchbaseService.KV, CouchbaseService.ANALYTICS) + ) { + assertThrows(ContainerLaunchException.class, () -> setUpClient(container, cluster -> {})); + } + } + private void setUpClient(CouchbaseContainer container, Consumer consumer) { container.start();