diff --git a/tests/e2e/ctl_v3_auth_cluster_test.go b/tests/e2e/ctl_v3_auth_cluster_test.go index 0e483578961f..979a93b33eb0 100644 --- a/tests/e2e/ctl_v3_auth_cluster_test.go +++ b/tests/e2e/ctl_v3_auth_cluster_test.go @@ -4,6 +4,9 @@ import ( "context" "fmt" "testing" + "time" + + "github.com/stretchr/testify/assert" clientv3 "go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/tests/v3/framework/config" @@ -37,12 +40,11 @@ func TestAuthCluster(t *testing.T) { t.Fatalf("could not enable Auth: (%v)", err) } - numKeys := 10 testUserClientOpts := e2e.WithAuth("test", "testPassword") rootUserClientOpts := e2e.WithAuth("root", "rootPassword") // write more than SnapshotCount keys to single leader to make sure snapshot is created - for i := 0; i <= numKeys; i++ { + for i := 0; i <= 10; i++ { if err := epc.Client(testUserClientOpts).Put(ctx, fmt.Sprintf("/test/%d", i), "test", config.PutOptions{}); err != nil { t.Fatalf("failed to Put (%v)", err) } @@ -54,37 +56,31 @@ func TestAuthCluster(t *testing.T) { } // make sure writes to both endpoints are successful + endpoints := epc.EndpointsV3() + assert.Equal(t, len(endpoints), 2) for _, endpoint := range epc.EndpointsV3() { if err := epc.Client(testUserClientOpts, e2e.WithEndpoints([]string{endpoint})).Put(ctx, "/test/key", endpoint, config.PutOptions{}); err != nil { t.Fatalf("failed to write to Put to %q (%v)", endpoint, err) } - numKeys++ - } - - // verify cluster state is consistent - hashKvs, err := epc.Client(rootUserClientOpts).HashKV(ctx, int64(numKeys)) - if err != nil { - t.Fatalf("could not HashKV (%v)", err) } - revisionSet := false - var revision int64 - var hash uint32 - for _, v := range hashKvs { - if !revisionSet { - revision = v.Header.GetRevision() - hash = v.Hash - revisionSet = true - continue + // verify all nodes have exact same revision and hash + assert.Eventually(t, func() bool { + hashKvs, err := epc.Client(rootUserClientOpts).HashKV(ctx, 0) + if err != nil { + t.Errorf("failed to get HashKV: %v", err) + return false } - if revision != v.Header.GetRevision() || hash != v.Hash { - t.Errorf("Inconsistent revisions found:") - for _, v := range hashKvs { - t.Errorf("%+v", v) - } - t.Fail() + + assert.Equal(t, 2, len(hashKvs)) + if hashKvs[0].Header.Revision != hashKvs[1].Header.Revision { + t.Errorf("The two members' revision (%d, %d) are not equal", hashKvs[0].Header.Revision, hashKvs[1].Header.Revision) + return false } - } + assert.Equal(t, hashKvs[0].Hash, hashKvs[1].Hash) + return true + }, time.Second*5, time.Millisecond*100) + } func createUsers(ctx context.Context, t *testing.T, client *e2e.EtcdctlV3) { diff --git a/tests/framework/e2e/etcdctl.go b/tests/framework/e2e/etcdctl.go index 76e67ad937e9..7addd9e23272 100644 --- a/tests/framework/e2e/etcdctl.go +++ b/tests/framework/e2e/etcdctl.go @@ -363,7 +363,7 @@ func (ctl *EtcdctlV3) HashKV(ctx context.Context, rev int64) ([]*clientv3.HashKV Endpoint string HashKV *clientv3.HashKVResponse } - err := ctl.spawnJsonCmd(ctx, &epHashKVs, "endpoint", "hashkv", "--endpoints", strings.Join(ctl.endpoints, ","), "--rev", fmt.Sprint(rev)) + err := ctl.spawnJsonCmd(ctx, &epHashKVs, "endpoint", "hashkv", "--rev", fmt.Sprint(rev)) if err != nil { return nil, err }