Skip to content

Commit

Permalink
Couchbase module (#688)
Browse files Browse the repository at this point in the history
* First implementation

* Activate primary index by default

* Add compatibility with Couchbase v5

* Fix backward compatibility with older version

* Use more lombok annotations to reduce boilerplate code

* Move src to modules/couchbase

* Add build with gradle

* Replace shaded libraries with real ones

* Fix waiting for Couchbase start

* Add AUTHORS and README

* Fix Couchbase version

* Add Cleanup lombok annotation to auto close resources

* Update README

* Use new AbstractWaitStrategy implementation

* Respect Codacy quality standards

* Some optimizations

* Update CHANGELOG

* Replace CouchbaseWaitStrategy by HttpWaitStrategy and ResponsePredicate
  • Loading branch information
tchlyah authored and bsideup committed Jun 8, 2018
1 parent 95d4e04 commit 3b974e9
Show file tree
Hide file tree
Showing 9 changed files with 527 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- New module: Apache Pulsar ([\#713](https://github.com/testcontainers/testcontainers-java/pull/713))
- Add support for defining container labels ([\#725](https://github.com/testcontainers/testcontainers-java/pull/725))
- Use `quay.io/testcontainers/ryuk` instead of `bsideup/ryuk` ([\#721](https://github.com/testcontainers/testcontainers-java/pull/721))
- Added Couchbase module ([\#688](https://github.com/testcontainers/testcontainers-java/pull/688))

## [1.7.3] - 2018-05-16

Expand Down
1 change: 1 addition & 0 deletions modules/couchbase/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tayeb Chlyah <tayebchlyah@gmail.com>
59 changes: 59 additions & 0 deletions modules/couchbase/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<img src="https://cdn.worldvectorlogo.com/logos/couchbase.svg" width="300" />

# TestContainers Couchbase Module
Testcontainers module for Couchbase. [Couchbase](https://www.couchbase.com/) is a Document oriented NoSQL database.

## Usage example

Running Couchbase as a stand-in in a test:

### Create you own bucket

```java
public class SomeTest {

@Rule
public CouchbaseContainer couchbase = new CouchbaseContainer()
.withNewBucket(DefaultBucketSettings.builder()
.enableFlush(true)
.name('bucket-name')
.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.
6 changes: 6 additions & 0 deletions modules/couchbase/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description = "Testcontainers :: Couchbase"

dependencies {
compile project(':testcontainers')
compile 'com.couchbase.client:java-client:2.5.7'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.testcontainers.couchbase;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.bucket.BucketType;
import com.couchbase.client.java.cluster.DefaultBucketSettings;
import com.couchbase.client.java.query.N1qlParams;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.consistency.ScanConsistency;
import lombok.Getter;
import org.junit.After;

/**
* @author ctayeb
*/
public abstract class AbstractCouchbaseTest {

public static final String TEST_BUCKET = "test";

public static final String DEFAULT_PASSWORD = "password";

@Getter(lazy = true)
private final static CouchbaseContainer couchbaseContainer = initCouchbaseContainer();

@Getter(lazy = true)
private final static Bucket bucket = openBucket(TEST_BUCKET, DEFAULT_PASSWORD);

@After
public void clear() {
if (getCouchbaseContainer().isIndex() && getCouchbaseContainer().isQuery() && getCouchbaseContainer().isPrimaryIndex()) {
getBucket().query(
N1qlQuery.simple(String.format("DELETE FROM `%s`", getBucket().name()),
N1qlParams.build().consistency(ScanConsistency.STATEMENT_PLUS)));
} else {
getBucket().bucketManager().flush();
}
}

private static CouchbaseContainer initCouchbaseContainer() {
CouchbaseContainer couchbaseContainer = new CouchbaseContainer()
.withNewBucket(DefaultBucketSettings.builder()
.enableFlush(true)
.name(TEST_BUCKET)
.password(DEFAULT_PASSWORD)
.quota(100)
.replicas(0)
.type(BucketType.COUCHBASE)
.build());
couchbaseContainer.start();
return couchbaseContainer;
}

private static Bucket openBucket(String bucketName, String password) {
CouchbaseCluster cluster = getCouchbaseContainer().getCouchbaseCluster();
Bucket bucket = cluster.openBucket(bucketName, password);
Runtime.getRuntime().addShutdownHook(new Thread(bucket::close));
return bucket;
}
}

0 comments on commit 3b974e9

Please sign in to comment.