-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
184 additions
and
10 deletions.
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,138 @@ | ||
package e2e | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
"go.etcd.io/etcd/tests/v3/framework/config" | ||
"go.etcd.io/etcd/tests/v3/framework/e2e" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
const ( | ||
numKeys = 10 | ||
writeThreads = 10 | ||
|
||
rootUser = "root" | ||
rootPassword = "root" | ||
rootRole = "root" | ||
testUser = "test" | ||
testPassword = "testPassword" | ||
testRole = "testRole" | ||
) | ||
|
||
func TestAuthCluster(t *testing.T) { | ||
e2e.BeforeTest(t) | ||
cfg := e2e.NewConfigNoTLS() | ||
cfg.ClusterSize = 1 | ||
cfg.SnapshotCount = 2 | ||
cfg.UserName = rootUser | ||
cfg.Password = rootPassword | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
epc, err := e2e.NewEtcdProcessCluster(ctx, t, cfg) | ||
if err != nil { | ||
t.Fatalf("could not start etcd process cluster (%v)", err) | ||
} | ||
defer func() { | ||
if err := epc.Close(); err != nil { | ||
t.Fatalf("could not close test cluster (%v)", err) | ||
} | ||
}() | ||
|
||
epcClient := epc.Client() | ||
createUsers(ctx, t, epcClient) | ||
|
||
if _, err := epcClient.AuthEnable(ctx); err != nil { | ||
t.Fatalf("could not enable Auth: (%v)", err) | ||
} | ||
|
||
writeKeys(ctx, epc.Client().WithAuth(testUser, testPassword), "test") | ||
|
||
if err := epc.StartNewProc(ctx, t); err != nil { | ||
t.Fatalf("could not start second etcd process (%v)", err) | ||
} | ||
|
||
writeKeys(ctx, epc.Client().WithAuth(testUser, testPassword), "test_two_nodes") | ||
|
||
hashKvs, err := epc.Client().HashKV(ctx, numKeys*2) | ||
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 | ||
} | ||
if revision != v.Header.GetRevision() || hash != v.Hash { | ||
t.Errorf("Inconsistent revisions found:") | ||
for _, v := range hashKvs { | ||
t.Errorf("%+v", v) | ||
} | ||
t.Fail() | ||
} | ||
} | ||
} | ||
|
||
func createUsers(ctx context.Context, t *testing.T, client *e2e.EtcdctlV3) { | ||
if _, err := client.UserAdd(ctx, rootUser, rootPassword, config.UserAddOptions{}); err != nil { | ||
t.Fatalf("could not add root user (%v)", err) | ||
} | ||
if _, err := client.UserGrantRole(ctx, rootUser, rootRole); err != nil { | ||
t.Fatalf("could not grant root role to root user (%v)", err) | ||
} | ||
|
||
if _, err := client.RoleAdd(ctx, testRole); err != nil { | ||
t.Fatalf("could not create 'test' role (%v)", err) | ||
} | ||
if _, err := client.RoleGrantPermission(ctx, testRole, "/test/", "/test0", clientv3.PermissionType(clientv3.PermReadWrite)); err != nil { | ||
t.Fatalf("could not RoleGrantPermission (%v)", err) | ||
} | ||
if _, err := client.UserAdd(ctx, testUser, testPassword, config.UserAddOptions{}); err != nil { | ||
t.Fatalf("could not add user test (%v)", err) | ||
} | ||
if _, err := client.UserGrantRole(ctx, testUser, testRole); err != nil { | ||
t.Fatalf("could not grant test role user (%v)", err) | ||
} | ||
} | ||
|
||
func writeKeys(ctx context.Context, client *e2e.EtcdctlV3, value string) { | ||
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() | ||
} |
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