v2_curl_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2016 CoreOS, Inc.
  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. "math/rand"
  17. "testing"
  18. "github.com/coreos/etcd/pkg/testutil"
  19. )
  20. func TestV2CurlNoTLS(t *testing.T) { testCurlPutGet(t, &configNoTLS) }
  21. func TestV2CurlAutoTLS(t *testing.T) { testCurlPutGet(t, &configAutoTLS) }
  22. func TestV2CurlAllTLS(t *testing.T) { testCurlPutGet(t, &configTLS) }
  23. func TestV2CurlPeerTLS(t *testing.T) { testCurlPutGet(t, &configPeerTLS) }
  24. func TestV2CurlClientTLS(t *testing.T) { testCurlPutGet(t, &configClientTLS) }
  25. func TestV2CurlProxyNoTLS(t *testing.T) { testCurlPutGet(t, &configWithProxy) }
  26. func TestV2CurlProxyTLS(t *testing.T) { testCurlPutGet(t, &configWithProxyTLS) }
  27. func TestV2CurlProxyPeerTLS(t *testing.T) { testCurlPutGet(t, &configWithProxyPeerTLS) }
  28. func TestV2CurlClientBoth(t *testing.T) { testCurlPutGet(t, &configClientBoth) }
  29. func testCurlPutGet(t *testing.T, cfg *etcdProcessClusterConfig) {
  30. defer testutil.AfterTest(t)
  31. // test doesn't use quorum gets, so ensure there are no followers to avoid
  32. // stale reads that will break the test
  33. cfg = configStandalone(*cfg)
  34. epc, err := newEtcdProcessCluster(cfg)
  35. if err != nil {
  36. t.Fatalf("could not start etcd process cluster (%v)", err)
  37. }
  38. defer func() {
  39. if err := epc.Close(); err != nil {
  40. t.Fatalf("error closing etcd processes (%v)", err)
  41. }
  42. }()
  43. expectPut := `{"action":"set","node":{"key":"/testKey","value":"foo","`
  44. expectGet := `{"action":"get","node":{"key":"/testKey","value":"foo","`
  45. if cfg.clientTLS == clientTLSAndNonTLS {
  46. if err := cURLPut(epc, "testKey", "foo", expectPut); err != nil {
  47. t.Fatalf("failed put with curl (%v)", err)
  48. }
  49. if err := cURLGet(epc, "testKey", expectGet); err != nil {
  50. t.Fatalf("failed get with curl (%v)", err)
  51. }
  52. if err := cURLGetUseTLS(epc, "testKey", expectGet); err != nil {
  53. t.Fatalf("failed get with curl (%v)", err)
  54. }
  55. } else {
  56. if err := cURLPut(epc, "testKey", "foo", expectPut); err != nil {
  57. t.Fatalf("failed put with curl (%v)", err)
  58. }
  59. if err := cURLGet(epc, "testKey", expectGet); err != nil {
  60. t.Fatalf("failed get with curl (%v)", err)
  61. }
  62. }
  63. }
  64. // cURLPrefixArgs builds the beginning of a curl command for a given key
  65. // addressed to a random URL in the given cluster.
  66. func cURLPrefixArgs(clus *etcdProcessCluster, key string) []string {
  67. cmdArgs := []string{"curl"}
  68. acurl := clus.procs[rand.Intn(clus.cfg.clusterSize)].cfg.acurl
  69. if clus.cfg.clientTLS == clientTLS {
  70. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  71. }
  72. keyURL := acurl + "/v2/keys/testKey"
  73. cmdArgs = append(cmdArgs, "-L", keyURL)
  74. return cmdArgs
  75. }
  76. func cURLPrefixArgsUseTLS(clus *etcdProcessCluster, key string) []string {
  77. cmdArgs := []string{"curl"}
  78. if clus.cfg.clientTLS != clientTLSAndNonTLS {
  79. panic("should not use cURLPrefixArgsUseTLS when serving only TLS or non-TLS")
  80. }
  81. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  82. acurl := clus.procs[rand.Intn(clus.cfg.clusterSize)].cfg.acurltls
  83. keyURL := acurl + "/v2/keys/testKey"
  84. cmdArgs = append(cmdArgs, "-L", keyURL)
  85. return cmdArgs
  86. }
  87. func cURLPut(clus *etcdProcessCluster, key, val, expected string) error {
  88. args := append(cURLPrefixArgs(clus, key), "-XPUT", "-d", "value="+val)
  89. return spawnWithExpect(args, expected)
  90. }
  91. func cURLGet(clus *etcdProcessCluster, key, expected string) error {
  92. return spawnWithExpect(cURLPrefixArgs(clus, key), expected)
  93. }
  94. func cURLGetUseTLS(clus *etcdProcessCluster, key, expected string) error {
  95. return spawnWithExpect(cURLPrefixArgsUseTLS(clus, key), expected)
  96. }