ctl_v3_test.go 4.1 KB

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