-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for Couchbase #269
Changes from 1 commit
178d022
c06aea0
e388c8d
4d73f54
b3e544d
253c00d
45324f3
5f8dcfe
e2a9c05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>testcontainers-parent</artifactId> | ||
<version>1.1.8-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>couchbase</artifactId> | ||
<name>TestContainers :: Couchbase</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>testcontainers</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<!-- Couchbase driver --> | ||
<dependency> | ||
<groupId>com.couchbase.client</groupId> | ||
<artifactId>java-client</artifactId> | ||
<version>2.4.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>selenium</artifactId> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I doubt we need Selenium here :) |
||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.seleniumhq.selenium</groupId> | ||
<artifactId>selenium-api</artifactId> | ||
<version>2.45.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.seleniumhq.selenium</groupId> | ||
<artifactId>selenium-remote-driver</artifactId> | ||
<version>2.45.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,284 @@ | ||
/* | ||
* Copyright (c) 2016 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.containers; | ||
|
||
import com.couchbase.client.core.message.config.RestApiResponse; | ||
import com.couchbase.client.core.utils.Base64; | ||
import com.couchbase.client.deps.io.netty.handler.codec.http.HttpHeaders; | ||
import com.couchbase.client.java.CouchbaseCluster; | ||
import com.couchbase.client.java.bucket.BucketType; | ||
import com.couchbase.client.java.cluster.BucketSettings; | ||
import com.couchbase.client.java.cluster.DefaultBucketSettings; | ||
import com.couchbase.client.java.cluster.api.ClusterApiClient; | ||
import com.couchbase.client.java.cluster.api.RestBuilder; | ||
import com.couchbase.client.java.document.json.JsonArray; | ||
import com.couchbase.client.java.env.CouchbaseEnvironment; | ||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; | ||
import com.couchbase.client.java.query.Index; | ||
import com.couchbase.client.java.query.N1qlQuery; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.traits.LinkableContainer; | ||
import org.testcontainers.containers.wait.HttpWaitStrategy; | ||
|
||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Laurent Doguin | ||
*/ | ||
public class CouchbaseContainer<SELF extends CouchbaseContainer<SELF>> extends GenericContainer<SELF> { | ||
|
||
private String memoryQuota = "400"; | ||
|
||
private String indexMemoryQuota = "400"; | ||
|
||
private String clusterUsername = "Administrator"; | ||
|
||
private String clusterPassword = "password"; | ||
|
||
private Boolean keyValue = true; | ||
|
||
private Boolean query = true; | ||
|
||
private Boolean index = true; | ||
|
||
private Boolean fts = true; | ||
|
||
private Boolean beerSample = false; | ||
|
||
private Boolean travelSample = false; | ||
|
||
private Boolean gamesIMSample = false; | ||
|
||
private CouchbaseEnvironment couchbaseEnvironment; | ||
|
||
private CouchbaseCluster couchbaseCluster; | ||
|
||
private List<BucketSettings> newBuckets = new ArrayList<>(); | ||
|
||
private String urlBase; | ||
|
||
public CouchbaseContainer() { | ||
super("couchbase/server:latest"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please lock the version? We try to avoid ":latest" everywhere. Thanks! |
||
} | ||
|
||
public CouchbaseContainer(String containerName) { | ||
super(containerName); | ||
} | ||
|
||
@Override | ||
protected Integer getLivenessCheckPort() { | ||
return getMappedPort(8091); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
addFixedExposedPort(8091, 8091); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I right that these ports are only needed if you use some sub-set of Couchbase APIs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that tests on Travis are failing because of it as well :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, unfortunately, some of the ports cannot be changed. Bad Couchbase design for some services, but not all of them. Out of consistency concerns, I chose to fix everything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If consistency isn't an issue, we can change configureable port. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we can use some free port finder on the host machine and then configure Couchbase with it. Still flaky, but at least will work for the most of the cases. |
||
addFixedExposedPort(8092, 8092); | ||
addFixedExposedPort(8093, 8093); | ||
addFixedExposedPort(8094, 8094); | ||
addFixedExposedPort(8095, 8095); | ||
addFixedExposedPort(11207, 11207); | ||
addFixedExposedPort(11210, 11210); | ||
addFixedExposedPort(11211, 11211); | ||
addFixedExposedPort(18091, 18091); | ||
addFixedExposedPort(18092, 18092); | ||
addFixedExposedPort(18093, 18093); | ||
setWaitStrategy(new HttpWaitStrategy().forPath("/ui/index.html#/")); | ||
} | ||
|
||
public CouchbaseEnvironment getCouchbaseEnvironnement() { | ||
if (couchbaseEnvironment == null) { | ||
initCluster(); | ||
couchbaseEnvironment = DefaultCouchbaseEnvironment.builder() | ||
.bootstrapCarrierDirectPort(getMappedPort(11210)) | ||
.bootstrapCarrierSslPort(getMappedPort(11207)) | ||
.bootstrapHttpDirectPort(getMappedPort(8091)) | ||
.bootstrapHttpSslPort(getMappedPort(18091)) | ||
.build(); | ||
} | ||
return couchbaseEnvironment; | ||
} | ||
|
||
public CouchbaseCluster getCouchbaseCluster() { | ||
if (couchbaseCluster == null) { | ||
couchbaseCluster = CouchbaseCluster.create(getCouchbaseEnvironnement(), getContainerIpAddress()); | ||
} | ||
return couchbaseCluster; | ||
} | ||
|
||
public SELF withClusterUsername(String username) { | ||
this.clusterUsername = username; | ||
return self(); | ||
} | ||
|
||
public SELF withClusterPassword(String password) { | ||
this.clusterPassword = password; | ||
return self(); | ||
} | ||
|
||
public SELF withMemoryQuota(String memoryQuota) { | ||
this.memoryQuota = memoryQuota; | ||
return self(); | ||
} | ||
|
||
public SELF withIndexMemoryQuota(String indexMemoryQuota) { | ||
this.indexMemoryQuota = indexMemoryQuota; | ||
return self(); | ||
} | ||
|
||
public SELF withKeyValue(Boolean withKV) { | ||
this.keyValue = withKV; | ||
return self(); | ||
} | ||
|
||
public SELF withIndex(Boolean withIndex) { | ||
this.index = withIndex; | ||
return self(); | ||
} | ||
|
||
public SELF withQuery(Boolean withQuery) { | ||
this.query = withQuery; | ||
return self(); | ||
} | ||
|
||
public SELF withFTS(Boolean withFTS) { | ||
this.fts = withFTS; | ||
return self(); | ||
} | ||
|
||
public SELF withTravelSample(Boolean withTravelSample) { | ||
this.travelSample = withTravelSample; | ||
return self(); | ||
} | ||
|
||
public SELF withBeerSample(Boolean withBeerSample) { | ||
this.beerSample = withBeerSample; | ||
return self(); | ||
} | ||
|
||
public SELF withGamesIMSample(Boolean withGamesIMSample) { | ||
this.gamesIMSample = withGamesIMSample; | ||
return self(); | ||
} | ||
|
||
public SELF withNewBucket(BucketSettings bucketSettings) { | ||
newBuckets.add(bucketSettings); | ||
return self(); | ||
} | ||
|
||
|
||
public void initCluster() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually even private would be better. I intend people to use start anyway, Will fix this. |
||
urlBase = String.format("http://%s:%s", getContainerIpAddress(), getMappedPort(8091)); | ||
try { | ||
String poolURL = "/pools/default"; | ||
String poolPayload = "memoryQuota=" + URLEncoder.encode(memoryQuota, "UTF-8") + "&indexMemoryQuota=" + URLEncoder.encode(indexMemoryQuota, "UTF-8"); | ||
|
||
String setupServicesURL = "/node/controller/setupServices"; | ||
StringBuilder servicePayloadBuilder = new StringBuilder(); | ||
if (keyValue) { | ||
servicePayloadBuilder.append("kv,"); | ||
} | ||
if (query) { | ||
servicePayloadBuilder.append("n1ql,"); | ||
} | ||
if (index) { | ||
servicePayloadBuilder.append("index,"); | ||
} | ||
if (fts) { | ||
servicePayloadBuilder.append("fts,"); | ||
} | ||
String setupServiceContent = "services=" + URLEncoder.encode(servicePayloadBuilder.toString(), "UTF-8"); | ||
|
||
String webSettingsURL = "/settings/web"; | ||
String webSettingsContent = "username=" + URLEncoder.encode(clusterUsername, "UTF-8") + "&password=" + URLEncoder.encode(clusterPassword, "UTF-8") + "&port=8091"; | ||
|
||
String bucketURL = "/sampleBuckets/install"; | ||
|
||
StringBuilder sampleBucketPayloadBuilder = new StringBuilder(); | ||
sampleBucketPayloadBuilder.append('['); | ||
if (travelSample) { | ||
sampleBucketPayloadBuilder.append("\"travel-sample\","); | ||
} | ||
if (beerSample) { | ||
sampleBucketPayloadBuilder.append("\"beer-sample\","); | ||
} | ||
if (gamesIMSample) { | ||
sampleBucketPayloadBuilder.append("\"gamesim-sample\","); | ||
} | ||
sampleBucketPayloadBuilder.append(']'); | ||
|
||
callCouchbaseRestAPI(poolURL, poolPayload); | ||
callCouchbaseRestAPI(setupServicesURL, setupServiceContent); | ||
callCouchbaseRestAPI(webSettingsURL, webSettingsContent); | ||
callCouchbaseRestAPI(bucketURL, sampleBucketPayloadBuilder.toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TestContainers has a lifecycle, you can put this code to
|
||
|
||
CouchbaseWaitStrategy s = new CouchbaseWaitStrategy(); | ||
s.withBasicCredentials(clusterUsername, clusterPassword); | ||
s.waitUntilReady(this); | ||
callCouchbaseRestAPI("/settings/indexes", "indexerThreads=0&logLevel=info&maxRollbackPoints=5&storageMode=memory_optimized"); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void createBucket(BucketSettings bucketSetting, Boolean createIndex){ | ||
BucketSettings bucketSettings = getCouchbaseCluster().clusterManager(clusterUsername, clusterPassword).insertBucket(bucketSetting); | ||
// allow some time for the query service to come up | ||
try { | ||
Thread.sleep(5000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we somehow continuously check if it's ready? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could not find an answer yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. http://docs.couchbase.com/admin/admin/CLI/CBcli/cbcli-bucket-create.html Also, at least something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or actually, just |
||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
if (createIndex) { | ||
getCouchbaseCluster().openBucket().query(Index.createPrimaryIndex().on(bucketSetting.name())); | ||
} | ||
} | ||
|
||
public void callCouchbaseRestAPI(String url, String payload) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed |
||
String fullUrl = urlBase + url; | ||
HttpURLConnection httpConnection = (HttpURLConnection) ((new URL(fullUrl).openConnection())); | ||
httpConnection.setDoOutput(true); | ||
httpConnection.setRequestMethod("POST"); | ||
httpConnection.setRequestProperty("Content-Type", | ||
"application/x-www-form-urlencoded"); | ||
String encoded = Base64.encode((clusterUsername + ":" + clusterPassword).getBytes("UTF-8")); | ||
httpConnection.setRequestProperty("Authorization", "Basic " + encoded); | ||
DataOutputStream out = new DataOutputStream(httpConnection.getOutputStream()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
out.writeBytes(payload); | ||
out.flush(); | ||
out.close(); | ||
httpConnection.getResponseCode(); | ||
httpConnection.disconnect(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
super.start(); | ||
if (!newBuckets.isEmpty()) { | ||
for (BucketSettings bucketSetting : newBuckets) { | ||
createBucket(bucketSetting, index); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since TestContainers is a library for testing, we probably should use
provided
scope here, because users will have Couchbase client library already in their app. Right?