-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
Add failover test cases #13737
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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