From 1c8adcd830c8006e23e21732601bd8e45783b48b Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Fri, 18 Feb 2022 12:46:59 +0100 Subject: [PATCH] tests: Handle simple TLS configuration for cluster in common framework --- tests/common/kv_test.go | 5 ++- .../options.go => config/client.go} | 2 +- tests/framework/config/cluster.go | 29 ++++++++++++++ tests/framework/e2e.go | 34 +++++++++++++++- tests/framework/e2e/etcdctl.go | 4 +- tests/framework/integration.go | 39 +++++++++++++++++-- tests/framework/interface.go | 6 +-- tests/framework/unit.go | 3 +- 8 files changed, 107 insertions(+), 15 deletions(-) rename tests/framework/{testutils/options.go => config/client.go} (97%) create mode 100644 tests/framework/config/cluster.go diff --git a/tests/common/kv_test.go b/tests/common/kv_test.go index 54bda9915..e2b6efc70 100644 --- a/tests/common/kv_test.go +++ b/tests/common/kv_test.go @@ -18,12 +18,13 @@ import ( "testing" "time" + "go.etcd.io/etcd/tests/v3/framework/config" "go.etcd.io/etcd/tests/v3/framework/testutils" ) func TestKVPut(t *testing.T) { testRunner.BeforeTest(t) - clus := testRunner.NewCluster(t) + clus := testRunner.NewCluster(t, config.ClusterConfig{ClusterSize: 1, PeerTLS: config.AutoTLS}) defer clus.Close() cc := clus.Client() @@ -33,7 +34,7 @@ func TestKVPut(t *testing.T) { if err := cc.Put(key, value); err != nil { t.Fatalf("count not put key %q, err: %s", key, err) } - resp, err := cc.Get(key, testutils.GetOptions{Serializable: true}) + resp, err := cc.Get(key, config.GetOptions{Serializable: true}) if err != nil { t.Fatalf("count not get key %q, err: %s", key, err) } diff --git a/tests/framework/testutils/options.go b/tests/framework/config/client.go similarity index 97% rename from tests/framework/testutils/options.go rename to tests/framework/config/client.go index 0ec7e56de..47c7ba138 100644 --- a/tests/framework/testutils/options.go +++ b/tests/framework/config/client.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package testutils +package config import clientv3 "go.etcd.io/etcd/client/v3" diff --git a/tests/framework/config/cluster.go b/tests/framework/config/cluster.go new file mode 100644 index 000000000..c9deab294 --- /dev/null +++ b/tests/framework/config/cluster.go @@ -0,0 +1,29 @@ +// Copyright 2022 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 config + +type TLSConfig string + +const ( + NoTLS TLSConfig = "" + AutoTLS TLSConfig = "auto-tls" + ManualTLS TLSConfig = "manual-tls" +) + +type ClusterConfig struct { + ClusterSize int + PeerTLS TLSConfig + ClientTLS TLSConfig +} diff --git a/tests/framework/e2e.go b/tests/framework/e2e.go index 7b7769ec9..d0e799163 100644 --- a/tests/framework/e2e.go +++ b/tests/framework/e2e.go @@ -19,6 +19,7 @@ import ( "testing" "go.etcd.io/etcd/client/pkg/v3/testutil" + "go.etcd.io/etcd/tests/v3/framework/config" "go.etcd.io/etcd/tests/v3/framework/e2e" ) @@ -37,8 +38,37 @@ func (e e2eRunner) BeforeTest(t testing.TB) { e2e.BeforeTest(t) } -func (e e2eRunner) NewCluster(t testing.TB) Cluster { - epc, err := e2e.NewEtcdProcessCluster(t, e2e.ConfigStandalone(*e2e.NewConfigAutoTLS())) +func (e e2eRunner) NewCluster(t testing.TB, cfg config.ClusterConfig) Cluster { + e2eConfig := e2e.EtcdProcessClusterConfig{ + InitialToken: "new", + ClusterSize: cfg.ClusterSize, + } + switch cfg.ClientTLS { + case config.NoTLS: + e2eConfig.ClientTLS = e2e.ClientNonTLS + case config.AutoTLS: + e2eConfig.IsClientAutoTLS = true + e2eConfig.ClientTLS = e2e.ClientTLS + case config.ManualTLS: + e2eConfig.IsClientAutoTLS = false + e2eConfig.ClientTLS = e2e.ClientTLS + default: + t.Fatalf("ClientTLS config %q not supported", cfg.ClientTLS) + } + switch cfg.PeerTLS { + case config.NoTLS: + e2eConfig.IsPeerTLS = false + e2eConfig.IsPeerAutoTLS = false + case config.AutoTLS: + e2eConfig.IsPeerTLS = true + e2eConfig.IsPeerAutoTLS = true + case config.ManualTLS: + e2eConfig.IsPeerTLS = true + e2eConfig.IsPeerAutoTLS = false + default: + t.Fatalf("PeerTLS config %q not supported", cfg.PeerTLS) + } + epc, err := e2e.NewEtcdProcessCluster(t, &e2eConfig) if err != nil { t.Fatalf("could not start etcd integrationCluster: %s", err) } diff --git a/tests/framework/e2e/etcdctl.go b/tests/framework/e2e/etcdctl.go index 90dd4856d..5d79bbfbb 100644 --- a/tests/framework/e2e/etcdctl.go +++ b/tests/framework/e2e/etcdctl.go @@ -20,7 +20,7 @@ import ( "strings" clientv3 "go.etcd.io/etcd/client/v3" - "go.etcd.io/etcd/tests/v3/framework/testutils" + "go.etcd.io/etcd/tests/v3/framework/config" ) type EtcdctlV3 struct { @@ -39,7 +39,7 @@ func (ctl *EtcdctlV3) DowngradeEnable(version string) error { return SpawnWithExpect(ctl.cmdArgs("downgrade", "enable", version), "Downgrade enable success") } -func (ctl *EtcdctlV3) Get(key string, o testutils.GetOptions) (*clientv3.GetResponse, error) { +func (ctl *EtcdctlV3) Get(key string, o config.GetOptions) (*clientv3.GetResponse, error) { args := ctl.cmdArgs() if o.Serializable { args = append(args, "--consistency", "s") diff --git a/tests/framework/integration.go b/tests/framework/integration.go index c628d7080..d484fe580 100644 --- a/tests/framework/integration.go +++ b/tests/framework/integration.go @@ -16,12 +16,15 @@ package framework import ( "context" + "fmt" "testing" "go.etcd.io/etcd/client/pkg/v3/testutil" + "go.etcd.io/etcd/client/pkg/v3/transport" clientv3 "go.etcd.io/etcd/client/v3" + "go.etcd.io/etcd/tests/v3/framework/config" "go.etcd.io/etcd/tests/v3/framework/integration" - "go.etcd.io/etcd/tests/v3/framework/testutils" + "go.uber.org/zap" ) type integrationRunner struct{} @@ -34,13 +37,41 @@ func (e integrationRunner) BeforeTest(t testing.TB) { integration.BeforeTest(t) } -func (e integrationRunner) NewCluster(t testing.TB) Cluster { +func (e integrationRunner) NewCluster(t testing.TB, cfg config.ClusterConfig) Cluster { + var err error + var integrationCfg integration.ClusterConfig + integrationCfg.Size = cfg.ClusterSize + integrationCfg.ClientTLS, err = tlsInfo(t, cfg.ClientTLS) + if err != nil { + t.Fatalf("ClientTLS: %s", err) + } + integrationCfg.PeerTLS, err = tlsInfo(t, cfg.PeerTLS) + if err != nil { + t.Fatalf("PeerTLS: %s", err) + } return &integrationCluster{ - Cluster: integration.NewCluster(t, &integration.ClusterConfig{Size: 1}), + Cluster: integration.NewCluster(t, &integrationCfg), t: t, } } +func tlsInfo(t testing.TB, cfg config.TLSConfig) (*transport.TLSInfo, error) { + switch cfg { + case config.NoTLS: + return nil, nil + case config.AutoTLS: + 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 + case config.ManualTLS: + return &integration.TestTLSInfo, nil + default: + return nil, fmt.Errorf("config %q not supported", cfg) + } +} + type integrationCluster struct { *integration.Cluster t testing.TB @@ -63,7 +94,7 @@ type integrationClient struct { *clientv3.Client } -func (c integrationClient) Get(key string, o testutils.GetOptions) (*clientv3.GetResponse, error) { +func (c integrationClient) Get(key string, o config.GetOptions) (*clientv3.GetResponse, error) { clientOpts := []clientv3.OpOption{} if o.Revision != 0 { clientOpts = append(clientOpts, clientv3.WithRev(int64(o.Revision))) diff --git a/tests/framework/interface.go b/tests/framework/interface.go index b430106c6..fb57b92ec 100644 --- a/tests/framework/interface.go +++ b/tests/framework/interface.go @@ -18,13 +18,13 @@ import ( "testing" clientv3 "go.etcd.io/etcd/client/v3" - "go.etcd.io/etcd/tests/v3/framework/testutils" + "go.etcd.io/etcd/tests/v3/framework/config" ) type testRunner interface { TestMain(m *testing.M) BeforeTest(testing.TB) - NewCluster(testing.TB) Cluster + NewCluster(testing.TB, config.ClusterConfig) Cluster } type Cluster interface { @@ -34,5 +34,5 @@ type Cluster interface { type Client interface { Put(key, value string) error - Get(key string, opts testutils.GetOptions) (*clientv3.GetResponse, error) + Get(key string, opts config.GetOptions) (*clientv3.GetResponse, error) } diff --git a/tests/framework/unit.go b/tests/framework/unit.go index 231e0f7df..45b10ff41 100644 --- a/tests/framework/unit.go +++ b/tests/framework/unit.go @@ -21,6 +21,7 @@ import ( "testing" "go.etcd.io/etcd/client/pkg/v3/testutil" + "go.etcd.io/etcd/tests/v3/framework/config" ) type unitRunner struct{} @@ -38,7 +39,7 @@ func (e unitRunner) TestMain(m *testing.M) { func (e unitRunner) BeforeTest(t testing.TB) { } -func (e unitRunner) NewCluster(t testing.TB) Cluster { +func (e unitRunner) NewCluster(t testing.TB, cfg config.ClusterConfig) Cluster { testutil.SkipTestIfShortMode(t, "Cannot create clusters in --short tests") return nil }