Skip to content

Commit

Permalink
simplify consistency check
Browse files Browse the repository at this point in the history
  • Loading branch information
veshij committed Oct 27, 2022
1 parent fcd3053 commit 1cb1d29
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
46 changes: 21 additions & 25 deletions tests/e2e/ctl_v3_auth_cluster_test.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/framework/e2e/etcdctl.go
Expand Up @@ -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
}
Expand Down

0 comments on commit 1cb1d29

Please sign in to comment.