ctl_v3_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "os"
  17. "strings"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. "github.com/coreos/etcd/version"
  22. )
  23. func TestCtlV3Version(t *testing.T) { testCtl(t, versionTest) }
  24. func versionTest(cx ctlCtx) {
  25. if err := ctlV3Version(cx); err != nil {
  26. cx.t.Fatalf("versionTest ctlV3Version error (%v)", err)
  27. }
  28. }
  29. func ctlV3Version(cx ctlCtx) error {
  30. cmdArgs := append(cx.PrefixArgs(), "version")
  31. return spawnWithExpect(cmdArgs, version.Version)
  32. }
  33. type ctlCtx struct {
  34. t *testing.T
  35. cfg etcdProcessClusterConfig
  36. quotaBackendBytes int64
  37. epc *etcdProcessCluster
  38. dialTimeout time.Duration
  39. quorum bool // if true, set up 3-node cluster and linearizable read
  40. interactive bool
  41. user string
  42. pass string
  43. }
  44. type ctlOption func(*ctlCtx)
  45. func (cx *ctlCtx) applyOpts(opts []ctlOption) {
  46. for _, opt := range opts {
  47. opt(cx)
  48. }
  49. }
  50. func withCfg(cfg etcdProcessClusterConfig) ctlOption {
  51. return func(cx *ctlCtx) { cx.cfg = cfg }
  52. }
  53. func withDialTimeout(timeout time.Duration) ctlOption {
  54. return func(cx *ctlCtx) { cx.dialTimeout = timeout }
  55. }
  56. func withQuorum() ctlOption {
  57. return func(cx *ctlCtx) { cx.quorum = true }
  58. }
  59. func withInteractive() ctlOption {
  60. return func(cx *ctlCtx) { cx.interactive = true }
  61. }
  62. func withQuota(b int64) ctlOption {
  63. return func(cx *ctlCtx) { cx.quotaBackendBytes = b }
  64. }
  65. func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
  66. defer testutil.AfterTest(t)
  67. ret := ctlCtx{
  68. t: t,
  69. cfg: configAutoTLS,
  70. dialTimeout: 7 * time.Second,
  71. }
  72. ret.applyOpts(opts)
  73. os.Setenv("ETCDCTL_API", "3")
  74. mustEtcdctl(t)
  75. if !ret.quorum {
  76. ret.cfg = *configStandalone(ret.cfg)
  77. }
  78. if ret.quotaBackendBytes > 0 {
  79. ret.cfg.quotaBackendBytes = ret.quotaBackendBytes
  80. }
  81. epc, err := newEtcdProcessCluster(&ret.cfg)
  82. if err != nil {
  83. t.Fatalf("could not start etcd process cluster (%v)", err)
  84. }
  85. ret.epc = epc
  86. defer func() {
  87. os.Unsetenv("ETCDCTL_API")
  88. if errC := ret.epc.Close(); errC != nil {
  89. t.Fatalf("error closing etcd processes (%v)", errC)
  90. }
  91. }()
  92. donec := make(chan struct{})
  93. go func() {
  94. defer close(donec)
  95. testFunc(ret)
  96. }()
  97. select {
  98. case <-time.After(2*ret.dialTimeout + time.Second):
  99. if ret.dialTimeout > 0 {
  100. t.Fatalf("test timed out for %v", ret.dialTimeout)
  101. }
  102. case <-donec:
  103. }
  104. }
  105. func (cx *ctlCtx) PrefixArgs() []string {
  106. if len(cx.epc.proxies()) > 0 { // TODO: add proxy check as in v2
  107. panic("v3 proxy not implemented")
  108. }
  109. endpoints := ""
  110. if backends := cx.epc.backends(); len(backends) != 0 {
  111. es := []string{}
  112. for _, b := range backends {
  113. es = append(es, stripSchema(b.cfg.acurl))
  114. }
  115. endpoints = strings.Join(es, ",")
  116. }
  117. cmdArgs := []string{"../bin/etcdctl", "--endpoints", endpoints, "--dial-timeout", cx.dialTimeout.String()}
  118. if cx.epc.cfg.clientTLS == clientTLS {
  119. if cx.epc.cfg.isClientAutoTLS {
  120. cmdArgs = append(cmdArgs, "--insecure-transport=false", "--insecure-skip-tls-verify")
  121. } else {
  122. cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
  123. }
  124. }
  125. if cx.user != "" {
  126. cmdArgs = append(cmdArgs, "--user="+cx.user+":"+cx.pass)
  127. }
  128. return cmdArgs
  129. }
  130. func isGRPCTimedout(err error) bool {
  131. return strings.Contains(err.Error(), "grpc: timed out trying to connect")
  132. }
  133. func stripSchema(s string) string {
  134. if strings.HasPrefix(s, "http://") {
  135. s = strings.Replace(s, "http://", "", -1)
  136. }
  137. if strings.HasPrefix(s, "https://") {
  138. s = strings.Replace(s, "https://", "", -1)
  139. }
  140. return s
  141. }