etcdctlv3_test.go 4.4 KB

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