Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failover test cases #13737

Merged
merged 1 commit into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/framework/integration/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,14 @@ func (c *Cluster) Client(i int) *clientv3.Client {
return c.Members[i].Client
}

func (c *Cluster) Endpoints() []string {
var endpoints []string
for _, m := range c.Members {
endpoints = append(endpoints, m.GrpcURL)
}
return endpoints
}

func (c *Cluster) ClusterClient() (client *clientv3.Client, err error) {
if c.clusterClient == nil {
endpoints := []string{}
Expand Down
173 changes: 173 additions & 0 deletions tests/integration/v3_failover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// 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 integration

import (
"bytes"
"context"
"crypto/tls"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
clientv3test "go.etcd.io/etcd/tests/v3/integration/clientv3"
"testing"
"time"

"go.etcd.io/etcd/client/v3"
integration2 "go.etcd.io/etcd/tests/v3/framework/integration"
"google.golang.org/grpc"
)

func TestFailover(t *testing.T) {
cases := []struct {
name string
testFunc func(*testing.T, *tls.Config, *integration2.Cluster) (*clientv3.Client, error)
}{
{
name: "create client before the first server down",
testFunc: createClientBeforeServerDown,
},
{
name: "create client after the first server down",
testFunc: createClientAfterServerDown,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Logf("Starting test [%s]", tc.name)
integration2.BeforeTest(t)

// Launch an etcd cluster with 3 members
t.Logf("Launching an etcd cluster with 3 members [%s]", tc.name)
clus := integration2.NewCluster(t, &integration2.ClusterConfig{Size: 3, ClientTLS: &integration2.TestTLSInfo})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this TLS config match what is expected in production w.r.t. validity of serving certificates and distinct hostnames (or is it using localhost / identical loopback IPs for all members)? I have memories of the etcd client not handling TLS validation on failover correctly (e.g. #10911) and wanted to make sure we're actually exercising the scenario

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good point, thanks for pointing out this.

It's using localhost & 127.0.0.1 (see below). The e2e & integration tests are supposed to be automatically executed in pipeline, so all etcd members are always running on the same host?

Probably we need to think about how to run & test etcd in an environment which is close to production? cc @ptabor @serathius @spzala

        X509v3 extensions:
            X509v3 Key Usage: critical
                Digital Signature, Key Encipherment
            X509v3 Extended Key Usage: 
                TLS Web Server Authentication, TLS Web Client Authentication
            X509v3 Basic Constraints: critical
                CA:FALSE
            X509v3 Subject Key Identifier: 
                F4:B1:45:B9:C4:18:A5:74:3B:5A:C9:21:10:60:4F:6F:5F:EF:12:64
            X509v3 Authority Key Identifier: 
                keyid:29:26:2B:F4:ED:D9:18:F5:60:74:CE:00:7A:C0:E6:FF:1C:C8:BC:A0

            X509v3 Subject Alternative Name: 
                DNS:localhost, IP Address:127.0.0.1

defer clus.Terminate(t)

cc, err := integration2.TestTLSInfo.ClientConfig()
if err != nil {
t.Fatal(err)
}
// Create an etcd client before or after first server down
t.Logf("Creating an etcd client [%s]", tc.name)
cli, err := tc.testFunc(t, cc, clus)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
defer cli.Close()

// Sanity test
t.Logf("Running sanity test [%s]", tc.name)
key, val := "key1", "val1"
putWithRetries(t, cli, key, val, 10)
getWithRetries(t, cli, key, val, 10)

t.Logf("Test done [%s]", tc.name)
})
}
}

func createClientBeforeServerDown(t *testing.T, cc *tls.Config, clus *integration2.Cluster) (*clientv3.Client, error) {
cli, err := createClient(t, cc, clus)
if err != nil {
return nil, err
}
clus.Members[0].Close()
return cli, nil
}

func createClientAfterServerDown(t *testing.T, cc *tls.Config, clus *integration2.Cluster) (*clientv3.Client, error) {
clus.Members[0].Close()
return createClient(t, cc, clus)
}

func createClient(t *testing.T, cc *tls.Config, clus *integration2.Cluster) (*clientv3.Client, error) {
cli, err := integration2.NewClient(t, clientv3.Config{
Endpoints: clus.Endpoints(),
DialTimeout: 5 * time.Second,
DialOptions: []grpc.DialOption{grpc.WithBlock()},
TLS: cc,
})
if err != nil {
return nil, err
}

return cli, nil
}

func putWithRetries(t *testing.T, cli *clientv3.Client, key, val string, retryCount int) {
for retryCount > 0 {
// put data test
err := func() error {
t.Log("Sanity test, putting data")
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

if _, putErr := cli.Put(ctx, key, val); putErr != nil {
t.Logf("Failed to put data (%v)", putErr)
return putErr
}
return nil
}()

if err != nil {
retryCount--
if shouldRetry(err) {
continue
} else {
t.Fatal(err)
}
}
break
}
}

func getWithRetries(t *testing.T, cli *clientv3.Client, key, val string, retryCount int) {
for retryCount > 0 {
// get data test
err := func() error {
t.Log("Sanity test, getting data")
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
resp, getErr := cli.Get(ctx, key)
if getErr != nil {
t.Logf("Failed to get key (%v)", getErr)
return getErr
}
if len(resp.Kvs) != 1 {
t.Fatalf("Expected 1 key, got %d", len(resp.Kvs))
}
if !bytes.Equal([]byte(val), resp.Kvs[0].Value) {
t.Fatalf("Unexpected value, expected: %s, got: %s", val, string(resp.Kvs[0].Value))
}
return nil
}()

if err != nil {
retryCount--
if shouldRetry(err) {
continue
} else {
t.Fatal(err)
}
}
break
}
}

func shouldRetry(err error) bool {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) ||
err == rpctypes.ErrTimeout || err == rpctypes.ErrTimeoutDueToLeaderFail {
return true
}
return false
}
14 changes: 14 additions & 0 deletions tests/integration/v3_kv_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// 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 integration

import (
Expand Down