ctl_v3_test.go 3.9 KB

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