Skip to content

Couchbase

Since testcontainers-go 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.RunContainer(ctx,
    testcontainers.WithImage("couchbase:community-7.1.1"),
    couchbase.WithAdminCredentials("testcontainers", "testcontainers.IS.cool!"),
    couchbase.WithBuckets(bucket),
)
if err != nil {
    log.Fatalf("failed to start container: %s", err)
}
defer func() {
    if err := couchbaseContainer.Terminate(ctx); err != nil {
        log.Fatalf("failed to terminate container: %s", err)
    }
}()

Module Reference

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

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*CouchbaseContainer, error)
  • context.Context, the Go context.
  • 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 of 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"

Container Options

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

Image

If you need to set a different Couchbase Docker image, you can use testcontainers.WithImage with a valid Docker image for Couchbase. E.g. testcontainers.WithImage("docker.io/couchbase:6.5.1").

By default, the container will use the following Docker image:

const defaultImage = "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.1.3"
communityEdition  = "couchbase:community-7.1.1"

Image Substitutions

In more locked down / secured environments, it can be problematic to pull images from Docker Hub and run them without additional precautions.

An image name substitutor converts a Docker image name, as may be specified in code, to an alternative name. This is intended to provide a way to override image names, for example to enforce pulling of images from a private registry.

Testcontainers for Go exposes an interface to perform this operations: ImageSubstitutor, and a No-operation implementation to be used as reference for custom implementations:

// ImageSubstitutor represents a way to substitute container image names
type ImageSubstitutor interface {
    // Description returns the name of the type and a short description of how it modifies the image.
    // Useful to be printed in logs
    Description() string
    Substitute(image string) (string, error)
}
type NoopImageSubstitutor struct{}

// Description returns a description of what is expected from this Substitutor,
// which is used in logs.
func (s NoopImageSubstitutor) Description() string {
    return "NoopImageSubstitutor (noop)"
}

// Substitute returns the original image, without any change
func (s NoopImageSubstitutor) Substitute(image string) (string, error) {
    return image, nil
}

Using the WithImageSubstitutors options, you could define your own substitutions to the container images. E.g. adding a prefix to the images so that they can be pulled from a Docker registry other than Docker Hub. This is the usual mechanism for using Docker image proxies, caches, etc.

WithEnv

  • Not available until the next release of testcontainers-go main

If you need to either pass additional environment variables to a container or override them, you can use testcontainers.WithEnv for example:

postgres, err = postgresModule.RunContainer(ctx, testcontainers.WithEnv(map[string]string{"POSTGRES_INITDB_ARGS", "--no-sync"}))

WithLogConsumers

If you need to consume the logs of the container, you can use testcontainers.WithLogConsumers with a valid log consumer. An example of a log consumer is the following:

type TestLogConsumer struct {
    Msgs []string
}

func (g *TestLogConsumer) Accept(l Log) {
    g.Msgs = append(g.Msgs, string(l.Content))
}

Please read the Following Container Logs documentation for more information about creating log consumers.

Wait Strategies

If you need to set a different wait strategy for the container, you can use testcontainers.WithWaitStrategy with a valid wait strategy.

Info

The default deadline for the wait strategy is 60 seconds.

At the same time, it's possible to set a wait strategy and a custom deadline with testcontainers.WithWaitStrategyAndDeadline.

Startup Commands

Testcontainers exposes the WithStartupCommand(e ...Executable) option to run arbitrary commands in the container right after it's started.

Info

To better understand how this feature works, please read the Create containers: Lifecycle Hooks documentation.

It also exports an Executable interface, defining the following methods:

  • AsCommand(), which returns a slice of strings to represent the command and positional arguments to be executed in the container;
  • Options(), which returns the slice of functional options with the Docker's ExecConfigs used to create the command in the container (the working directory, environment variables, user executing the command, etc) and the possible output format (Multiplexed).

You could use this feature to run a custom script, or to run a command that is not supported by the module right after the container is started.

Ready Commands

Testcontainers exposes the WithAfterReadyCommand(e ...Executable) option to run arbitrary commands in the container right after it's ready, which happens when the defined wait strategies have finished with success.

Info

To better understand how this feature works, please read the Create containers: Lifecycle Hooks documentation.

It leverages the Executable interface to represent the command and positional arguments to be executed in the container.

You could use this feature to run a custom script, or to run a command that is not supported by the module right after the container is ready.

WithNetwork

By default, the container is started in the default Docker network. If you want to use an already existing Docker network you created in your code, you can use the network.WithNetwork(aliases []string, nw *testcontainers.DockerNetwork) option, which receives an alias as parameter and your network, attaching the container to it, and setting the network alias for that network.

In the case you need to retrieve the network name, you can simply read it from the struct's Name field. E.g. nw.Name.

Warning

This option is not checking whether the network exists or not. If you use a network that doesn't exist, the container will start in the default Docker network, as in the default behavior.

WithNewNetwork

If you want to attach your containers to a throw-away network, you can use the network.WithNewNetwork(ctx context.Context, aliases []string, opts ...network.NetworkCustomizer) option, which receives an alias as parameter, creating the new network with a random name, attaching the container to it, and setting the network alias for that network.

In the case you need to retrieve the network name, you can use the Networks(ctx) method of the Container interface, right after it's running, which returns a slice of strings with the names of the networks where the container is attached.

Docker type modifiers

If you need an advanced configuration for the container, you can leverage the following Docker type modifiers:

  • testcontainers.WithConfigModifier
  • testcontainers.WithHostConfigModifier
  • testcontainers.WithEndpointSettingsModifier

Please read the Create containers: Advanced Settings documentation for more information.

Customising the ContainerRequest

This option will merge the customized request into the module's own ContainerRequest.

container, err := RunContainer(ctx,
    /* Other module options */
    testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
        ContainerRequest: testcontainers.ContainerRequest{
            Cmd: []string{"-c", "log_statement=all"},
        },
    }),
)

The above example is updating the predefined command of the image, appending them to the module's command.

Info

This can't be used to replace the command, only to append options.

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 RunContainer 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 an 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)

container, err := tccouchbase.RunContainer(ctx, testcontainers.WithImage(communityEdition), tccouchbase.WithBuckets(bucket))
if err != nil {
    t.Fatal(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.

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.