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
/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 theisEnterprise
key 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/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 isforestdb
. If the Enterprise Edition is used, it will make sure the storage mode is notforestdb
, changing tomemory_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 thehealthy
key set totrue
. - 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.
- 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¶
WithExposedPorts
Since v0.37.0WithEnv
Since v0.29.0WithWaitStrategy
Since v0.20.0WithAdditionalWaitStrategy
Not available until the next release mainWithWaitStrategyAndDeadline
Since v0.20.0WithAdditionalWaitStrategyAndDeadline
Not available until the next release mainWithEntrypoint
Since v0.37.0WithEntrypointArgs
Since v0.37.0WithCmd
Since v0.37.0WithCmdArgs
Since v0.37.0WithLabels
Since v0.37.0
Lifecycle Options¶
WithLifecycleHooks
Not available until the next release mainWithAdditionalLifecycleHooks
Not available until the next release mainWithStartupCommand
Since v0.25.0WithAfterReadyCommand
Since v0.28.0
Files & Mounts Options¶
WithFiles
Since v0.37.0WithMounts
Since v0.37.0WithTmpfs
Since v0.37.0WithImageMount
Since v0.37.0
Build Options¶
WithDockerfile
Since v0.37.0
Logging Options¶
WithLogConsumers
Since v0.28.0WithLogConsumerConfig
Not available until the next release mainWithLogger
Since v0.29.0
Image Options¶
WithAlwaysPull
Not available until the next release mainWithImageSubstitutors
Since v0.26.0WithImagePlatform
Not available until the next release main
Networking Options¶
WithNetwork
Since v0.27.0WithNetworkByName
Not available until the next release mainWithBridgeNetwork
Not available until the next release mainWithNewNetwork
Since v0.27.0
Advanced Options¶
WithHostPortAccess
Since v0.31.0WithConfigModifier
Since v0.20.0WithHostConfigModifier
Since v0.20.0WithEndpointSettingsModifier
Since v0.20.0CustomizeRequest
Since v0.20.0WithName
Not available until the next release mainWithNoStart
Not available until the next release main
Experimental Options¶
WithReuseByName
Since 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.