etcdctlv3_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "strings"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/pkg/fileutil"
  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. epc := setupCtlV3Test(t, cfg, quorum)
  49. defer func() {
  50. if errC := epc.Close(); errC != nil {
  51. t.Fatalf("error closing etcd processes (%v)", errC)
  52. }
  53. }()
  54. key, value := "foo", "bar"
  55. donec := make(chan struct{})
  56. go func() {
  57. if err := ctlV3Put(epc, key, value, dialTimeout); err != nil {
  58. if dialTimeout > 0 && dialTimeout <= time.Nanosecond && isGRPCTimedout(err) { // timeout expected
  59. donec <- struct{}{}
  60. return
  61. }
  62. t.Fatalf("put error (%v)", err)
  63. }
  64. if err := ctlV3Get(epc, key, value, dialTimeout, quorum); err != nil {
  65. if dialTimeout > 0 && dialTimeout <= time.Nanosecond && isGRPCTimedout(err) { // timeout expected
  66. donec <- struct{}{}
  67. return
  68. }
  69. t.Fatalf("get error (%v)", err)
  70. }
  71. donec <- struct{}{}
  72. }()
  73. select {
  74. case <-time.After(2*dialTimeout + time.Second):
  75. if dialTimeout > 0 {
  76. t.Fatalf("test timed out for %v", dialTimeout)
  77. }
  78. case <-donec:
  79. }
  80. }
  81. func ctlV3PrefixArgs(clus *etcdProcessCluster, dialTimeout time.Duration) []string {
  82. if len(clus.proxies()) > 0 { // TODO: add proxy check as in v2
  83. panic("v3 proxy not implemented")
  84. }
  85. endpoints := ""
  86. if backends := clus.backends(); len(backends) != 0 {
  87. es := []string{}
  88. for _, b := range backends {
  89. es = append(es, stripSchema(b.cfg.acurl))
  90. }
  91. endpoints = strings.Join(es, ",")
  92. }
  93. cmdArgs := []string{"../bin/etcdctlv3", "--endpoints", endpoints, "--dial-timeout", dialTimeout.String()}
  94. if clus.cfg.clientTLS == clientTLS {
  95. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  96. }
  97. return cmdArgs
  98. }
  99. func ctlV3Put(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration) error {
  100. cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "put", key, value)
  101. return spawnWithExpectedString(cmdArgs, "OK")
  102. }
  103. func ctlV3Get(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration, quorum bool) error {
  104. cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "get", key)
  105. if !quorum {
  106. cmdArgs = append(cmdArgs, "--consistency", "s")
  107. }
  108. // TODO: match by value. Currently it prints out both key and value in multi-lines.
  109. return spawnWithExpectedString(cmdArgs, key)
  110. }
  111. func mustCtlV3(t *testing.T) {
  112. if !fileutil.Exist("../bin/etcdctlv3") {
  113. t.Fatalf("could not find etcdctlv3 binary")
  114. }
  115. }
  116. func setupCtlV3Test(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) *etcdProcessCluster {
  117. mustCtlV3(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. }