Browse Source

clientv3/integration: add basic lease test

Xiang Li 10 years ago
parent
commit
bef7887c0d
3 changed files with 78 additions and 0 deletions
  1. 0 0
      clientv3/integration/kv_test.go
  2. 75 0
      clientv3/integration/lease_test.go
  3. 3 0
      etcdserver/api/v3rpc/key.go

+ 0 - 0
clientv3/integration/client_test.go → clientv3/integration/kv_test.go


+ 75 - 0
clientv3/integration/lease_test.go

@@ -0,0 +1,75 @@
+// 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/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/clientv3"
+	"github.com/coreos/etcd/etcdserver/api/v3rpc"
+	"github.com/coreos/etcd/integration"
+	"github.com/coreos/etcd/lease"
+	"github.com/coreos/etcd/pkg/testutil"
+)
+
+func TestLeaseCreate(t *testing.T) {
+	defer testutil.AfterTest(t)
+
+	clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
+	defer clus.Terminate(t)
+
+	lapi := clientv3.NewLease(clus.RandClient())
+	defer lapi.Close()
+
+	kv := clientv3.NewKV(clus.RandClient())
+
+	resp, err := lapi.Create(context.Background(), 10)
+	if err != nil {
+		t.Errorf("failed to create lease %v", err)
+	}
+
+	_, err = kv.Put("foo", "bar", lease.LeaseID(resp.ID))
+	if err != nil {
+		t.Fatalf("failed to create key with lease %v", err)
+	}
+}
+
+func TestLeaseRevoke(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())
+
+	resp, err := lapi.Create(context.Background(), 10)
+	if err != nil {
+		t.Errorf("failed to create lease %v", err)
+	}
+
+	_, err = lapi.Revoke(context.Background(), lease.LeaseID(resp.ID))
+	if err != nil {
+		t.Errorf("failed to revoke lease", err)
+	}
+
+	_, err = kv.Put("foo", "bar", lease.LeaseID(resp.ID))
+	if err != v3rpc.ErrLeaseNotFound {
+		t.Fatalf("err = %v, want %v", err, v3rpc.ErrLeaseNotFound)
+	}
+}

+ 3 - 0
etcdserver/api/v3rpc/key.go

@@ -22,6 +22,7 @@ import (
 	"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/codes"
 	"github.com/coreos/etcd/etcdserver"
 	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
+	"github.com/coreos/etcd/lease"
 	"github.com/coreos/etcd/storage"
 )
 
@@ -213,6 +214,8 @@ func togRPCError(err error) error {
 		return ErrCompacted
 	case storage.ErrFutureRev:
 		return ErrFutureRev
+	case lease.ErrLeaseNotFound:
+		return ErrLeaseNotFound
 	// TODO: handle error from raft and timeout
 	default:
 		return grpc.Errorf(codes.Internal, err.Error())