From b4f0a8853bddd9530a81b0ccaa658f82413cd8b9 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Mon, 20 Jun 2016 14:25:26 -0700 Subject: [PATCH] e2e: grpc-gateway cURL tests --- e2e/v2_curl_test.go | 8 +++-- e2e/v3_curl_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 e2e/v3_curl_test.go diff --git a/e2e/v2_curl_test.go b/e2e/v2_curl_test.go index 173fa2979..ee536c3de 100644 --- a/e2e/v2_curl_test.go +++ b/e2e/v2_curl_test.go @@ -153,16 +153,20 @@ func cURLPrefixArgs(clus *etcdProcessCluster, method string, req cURLReq) []stri } switch method { - case "PUT": + case "POST", "PUT": dt := req.value if !strings.HasPrefix(dt, "{") { // for non-JSON value dt = "value=" + dt } - cmdArgs = append(cmdArgs, "-XPUT", "-d", dt) + cmdArgs = append(cmdArgs, "-X", method, "-d", dt) } return cmdArgs } +func cURLPost(clus *etcdProcessCluster, req cURLReq) error { + return spawnWithExpect(cURLPrefixArgs(clus, "POST", req), req.expected) +} + func cURLPut(clus *etcdProcessCluster, req cURLReq) error { return spawnWithExpect(cURLPrefixArgs(clus, "PUT", req), req.expected) } diff --git a/e2e/v3_curl_test.go b/e2e/v3_curl_test.go new file mode 100644 index 000000000..f3dee62d4 --- /dev/null +++ b/e2e/v3_curl_test.go @@ -0,0 +1,76 @@ +// 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" + "testing" + + "github.com/coreos/etcd/etcdserver/etcdserverpb" + "github.com/coreos/etcd/pkg/testutil" +) + +func TestV3CurlPutGetNoTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configNoTLS) } +func TestV3CurlPutGetAutoTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configAutoTLS) } +func TestV3CurlPutGetAllTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configTLS) } +func TestV3CurlPutGetPeerTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configPeerTLS) } +func TestV3CurlPutGetClientTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configClientTLS) } +func testCurlPutGetGRPCGateway(t *testing.T, cfg *etcdProcessClusterConfig) { + defer testutil.AfterTest(t) + + epc, err := newEtcdProcessCluster(cfg) + if err != nil { + t.Fatalf("could not start etcd process cluster (%v)", err) + } + defer func() { + if cerr := epc.Close(); err != nil { + t.Fatalf("error closing etcd processes (%v)", cerr) + } + }() + + var ( + key = []byte("foo") + value = []byte("bar") // this will be automatically base64-encoded by Go + + expectPut = `"revision":"` + expectGet = `"value":"` + ) + putData, err := json.Marshal(&etcdserverpb.PutRequest{ + Key: key, + Value: value, + }) + if err != nil { + t.Fatal(err) + } + rangeData, err := json.Marshal(&etcdserverpb.RangeRequest{ + Key: key, + }) + if err != nil { + t.Fatal(err) + } + + if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/put", value: string(putData), expected: expectPut}); err != nil { + t.Fatalf("failed put with curl (%v)", err) + } + if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/range", value: string(rangeData), expected: expectGet}); err != nil { + t.Fatalf("failed get with curl (%v)", err) + } + + if cfg.clientTLS == clientTLSAndNonTLS { + if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/range", value: string(rangeData), expected: expectGet, isTLS: true}); err != nil { + t.Fatalf("failed get with curl (%v)", err) + } + } +}