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¶
- Since v0.32.0
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
/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 on 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¶
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¶
- Since v0.20.0
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¶
- Since v0.20.0
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¶
- Since v0.20.0
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¶
- Since v0.20.0
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¶
WithExposedPortsSince v0.37.0WithEnvSince v0.29.0WithWaitStrategySince v0.20.0WithAdditionalWaitStrategySince v0.38.0WithWaitStrategyAndDeadlineSince v0.20.0WithAdditionalWaitStrategyAndDeadlineSince v0.38.0WithEntrypointSince v0.37.0WithEntrypointArgsSince v0.37.0WithCmdSince v0.37.0WithCmdArgsSince v0.37.0WithLabelsSince v0.37.0
Lifecycle Options¶
WithLifecycleHooksSince v0.38.0WithAdditionalLifecycleHooksSince v0.38.0WithStartupCommandSince v0.25.0WithAfterReadyCommandSince v0.28.0
Files & Mounts Options¶
WithFilesSince v0.37.0WithMountsSince v0.37.0WithTmpfsSince v0.37.0WithImageMountSince v0.37.0
Build Options¶
WithDockerfileSince v0.37.0
Logging Options¶
WithLogConsumersSince v0.28.0WithLogConsumerConfigSince v0.38.0WithLoggerSince v0.29.0
Image Options¶
WithAlwaysPullSince v0.38.0WithImageSubstitutorsSince v0.26.0WithImagePlatformSince v0.38.0
Networking Options¶
WithNetworkSince v0.27.0WithNetworkByNameSince v0.38.0WithBridgeNetworkSince v0.38.0WithNewNetworkSince v0.27.0
Advanced Options¶
WithHostPortAccessSince v0.31.0WithConfigModifierSince v0.20.0WithHostConfigModifierSince v0.20.0WithEndpointSettingsModifierSince v0.20.0CustomizeRequestSince v0.20.0WithNameSince v0.38.0WithNoStartSince v0.38.0WithProviderNot available until the next release main
Experimental Options¶
WithReuseByNameSince v0.37.0
Container Methods¶
ConnectionString¶
- Since v0.20.0
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¶
- Since v0.20.0
The Username method returns the username of the Couchbase administrator.
Password¶
- Since v0.20.0
The Password method returns the password of the Couchbase administrator.