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 5e9a4b45f4d..25e7637bbaf 100644 --- a/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java +++ b/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java @@ -123,6 +123,8 @@ public class CouchbaseContainer extends GenericContainer<CouchbaseContainer> { private boolean isEnterprise = false; + private boolean hasTlsPorts = false; + /** * Creates a new couchbase container with the default image and version. * @deprecated use {@link #CouchbaseContainer(DockerImageName)} instead @@ -345,6 +347,7 @@ protected void containerIsStarting(final InspectContainerResponse containerInfo) timePhase("waitUntilNodeIsOnline", this::waitUntilNodeIsOnline); timePhase("initializeIsEnterprise", this::initializeIsEnterprise); + timePhase("initializeHasTlsPorts", this::initializeHasTlsPorts); timePhase("renameNode", this::renameNode); timePhase("initializeServices", this::initializeServices); timePhase("setMemoryQuotas", this::setMemoryQuotas); @@ -394,6 +397,31 @@ private void initializeIsEnterprise() { } } + /** + * Initializes the {@link #hasTlsPorts} flag. + * <p> + * Community Edition might support TLS one happy day, so use a "supports TLS" flag separate from + * the "enterprise edition" flag. + */ + private void initializeHasTlsPorts() { + @Cleanup + Response response = doHttpRequest(MGMT_PORT, "/pools/default/nodeServices", "GET", null, true); + + try { + String clusterTopology = response.body().string(); + hasTlsPorts = + !MAPPER + .readTree(clusterTopology) + .path("nodesExt") + .path(0) + .path("services") + .path("mgmtSSL") + .isMissingNode(); + } catch (IOException e) { + throw new IllegalStateException("Couchbase /pools/default/nodeServices did not return valid JSON"); + } + } + /** * Rebinds/renames the internal hostname. * <p> @@ -503,33 +531,45 @@ private void configureExternalPorts() { final FormBody.Builder builder = new FormBody.Builder(); builder.add("hostname", getHost()); builder.add("mgmt", Integer.toString(getMappedPort(MGMT_PORT))); - builder.add("mgmtSSL", Integer.toString(getMappedPort(MGMT_SSL_PORT))); + if (hasTlsPorts) { + builder.add("mgmtSSL", Integer.toString(getMappedPort(MGMT_SSL_PORT))); + } if (enabledServices.contains(CouchbaseService.KV)) { builder.add("kv", Integer.toString(getMappedPort(KV_PORT))); - builder.add("kvSSL", Integer.toString(getMappedPort(KV_SSL_PORT))); builder.add("capi", Integer.toString(getMappedPort(VIEW_PORT))); - builder.add("capiSSL", Integer.toString(getMappedPort(VIEW_SSL_PORT))); + if (hasTlsPorts) { + builder.add("kvSSL", Integer.toString(getMappedPort(KV_SSL_PORT))); + builder.add("capiSSL", Integer.toString(getMappedPort(VIEW_SSL_PORT))); + } } if (enabledServices.contains(CouchbaseService.QUERY)) { builder.add("n1ql", Integer.toString(getMappedPort(QUERY_PORT))); - builder.add("n1qlSSL", Integer.toString(getMappedPort(QUERY_SSL_PORT))); + if (hasTlsPorts) { + builder.add("n1qlSSL", Integer.toString(getMappedPort(QUERY_SSL_PORT))); + } } if (enabledServices.contains(CouchbaseService.SEARCH)) { builder.add("fts", Integer.toString(getMappedPort(SEARCH_PORT))); - builder.add("ftsSSL", Integer.toString(getMappedPort(SEARCH_SSL_PORT))); + if (hasTlsPorts) { + 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))); + if (hasTlsPorts) { + builder.add("cbasSSL", Integer.toString(getMappedPort(ANALYTICS_SSL_PORT))); + } } if (enabledServices.contains(CouchbaseService.EVENTING)) { builder.add("eventingAdminPort", Integer.toString(getMappedPort(EVENTING_PORT))); - builder.add("eventingSSL", Integer.toString(getMappedPort(EVENTING_SSL_PORT))); + if (hasTlsPorts) { + builder.add("eventingSSL", Integer.toString(getMappedPort(EVENTING_SSL_PORT))); + } } @Cleanup 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 691dc1307ef..b78e3666f31 100644 --- a/modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java +++ b/modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java @@ -22,7 +22,6 @@ 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; import java.util.function.Consumer; @@ -33,51 +32,43 @@ public class CouchbaseContainerTest { - private static final DockerImageName COUCHBASE_IMAGE_ENTERPRISE = DockerImageName.parse( - "couchbase/server:enterprise-7.0.3" - ); + private static final String COUCHBASE_IMAGE_ENTERPRISE = "couchbase/server:enterprise-7.0.3"; - private static final DockerImageName COUCHBASE_IMAGE_COMMUNITY = DockerImageName.parse( - "couchbase/server:community-7.0.2" - ); + private static final String COUCHBASE_IMAGE_ENTERPRISE_RECENT = "couchbase/server:enterprise-7.6.2"; - @Test - public void testBasicContainerUsageForEnterpriseContainer() { - // bucket_definition { - BucketDefinition bucketDefinition = new BucketDefinition("mybucket"); - // } - - try ( - // container_definition { - CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_ENTERPRISE) - .withBucket(bucketDefinition) - // } - ) { - setUpClient( - container, - cluster -> { - Bucket bucket = cluster.bucket(bucketDefinition.getName()); - bucket.waitUntilReady(Duration.ofSeconds(10L)); + private static final String COUCHBASE_IMAGE_COMMUNITY = "couchbase/server:community-7.0.2"; - Collection collection = bucket.defaultCollection(); - - collection.upsert("foo", JsonObject.create().put("key", "value")); + private static final String COUCHBASE_IMAGE_COMMUNITY_RECENT = "couchbase/server:community-7.6.2"; - JsonObject fooObject = collection.get("foo").contentAsObject(); + @Test + public void testBasicContainerUsageForEnterpriseContainer() { + testBasicContainerUsage(COUCHBASE_IMAGE_ENTERPRISE); + } - assertThat(fooObject.getString("key")).isEqualTo("value"); - } - ); - } + @Test + public void testBasicContainerUsageForEnterpriseContainerRecent() { + testBasicContainerUsage(COUCHBASE_IMAGE_ENTERPRISE_RECENT); } @Test public void testBasicContainerUsageForCommunityContainer() { + testBasicContainerUsage(COUCHBASE_IMAGE_COMMUNITY); + } + + @Test + public void testBasicContainerUsageForCommunityContainerRecent() { + testBasicContainerUsage(COUCHBASE_IMAGE_COMMUNITY_RECENT); + } + + private void testBasicContainerUsage(String couchbaseImage) { + // bucket_definition { BucketDefinition bucketDefinition = new BucketDefinition("mybucket"); + // } try ( - CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_COMMUNITY) - .withBucket(bucketDefinition) + // container_definition { + CouchbaseContainer container = new CouchbaseContainer(couchbaseImage).withBucket(bucketDefinition) + // } ) { setUpClient( container,