etcdctlv3_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "fmt"
  17. "os"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. func TestCtlV3Set(t *testing.T) {
  24. testCtlV3Set(t, &configNoTLS, 3*time.Second, false)
  25. }
  26. func TestCtlV3SetZeroTimeout(t *testing.T) {
  27. testCtlV3Set(t, &configNoTLS, 0, false)
  28. }
  29. func TestCtlV3SetTimeout(t *testing.T) {
  30. testCtlV3Set(t, &configNoTLS, time.Nanosecond, false)
  31. }
  32. func TestCtlV3SetPeerTLS(t *testing.T) {
  33. testCtlV3Set(t, &configPeerTLS, 3*time.Second, false)
  34. }
  35. func TestCtlV3SetQuorum(t *testing.T) {
  36. testCtlV3Set(t, &configNoTLS, 3*time.Second, true)
  37. }
  38. func TestCtlV3SetQuorumZeroTimeout(t *testing.T) {
  39. testCtlV3Set(t, &configNoTLS, 0, true)
  40. }
  41. func TestCtlV3SetQuorumTimeout(t *testing.T) {
  42. testCtlV3Set(t, &configNoTLS, time.Nanosecond, true)
  43. }
  44. func TestCtlV3SetPeerTLSQuorum(t *testing.T) {
  45. testCtlV3Set(t, &configPeerTLS, 3*time.Second, true)
  46. }
  47. func testCtlV3Set(t *testing.T, cfg *etcdProcessClusterConfig, dialTimeout time.Duration, quorum bool) {
  48. defer testutil.AfterTest(t)
  49. os.Setenv("ETCDCTL_API", "3")
  50. epc := setupCtlV3Test(t, cfg, quorum)
  51. defer func() {
  52. os.Unsetenv("ETCDCTL_API")
  53. if errC := epc.Close(); errC != nil {
  54. t.Fatalf("error closing etcd processes (%v)", errC)
  55. }
  56. }()
  57. key, value := "foo", "bar"
  58. errc := make(chan error, 1)
  59. expectTimeout := dialTimeout > 0 && dialTimeout <= time.Nanosecond
  60. go func() {
  61. defer close(errc)
  62. if err := ctlV3Put(epc, key, value, dialTimeout); err != nil {
  63. if expectTimeout && isGRPCTimedout(err) {
  64. errc <- fmt.Errorf("put error (%v)", err)
  65. return
  66. }
  67. }
  68. if err := ctlV3Get(epc, key, value, dialTimeout, quorum); err != nil {
  69. if expectTimeout && isGRPCTimedout(err) {
  70. errc <- fmt.Errorf("get error (%v)", err)
  71. return
  72. }
  73. }
  74. }()
  75. select {
  76. case <-time.After(2*dialTimeout + time.Second):
  77. if dialTimeout > 0 {
  78. t.Fatalf("test timed out for %v", dialTimeout)
  79. }
  80. case err := <-errc:
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. }
  85. }
  86. func ctlV3PrefixArgs(clus *etcdProcessCluster, dialTimeout time.Duration) []string {
  87. if len(clus.proxies()) > 0 { // TODO: add proxy check as in v2
  88. panic("v3 proxy not implemented")
  89. }
  90. endpoints := ""
  91. if backends := clus.backends(); len(backends) != 0 {
  92. es := []string{}
  93. for _, b := range backends {
  94. es = append(es, stripSchema(b.cfg.acurl))
  95. }
  96. endpoints = strings.Join(es, ",")
  97. }
  98. cmdArgs := []string{"../bin/etcdctl", "--endpoints", endpoints, "--dial-timeout", dialTimeout.String()}
  99. if clus.cfg.clientTLS == clientTLS {
  100. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  101. }
  102. return cmdArgs
  103. }
  104. func ctlV3Put(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration) error {
  105. cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "put", key, value)
  106. return spawnWithExpect(cmdArgs, "OK")
  107. }
  108. func ctlV3Get(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration, quorum bool) error {
  109. cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "get", key)
  110. if !quorum {
  111. cmdArgs = append(cmdArgs, "--consistency", "s")
  112. }
  113. // TODO: match by value. Currently it prints out both key and value in multi-lines.
  114. return spawnWithExpect(cmdArgs, key)
  115. }
  116. func setupCtlV3Test(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) *etcdProcessCluster {
  117. mustEtcdctl(t)
  118. if !quorum {
  119. cfg = configStandalone(*cfg)
  120. }
  121. copied := *cfg
  122. epc, err := newEtcdProcessCluster(&copied)
  123. if err != nil {
  124. t.Fatalf("could not start etcd process cluster (%v)", err)
  125. }
  126. return epc
  127. }
  128. func isGRPCTimedout(err error) bool {
  129. return strings.Contains(err.Error(), "grpc: timed out trying to connect")
  130. }
  131. func stripSchema(s string) string {
  132. if strings.HasPrefix(s, "http://") {
  133. s = strings.Replace(s, "http://", "", -1)
  134. }
  135. if strings.HasPrefix(s, "https://") {
  136. s = strings.Replace(s, "https://", "", -1)
  137. }
  138. return s
  139. }