From 9e912ba3edac5c96684f783dc806ad0ab70c687b Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Tue, 14 Mar 2023 16:35:32 +0100 Subject: [PATCH] tests: Extract e2e test utils Consider creating generic testutils for both e2e and integration tests. Signed-off-by: Marek Siarkowicz --- tests/e2e/cluster_test.go | 8 --- tests/e2e/utils.go | 102 ++++++++++++++++++++++++++++++++++ tests/e2e/watch_delay_test.go | 73 +----------------------- 3 files changed, 105 insertions(+), 78 deletions(-) create mode 100644 tests/e2e/utils.go diff --git a/tests/e2e/cluster_test.go b/tests/e2e/cluster_test.go index 48536292e..871744405 100644 --- a/tests/e2e/cluster_test.go +++ b/tests/e2e/cluster_test.go @@ -27,14 +27,6 @@ import ( const etcdProcessBasePort = 20000 -type clientConnType int - -const ( - clientNonTLS clientConnType = iota - clientTLS - clientTLSAndNonTLS -) - var ( configNoTLS = etcdProcessClusterConfig{ clusterSize: 3, diff --git a/tests/e2e/utils.go b/tests/e2e/utils.go new file mode 100644 index 000000000..66b4d1789 --- /dev/null +++ b/tests/e2e/utils.go @@ -0,0 +1,102 @@ +// Copyright 2023 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package e2e + +import ( + "context" + "fmt" + "testing" + "time" + + "go.uber.org/zap" + "golang.org/x/sync/errgroup" + "google.golang.org/grpc" + + "go.etcd.io/etcd/clientv3" + "go.etcd.io/etcd/pkg/stringutil" + "go.etcd.io/etcd/pkg/transport" +) + +type clientConnType int + +const ( + clientNonTLS clientConnType = iota + clientTLS + clientTLSAndNonTLS +) + +func newClient(t *testing.T, entpoints []string, connType clientConnType, isAutoTLS bool) *clientv3.Client { + tlscfg, err := tlsInfo(t, connType, isAutoTLS) + if err != nil { + t.Fatal(err) + } + ccfg := clientv3.Config{ + Endpoints: entpoints, + DialTimeout: 5 * time.Second, + DialOptions: []grpc.DialOption{grpc.WithBlock()}, + } + if tlscfg != nil { + tls, err := tlscfg.ClientConfig() + if err != nil { + t.Fatal(err) + } + ccfg.TLS = tls + } + c, err := clientv3.New(ccfg) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + c.Close() + }) + return c +} + +func tlsInfo(t testing.TB, connType clientConnType, isAutoTLS bool) (*transport.TLSInfo, error) { + switch connType { + case clientNonTLS, clientTLSAndNonTLS: + return nil, nil + case clientTLS: + if isAutoTLS { + tls, err := transport.SelfCert(zap.NewNop(), t.TempDir(), []string{"localhost"}, 1) + if err != nil { + return nil, fmt.Errorf("failed to generate cert: %s", err) + } + return &tls, nil + } + panic("Unsupported non-auto tls") + default: + return nil, fmt.Errorf("config %v not supported", connType) + } +} + +func fillEtcdWithData(ctx context.Context, c *clientv3.Client, keyCount int, valueSize uint) error { + g := errgroup.Group{} + concurrency := 10 + keysPerRoutine := keyCount / concurrency + for i := 0; i < concurrency; i++ { + i := i + g.Go(func() error { + for j := 0; j < keysPerRoutine; j++ { + _, err := c.Put(ctx, fmt.Sprintf("%d", i*keysPerRoutine+j), stringutil.RandString(valueSize)) + if err != nil { + return err + } + } + return nil + }) + } + return g.Wait() +} diff --git a/tests/e2e/watch_delay_test.go b/tests/e2e/watch_delay_test.go index 9e298c838..1468128b4 100644 --- a/tests/e2e/watch_delay_test.go +++ b/tests/e2e/watch_delay_test.go @@ -26,13 +26,10 @@ import ( "time" "github.com/stretchr/testify/require" - "go.etcd.io/etcd/clientv3" - "go.etcd.io/etcd/pkg/stringutil" - "go.etcd.io/etcd/pkg/testutil" - "go.etcd.io/etcd/pkg/transport" - "go.uber.org/zap" "golang.org/x/sync/errgroup" - "google.golang.org/grpc" + + "go.etcd.io/etcd/clientv3" + "go.etcd.io/etcd/pkg/testutil" ) const ( @@ -176,25 +173,6 @@ func validateWatchDelay(t *testing.T, watch clientv3.WatchChan) { } } -func fillEtcdWithData(ctx context.Context, c *clientv3.Client, keyCount int, valueSize uint) error { - g := errgroup.Group{} - concurrency := 10 - keysPerRoutine := keyCount / concurrency - for i := 0; i < concurrency; i++ { - i := i - g.Go(func() error { - for j := 0; j < keysPerRoutine; j++ { - _, err := c.Put(ctx, fmt.Sprintf("%d", i*keysPerRoutine+j), stringutil.RandString(valueSize)) - if err != nil { - return err - } - } - return nil - }) - } - return g.Wait() -} - func continuouslyExecuteGetAll(ctx context.Context, t *testing.T, g *errgroup.Group, c *clientv3.Client) { mux := sync.RWMutex{} size := 0 @@ -230,48 +208,3 @@ func continuouslyExecuteGetAll(ctx context.Context, t *testing.T, g *errgroup.Gr return nil }) } - -func newClient(t *testing.T, entpoints []string, connType clientConnType, isAutoTLS bool) *clientv3.Client { - tlscfg, err := tlsInfo(t, connType, isAutoTLS) - if err != nil { - t.Fatal(err) - } - ccfg := clientv3.Config{ - Endpoints: entpoints, - DialTimeout: 5 * time.Second, - DialOptions: []grpc.DialOption{grpc.WithBlock()}, - } - if tlscfg != nil { - tls, err := tlscfg.ClientConfig() - if err != nil { - t.Fatal(err) - } - ccfg.TLS = tls - } - c, err := clientv3.New(ccfg) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { - c.Close() - }) - return c -} - -func tlsInfo(t testing.TB, connType clientConnType, isAutoTLS bool) (*transport.TLSInfo, error) { - switch connType { - case clientNonTLS, clientTLSAndNonTLS: - return nil, nil - case clientTLS: - if isAutoTLS { - tls, err := transport.SelfCert(zap.NewNop(), t.TempDir(), []string{"localhost"}, 1) - if err != nil { - return nil, fmt.Errorf("failed to generate cert: %s", err) - } - return &tls, nil - } - panic("Unsupported non-auto tls") - default: - return nil, fmt.Errorf("config %v not supported", connType) - } -}