etcd

package
v1.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2026 License: BSD-2-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package etcd provides a reusable, embedded single-node etcd cluster for integration tests.

Importers are expected to alias the package to etcdtest to disambiguate it from sibling packages named etcd (notably driver/etcd):

import etcdtest "github.com/tarantool/go-storage/test_helpers/etcd"

The helper is backed by go.etcd.io/etcd/server/v3/embed (the etcd server library) rather than go.etcd.io/etcd/tests/v3. The test framework drags in a v1 build of github.com/grpc-ecosystem/go-grpc-middleware, which in turn pins the pre-split google.golang.org/genproto monolith; that collides with modern genproto/googleapis/{api,rpc} and breaks plain module-mode builds with an "ambiguous import" error. embed imports none of that, so a helper built on it keeps the offending dependency out of consumers' module graphs.

TLS semantics

When ClusterConfig.ClientTLS is set, the embedded server listens for clients over TLS and EndpointsGRPC/EndpointsHTTP return https:// URLs; a clientv3 dialer must be configured with matching TLS. This differs from the historical tests/v3-based helper, which mapped certificates to the peer listener while dialing clients in plaintext. Set ClientTLS (not just PeerTLS) when you want the client connection itself to be encrypted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster

type Cluster struct {
	// contains filtered or unexported fields
}

Cluster is a running embedded etcd cluster handle.

func New

func New(tb testing.TB, cfg ClusterConfig) *Cluster

New starts an embedded single-node etcd cluster and registers its shutdown via tb.Cleanup. It fails the test on any startup error.

func (*Cluster) EndpointsGRPC

func (c *Cluster) EndpointsGRPC() []string

EndpointsGRPC returns the client endpoints to pass to clientv3.

func (*Cluster) EndpointsHTTP

func (c *Cluster) EndpointsHTTP() []string

EndpointsHTTP returns the client endpoints as HTTP(S) URLs.

func (*Cluster) Terminate

func (c *Cluster) Terminate()

Terminate stops the embedded server and, when the helper owns the data directory, removes it. It is safe to call multiple times.

type ClusterConfig

type ClusterConfig struct {
	// Size is the number of nodes. Only 1 is supported; 0 is treated as 1.
	Size int

	// ClientTLS, when non-nil, makes the server serve clients over TLS. The
	// returned endpoints become https:// and clientv3 must dial with matching
	// TLS settings.
	ClientTLS *transport.TLSInfo

	// PeerTLS, when non-nil, secures the peer (inter-node) listener. For a
	// single node it has no functional effect but is honored for parity.
	PeerTLS *transport.TLSInfo
}

ClusterConfig configures the embedded cluster. It is the helper's own type; it intentionally does not re-export any tests/v3 type.

type LazyCluster

type LazyCluster struct {
	// contains filtered or unexported fields
}

LazyCluster lazily starts a single embedded etcd cluster on first endpoint access and is intended to be shared across many tests in a suite. Typical use is to instantiate it in TestMain and tear it down before os.Exit:

var shared *etcdtest.LazyCluster

func TestMain(m *testing.M) {
    shared = etcdtest.NewLazyCluster(etcdtest.ClusterConfig{Size: 1})
    code := m.Run()
    shared.Terminate()
    os.Exit(code)
}

Unlike New, LazyCluster does not register cleanup via testing.TB — its lifetime spans many tests, so the caller must Terminate it explicitly. Startup failures panic, since there is no TB to report through.

Tests sharing a LazyCluster see each other's keys; isolate writes with per-test prefixes or explicit cleanup.

func NewLazyCluster

func NewLazyCluster(cfg ClusterConfig) *LazyCluster

NewLazyCluster returns a handle to a cluster that is not started until EndpointsGRPC or EndpointsHTTP is first called.

func (*LazyCluster) EndpointsGRPC

func (c *LazyCluster) EndpointsGRPC() []string

EndpointsGRPC starts the cluster on first call and returns clientv3 endpoints.

func (*LazyCluster) EndpointsHTTP

func (c *LazyCluster) EndpointsHTTP() []string

EndpointsHTTP starts the cluster on first call and returns HTTP(S) endpoints.

func (*LazyCluster) Terminate

func (c *LazyCluster) Terminate()

Terminate stops the embedded server (if started) and removes the helper- owned data directory. It is safe to call multiple times and on a never- initialized LazyCluster.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL