Skip to content

Commit

Permalink
Rewrite Couchbase module. closes testcontainers#2447 (testcontainers#…
Browse files Browse the repository at this point in the history
…2491)

* Rewrite couchbase module. closes testcontainers#2447

This changeset completely reworks the couchbase module and hopefully
greatly improve the out-of-the-box experience. Note that this is a
breaking change over the previous code because by intention it does
NOT depend on SDK 2 so you can test SDK 2 and 3 with it at the same
time.

Highlights:

 - Removed the need for a SDK, so both 2 and 3 can be used with it.
 - Updated to 6.5.0 baseline and using alternate addresses for
   "proper" port exposure without having to rely on the socat
   proxy container like the previous version had to.
 - Allows to define which services should be exposed and handles
   states automatically (i.e. will not try to create the primary
   index if the query service is not enabled).

Note that a bunch of tests have been removed since they are not
adequate anymore. A side effect of the alternate address change
is that older servers cannot be used. 6.5.0 is available in both
CE and EE, and Couchbase in general allows EE versions to be used
in development and testing so we should use it if we can.

* Wait until query respsonds with 200

* fixes

* add `getBootstrap*DirectPort()` methods, review fixes

* Restore `getConnectionString()`

* Apply suggestions from code review

Co-Authored-By: Kevin Wittek <kiview@users.noreply.github.com>

* Update docs

* Update docs (2)

Co-authored-by: Michael Nitschinger <michael@nitschinger.at>
Co-authored-by: Kevin Wittek <kiview@users.noreply.github.com>
  • Loading branch information
3 people authored and quincy committed May 28, 2020
1 parent 4c6e664 commit 2763c1d
Show file tree
Hide file tree
Showing 13 changed files with 530 additions and 708 deletions.
78 changes: 19 additions & 59 deletions docs/modules/databases/couchbase.md
Expand Up @@ -8,65 +8,25 @@ Testcontainers module for Couchbase. [Couchbase](https://www.couchbase.com/) is

Running Couchbase as a stand-in in a test:

### Create your own bucket

```java
public class SomeTest {

@Rule
public CouchbaseContainer couchbase = new CouchbaseContainer()
.withClusterAdmin("admin", "secret")
.withNewBucket(DefaultBucketSettings.builder()
.enableFlush(true)
.name("bucket-name")
.password("secret")
.quota(100)
.type(BucketType.COUCHBASE)
.build());

@Test
public void someTestMethod() {
Bucket bucket = couchbase.getCouchbaseCluster().openBucket("bucket-name");

// ... interact with client as if using Couchbase normally
}
}
```

### Use preconfigured default bucket

Bucket is cleared after each test

```java
public class SomeTest extends AbstractCouchbaseTest {

@Test
public void someTestMethod() {
Bucket bucket = getBucket();

// ... interact with client as if using Couchbase normally
}
}
```

### Special consideration

Couchbase container is configured to use random available [ports](https://developer.couchbase.com/documentation/server/current/install/install-ports.html) for some ports only, as [Couchbase Java SDK](https://developer.couchbase.com/documentation/server/current/sdk/java/start-using-sdk.html) permit to configure only some ports:

- **8091** : REST/HTTP traffic ([bootstrapHttpDirectPort](http://docs.couchbase.com/sdk-api/couchbase-java-client-2.4.6/com/couchbase/client/java/env/DefaultCouchbaseEnvironment.Builder.html#bootstrapCarrierDirectPort-int-))
- **18091** : REST/HTTP traffic with SSL ([bootstrapHttpSslPort](http://docs.couchbase.com/sdk-api/couchbase-java-client-2.4.6/com/couchbase/client/java/env/DefaultCouchbaseEnvironment.Builder.html#bootstrapCarrierSslPort-int-))
- **11210** : memcached ([bootstrapCarrierDirectPort](http://docs.couchbase.com/sdk-api/couchbase-java-client-2.4.6/com/couchbase/client/java/env/DefaultCouchbaseEnvironment.Builder.html#bootstrapCarrierDirectPort-int-))
- **11207** : memcached SSL ([bootstrapCarrierSslPort](http://docs.couchbase.com/sdk-api/couchbase-java-client-2.4.6/com/couchbase/client/java/env/DefaultCouchbaseEnvironment.Builder.html#bootstrapCarrierSslPort-int-))

All other ports cannot be changed by Java SDK, there are sadly fixed:

- **8092** : Queries, views, XDCR
- **8093** : REST/HTTP Query service
- **8094** : REST/HTTP Search Service
- **8095** : REST/HTTP Analytic service

So if you disable Query, Search and Analytic service, you can run multiple instance of this container, otherwise, you're stuck with one instance, for now.

1. Define a bucket:
<!--codeinclude-->
[Bucket Definition](../../../modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java) inside_block:bucket_definition
<!--/codeinclude-->

2. define a container:
<!--codeinclude-->
[Container definition](../../../modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java) inside_block:container_definition
<!--/codeinclude-->

3. create an environment & cluster:
<!--codeinclude-->
[Cluster creation](../../../modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java) inside_block:cluster_creation
<!--/codeinclude-->

4. authenticate:
<!--codeinclude-->
[Authentication](../../../modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java) inside_block:auth
<!--/codeinclude-->

## Adding this module to your project dependencies

Expand Down
3 changes: 1 addition & 2 deletions modules/couchbase/build.gradle
Expand Up @@ -2,7 +2,6 @@ description = "Testcontainers :: Couchbase"

dependencies {
compile project(':testcontainers')
compile 'com.couchbase.client:java-client:2.7.13'

testCompile project(':test-support')
testCompile 'com.couchbase.client:java-client:2.7.13'
}

This file was deleted.

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.testcontainers.couchbase;

/**
* Allows to configure the properties of a bucket that should be created.
*/
public class BucketDefinition {

private final String name;
private boolean queryPrimaryIndex = true;
private int quota = 100;

public BucketDefinition(final String name) {
this.name = name;
}

/**
* Sets a custom bucket quota (100MB by default).
*
* @param quota the quota to set for the bucket.
* @return this {@link BucketDefinition} for chaining purposes.
*/
public BucketDefinition withQuota(final int quota) {
if (quota < 100) {
throw new IllegalArgumentException("Bucket quota cannot be less than 100MB!");
}
this.quota = quota;
return this;
}

/**
* Allows to disable creating a primary index for this bucket (enabled by default).
*
* @param create if false, a primary index will not be created.
* @return this {@link BucketDefinition} for chaining purposes.
*/
public BucketDefinition withPrimaryIndex(final boolean create) {
this.queryPrimaryIndex = create;
return this;
}

public String getName() {
return name;
}

public boolean hasPrimaryIndex() {
return queryPrimaryIndex;
}

public int getQuota() {
return quota;
}

}

0 comments on commit 2763c1d

Please sign in to comment.