-
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.
e2e: refactor of grpc-gateway tests.
- Loading branch information
Showing
1 changed file
with
196 additions
and
0 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
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(leaseID), | ||
expected: gwLeaseIDExpected(leaseID), | ||
}, | ||
{ | ||
endpoint: "/kv/put", | ||
value: gwKVPutLease("foo", "bar", leaseID), | ||
expected: `"revision":"`, | ||
}, | ||
{ | ||
endpoint: "/kv/range", | ||
value: gwKVRange("foo"), | ||
expected: leaseExp, | ||
}, | ||
{ | ||
endpoint: "/kv/lease/revoke", | ||
value: gwLeaseRevoke(leaseID), | ||
expected: `"revision":"`, | ||
}, | ||
{ | ||
endpoint: "/kv/range", | ||
value: gwKVRange("foo"), | ||
expected: `"revision":"`, | ||
}, | ||
} | ||
|
||
if err := cURLWithExpected(cx, tests); err != nil { | ||
cx.t.Errorf("testV3CurlLeaseRevoke: %v", err) | ||
} | ||
} | ||
|
||
func testV3CurlLeaseTTL(cx ctlCtx) { | ||
leaseID := randomLeaseID() | ||
|
||
tests := []v3cURLTest{ | ||
{ | ||
endpoint: "/lease/grant", | ||
value: gwLeaseWithID(leaseID), | ||
expected: gwLeaseIDExpected(leaseID), | ||
}, | ||
{ | ||
endpoint: "/kv/put", | ||
value: gwKVPutLease("foo", "bar", leaseID), | ||
expected: `"revision":"`, | ||
}, | ||
{ | ||
endpoint: "/kv/lease/timetolive", | ||
value: gwLeaseTTLWithKeys(leaseID), | ||
expected: `"grantedTTL"`, | ||
}, | ||
} | ||
if err := cURLWithExpected(cx, tests); err != nil { | ||
cx.t.Errorf("testV3CurlLeaseTTL: %v", err) | ||
} | ||
|
||
} | ||
|
||
func gwLeaseIDExpected(leaseID int64) string { | ||
return fmt.Sprintf(`"ID":"%d"`, leaseID) | ||
} | ||
|
||
func gwLeaseTTLWithKeys(leaseID int64) string { | ||
d := &pb.LeaseTimeToLiveRequest{ID: leaseID, Keys: true} | ||
s, err := pbMarshal(d) | ||
if err != nil { | ||
fmt.Errorf("leaseTTLWithKeys: error (%v)", err) | ||
} | ||
return s | ||
} | ||
|
||
func gwLeaseTTL(leaseID int64) string { | ||
d := &pb.LeaseTimeToLiveRequest{ID: leaseID} | ||
s, err := pbMarshal(d) | ||
if err != nil { | ||
fmt.Errorf("leaseTTL: Marshal error (%v)", err) | ||
} | ||
return s | ||
} | ||
|
||
func gwLeaseWithID(leaseID int64) string { | ||
d := &pb.LeaseGrantRequest{ID: leaseID, TTL: 20} | ||
s, err := pbMarshal(d) | ||
if err != nil { | ||
fmt.Errorf("leaseWithID: Marshal error (%v)", err) | ||
} | ||
return s | ||
} | ||
|
||
func gwLeaseNoID() string { | ||
d := &pb.LeaseGrantRequest{ID: 0, TTL: 20} | ||
s, err := pbMarshal(d) | ||
if err != nil { | ||
fmt.Errorf("leaseNoID: Marshal error (%v)", err) | ||
} | ||
return s | ||
} | ||
|
||
func gwLeaseRevoke(leaseID int64) string { | ||
d := &pb.LeaseRevokeRequest{ID: leaseID} | ||
s, err := pbMarshal(d) | ||
if err != nil { | ||
fmt.Errorf("gwLeaseRevoke: Marshal error (%v)", err) | ||
} | ||
return s | ||
} | ||
|
||
func gwKVPutLease(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 { | ||
fmt.Errorf("putLease: Marshal error (%v)", err) | ||
} | ||
return s | ||
} | ||
|
||
func gwKVRange(k string) string { | ||
d := &pb.RangeRequest{Key: []byte(k)} | ||
s, err := pbMarshal(d) | ||
if err != nil { | ||
fmt.Errorf("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() | ||
} |