v3_curl_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package e2e
  15. import (
  16. "encoding/json"
  17. "testing"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "github.com/coreos/etcd/pkg/testutil"
  20. )
  21. func TestV3CurlPutGetNoTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configNoTLS) }
  22. func TestV3CurlPutGetAutoTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configAutoTLS) }
  23. func TestV3CurlPutGetAllTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configTLS) }
  24. func TestV3CurlPutGetPeerTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configPeerTLS) }
  25. func TestV3CurlPutGetClientTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configClientTLS) }
  26. func testCurlPutGetGRPCGateway(t *testing.T, cfg *etcdProcessClusterConfig) {
  27. defer testutil.AfterTest(t)
  28. epc, err := newEtcdProcessCluster(cfg)
  29. if err != nil {
  30. t.Fatalf("could not start etcd process cluster (%v)", err)
  31. }
  32. defer func() {
  33. if cerr := epc.Close(); err != nil {
  34. t.Fatalf("error closing etcd processes (%v)", cerr)
  35. }
  36. }()
  37. var (
  38. key = []byte("foo")
  39. value = []byte("bar") // this will be automatically base64-encoded by Go
  40. expectPut = `"revision":"`
  41. expectGet = `"value":"`
  42. )
  43. putData, err := json.Marshal(&pb.PutRequest{
  44. Key: key,
  45. Value: value,
  46. })
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. rangeData, err := json.Marshal(&pb.RangeRequest{
  51. Key: key,
  52. })
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/put", value: string(putData), expected: expectPut}); err != nil {
  57. t.Fatalf("failed put with curl (%v)", err)
  58. }
  59. if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/range", value: string(rangeData), expected: expectGet}); err != nil {
  60. t.Fatalf("failed get with curl (%v)", err)
  61. }
  62. if cfg.clientTLS == clientTLSAndNonTLS {
  63. if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/range", value: string(rangeData), expected: expectGet, isTLS: true}); err != nil {
  64. t.Fatalf("failed get with curl (%v)", err)
  65. }
  66. }
  67. }
  68. func TestV3CurlWatch(t *testing.T) {
  69. defer testutil.AfterTest(t)
  70. epc, err := newEtcdProcessCluster(&configNoTLS)
  71. if err != nil {
  72. t.Fatalf("could not start etcd process cluster (%v)", err)
  73. }
  74. defer func() {
  75. if cerr := epc.Close(); err != nil {
  76. t.Fatalf("error closing etcd processes (%v)", cerr)
  77. }
  78. }()
  79. // store "bar" into "foo"
  80. putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if err = cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/put", value: string(putreq), expected: "revision"}); err != nil {
  85. t.Fatalf("failed put with curl (%v)", err)
  86. }
  87. // watch for first update to "foo"
  88. wcr := &pb.WatchCreateRequest{Key: []byte("foo"), StartRevision: 1}
  89. wreq, err := json.Marshal(wcr)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. // marshaling the grpc to json gives:
  94. // "{"RequestUnion":{"CreateRequest":{"key":"Zm9v","start_revision":1}}}"
  95. // but the gprc-gateway expects a different format..
  96. wstr := `{"create_request" : ` + string(wreq) + "}"
  97. // expects "bar", timeout after 2 seconds since stream waits forever
  98. if err = cURLPost(epc, cURLReq{endpoint: "/v3alpha/watch", value: wstr, expected: `"YmFy"`, timeout: 2}); err != nil {
  99. t.Fatal(err)
  100. }
  101. }