Documentation
¶
Index ¶
- type EtcdContainer
- func (c *EtcdContainer) ClientEndpoint(ctx context.Context) (string, error)
- func (c *EtcdContainer) ClientEndpoints(ctx context.Context) ([]string, error)
- func (c *EtcdContainer) PeerEndpoint(ctx context.Context) (string, error)
- func (c *EtcdContainer) PeerEndpoints(ctx context.Context) ([]string, error)
- func (c *EtcdContainer) Terminate(ctx context.Context, opts ...testcontainers.TerminateOption) error
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EtcdContainer ¶
type EtcdContainer struct {
testcontainers.Container
// contains filtered or unexported fields
}
EtcdContainer represents the etcd container type used in the module. It can be used to create a single-node instance or a cluster. For the cluster, the first node creates the cluster and the other nodes join it as child nodes.
func Run ¶
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*EtcdContainer, error)
Run creates an instance of the etcd container type
Example ¶
// runetcdContainer {
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
}
// }
state, err := etcdContainer.State(ctx)
if err != nil {
log.Printf("failed to get container state: %s", err)
return
}
fmt.Println(state.Running)
Output: true
Example (Cluster) ¶
ctx := context.Background()
ctr, err := etcd.Run(ctx, "gcr.io/etcd-development/etcd:v3.5.14", etcd.WithNodes("etcd-1", "etcd-2", "etcd-3"))
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
defer func() {
if err := testcontainers.TerminateContainer(ctr); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
clientEndpoints, err := ctr.ClientEndpoints(ctx)
if err != nil {
log.Printf("failed to get client endpoints: %s", err)
return
}
// we have 3 nodes, 1 cluster node and 2 child nodes
fmt.Println(len(clientEndpoints))
cli, err := clientv3.New(clientv3.Config{
Endpoints: clientEndpoints,
DialTimeout: 5 * time.Second,
})
if err != nil {
log.Printf("failed to create etcd client: %s", err)
return
}
defer cli.Close()
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
_, err = cli.Put(ctx, "sample_key", "sample_value")
if err != nil {
log.Printf("failed to put key: %s", err)
return
}
resp, err := cli.Get(ctx, "sample_key")
if err != nil {
log.Printf("failed to get key: %s", err)
return
}
fmt.Println(len(resp.Kvs))
fmt.Println(string(resp.Kvs[0].Value))
Output: 3 1 sample_value
func (*EtcdContainer) ClientEndpoint ¶
func (c *EtcdContainer) ClientEndpoint(ctx context.Context) (string, error)
ClientEndpoint returns the client endpoint for the etcd container, and an error if any. For a cluster, it returns the client endpoint of the first node.
func (*EtcdContainer) ClientEndpoints ¶
func (c *EtcdContainer) ClientEndpoints(ctx context.Context) ([]string, error)
ClientEndpoints returns the client endpoints for the etcd cluster.
func (*EtcdContainer) PeerEndpoint ¶
func (c *EtcdContainer) PeerEndpoint(ctx context.Context) (string, error)
PeerEndpoint returns the peer endpoint for the etcd container, and an error if any. For a cluster, it returns the peer endpoint of the first node.
func (*EtcdContainer) PeerEndpoints ¶
func (c *EtcdContainer) PeerEndpoints(ctx context.Context) ([]string, error)
PeerEndpoints returns the peer endpoints for the etcd cluster.
func (*EtcdContainer) Terminate ¶
func (c *EtcdContainer) Terminate(ctx context.Context, opts ...testcontainers.TerminateOption) error
Terminate terminates the etcd container, its child nodes, and the network in which the cluster is running to communicate between the nodes.
type Option ¶
type Option func(*options) error
Option is an option for the Etcd container.
func WithAdditionalArgs ¶
WithAdditionalArgs is an option to pass additional arguments to the etcd container. They will be appended last to the command line.
func WithClusterToken ¶
WithClusterToken is an option to set the cluster token.
func WithDataDir ¶
func WithDataDir() Option
WithDataDir is an option to mount the data directory, which is located at /data.etcd. The option will add a lifecycle hook to the container to change the permissions of the data directory.
func WithNodes ¶
WithNodes is an option to set the nodes of the etcd cluster. It should be used to create a cluster with more than one node.
func (Option) Customize ¶
func (o Option) Customize(*testcontainers.GenericContainerRequest) error
Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.