From 02c01f04d78f42d5a48e274dddb2eb99deb409a1 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Wed, 30 Aug 2023 16:17:07 +0100 Subject: [PATCH] test: move all KV related RESTful APIs into a separate test file Signed-off-by: Benjamin Wang --- tests/e2e/v3_curl_kv_test.go | 135 +++++++++++++++++++++++++++++++++++ tests/e2e/v3_curl_test.go | 110 +--------------------------- 2 files changed, 136 insertions(+), 109 deletions(-) create mode 100644 tests/e2e/v3_curl_kv_test.go diff --git a/tests/e2e/v3_curl_kv_test.go b/tests/e2e/v3_curl_kv_test.go new file mode 100644 index 00000000000..543f27351a3 --- /dev/null +++ b/tests/e2e/v3_curl_kv_test.go @@ -0,0 +1,135 @@ +// Copyright 2023 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) + } +} diff --git a/tests/e2e/v3_curl_test.go b/tests/e2e/v3_curl_test.go index 59b0b016550..1aa39e5082e 100644 --- a/tests/e2e/v3_curl_test.go +++ b/tests/e2e/v3_curl_test.go @@ -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)) @@ -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")}) @@ -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"}