Skip to content

Commit

Permalink
test: move all KV related RESTful APIs into a separate test file
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <wachao@vmware.com>
  • Loading branch information
ahrtr committed Aug 30, 2023
1 parent 2888a95 commit fb4dfba
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 109 deletions.
135 changes: 135 additions & 0 deletions tests/e2e/v3_curl_kv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2016 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"
"path"
"testing"

pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/pkg/v3/expect"
"go.etcd.io/etcd/tests/v3/framework/e2e"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
)

func TestV3CurlPutGetNoTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigNoTLS()))
}
}
func TestV3CurlPutGetAutoTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigAutoTLS()))
}
}
func TestV3CurlPutGetAllTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigTLS()))
}
}
func TestV3CurlPutGetPeerTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigPeerTLS()))
}
}
func TestV3CurlPutGetClientTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigClientTLS()))
}
}

func TestV3CurlTxn(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlTxn, withApiPrefix(p))
}
}

func testV3CurlPutGet(cx ctlCtx) {
var (
key = []byte("foo")
value = []byte("bar") // this will be automatically base64-encoded by Go

expectPut = `"revision":"`
expectGet = `"value":"`
)
putData, err := json.Marshal(&pb.PutRequest{
Key: key,
Value: value,
})
if err != nil {
cx.t.Fatal(err)
}
rangeData, err := json.Marshal(&pb.RangeRequest{
Key: key,
})
if err != nil {
cx.t.Fatal(err)
}

p := cx.apiPrefix

if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/put"), Value: string(putData), Expected: expect.ExpectedResponse{Value: expectPut}}); err != nil {
cx.t.Fatalf("failed testV3CurlPutGet put with curl using prefix (%s) (%v)", p, err)
}
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/range"), Value: string(rangeData), Expected: expect.ExpectedResponse{Value: expectGet}}); err != nil {
cx.t.Fatalf("failed testV3CurlPutGet get with curl using prefix (%s) (%v)", p, err)
}
if cx.cfg.Client.ConnectionType == e2e.ClientTLSAndNonTLS {
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/range"), Value: string(rangeData), Expected: expect.ExpectedResponse{Value: expectGet}, IsTLS: true}); err != nil {
cx.t.Fatalf("failed testV3CurlPutGet get with curl using prefix (%s) (%v)", p, err)
}
}
}

func testV3CurlTxn(cx ctlCtx) {
txn := &pb.TxnRequest{
Compare: []*pb.Compare{
{
Key: []byte("foo"),
Result: pb.Compare_EQUAL,
Target: pb.Compare_CREATE,
TargetUnion: &pb.Compare_CreateRevision{CreateRevision: 0},
},
},
Success: []*pb.RequestOp{
{
Request: &pb.RequestOp_RequestPut{
RequestPut: &pb.PutRequest{
Key: []byte("foo"),
Value: []byte("bar"),
},
},
},
},
}
m := &runtime.JSONPb{}
jsonDat, jerr := m.Marshal(txn)
if jerr != nil {
cx.t.Fatal(jerr)
}
expected := `"succeeded":true,"responses":[{"response_put":{"header":{"revision":"2"}}}]`
p := cx.apiPrefix
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/txn"), Value: string(jsonDat), Expected: expect.ExpectedResponse{Value: expected}}); err != nil {
cx.t.Fatalf("failed testV3CurlTxn txn with curl using prefix (%s) (%v)", p, err)
}

// was crashing etcd server
malformed := `{"compare":[{"result":0,"target":1,"key":"Zm9v","TargetUnion":null}],"success":[{"Request":{"RequestPut":{"key":"Zm9v","value":"YmFy"}}}]}`
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/txn"), Value: malformed, Expected: expect.ExpectedResponse{Value: "error"}}); err != nil {
cx.t.Fatalf("failed testV3CurlTxn put with curl using prefix (%s) (%v)", p, err)
}
}
110 changes: 1 addition & 109 deletions tests/e2e/v3_curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,16 @@ import (
"go.etcd.io/etcd/pkg/v3/expect"
epb "go.etcd.io/etcd/server/v3/etcdserver/api/v3election/v3electionpb"
"go.etcd.io/etcd/tests/v3/framework/e2e"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
)

var apiPrefix = []string{"/v3"}

