Skip to content

Commit

Permalink
e2e: refactor of grpc-gateway tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexfusion committed Mar 19, 2018
1 parent c22afc2 commit f400153
Showing 1 changed file with 196 additions and 0 deletions.
196 changes: 196 additions & 0 deletions e2e/v3_curl_lease_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Copyright 2018 The etcd Authors
//
// 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 e2e

import (
"encoding/json"
"fmt"
"math/rand"
"path"
"testing"
"time"

pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
)

func TestV3CurlLeaseRevoke(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlLeaseRevoke, withApiPrefix(p))
}
}
func TestV3CurlLeaseTTL(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlLeaseTTL, withApiPrefix(p))
}
}

type v3cURLTest struct {
endpoint string
value interface{}
expected string
}

func testV3CurlLeaseRevoke(cx ctlCtx) {
leaseID := randomLeaseID()
leaseExp := fmt.Sprintf(`"lease":"%d"`, leaseID)

tests := []v3cURLTest{
{
endpoint: "/lease/grant",
value: gwLeaseWithID(cx, leaseID),
expected: gwLeaseIDExpected(leaseID),
},
{
endpoint: "/kv/put",
value: gwKVPutLease(cx, "foo", "bar", leaseID),
expected: `"revision":"`,
},
{
endpoint: "/kv/range",
value: gwKVRange(cx, "foo"),
expected: leaseExp,
},
{
endpoint: "/kv/lease/revoke",
value: gwLeaseRevoke(cx, leaseID),
expected: `"revision":"`,
},
{
endpoint: "/kv/range",
value: gwKVRange(cx, "foo"),
expected: `"revision":"`,
},
}

if err := cURLWithExpected(cx, tests); err != nil {
cx.t.Fatalf("testV3CurlLeaseRevoke: %v", err)
}
}

func testV3CurlLeaseTTL(cx ctlCtx) {
leaseID := randomLeaseID()

tests := []v3cURLTest{
{
endpoint: "/lease/grant",
value: gwLeaseWithID(cx, leaseID),
expected: gwLeaseIDExpected(leaseID),
},
{
endpoint: "/kv/put",
value: gwKVPutLease(cx, "foo", "bar", leaseID),
expected: `"revision":"`,
},
{
endpoint: "/kv/lease/timetolive",
value: gwLeaseTTLWithKeys(cx, leaseID),
expected: `"grantedTTL"`,
},
}
if err := cURLWithExpected(cx, tests); err != nil {
cx.t.Fatalf("testV3CurlLeaseTTL: %v", err)
}

}

func gwLeaseIDExpected(leaseID int64) string {
return fmt.Sprintf(`"ID":"%d"`, leaseID)
}

func gwLeaseTTLWithKeys(cx ctlCtx, leaseID int64) string {
d := &pb.LeaseTimeToLiveRequest{ID: leaseID, Keys: true}
s, err := pbMarshal(d)
if err != nil {
cx.t.Fatalf("leaseTTLWithKeys: error (%v)", err)
}
return s
}

func gwLeaseTTL(cx ctlCtx, leaseID int64) string {
d := &pb.LeaseTimeToLiveRequest{ID: leaseID}
s, err := pbMarshal(d)
if err != nil {
cx.t.Fatalf("leaseTTL: Marshal error (%v)", err)
}
return s
}

func gwLeaseWithID(cx ctlCtx, leaseID int64) string {
d := &pb.LeaseGrantRequest{ID: leaseID, TTL: 20}
s, err := pbMarshal(d)
if err != nil {
cx.t.Fatalf("leaseWithID: Marshal error (%v)", err)
}
return s
}

func gwLeaseNoID(cx ctlCtx) string {
d := &pb.LeaseGrantRequest{ID: 0, TTL: 20}
s, err := pbMarshal(d)
if err != nil {
cx.t.Fatalf("leaseNoID: Marshal error (%v)", err)
}
return s
}

func gwLeaseRevoke(cx ctlCtx, leaseID int64) string {
d := &pb.LeaseRevokeRequest{ID: leaseID}
s, err := pbMarshal(d)
if err != nil {
cx.t.Fatalf("gwLeaseRevoke: Marshal error (%v)", err)
}
return s
}

func gwKVPutLease(cx ctlCtx, k string, v string, leaseID int64) string {
d := pb.PutRequest{Key: []byte(k), Value: []byte(v), Lease: leaseID}
s, err := pbMarshal(d)
if err != nil {
cx.t.Fatalf("putLease: Marshal error (%v)", err)
}
return s
}

func gwKVRange(cx ctlCtx, k string) string {
d := &pb.RangeRequest{Key: []byte(k)}
s, err := pbMarshal(d)
if err != nil {
cx.t.Fatalf("kvRange: Marshal error (%v)", err)
}
return s
}

func pbMarshal(data interface{}) (pbm string, e error) {
m, err := json.Marshal(data)
if err != nil {
return "", err
}
return string(m), nil
}

func cURLWithExpected(cx ctlCtx, tests []v3cURLTest) error {
p := cx.apiPrefix
for _, t := range tests {
value := fmt.Sprintf("%v", t.value)
if err := cURLPost(cx.epc, cURLReq{endpoint: path.Join(p, t.endpoint), value: value, expected: t.expected}); err != nil {
return fmt.Errorf("prefix (%s) endpoint (%s): error (%v), wanted %v", p, t.endpoint, err, t.expected)
}
}
return nil
}

func randomLeaseID() int64 {
return rand.New(rand.NewSource(time.Now().UnixNano())).Int63()
}

0 comments on commit f400153

Please sign in to comment.