2016-09-19 22:36:14 +03:00
|
|
|
// Copyright 2016 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 integration
|
|
|
|
|
|
|
|
import (
|
2017-09-07 00:57:25 +03:00
|
|
|
"context"
|
2016-09-19 22:36:14 +03:00
|
|
|
"math/rand"
|
2017-08-01 03:25:13 +03:00
|
|
|
"strings"
|
2016-09-19 22:36:14 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-08-29 03:13:10 +03:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
|
|
|
|
"go.etcd.io/etcd/integration"
|
|
|
|
"go.etcd.io/etcd/pkg/testutil"
|
|
|
|
"go.etcd.io/etcd/pkg/transport"
|
2018-04-27 00:54:07 +03:00
|
|
|
"google.golang.org/grpc"
|
2017-04-26 21:41:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testTLSInfo = transport.TLSInfo{
|
|
|
|
KeyFile: "../../integration/fixtures/server.key.insecure",
|
|
|
|
CertFile: "../../integration/fixtures/server.crt",
|
|
|
|
TrustedCAFile: "../../integration/fixtures/ca.crt",
|
|
|
|
ClientCertAuth: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
testTLSInfoExpired = transport.TLSInfo{
|
2018-04-09 20:59:52 +03:00
|
|
|
KeyFile: "../../integration/fixtures-expired/server.key.insecure",
|
|
|
|
CertFile: "../../integration/fixtures-expired/server.crt",
|
|
|
|
TrustedCAFile: "../../integration/fixtures-expired/ca.crt",
|
2017-04-26 21:41:58 +03:00
|
|
|
ClientCertAuth: true,
|
|
|
|
}
|
2016-09-19 22:36:14 +03:00
|
|
|
)
|
|
|
|
|
2017-04-26 21:41:58 +03:00
|
|
|
// TestDialTLSExpired tests client with expired certs fails to dial.
|
|
|
|
func TestDialTLSExpired(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
2017-10-25 00:57:33 +03:00
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, PeerTLS: &testTLSInfo, ClientTLS: &testTLSInfo, SkipCreatingClient: true})
|
2017-04-26 21:41:58 +03:00
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
tls, err := testTLSInfoExpired.ClientConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-04 13:09:32 +03:00
|
|
|
// expect remote errors "tls: bad certificate"
|
2017-04-26 21:41:58 +03:00
|
|
|
_, err = clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: []string{clus.Members[0].GRPCAddr()},
|
|
|
|
DialTimeout: 3 * time.Second,
|
2018-04-27 00:54:07 +03:00
|
|
|
DialOptions: []grpc.DialOption{grpc.WithBlock()},
|
2017-04-26 21:41:58 +03:00
|
|
|
TLS: tls,
|
|
|
|
})
|
2018-04-27 00:54:07 +03:00
|
|
|
if !isClientTimeout(err) {
|
|
|
|
t.Fatalf("expected dial timeout error, got %v", err)
|
2017-04-26 21:41:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 01:38:44 +03:00
|
|
|
// TestDialTLSNoConfig ensures the client fails to dial / times out
|
|
|
|
// when TLS endpoints (https, unixs) are given but no tls config.
|
|
|
|
func TestDialTLSNoConfig(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
2017-10-25 00:57:33 +03:00
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, ClientTLS: &testTLSInfo, SkipCreatingClient: true})
|
2017-06-01 01:38:44 +03:00
|
|
|
defer clus.Terminate(t)
|
2017-09-04 13:09:32 +03:00
|
|
|
// expect "signed by unknown authority"
|
2018-04-27 00:54:07 +03:00
|
|
|
c, err := clientv3.New(clientv3.Config{
|
2017-06-01 01:38:44 +03:00
|
|
|
Endpoints: []string{clus.Members[0].GRPCAddr()},
|
|
|
|
DialTimeout: time.Second,
|
2018-04-27 00:54:07 +03:00
|
|
|
DialOptions: []grpc.DialOption{grpc.WithBlock()},
|
2017-06-01 01:38:44 +03:00
|
|
|
})
|
2018-07-24 20:45:03 +03:00
|
|
|
defer func() {
|
|
|
|
if c != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}()
|
2018-04-27 00:54:07 +03:00
|
|
|
if !isClientTimeout(err) {
|
|
|
|
t.Fatalf("expected dial timeout error, got %v", err)
|
2017-06-01 01:38:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 13:09:32 +03:00
|
|
|
// TestDialSetEndpointsBeforeFail ensures SetEndpoints can replace unavailable
|
|
|
|
// endpoints with available ones.
|
2017-01-26 23:17:34 +03:00
|
|
|
func TestDialSetEndpointsBeforeFail(t *testing.T) {
|
|
|
|
testDialSetEndpoints(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialSetEndpointsAfterFail(t *testing.T) {
|
|
|
|
testDialSetEndpoints(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testDialSetEndpoints ensures SetEndpoints can replace unavailable endpoints with available ones.
|
|
|
|
func testDialSetEndpoints(t *testing.T, setBefore bool) {
|
2016-09-19 22:36:14 +03:00
|
|
|
defer testutil.AfterTest(t)
|
2017-10-25 00:57:33 +03:00
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3, SkipCreatingClient: true})
|
2016-09-19 22:36:14 +03:00
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
// get endpoint list
|
|
|
|
eps := make([]string, 3)
|
|
|
|
for i := range eps {
|
|
|
|
eps[i] = clus.Members[i].GRPCAddr()
|
|
|
|
}
|
|
|
|
toKill := rand.Intn(len(eps))
|
|
|
|
|
2018-04-27 00:54:07 +03:00
|
|
|
cfg := clientv3.Config{
|
|
|
|
Endpoints: []string{eps[toKill]},
|
|
|
|
DialTimeout: 1 * time.Second,
|
|
|
|
DialOptions: []grpc.DialOption{grpc.WithBlock()},
|
|
|
|
}
|
2016-09-19 22:36:14 +03:00
|
|
|
cli, err := clientv3.New(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
|
2017-01-26 23:17:34 +03:00
|
|
|
if setBefore {
|
|
|
|
cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
|
|
|
|
}
|
2016-09-19 22:36:14 +03:00
|
|
|
// make a dead node
|
|
|
|
clus.Members[toKill].Stop(t)
|
|
|
|
clus.WaitLeader(t)
|
|
|
|
|
2017-01-26 23:17:34 +03:00
|
|
|
if !setBefore {
|
|
|
|
cli.SetEndpoints(eps[toKill%3], eps[(toKill+1)%3])
|
|
|
|
}
|
2018-04-27 00:54:07 +03:00
|
|
|
time.Sleep(time.Second * 2)
|
2018-01-04 04:26:43 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), integration.RequestWaitTimeout)
|
2016-09-19 22:36:14 +03:00
|
|
|
if _, err = cli.Get(ctx, "foo", clientv3.WithSerializable()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
}
|
2017-02-17 01:48:57 +03:00
|
|
|
|
2017-03-15 01:42:56 +03:00
|
|
|
// TestSwitchSetEndpoints ensures SetEndpoints can switch one endpoint
|
|
|
|
// with a new one that doesn't include original endpoint.
|
|
|
|
func TestSwitchSetEndpoints(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
// get non partitioned members endpoints
|
|
|
|
eps := []string{clus.Members[1].GRPCAddr(), clus.Members[2].GRPCAddr()}
|
|
|
|
|
|
|
|
cli := clus.Client(0)
|
2017-10-26 00:52:35 +03:00
|
|
|
clus.Members[0].InjectPartition(t, clus.Members[1:]...)
|
2017-03-15 01:42:56 +03:00
|
|
|
|
|
|
|
cli.SetEndpoints(eps...)
|
2018-05-03 02:45:38 +03:00
|
|
|
|
2017-03-15 01:42:56 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if _, err := cli.Get(ctx, "foo"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 01:48:57 +03:00
|
|
|
func TestRejectOldCluster(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
|
|
|
// 2 endpoints to test multi-endpoint Status
|
2017-10-25 00:57:33 +03:00
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2, SkipCreatingClient: true})
|
2017-02-17 01:48:57 +03:00
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
cfg := clientv3.Config{
|
|
|
|
Endpoints: []string{clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr()},
|
|
|
|
DialTimeout: 5 * time.Second,
|
2018-04-27 00:54:07 +03:00
|
|
|
DialOptions: []grpc.DialOption{grpc.WithBlock()},
|
2017-02-17 01:48:57 +03:00
|
|
|
RejectOldCluster: true,
|
|
|
|
}
|
|
|
|
cli, err := clientv3.New(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cli.Close()
|
|
|
|
}
|
2017-04-13 03:55:02 +03:00
|
|
|
|
|
|
|
// TestDialForeignEndpoint checks an endpoint that is not registered
|
|
|
|
// with the balancer can be dialed.
|
|
|
|
func TestDialForeignEndpoint(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
conn, err := clus.Client(0).Dial(clus.Client(1).Endpoints()[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// grpc can return a lazy connection that's not connected yet; confirm
|
|
|
|
// that it can communicate with the cluster.
|
2017-12-20 02:06:57 +03:00
|
|
|
kvc := clientv3.NewKVFromKVClient(pb.NewKVClient(conn), clus.Client(0))
|
2017-04-13 03:55:02 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if _, gerr := kvc.Get(ctx, "abc"); gerr != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 03:25:13 +03:00
|
|
|
|
2017-09-04 13:09:32 +03:00
|
|
|
// TestSetEndpointAndPut checks that a Put following a SetEndpoints
|
2017-08-01 03:25:13 +03:00
|
|
|
// to a working endpoint will always succeed.
|
|
|
|
func TestSetEndpointAndPut(t *testing.T) {
|
|
|
|
defer testutil.AfterTest(t)
|
|
|
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
|
|
|
|
defer clus.Terminate(t)
|
|
|
|
|
|
|
|
clus.Client(1).SetEndpoints(clus.Members[0].GRPCAddr())
|
|
|
|
_, err := clus.Client(1).Put(context.TODO(), "foo", "bar")
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "closing") {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|