Skip to content

Commit

Permalink
tests: Allow creating client for EtcdProcess
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
  • Loading branch information
serathius committed Nov 17, 2022
1 parent 8329c9c commit bf3eea8
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 66 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/cluster_downgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func startEtcd(t *testing.T, ep e2e.EtcdProcess, execPath string) {
}

func downgradeEnable(t *testing.T, epc *e2e.EtcdProcessCluster, ver *semver.Version) {
c, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
c, err := e2e.NewEtcdctl(epc.Cfg.Client, epc.EndpointsV3())
assert.NoError(t, err)
testutils.ExecuteWithTimeout(t, 20*time.Second, func() {
err := c.DowngradeEnable(context.TODO(), ver.String())
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestPeriodicCheckDetectsCorruption(t *testing.T) {
}
})

cc, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc, err := e2e.NewEtcdctl(epc.Cfg.Client, epc.EndpointsV3())
assert.NoError(t, err)

for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestCompactHashCheckDetectCorruption(t *testing.T) {
}
})

cc, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc, err := e2e.NewEtcdctl(epc.Cfg.Client, epc.EndpointsV3())
assert.NoError(t, err)

for i := 0; i < 10; i++ {
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/ctl_v3_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func TestAuthority(t *testing.T) {
cfg := e2e.NewConfigNoTLS()
cfg.ClusterSize = clusterSize
if tc.useTLS {
cfg.ClientTLS = e2e.ClientTLS
cfg.Client.ConnectionType = e2e.ClientTLS
}
cfg.IsClientAutoTLS = tc.useInsecureTLS
cfg.Client.AutoTLS = tc.useInsecureTLS
// Enable debug mode to get logs with http2 headers (including authority)
cfg.EnvVars = map[string]string{"GODEBUG": "http2debug=2"}

Expand All @@ -98,7 +98,7 @@ func TestAuthority(t *testing.T) {
defer epc.Close()
endpoints := templateEndpoints(t, tc.clientURLPattern, epc)

client, err := e2e.NewEtcdctl(cfg, endpoints)
client, err := e2e.NewEtcdctl(cfg.Client, endpoints)
assert.NoError(t, err)
err = client.Put(ctx, "foo", "bar", config.PutOptions{})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/ctl_v3_kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func TestCtlV3DelTimeout(t *testing.T) { testCtl(t, delTest, withDialTimeout(0))
func TestCtlV3GetRevokedCRL(t *testing.T) {
cfg := e2e.NewConfig(
e2e.WithClusterSize(1),
e2e.WithClientTLS(e2e.ClientTLS),
e2e.WithIsClientCRL(true),
e2e.WithClientCertAuthEnabled(true),
e2e.WithClientConnType(e2e.ClientTLS),
e2e.WithClientRevokeCerts(true),
e2e.WithClientCertAuthority(true),
)
testCtl(t, testGetRevokedCRL, withCfg(*cfg))
}
Expand All @@ -56,7 +56,7 @@ func testGetRevokedCRL(cx ctlCtx) {
require.ErrorContains(cx.t, err, "context deadline exceeded")

// test accept
cx.epc.Cfg.IsClientCRL = false
cx.epc.Cfg.Client.RevokeCerts = false
if err := ctlV3Put(cx, "k", "v", ""); err != nil {
cx.t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/ctl_v3_move_leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig, envVars
}()

var tcfg *tls.Config
if cfg.ClientTLS == e2e.ClientTLS {
if cfg.Client.ConnectionType == e2e.ClientTLS {
tinfo := transport.TLSInfo{
CertFile: e2e.CertPath,
KeyFile: e2e.PrivateKeyPath,
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/ctl_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ func (cx *ctlCtx) prefixArgs(eps []string) []string {
fmap := make(map[string]string)
fmap["endpoints"] = strings.Join(eps, ",")
fmap["dial-timeout"] = cx.dialTimeout.String()
if cx.epc.Cfg.ClientTLS == e2e.ClientTLS {
if cx.epc.Cfg.IsClientAutoTLS {
if cx.epc.Cfg.Client.ConnectionType == e2e.ClientTLS {
if cx.epc.Cfg.Client.AutoTLS {
fmap["insecure-transport"] = "false"
fmap["insecure-skip-tls-verify"] = "true"
} else if cx.epc.Cfg.IsClientCRL {
} else if cx.epc.Cfg.Client.RevokeCerts {
fmap["cacert"] = e2e.CaPath
fmap["cert"] = e2e.RevokedCertPath
fmap["key"] = e2e.RevokedPrivateKeyPath
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/discovery_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func testClusterUsingV3Discovery(t *testing.T, discoveryClusterSize, targetClust
ds, err := e2e.NewEtcdProcessCluster(context.TODO(), t,
e2e.WithBasePort(2000),
e2e.WithClusterSize(discoveryClusterSize),
e2e.WithClientTLS(clientTlsType),
e2e.WithIsClientAutoTLS(isClientAutoTls),
e2e.WithClientConnType(clientTlsType),
e2e.WithClientAutoTLS(isClientAutoTls),
)
if err != nil {
t.Fatalf("could not start discovery etcd cluster (%v)", err)
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/etcd_grpcproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestGrpcProxyAutoSync(t *testing.T) {
assert.NoError(t, proxyProc.Stop())
}()

proxyCtl, err := e2e.NewEtcdctl(e2e.DefaultConfig(), []string{proxyClientURL})
proxyCtl, err := e2e.NewEtcdctl(e2e.ClientConfig{}, []string{proxyClientURL})
require.NoError(t, err)
err = proxyCtl.Put(ctx, "k1", "v1", config.PutOptions{})
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/v2store_deprecation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ func TestV2DeprecationSnapshotMatches(t *testing.T) {
snapshotCount := 10
epc := runEtcdAndCreateSnapshot(t, e2e.LastVersion, lastReleaseData, snapshotCount)
oldMemberDataDir := epc.Procs[0].Config().DataDirPath
cc1, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc1, err := e2e.NewEtcdctl(epc.Cfg.Client, epc.EndpointsV3())
assert.NoError(t, err)
members1 := addAndRemoveKeysAndMembers(ctx, t, cc1, snapshotCount)
assert.NoError(t, epc.Close())
epc = runEtcdAndCreateSnapshot(t, e2e.CurrentVersion, currentReleaseData, snapshotCount)
newMemberDataDir := epc.Procs[0].Config().DataDirPath
cc2, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc2, err := e2e.NewEtcdctl(epc.Cfg.Client, epc.EndpointsV3())
assert.NoError(t, err)
members2 := addAndRemoveKeysAndMembers(ctx, t, cc2, snapshotCount)
assert.NoError(t, epc.Close())
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestV2DeprecationSnapshotRecover(t *testing.T) {
}
epc := runEtcdAndCreateSnapshot(t, e2e.LastVersion, dataDir, 10)

cc, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc, err := e2e.NewEtcdctl(epc.Cfg.Client, epc.EndpointsV3())
assert.NoError(t, err)

lastReleaseGetResponse, err := cc.Get(ctx, "", config.GetOptions{Prefix: true})
Expand All @@ -168,7 +168,7 @@ func TestV2DeprecationSnapshotRecover(t *testing.T) {
epc, err = e2e.NewEtcdProcessCluster(context.TODO(), t, e2e.WithConfig(cfg))
assert.NoError(t, err)

cc, err = e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc, err = e2e.NewEtcdctl(epc.Cfg.Client, epc.EndpointsV3())
assert.NoError(t, err)
currentReleaseGetResponse, err := cc.Get(ctx, "", config.GetOptions{Prefix: true})
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/v3_curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func testV3CurlPutGet(cx ctlCtx) {
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/range"), Value: string(rangeData), Expected: expectGet}); err != nil {
cx.t.Fatalf("failed testV3CurlPutGet get with curl using prefix (%s) (%v)", p, err)
}
if cx.cfg.ClientTLS == e2e.ClientTLSAndNonTLS {
if cx.cfg.Client.ConnectionType == e2e.ClientTLSAndNonTLS {
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/range"), Value: string(rangeData), Expected: expectGet, IsTLS: true}); err != nil {
cx.t.Fatalf("failed testV3CurlPutGet get with curl using prefix (%s) (%v)", p, err)
}
Expand Down
65 changes: 35 additions & 30 deletions tests/framework/e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ const (
ClientTLSAndNonTLS
)

type ClientConfig struct {
ConnectionType ClientConnType
CertAuthority bool
AutoTLS bool
RevokeCerts bool
}

// allow alphanumerics, underscores and dashes
var testNameCleanRegex = regexp.MustCompile(`[^a-zA-Z0-9 \-_]+`)

Expand All @@ -60,20 +67,20 @@ func NewConfigAutoTLS() *EtcdProcessClusterConfig {

func NewConfigTLS() *EtcdProcessClusterConfig {
return NewConfig(
WithClientTLS(ClientTLS),
WithClientConnType(ClientTLS),
WithIsPeerTLS(true),
)
}

func NewConfigClientTLS() *EtcdProcessClusterConfig {
return NewConfig(WithClientTLS(ClientTLS))
return NewConfig(WithClientConnType(ClientTLS))
}

func NewConfigClientAutoTLS() *EtcdProcessClusterConfig {
return NewConfig(
WithClusterSize(1),
WithIsClientAutoTLS(true),
WithClientTLS(ClientTLS),
WithClientAutoTLS(true),
WithClientConnType(ClientTLS),
)
}

Expand All @@ -86,16 +93,16 @@ func NewConfigPeerTLS() *EtcdProcessClusterConfig {
func NewConfigClientTLSCertAuth() *EtcdProcessClusterConfig {
return NewConfig(
WithClusterSize(1),
WithClientTLS(ClientTLS),
WithClientCertAuthEnabled(true),
WithClientConnType(ClientTLS),
WithClientCertAuthority(true),
)
}

func NewConfigClientTLSCertAuthWithNoCN() *EtcdProcessClusterConfig {
return NewConfig(
WithClusterSize(1),
WithClientTLS(ClientTLS),
WithClientCertAuthEnabled(true),
WithClientConnType(ClientTLS),
WithClientCertAuthority(true),
WithNoCN(true),
)
}
Expand Down Expand Up @@ -142,13 +149,10 @@ type EtcdProcessClusterConfig struct {

SnapshotCount int // default is 10000

ClientTLS ClientConnType
ClientCertAuthEnabled bool
IsPeerTLS bool
IsPeerAutoTLS bool
IsClientAutoTLS bool
IsClientCRL bool
NoCN bool
Client ClientConfig
IsPeerTLS bool
IsPeerAutoTLS bool
NoCN bool

CipherSuites []string

Expand Down Expand Up @@ -226,12 +230,12 @@ func WithBasePort(port int) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.BasePort = port }
}

func WithClientTLS(clientTLS ClientConnType) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.ClientTLS = clientTLS }
func WithClientConnType(clientConnType ClientConnType) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.Client.ConnectionType = clientConnType }
}

func WithClientCertAuthEnabled(enabled bool) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.ClientCertAuthEnabled = enabled }
func WithClientCertAuthority(enabled bool) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.Client.CertAuthority = enabled }
}

func WithIsPeerTLS(isPeerTLS bool) EPClusterOption {
Expand All @@ -242,12 +246,12 @@ func WithIsPeerAutoTLS(isPeerAutoTLS bool) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.IsPeerAutoTLS = isPeerAutoTLS }
}

func WithIsClientAutoTLS(isClientAutoTLS bool) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.IsClientAutoTLS = isClientAutoTLS }
func WithClientAutoTLS(isClientAutoTLS bool) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.Client.AutoTLS = isClientAutoTLS }
}

func WithIsClientCRL(isClientCRL bool) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.IsClientCRL = isClientCRL }
func WithClientRevokeCerts(isClientCRL bool) EPClusterOption {
return func(c *EtcdProcessClusterConfig) { c.Client.RevokeCerts = isClientCRL }
}

func WithNoCN(noCN bool) EPClusterOption {
Expand Down Expand Up @@ -374,7 +378,7 @@ func StartEtcdProcessCluster(ctx context.Context, epc *EtcdProcessCluster, cfg *
}

func (cfg *EtcdProcessClusterConfig) ClientScheme() string {
if cfg.ClientTLS == ClientTLS {
if cfg.Client.ConnectionType == ClientTLS {
return "https"
}
return "http"
Expand Down Expand Up @@ -426,7 +430,7 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
port := cfg.BasePort + 5*i
curlHost := fmt.Sprintf("localhost:%d", port)

switch cfg.ClientTLS {
switch cfg.Client.ConnectionType {
case ClientNonTLS, ClientTLS:
curl = (&url.URL{Scheme: cfg.ClientScheme(), Host: curlHost}).String()
curls = []string{curl}
Expand Down Expand Up @@ -561,6 +565,7 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
Args: args,
EnvVars: envVars,
TlsArgs: cfg.TlsArgs(),
Client: cfg.Client,
DataDirPath: dataDirPath,
KeepDataDir: cfg.KeepDataDir,
Name: name,
Expand All @@ -573,8 +578,8 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
}

func (cfg *EtcdProcessClusterConfig) TlsArgs() (args []string) {
if cfg.ClientTLS != ClientNonTLS {
if cfg.IsClientAutoTLS {
if cfg.Client.ConnectionType != ClientNonTLS {
if cfg.Client.AutoTLS {
args = append(args, "--auto-tls")
} else {
tlsClientArgs := []string{
Expand All @@ -584,7 +589,7 @@ func (cfg *EtcdProcessClusterConfig) TlsArgs() (args []string) {
}
args = append(args, tlsClientArgs...)

if cfg.ClientCertAuthEnabled {
if cfg.Client.CertAuthority {
args = append(args, "--client-cert-auth")
}
}
Expand All @@ -603,7 +608,7 @@ func (cfg *EtcdProcessClusterConfig) TlsArgs() (args []string) {
}
}

if cfg.IsClientCRL {
if cfg.Client.RevokeCerts {
args = append(args, "--client-crl-file", CrlPath, "--client-cert-auth")
}

Expand Down Expand Up @@ -784,7 +789,7 @@ func (epc *EtcdProcessCluster) Stop() (err error) {
}

func (epc *EtcdProcessCluster) Client(opts ...config.ClientOption) *EtcdctlV3 {
etcdctl, err := NewEtcdctl(epc.Cfg, epc.EndpointsV3(), opts...)
etcdctl, err := NewEtcdctl(epc.Cfg.Client, epc.EndpointsV3(), opts...)
if err != nil {
panic(err)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/framework/e2e/cluster_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.uber.org/zap"

"go.etcd.io/etcd/pkg/v3/expect"
"go.etcd.io/etcd/tests/v3/framework/config"
)

type proxyEtcdProcess struct {
Expand Down Expand Up @@ -99,6 +100,14 @@ func (p *proxyEtcdProcess) Close() error {
return err
}

func (p *proxyEtcdProcess) Client(opts ...config.ClientOption) *EtcdctlV3 {
etcdctl, err := NewEtcdctl(p.etcdProc.Config().Client, p.etcdProc.EndpointsV3(), opts...)
if err != nil {
panic(err)
}
return etcdctl
}

func (p *proxyEtcdProcess) Logs() LogsExpect {
return p.etcdProc.Logs()
}
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/e2e/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func CURLPrefixArgs(cfg *EtcdProcessClusterConfig, member EtcdProcess, method st
)
if req.MetricsURLScheme != "https" {
if req.IsTLS {
if cfg.ClientTLS != ClientTLSAndNonTLS {
if cfg.Client.ConnectionType != ClientTLSAndNonTLS {
panic("should not use cURLPrefixArgsUseTLS when serving only TLS or non-TLS")
}
cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath, "--key", PrivateKeyPath)
acurl = ToTLS(member.Config().Acurl)
} else if cfg.ClientTLS == ClientTLS {
} else if cfg.Client.ConnectionType == ClientTLS {
if !cfg.NoCN {
cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath, "--key", PrivateKeyPath)
} else {
Expand Down
Loading

0 comments on commit bf3eea8

Please sign in to comment.