func TestV3CurlPutGetNoTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigNoTLS()))
}
}
func TestV3CurlPutGetAutoTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigAutoTLS()))
}
}
func TestV3CurlPutGetAllTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigTLS()))
}
}
func TestV3CurlPutGetPeerTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigPeerTLS()))
}
}
func TestV3CurlPutGetClientTLS(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlPutGet, withApiPrefix(p), withCfg(*e2e.NewConfigClientTLS()))
}
}
func TestV3CurlWatch(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlWatch, withApiPrefix(p))
}
}
func TestV3CurlTxn(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlTxn, withApiPrefix(p))
}
}

func TestV3CurlAuth(t *testing.T) {
for _, p := range apiPrefix {
testCtl(t, testV3CurlAuth, withApiPrefix(p))
Expand All @@ -85,43 +54,6 @@ func TestV3CurlAuthClientTLSCertAuth(t *testing.T) {
}
}

func testV3CurlPutGet(cx ctlCtx) {
var (
key = []byte("foo")
value = []byte("bar") // this will be automatically base64-encoded by Go

expectPut = `"revision":"`
expectGet = `"value":"`
)
putData, err := json.Marshal(&pb.PutRequest{
Key: key,
Value: value,
})
if err != nil {
cx.t.Fatal(err)
}
rangeData, err := json.Marshal(&pb.RangeRequest{
Key: key,
})
if err != nil {
cx.t.Fatal(err)
}

p := cx.apiPrefix

if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/put"), Value: string(putData), Expected: expect.ExpectedResponse{Value: expectPut}}); err != nil {
cx.t.Fatalf("failed testV3CurlPutGet put with curl using prefix (%s) (%v)", p, err)
}
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/range"), Value: string(rangeData), Expected: expect.ExpectedResponse{Value: expectGet}}); err != nil {
cx.t.Fatalf("failed testV3CurlPutGet get with curl using prefix (%s) (%v)", p, err)
}
if cx.cfg.Client.ConnectionType == e2e.ClientTLSAndNonTLS {
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/range"), Value: string(rangeData), Expected: expect.ExpectedResponse{Value: expectGet}, IsTLS: true}); err != nil {
cx.t.Fatalf("failed testV3CurlPutGet get with curl using prefix (%s) (%v)", p, err)
}
}
}

func testV3CurlWatch(cx ctlCtx) {
// store "bar" into "foo"
putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
Expand All @@ -148,46 +80,6 @@ func testV3CurlWatch(cx ctlCtx) {
require.ErrorContains(cx.t, err, "unexpected exit code")
}

func testV3CurlTxn(cx ctlCtx) {
txn := &pb.TxnRequest{
Compare: []*pb.Compare{
{
Key: []byte("foo"),
Result: pb.Compare_EQUAL,
Target: pb.Compare_CREATE,
TargetUnion: &pb.Compare_CreateRevision{CreateRevision: 0},
},
},
Success: []*pb.RequestOp{
{
Request: &pb.RequestOp_RequestPut{
RequestPut: &pb.PutRequest{
Key: []byte("foo"),
Value: []byte("bar"),
},
},
},
},
}
m := &runtime.JSONPb{}
jsonDat, jerr := m.Marshal(txn)
if jerr != nil {
cx.t.Fatal(jerr)
}
expected := `"succeeded":true,"responses":[{"response_put":{"header":{"revision":"2"}}}]`
p := cx.apiPrefix
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/txn"), Value: string(jsonDat), Expected: expect.ExpectedResponse{Value: expected}}); err != nil {
cx.t.Fatalf("failed testV3CurlTxn txn with curl using prefix (%s) (%v)", p, err)
}

// was crashing etcd server
malformed := `{"compare":[{"result":0,"target":1,"key":"Zm9v","TargetUnion":null}],"success":[{"Request":{"RequestPut":{"key":"Zm9v","value":"YmFy"}}}]}`
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/txn"), Value: malformed, Expected: expect.ExpectedResponse{Value: "error"}}); err != nil {
cx.t.Fatalf("failed testV3CurlTxn put with curl using prefix (%s) (%v)", p, err)
}

}

func testV3CurlAuth(cx ctlCtx) {
p := cx.apiPrefix
usernames := []string{"root", "nonroot", "nooption"}
Expand Down

0 comments on commit fb4dfba

Please sign in to comment.