Skip to content

Commit

Permalink
simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
veshij committed Oct 27, 2022
1 parent 03a9bc9 commit fcd3053
Showing 1 changed file with 17 additions and 38 deletions.
55 changes: 17 additions & 38 deletions tests/e2e/ctl_v3_auth_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package e2e
import (
"context"
"fmt"
"sync"
"testing"

clientv3 "go.etcd.io/etcd/client/v3"
Expand Down Expand Up @@ -41,15 +40,29 @@ func TestAuthCluster(t *testing.T) {
numKeys := 10
testUserClientOpts := e2e.WithAuth("test", "testPassword")
rootUserClientOpts := e2e.WithAuth("root", "rootPassword")
writeKeys(ctx, epc.Client(testUserClientOpts), numKeys, "test")

// write more than SnapshotCount keys to single leader to make sure snapshot is created
for i := 0; i <= numKeys; 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)
}
}

// start second process
if err := epc.StartNewProc(ctx, t, rootUserClientOpts); err != nil {
t.Fatalf("could not start second etcd process (%v)", err)
}

writeKeys(ctx, epc.Client(testUserClientOpts), numKeys, "test_two_nodes")
// make sure writes to both endpoints are successful
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++
}

hashKvs, err := epc.Client(rootUserClientOpts).HashKV(ctx, int64(numKeys*2))
// verify cluster state is consistent
hashKvs, err := epc.Client(rootUserClientOpts).HashKV(ctx, int64(numKeys))
if err != nil {
t.Fatalf("could not HashKV (%v)", err)
}
Expand Down Expand Up @@ -98,37 +111,3 @@ func createUsers(ctx context.Context, t *testing.T, client *e2e.EtcdctlV3) {
t.Fatalf("could not grant test role user (%v)", err)
}
}

func writeKeys(ctx context.Context, client *e2e.EtcdctlV3, numKeys int, value string) {
writeThreads := 10

type kv struct {
key string
value string
}
q := make(chan kv)

wg := sync.WaitGroup{}
for i := 0; i <= writeThreads; i++ {
wg.Add(1)
go func() {
for {
item, ok := <-q
if !ok {
break
}
if err := client.Put(ctx, item.key, item.value, config.PutOptions{}); err != nil {
panic(err)
}
}
wg.Done()
}()
}

for i := 0; i <= numKeys; i++ {
q <- kv{key: fmt.Sprintf("/test/%d", i), value: value}
}
close(q)

wg.Wait()
}

0 comments on commit fcd3053

Please sign in to comment.