Skip to content

Couchbase

Since v0.20.0

Introduction

The Testcontainers module for Couchbase.

Adding this module to your project dependencies

Please run the following command to add the Couchbase module to your Go dependencies:

go get github.com/testcontainers/testcontainers-go/modules/couchbase

Usage example

ctx := context.Background()

bucketName := "testBucket"
bucket := couchbase.NewBucket(bucketName)

bucket = bucket.WithQuota(100).
    WithReplicas(0).
    WithFlushEnabled(false).
    WithPrimaryIndex(true)

couchbaseContainer, err := couchbase.Run(ctx,
    "couchbase:community-7.1.1",
    couchbase.WithAdminCredentials("testcontainers", "testcontainers.IS.cool!"),
    couchbase.WithBuckets(bucket),
)
defer func() {
    if err := testcontainers.TerminateContainer(couchbaseContainer); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start container: %s", err)
    return
}

Module Reference

Run function

Info

The RunContainer(ctx, opts...) function is deprecated and will be removed in the next major release of Testcontainers for Go.

The Couchbase module exposes one entrypoint function to create the Couchbase container, and this function receives three parameters:

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*CouchbaseContainer, error)
  • context.Context, the Go context.
  • string, the Docker image to use.
  • testcontainers.ContainerCustomizer, a variadic argument for passing options.

Once the container is started, it will perform the following operations, in this particular order:

  • Wait until the node is online, waiting for the /pools endpoint in the management port to return a 200 HTTP status code.
  • Check for Enterprise services, sending a GET request to the /pools endpoint in the management port. If the response contains the isEnterprise key set to false, it will check if the Analytics or the Eventing services are enabled. If so, it will raise an error.
  • Rename the node, sending a POST request to the /node/controller/rename endpoint in the management port.
  • Initialize the services, sending a POST request to the /node/controller/setupServices endpoint in the management port, passing as body of the request the list of enabled services.
  • Set the memory quotas, sending a POST request to the /pools/default endpoint in the management port, passing as body of the request the memory quota for each enabled service.
  • Configure the Admin user, sending a POST request to the /settings/web endpoint in the management port, passing as body of the request the username and password of the admin user.
  • Configure the external ports, sending a POST request to the /node/controller/setupAlternateAddresses/external endpoint in the management port, passing as body of the request the external mapped ports for each enabled service.
  • If the Index service is enabled, configure the indexer, sending a POST request to the /settings/indexes endpoint in the management port, passing as body of the request the defined storage mode. If the Community Edition is used, it will make sure the storage mode is forestdb. If the Enterprise Edition is used, it will make sure the storage mode is not forestdb, changing to memory_optimized in that case.
  • Finally, it will wait for all nodes to be healthy. Depending on the enabled services, it will use a different wait strategy to check if the node is healthy:
    • It will wait for the /pools/default endpoint in the management port to return a 200 HTTP status code and the response body to contain the healthy key set to true.
    • If the Query service is enabled, it will wait for the /admin/ping endpoint in the query port to return a 200 HTTP status code.
    • If the Analytics service is enabled, it will wait for the /admin/ping endpoint in the analytics port to return a 200 HTTP status code.
    • If the Eventing service is enabled, it will wait for the /api/v1/config endpoint in the eventing port to return a 200 HTTP status code.

Container Ports

Here you can find the list with the default ports used by the Couchbase container. The Management ports (MGMT_PORT and MGMT_SSL_PORT) and the Service ports for kv, query and search are exposed by default.

Tip

You can export the service ports for Analytics and Eventing by using the WithServiceAnalytics and WithServiceEventing optional functions.

MGMT_PORT     = "8091"
MGMT_SSL_PORT = "18091"

VIEW_PORT     = "8092"
VIEW_SSL_PORT = "18092"

QUERY_PORT     = "8093"
QUERY_SSL_PORT = "18093"

SEARCH_PORT     = "8094"
SEARCH_SSL_PORT = "18094"

ANALYTICS_PORT     = "8095"
ANALYTICS_SSL_PORT = "18095"

EVENTING_PORT     = "8096"
EVENTING_SSL_PORT = "18096"

KV_PORT     = "11210"
KV_SSL_PORT = "11207"

Image

Use the second argument in the Run function to set a valid Docker image. In example: Run(context.Background(), "couchbase:6.5.1").

You can find the Docker images that are currently tested in this module, for the Enterprise and Community editions, in the following list:

enterpriseEdition = "couchbase:enterprise-7.6.1"
communityEdition  = "couchbase:community-7.1.1"

Container Options

When starting the Couchbase container, you can pass options in a variadic way to configure it.

Credentials

If you need to change the default credentials for the admin user, you can use WithAdminCredentials(user, password) with a valid username and password. When the password has less than 6 characters, the container won't be created and the New function will throw an error.

Info

In the case this optional function is not called, the default username is Administrator and the default password is password.

Buckets

When creating a new Couchbase container, you can create one or more buckets. The module exposes a WithBuckets optional function that accepts a slice of buckets to be created. To create a new bucket, the module also exposes a NewBucket function, where you can pass the bucket name.

It's possible to customize a newly created bucket, using the following options:

  • WithQuota: sets the bucket quota in megabytes. The minimum value is 100 MB.
  • WithReplicas: sets the number of replicas for this bucket. The minimum value is 0 and the maximum value is 3.
  • WithFlushEnabled: sets whether the bucket should be flushed when the container is stopped.
  • WithPrimaryIndex: sets whether the primary index should be created for this bucket.
bucketName := "testBucket"
bucket := tccouchbase.NewBucket(bucketName)

bucket = bucket.WithQuota(100).
    WithReplicas(0).
    WithFlushEnabled(false).
    WithPrimaryIndex(true)

ctr, err := tccouchbase.Run(ctx, communityEdition, tccouchbase.WithBuckets(bucket))
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

Index Storage

It's possible to set the storage mode to be used for all global secondary indexes in the cluster.

Warning

Please note: plasma and memory optimized are options in the Enterprise Edition of Couchbase Server. If you are using the Community Edition, the only value allowed is forestdb.

const (
    // MemoryOptimized sets the cluster-wide index storage mode to use memory optimized global
    // secondary indexes which can perform index maintenance and index scan faster at in-memory speeds.
    // This is the default value for the testcontainers couchbase implementation.
    MemoryOptimized indexStorageMode = "memory_optimized"

    // Plasma sets the cluster-wide index storage mode to use the Plasma storage engine,
    // which can utilize both memory and persistent storage for index maintenance and index scans.
    Plasma indexStorageMode = "plasma"

    // ForestDB sets the cluster-wide index storage mode to use the forestdb storage engine,
    // which only utilizes persistent storage for index maintenance and scans. It is the only option available
    // for the community edition.
    ForestDB indexStorageMode = "forestdb"
)

Services

By default, the container will start with the following services: kv, n1ql, fts and index.

Warning

When running the Enterprise Edition of Couchbase Server, the module provides two functions to enable or disable services: WithServiceAnalytics and WithServiceEventing. Else, it will throw an error and the container won't be created.

The following options are exposed by the testcontainers package.

Basic Options

Lifecycle Options

Files & Mounts Options

Build Options

Logging Options

Image Options

Networking Options

Advanced Options

Experimental Options

Container Methods

ConnectionString

The ConnectionString method returns the connection string to connect to the Couchbase container instance. It returns a string with the format couchbase://<host>:<port>.

Username

The Username method returns the username of the Couchbase administrator.

Password

The Password method returns the password of the Couchbase administrator.