From 37804358312c886a260331ad246f41bb1e1a6e6d Mon Sep 17 00:00:00 2001 From: ahrtr Date: Tue, 22 Mar 2022 06:24:32 +0800 Subject: [PATCH] added unit test for newClientCfg --- etcdctl/ctlv3/command/global_test.go | 131 +++++++++++++++++++++++++++ etcdctl/go.mod | 4 + etcdctl/go.sum | 3 + 3 files changed, 138 insertions(+) create mode 100644 etcdctl/ctlv3/command/global_test.go diff --git a/etcdctl/ctlv3/command/global_test.go b/etcdctl/ctlv3/command/global_test.go new file mode 100644 index 00000000000..0dc56abdd25 --- /dev/null +++ b/etcdctl/ctlv3/command/global_test.go @@ -0,0 +1,131 @@ +// 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 command + +import ( + "crypto/tls" + + "go.uber.org/zap" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "go.etcd.io/etcd/client/pkg/v3/transport" + clientv3 "go.etcd.io/etcd/client/v3" +) + +func TestNewClientConfig(t *testing.T) { + cases := []struct { + name string + spec clientv3.ConfigSpec + expectedConf clientv3.Config + }{ + { + name: "default secure transport", + spec: clientv3.ConfigSpec{ + Endpoints: []string{"http://192.168.0.10:2379"}, + DialTimeout: 2 * time.Second, + KeepAliveTime: 3 * time.Second, + KeepAliveTimeout: 5 * time.Second, + Secure: &clientv3.SecureConfig{ + InsecureTransport: false, + }, + }, + expectedConf: clientv3.Config{ + Endpoints: []string{"http://192.168.0.10:2379"}, + DialTimeout: 2 * time.Second, + DialKeepAliveTime: 3 * time.Second, + DialKeepAliveTimeout: 5 * time.Second, + TLS: &tls.Config{}, + }, + }, + { + name: "default secure transport and auth enabled", + spec: clientv3.ConfigSpec{ + Endpoints: []string{"http://192.168.0.12:2379"}, + DialTimeout: 1 * time.Second, + KeepAliveTime: 4 * time.Second, + KeepAliveTimeout: 6 * time.Second, + Secure: &clientv3.SecureConfig{ + InsecureTransport: false, + }, + Auth: &clientv3.AuthConfig{ + Username: "test", + Password: "changeme", + }, + }, + expectedConf: clientv3.Config{ + Endpoints: []string{"http://192.168.0.12:2379"}, + DialTimeout: 1 * time.Second, + DialKeepAliveTime: 4 * time.Second, + DialKeepAliveTimeout: 6 * time.Second, + TLS: &tls.Config{}, + Username: "test", + Password: "changeme", + }, + }, + { + name: "default secure transport and skip TLS verification", + spec: clientv3.ConfigSpec{ + Endpoints: []string{"http://192.168.0.13:2379"}, + DialTimeout: 1 * time.Second, + KeepAliveTime: 3 * time.Second, + KeepAliveTimeout: 5 * time.Second, + Secure: &clientv3.SecureConfig{ + InsecureTransport: false, + InsecureSkipVerify: true, + }, + }, + expectedConf: clientv3.Config{ + Endpoints: []string{"http://192.168.0.13:2379"}, + DialTimeout: 1 * time.Second, + DialKeepAliveTime: 3 * time.Second, + DialKeepAliveTimeout: 5 * time.Second, + TLS: &tls.Config{ + InsecureSkipVerify: true, + }, + }, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + cfg, err := newClientCfg(tc.spec.Endpoints, tc.spec.DialTimeout, tc.spec.KeepAliveTime, tc.spec.KeepAliveTimeout, tc.spec.Secure, tc.spec.Auth) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + assert.Equal(t, tc.expectedConf, *cfg) + }) + } +} + +func TestNewClientConfigWithSecureCfg(t *testing.T) { + tls, err := transport.SelfCert(zap.NewNop(), t.TempDir(), []string{"localhost"}, 1) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + scfg := &clientv3.SecureConfig{ + Cert: tls.CertFile, + Key: tls.KeyFile, + Cacert: tls.TrustedCAFile, + } + + cfg, err := newClientCfg([]string{"http://192.168.0.13:2379"}, 2*time.Second, 3*time.Second, 5*time.Second, scfg, nil) + if cfg == nil || err != nil { + t.Fatalf("Unexpected result client config: %v", err) + } +} diff --git a/etcdctl/go.mod b/etcdctl/go.mod index d6b770a4d61..49ded5f42e7 100644 --- a/etcdctl/go.mod +++ b/etcdctl/go.mod @@ -8,6 +8,7 @@ require ( github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.2.1 github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.7.0 github.com/urfave/cli v1.22.4 go.etcd.io/etcd/api/v3 v3.6.0-alpha.0 go.etcd.io/etcd/client/pkg/v3 v3.6.0-alpha.0 @@ -27,6 +28,7 @@ require ( github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/golang/protobuf v1.5.2 // indirect @@ -35,6 +37,7 @@ require ( github.com/jonboulle/clockwork v0.2.2 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.11.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.26.0 // indirect @@ -56,6 +59,7 @@ require ( golang.org/x/text v0.3.7 // indirect google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 // indirect google.golang.org/protobuf v1.27.1 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect ) diff --git a/etcdctl/go.sum b/etcdctl/go.sum index af19c33748f..5e8427fda7a 100644 --- a/etcdctl/go.sum +++ b/etcdctl/go.sum @@ -242,9 +242,11 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= @@ -729,6 +731,7 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/cheggaaa/pb.v1 v1.0.28 h1:n1tBJnnK2r7g9OW2btFH91V92STTUevLXYFb8gy9EMk= gopkg.in/cheggaaa/pb.v1 v1.0.28/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=