ctl_v3_test.go 4.4 KB

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