tests: Handle simple TLS configuration for cluster in common framework

This commit is contained in:
Marek Siarkowicz 2022-02-18 12:46:59 +01:00
parent f7ee30cc41
commit 1c8adcd830
8 changed files with 107 additions and 15 deletions

View File

@ -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)
}

View File

@ -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"

View File

@ -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
}

View File

@ -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)
}

View File

@ -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")

View File

@ -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)))

View File

@ -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)
}

View File

@ -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
}