diff --git a/client/v3/config_test.go b/client/v3/config_test.go new file mode 100644 index 000000000000..9d45251bc951 --- /dev/null +++ b/client/v3/config_test.go @@ -0,0 +1,123 @@ +// 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 clientv3 + +import ( + "crypto/tls" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "go.uber.org/zap" +) + +func TestNewClientConfig(t *testing.T) { + cases := []struct { + name string + spec ConfigSpec + conf Config + }{ + { + name: "only has basic info", + spec: ConfigSpec{ + Endpoints: []string{"http://192.168.0.10:2379"}, + DialTimeout: 2 * time.Second, + KeepAliveTime: 3 * time.Second, + KeepAliveTimeout: 5 * time.Second, + }, + conf: Config{ + Endpoints: []string{"http://192.168.0.10:2379"}, + DialTimeout: 2 * time.Second, + DialKeepAliveTime: 3 * time.Second, + DialKeepAliveTimeout: 5 * time.Second, + }, + }, + { + name: "auth enabled", + spec: ConfigSpec{ + Endpoints: []string{"http://192.168.0.12:2379"}, + DialTimeout: 1 * time.Second, + KeepAliveTime: 4 * time.Second, + KeepAliveTimeout: 6 * time.Second, + Auth: &AuthConfig{ + Username: "test", + Password: "changeme", + }, + }, + conf: Config{ + Endpoints: []string{"http://192.168.0.12:2379"}, + DialTimeout: 1 * time.Second, + DialKeepAliveTime: 4 * time.Second, + DialKeepAliveTimeout: 6 * time.Second, + Username: "test", + Password: "changeme", + }, + }, + { + name: "default secure transport", + spec: ConfigSpec{ + Endpoints: []string{"http://192.168.0.10:2379"}, + DialTimeout: 2 * time.Second, + KeepAliveTime: 3 * time.Second, + KeepAliveTimeout: 5 * time.Second, + Secure: &SecureConfig{ + InsecureTransport: false, + }, + }, + conf: 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 skip TLS verification", + spec: ConfigSpec{ + Endpoints: []string{"http://192.168.0.13:2379"}, + DialTimeout: 1 * time.Second, + KeepAliveTime: 3 * time.Second, + KeepAliveTimeout: 5 * time.Second, + Secure: &SecureConfig{ + InsecureTransport: false, + InsecureSkipVerify: true, + }, + }, + conf: 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) { + lg, _ := zap.NewProduction() + + cfg, err := NewClientConfig(&tc.spec, lg) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + assert.Equal(t, tc.conf, *cfg) + }) + } +} diff --git a/client/v3/go.mod b/client/v3/go.mod index 18ada51125b7..f8b11edbd926 100644 --- a/client/v3/go.mod +++ b/client/v3/go.mod @@ -6,6 +6,7 @@ require ( github.com/dustin/go-humanize v1.0.0 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/prometheus/client_golang v1.11.0 + github.com/stretchr/testify v1.7.0 go.etcd.io/etcd/api/v3 v3.6.0-alpha.0 go.etcd.io/etcd/client/pkg/v3 v3.6.0-alpha.0 go.uber.org/zap v1.17.0 @@ -26,7 +27,6 @@ require ( github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.26.0 // indirect github.com/prometheus/procfs v0.6.0 // indirect - github.com/stretchr/testify v1.7.0 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect