etcd¶
Since v0.34.0
Introduction¶
The Testcontainers module for etcd.
Adding this module to your project dependencies¶
Please run the following command to add the etcd module to your Go dependencies:
go get github.com/testcontainers/testcontainers-go/modules/etcd
Usage example¶
ctx := context.Background()
etcdContainer, err := etcd.Run(ctx, "gcr.io/etcd-development/etcd:v3.5.14")
defer func() {
if err := testcontainers.TerminateContainer(etcdContainer); 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.34.0
The etcd module exposes one entrypoint function to create the etcd container, and this function receives three parameters:
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*EtcdContainer, error)
context.Context, the Go context.string, the Docker image to use.testcontainers.ContainerCustomizer, a variadic argument for passing options.
Image¶
Use the second argument in the Run function to set a valid Docker image.
In example: Run(context.Background(), "bitnami/etcd:latest").
Container Options¶
When starting the etcd container, you can pass options in a variadic way to configure it.
WithAdditionalArgs¶
- Since v0.34.0
You can pass additional arguments to the etcd container by using the WithAdditionalArgs option. The arguments are passed to the CMD of the etcd container.
WithDataDir¶
- Since v0.34.0
You can set the data directory for the etcd container by using the WithDataDir boolean option. The data directory where the etcd data is stored is /data.etcd.
WithNodes¶
- Since v0.34.0
You can set the number of nodes for the etcd cluster by using the WithNodes option, passing the node names for each of the nodes. Single-node clusters are not allowed,
for that reason the functional option receives three string arguments: the first node, the second node, and a variadic argument for the rest of the nodes.
The module starts a container for each node, having the first node a reference to the other nodes. E.g. WithNodes("etcd-1", "etcd-2"), WithNodes("etcd-1", "etcd-2", "etcd-3") and so on.
The module creates a Docker network for the etcd cluster, and the nodes are connected to this network, so that they can communicate with each other through the network.
WithClusterToken¶
- Since v0.34.0
Sets the cluster token for the etcd cluster. The cluster token is used to identify the etcd cluster. The default value is mys3cr3ttok3n.
The etcd container holds a reference to the cluster token, so you can use it with e.g. ctr.ClusterToken.
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¶
The etcd container exposes the following methods:
ClientEndpoint¶
- Since v0.34.0
Returns the client endpoint for the etcd container and an error, if any. In the case of a cluster, it returns the client endpoint for the first node.
PeerEndpoint¶
- Since v0.34.0
Returns the peer endpoint for the etcd container and an error, if any. In the case of a cluster, it returns the peer endpoint for the first node.