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¶
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)
}
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
/poolsendpoint in the management port to return a 200 HTTP status code. - Check for Enterprise services, sending a GET request to the
/poolsendpoint in the management port. If the response contains theisEnterprisekey set tofalse, 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/renameendpoint in the management port. - Initialize the services, sending a POST request to the
/node/controller/setupServicesendpoint 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/defaultendpoint 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/webendpoint 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/externalendpoint in the management port, passing as body of the request the external mapped ports for each enabled service. - If the
Indexservice is enabled, configure the indexer, sending a POST request to the/settings/indexesendpoint 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 isforestdb. If the Enterprise Edition is used, it will make sure the storage mode is notforestdb, changing tomemory_optimizedin 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/defaultendpoint in the management port to return a 200 HTTP status code and the response body to contain thehealthykey set totrue. - If the
Queryservice is enabled, it will wait for the/admin/pingendpoint in the query port to return a 200 HTTP status code. - If the
Analyticsservice is enabled, it will wait for the/admin/pingendpoint in the analytics port to return a 200 HTTP status code. - If the
Eventingservice is enabled, it will wait for the/api/v1/configendpoint in the eventing port to return a 200 HTTP status code.
- It will wait for the
Container Ports¶
const (
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:
Image: "couchbase:6.5.1",
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
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 provides with a WithBuckets function that accepts an array of buckets to be created.
To create a new bucket, the module 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.
const (
enterpriseEdition = "couchbase:enterprise-7.1.3"
communityEdition = "couchbase:community-7.1.1"
)
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>.
func connectCluster(ctx context.Context, container *tccouchbase.CouchbaseContainer) (*gocb.Cluster, error) {
connectionString, err := container.ConnectionString(ctx)
if err != nil {
return nil, err
}
// getCredentials {
return gocb.Connect(connectionString, gocb.ClusterOptions{
Username: container.Username(),
Password: container.Password(),
})
// }
}
Username¶
The Username method returns the username of the Couchbase administrator.
return gocb.Connect(connectionString, gocb.ClusterOptions{
Username: container.Username(),
Password: container.Password(),
})
Password¶
The Password method returns the password of the Couchbase administrator.
return gocb.Connect(connectionString, gocb.ClusterOptions{
Username: container.Username(),
Password: container.Password(),
})