Skip to content

Commit ee5e737

Browse files
dnaulteddumelendez
authored andcommitted
Couchbase: Don't configure external TLS ports if they're not supported (testcontainers#8990)
Fixes testcontainers#8989 --------- Co-authored-by: Eddú Meléndez <[email protected]>
1 parent 9d6380c commit ee5e737

File tree

2 files changed

+72
-41
lines changed

2 files changed

+72
-41
lines changed

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

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public class CouchbaseContainer extends GenericContainer<CouchbaseContainer> {
123123

124124
private boolean isEnterprise = false;
125125

126+
private boolean hasTlsPorts = false;
127+
126128
/**
127129
* Creates a new couchbase container with the default image and version.
128130
* @deprecated use {@link #CouchbaseContainer(DockerImageName)} instead
@@ -345,6 +347,7 @@ protected void containerIsStarting(final InspectContainerResponse containerInfo)
345347

346348
timePhase("waitUntilNodeIsOnline", this::waitUntilNodeIsOnline);
347349
timePhase("initializeIsEnterprise", this::initializeIsEnterprise);
350+
timePhase("initializeHasTlsPorts", this::initializeHasTlsPorts);
348351
timePhase("renameNode", this::renameNode);
349352
timePhase("initializeServices", this::initializeServices);
350353
timePhase("setMemoryQuotas", this::setMemoryQuotas);
@@ -394,6 +397,31 @@ private void initializeIsEnterprise() {
394397
}
395398
}
396399

400+
/**
401+
* Initializes the {@link #hasTlsPorts} flag.
402+
* <p>
403+
* Community Edition might support TLS one happy day, so use a "supports TLS" flag separate from
404+
* the "enterprise edition" flag.
405+
*/
406+
private void initializeHasTlsPorts() {
407+
@Cleanup
408+
Response response = doHttpRequest(MGMT_PORT, "/pools/default/nodeServices", "GET", null, true);
409+
410+
try {
411+
String clusterTopology = response.body().string();
412+
hasTlsPorts =
413+
!MAPPER
414+
.readTree(clusterTopology)
415+
.path("nodesExt")
416+
.path(0)
417+
.path("services")
418+
.path("mgmtSSL")
419+
.isMissingNode();
420+
} catch (IOException e) {
421+
throw new IllegalStateException("Couchbase /pools/default/nodeServices did not return valid JSON");
422+
}
423+
}
424+
397425
/**
398426
* Rebinds/renames the internal hostname.
399427
* <p>
@@ -503,33 +531,45 @@ private void configureExternalPorts() {
503531
final FormBody.Builder builder = new FormBody.Builder();
504532
builder.add("hostname", getHost());
505533
builder.add("mgmt", Integer.toString(getMappedPort(MGMT_PORT)));
506-
builder.add("mgmtSSL", Integer.toString(getMappedPort(MGMT_SSL_PORT)));
534+
if (hasTlsPorts) {
535+
builder.add("mgmtSSL", Integer.toString(getMappedPort(MGMT_SSL_PORT)));
536+
}
507537

508538
if (enabledServices.contains(CouchbaseService.KV)) {
509539
builder.add("kv", Integer.toString(getMappedPort(KV_PORT)));
510-
builder.add("kvSSL", Integer.toString(getMappedPort(KV_SSL_PORT)));
511540
builder.add("capi", Integer.toString(getMappedPort(VIEW_PORT)));
512-
builder.add("capiSSL", Integer.toString(getMappedPort(VIEW_SSL_PORT)));
541+
if (hasTlsPorts) {
542+
builder.add("kvSSL", Integer.toString(getMappedPort(KV_SSL_PORT)));
543+
builder.add("capiSSL", Integer.toString(getMappedPort(VIEW_SSL_PORT)));
544+
}
513545
}
514546

515547
if (enabledServices.contains(CouchbaseService.QUERY)) {
516548
builder.add("n1ql", Integer.toString(getMappedPort(QUERY_PORT)));
517-
builder.add("n1qlSSL", Integer.toString(getMappedPort(QUERY_SSL_PORT)));
549+
if (hasTlsPorts) {
550+
builder.add("n1qlSSL", Integer.toString(getMappedPort(QUERY_SSL_PORT)));
551+
}
518552
}
519553

520554
if (enabledServices.contains(CouchbaseService.SEARCH)) {
521555
builder.add("fts", Integer.toString(getMappedPort(SEARCH_PORT)));
522-
builder.add("ftsSSL", Integer.toString(getMappedPort(SEARCH_SSL_PORT)));
556+
if (hasTlsPorts) {
557+
builder.add("ftsSSL", Integer.toString(getMappedPort(SEARCH_SSL_PORT)));
558+
}
523559
}
524560

525561
if (enabledServices.contains(CouchbaseService.ANALYTICS)) {
526562
builder.add("cbas", Integer.toString(getMappedPort(ANALYTICS_PORT)));
527-
builder.add("cbasSSL", Integer.toString(getMappedPort(ANALYTICS_SSL_PORT)));
563+
if (hasTlsPorts) {
564+
builder.add("cbasSSL", Integer.toString(getMappedPort(ANALYTICS_SSL_PORT)));
565+
}
528566
}
529567

530568
if (enabledServices.contains(CouchbaseService.EVENTING)) {
531569
builder.add("eventingAdminPort", Integer.toString(getMappedPort(EVENTING_PORT)));
532-
builder.add("eventingSSL", Integer.toString(getMappedPort(EVENTING_SSL_PORT)));
570+
if (hasTlsPorts) {
571+
builder.add("eventingSSL", Integer.toString(getMappedPort(EVENTING_SSL_PORT)));
572+
}
533573
}
534574

535575
@Cleanup

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

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.couchbase.client.java.json.JsonObject;
2323
import org.junit.Test;
2424
import org.testcontainers.containers.ContainerLaunchException;
25-
import org.testcontainers.utility.DockerImageName;
2625

2726
import java.time.Duration;
2827
import java.util.function.Consumer;
@@ -33,51 +32,43 @@
3332

3433
public class CouchbaseContainerTest {
3534

36-
private static final DockerImageName COUCHBASE_IMAGE_ENTERPRISE = DockerImageName.parse(
37-
"couchbase/server:enterprise-7.0.3"
38-
);
35+
private static final String COUCHBASE_IMAGE_ENTERPRISE = "couchbase/server:enterprise-7.0.3";
3936

40-
private static final DockerImageName COUCHBASE_IMAGE_COMMUNITY = DockerImageName.parse(
41-
"couchbase/server:community-7.0.2"
42-
);
37+
private static final String COUCHBASE_IMAGE_ENTERPRISE_RECENT = "couchbase/server:enterprise-7.6.2";
4338

44-
@Test
45-
public void testBasicContainerUsageForEnterpriseContainer() {
46-
// bucket_definition {
47-
BucketDefinition bucketDefinition = new BucketDefinition("mybucket");
48-
// }
49-
50-
try (
51-
// container_definition {
52-
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_ENTERPRISE)
53-
.withBucket(bucketDefinition)
54-
// }
55-
) {
56-
setUpClient(
57-
container,
58-
cluster -> {
59-
Bucket bucket = cluster.bucket(bucketDefinition.getName());
60-
bucket.waitUntilReady(Duration.ofSeconds(10L));
39+
private static final String COUCHBASE_IMAGE_COMMUNITY = "couchbase/server:community-7.0.2";
6140

62-
Collection collection = bucket.defaultCollection();
63-
64-
collection.upsert("foo", JsonObject.create().put("key", "value"));
41+
private static final String COUCHBASE_IMAGE_COMMUNITY_RECENT = "couchbase/server:community-7.6.2";
6542

66-
JsonObject fooObject = collection.get("foo").contentAsObject();
43+
@Test
44+
public void testBasicContainerUsageForEnterpriseContainer() {
45+
testBasicContainerUsage(COUCHBASE_IMAGE_ENTERPRISE);
46+
}
6747

68-
assertThat(fooObject.getString("key")).isEqualTo("value");
69-
}
70-
);
71-
}
48+
@Test
49+
public void testBasicContainerUsageForEnterpriseContainerRecent() {
50+
testBasicContainerUsage(COUCHBASE_IMAGE_ENTERPRISE_RECENT);
7251
}
7352

7453
@Test
7554
public void testBasicContainerUsageForCommunityContainer() {
55+
testBasicContainerUsage(COUCHBASE_IMAGE_COMMUNITY);
56+
}
57+
58+
@Test
59+
public void testBasicContainerUsageForCommunityContainerRecent() {
60+
testBasicContainerUsage(COUCHBASE_IMAGE_COMMUNITY_RECENT);
61+
}
62+
63+
private void testBasicContainerUsage(String couchbaseImage) {
64+
// bucket_definition {
7665
BucketDefinition bucketDefinition = new BucketDefinition("mybucket");
66+
// }
7767

7868
try (
79-
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_COMMUNITY)
80-
.withBucket(bucketDefinition)
69+
// container_definition {
70+
CouchbaseContainer container = new CouchbaseContainer(couchbaseImage).withBucket(bucketDefinition)
71+
// }
8172
) {
8273
setUpClient(
8374
container,

0 commit comments

Comments
 (0)