v3_curl_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "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(&etcdserverpb.PutRequest{
  44. Key: key,
  45. Value: value,
  46. })
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. rangeData, err := json.Marshal(&etcdserverpb.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. }