From f148f4b2b952db4d733223df075576fdb8fa9281 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Thu, 28 Apr 2016 12:18:24 -0700 Subject: [PATCH] clientv3/integration: tests error types (rpctypes) --- clientv3/integration/auth_test.go | 44 ++++++++++++++++++++ clientv3/integration/kv_test.go | 67 ++++++++++++++++++++++++++++++ clientv3/integration/lease_test.go | 17 ++++++++ clientv3/integration/role_test.go | 44 ++++++++++++++++++++ clientv3/integration/txn_test.go | 27 ++++++++++++ clientv3/integration/user_test.go | 54 ++++++++++++++++++++++++ 6 files changed, 253 insertions(+) create mode 100644 clientv3/integration/auth_test.go create mode 100644 clientv3/integration/role_test.go create mode 100644 clientv3/integration/user_test.go diff --git a/clientv3/integration/auth_test.go b/clientv3/integration/auth_test.go new file mode 100644 index 000000000..9b8c2baf9 --- /dev/null +++ b/clientv3/integration/auth_test.go @@ -0,0 +1,44 @@ +// Copyright 2016 CoreOS, Inc. +// +// 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 ( + "testing" + + "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" + "github.com/coreos/etcd/integration" + "github.com/coreos/etcd/pkg/testutil" + "golang.org/x/net/context" +) + +func TestAuthError(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) + defer clus.Terminate(t) + + authapi := clientv3.NewAuth(clus.RandClient()) + + _, err := authapi.UserAdd(context.TODO(), "foo", "bar") + if err != nil { + t.Fatal(err) + } + + _, err = authapi.Authenticate(context.TODO(), "foo", "bar111") + if err != rpctypes.ErrAuthFailed { + t.Fatalf("expected %v, got %v", rpctypes.ErrAuthFailed, err) + } +} diff --git a/clientv3/integration/kv_test.go b/clientv3/integration/kv_test.go index 006623cf6..99ccdff83 100644 --- a/clientv3/integration/kv_test.go +++ b/clientv3/integration/kv_test.go @@ -17,6 +17,7 @@ package integration import ( "bytes" "reflect" + "strings" "testing" "time" @@ -28,6 +29,42 @@ import ( "golang.org/x/net/context" ) +func TestKVPutError(t *testing.T) { + defer testutil.AfterTest(t) + + var ( + maxReqBytes = 1.5 * 1024 * 1024 + quota = int64(maxReqBytes * 1.2) + ) + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, QuotaBackendBytes: quota}) + defer clus.Terminate(t) + + kv := clientv3.NewKV(clus.RandClient()) + ctx := context.TODO() + + _, err := kv.Put(ctx, "", "bar") + if err != rpctypes.ErrEmptyKey { + t.Fatalf("expected %v, got %v", rpctypes.ErrEmptyKey, err) + } + + _, err = kv.Put(ctx, "key", strings.Repeat("a", int(maxReqBytes+100))) // 1.5MB + if err != rpctypes.ErrRequestTooLarge { + t.Fatalf("expected %v, got %v", rpctypes.ErrRequestTooLarge, err) + } + + _, err = kv.Put(ctx, "foo1", strings.Repeat("a", int(maxReqBytes-50))) + if err != nil { // below quota + t.Fatal(err) + } + + time.Sleep(500 * time.Millisecond) // give enough time for commit + + _, err = kv.Put(ctx, "foo2", strings.Repeat("a", int(maxReqBytes-50))) + if err != rpctypes.ErrNoSpace { // over quota + t.Fatalf("expected %v, got %v", rpctypes.ErrNoSpace, err) + } +} + func TestKVPut(t *testing.T) { defer testutil.AfterTest(t) @@ -323,6 +360,36 @@ func TestKVDelete(t *testing.T) { } } +func TestKVCompactError(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) + defer clus.Terminate(t) + + kv := clientv3.NewKV(clus.RandClient()) + ctx := context.TODO() + + for i := 0; i < 5; i++ { + if _, err := kv.Put(ctx, "foo", "bar"); err != nil { + t.Fatalf("couldn't put 'foo' (%v)", err) + } + } + err := kv.Compact(ctx, 6) + if err != nil { + t.Fatalf("couldn't compact 6 (%v)", err) + } + + err = kv.Compact(ctx, 6) + if err != rpctypes.ErrCompacted { + t.Fatalf("expected %v, got %v", rpctypes.ErrCompacted, err) + } + + err = kv.Compact(ctx, 100) + if err != rpctypes.ErrFutureRev { + t.Fatalf("expected %v, got %v", rpctypes.ErrFutureRev, err) + } +} + func TestKVCompact(t *testing.T) { defer testutil.AfterTest(t) diff --git a/clientv3/integration/lease_test.go b/clientv3/integration/lease_test.go index bf32607c3..95addc65b 100644 --- a/clientv3/integration/lease_test.go +++ b/clientv3/integration/lease_test.go @@ -27,6 +27,23 @@ import ( "google.golang.org/grpc/codes" ) +func TestLeastNotFoundError(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) + defer clus.Terminate(t) + + lapi := clientv3.NewLease(clus.RandClient()) + defer lapi.Close() + + kv := clientv3.NewKV(clus.RandClient()) + + _, err := kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(500))) + if err != rpctypes.ErrLeaseNotFound { + t.Fatalf("expected %v, got %v", rpctypes.ErrLeaseNotFound, err) + } +} + func TestLeaseGrant(t *testing.T) { defer testutil.AfterTest(t) diff --git a/clientv3/integration/role_test.go b/clientv3/integration/role_test.go new file mode 100644 index 000000000..afdd7e215 --- /dev/null +++ b/clientv3/integration/role_test.go @@ -0,0 +1,44 @@ +// Copyright 2016 CoreOS, Inc. +// +// 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 ( + "testing" + + "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" + "github.com/coreos/etcd/integration" + "github.com/coreos/etcd/pkg/testutil" + "golang.org/x/net/context" +) + +func TestRoleError(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) + defer clus.Terminate(t) + + authapi := clientv3.NewAuth(clus.RandClient()) + + _, err := authapi.RoleAdd(context.TODO(), "test-role") + if err != nil { + t.Fatal(err) + } + + _, err = authapi.RoleAdd(context.TODO(), "test-role") + if err != rpctypes.ErrRoleAlreadyExist { + t.Fatalf("expected %v, got %v", rpctypes.ErrRoleAlreadyExist, err) + } +} diff --git a/clientv3/integration/txn_test.go b/clientv3/integration/txn_test.go index a7484e3b8..5c0c5b6a8 100644 --- a/clientv3/integration/txn_test.go +++ b/clientv3/integration/txn_test.go @@ -15,15 +15,42 @@ package integration import ( + "fmt" "testing" "time" "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/etcdserver/api/v3rpc" + "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" "github.com/coreos/etcd/integration" "github.com/coreos/etcd/pkg/testutil" "golang.org/x/net/context" ) +func TestTxnError(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) + defer clus.Terminate(t) + + kv := clientv3.NewKV(clus.RandClient()) + ctx := context.TODO() + + _, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar1"), clientv3.OpPut("foo", "bar2")).Commit() + if err != rpctypes.ErrDuplicateKey { + t.Fatalf("expected %v, got %v", rpctypes.ErrDuplicateKey, err) + } + + ops := make([]clientv3.Op, v3rpc.MaxOpsPerTxn+10) + for i := range ops { + ops[i] = clientv3.OpPut(fmt.Sprintf("foo%d", i), "") + } + _, err = kv.Txn(ctx).Then(ops...).Commit() + if err != rpctypes.ErrTooManyOps { + t.Fatalf("expected %v, got %v", rpctypes.ErrTooManyOps, err) + } +} + func TestTxnWriteFail(t *testing.T) { defer testutil.AfterTest(t) diff --git a/clientv3/integration/user_test.go b/clientv3/integration/user_test.go new file mode 100644 index 000000000..cc865ad73 --- /dev/null +++ b/clientv3/integration/user_test.go @@ -0,0 +1,54 @@ +// Copyright 2016 CoreOS, Inc. +// +// 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 ( + "testing" + + "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" + "github.com/coreos/etcd/integration" + "github.com/coreos/etcd/pkg/testutil" + "golang.org/x/net/context" +) + +func TestUserError(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) + defer clus.Terminate(t) + + authapi := clientv3.NewAuth(clus.RandClient()) + + _, err := authapi.UserAdd(context.TODO(), "foo", "bar") + if err != nil { + t.Fatal(err) + } + + _, err = authapi.UserAdd(context.TODO(), "foo", "bar") + if err != rpctypes.ErrUserAlreadyExist { + t.Fatalf("expected %v, got %v", rpctypes.ErrUserAlreadyExist, err) + } + + _, err = authapi.UserDelete(context.TODO(), "not-exist-user") + if err != rpctypes.ErrUserNotFound { + t.Fatalf("expected %v, got %v", rpctypes.ErrUserNotFound, err) + } + + _, err = authapi.UserGrant(context.TODO(), "foo", "test-role-does-not-exist") + if err != rpctypes.ErrRoleNotFound { + t.Fatalf("expected %v, got %v", rpctypes.ErrRoleNotFound, err) + } +}