ctl_v3_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "fmt"
  17. "os"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/pkg/flags"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "github.com/coreos/etcd/version"
  24. )
  25. func TestCtlV3Version(t *testing.T) { testCtl(t, versionTest) }
  26. func versionTest(cx ctlCtx) {
  27. if err := ctlV3Version(cx); err != nil {
  28. cx.t.Fatalf("versionTest ctlV3Version error (%v)", err)
  29. }
  30. }
  31. func ctlV3Version(cx ctlCtx) error {
  32. cmdArgs := append(cx.PrefixArgs(), "version")
  33. return spawnWithExpect(cmdArgs, version.Version)
  34. }
  35. // TestCtlV3DialWithHTTPScheme ensures that client handles endpoints with HTTPS scheme.
  36. func TestCtlV3DialWithHTTPScheme(t *testing.T) {
  37. testCtl(t, dialWithSchemeTest, withCfg(configClientTLS))
  38. }
  39. func dialWithSchemeTest(cx ctlCtx) {
  40. cmdArgs := append(cx.prefixArgs(cx.epc.endpoints()), "put", "foo", "bar")
  41. if err := spawnWithExpect(cmdArgs, "OK"); err != nil {
  42. cx.t.Fatal(err)
  43. }
  44. }
  45. type ctlCtx struct {
  46. t *testing.T
  47. cfg etcdProcessClusterConfig
  48. quotaBackendBytes int64
  49. noStrictReconfig bool
  50. epc *etcdProcessCluster
  51. envMap map[string]struct{}
  52. dialTimeout time.Duration
  53. quorum bool // if true, set up 3-node cluster and linearizable read
  54. interactive bool
  55. user string
  56. pass string
  57. // for compaction
  58. compactPhysical bool
  59. }
  60. type ctlOption func(*ctlCtx)
  61. func (cx *ctlCtx) applyOpts(opts []ctlOption) {
  62. for _, opt := range opts {
  63. opt(cx)
  64. }
  65. }
  66. func withCfg(cfg etcdProcessClusterConfig) ctlOption {
  67. return func(cx *ctlCtx) { cx.cfg = cfg }
  68. }
  69. func withDialTimeout(timeout time.Duration) ctlOption {
  70. return func(cx *ctlCtx) { cx.dialTimeout = timeout }
  71. }
  72. func withQuorum() ctlOption {
  73. return func(cx *ctlCtx) { cx.quorum = true }
  74. }
  75. func withInteractive() ctlOption {
  76. return func(cx *ctlCtx) { cx.interactive = true }
  77. }
  78. func withQuota(b int64) ctlOption {
  79. return func(cx *ctlCtx) { cx.quotaBackendBytes = b }
  80. }
  81. func withCompactPhysical() ctlOption {
  82. return func(cx *ctlCtx) { cx.compactPhysical = true }
  83. }
  84. func withNoStrictReconfig() ctlOption {
  85. return func(cx *ctlCtx) { cx.noStrictReconfig = true }
  86. }
  87. func withFlagByEnv() ctlOption {
  88. return func(cx *ctlCtx) { cx.envMap = make(map[string]struct{}) }
  89. }
  90. func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
  91. defer testutil.AfterTest(t)
  92. ret := ctlCtx{
  93. t: t,
  94. cfg: configAutoTLS,
  95. dialTimeout: 7 * time.Second,
  96. }
  97. ret.applyOpts(opts)
  98. os.Setenv("ETCDCTL_API", "3")
  99. mustEtcdctl(t)
  100. if !ret.quorum {
  101. ret.cfg = *configStandalone(ret.cfg)
  102. }
  103. if ret.quotaBackendBytes > 0 {
  104. ret.cfg.quotaBackendBytes = ret.quotaBackendBytes
  105. }
  106. ret.cfg.noStrictReconfig = ret.noStrictReconfig
  107. epc, err := newEtcdProcessCluster(&ret.cfg)
  108. if err != nil {
  109. t.Fatalf("could not start etcd process cluster (%v)", err)
  110. }
  111. ret.epc = epc
  112. defer func() {
  113. os.Unsetenv("ETCDCTL_API")
  114. if ret.envMap != nil {
  115. for k := range ret.envMap {
  116. os.Unsetenv(k)
  117. }
  118. }
  119. if errC := ret.epc.Close(); errC != nil {
  120. t.Fatalf("error closing etcd processes (%v)", errC)
  121. }
  122. }()
  123. donec := make(chan struct{})
  124. go func() {
  125. defer close(donec)
  126. testFunc(ret)
  127. }()
  128. timeout := 2*ret.dialTimeout + time.Second
  129. if ret.dialTimeout == 0 {
  130. timeout = 30 * time.Second
  131. }
  132. select {
  133. case <-time.After(timeout):
  134. testutil.FatalStack(t, fmt.Sprintf("test timed out after %v", timeout))
  135. case <-donec:
  136. }
  137. }
  138. func (cx *ctlCtx) prefixArgs(eps []string) []string {
  139. if len(cx.epc.proxies()) > 0 { // TODO: add proxy check as in v2
  140. panic("v3 proxy not implemented")
  141. }
  142. fmap := make(map[string]string)
  143. fmap["endpoints"] = strings.Join(eps, ",")
  144. fmap["dial-timeout"] = cx.dialTimeout.String()
  145. if cx.epc.cfg.clientTLS == clientTLS {
  146. if cx.epc.cfg.isClientAutoTLS {
  147. fmap["insecure-transport"] = "false"
  148. fmap["insecure-skip-tls-verify"] = "true"
  149. } else {
  150. fmap["cacert"] = caPath
  151. fmap["cert"] = certPath
  152. fmap["key"] = privateKeyPath
  153. }
  154. }
  155. if cx.user != "" {
  156. fmap["user"] = cx.user + ":" + cx.pass
  157. }
  158. useEnv := cx.envMap != nil
  159. cmdArgs := []string{ctlBinPath}
  160. for k, v := range fmap {
  161. if useEnv {
  162. ek := flags.FlagToEnv("ETCDCTL", k)
  163. os.Setenv(ek, v)
  164. cx.envMap[ek] = struct{}{}
  165. } else {
  166. cmdArgs = append(cmdArgs, fmt.Sprintf("--%s=%s", k, v))
  167. }
  168. }
  169. return cmdArgs
  170. }
  171. // PrefixArgs prefixes etcdctl command.
  172. // Make sure to unset environment variables after tests.
  173. func (cx *ctlCtx) PrefixArgs() []string {
  174. return cx.prefixArgs(cx.epc.grpcEndpoints())
  175. }
  176. func isGRPCTimedout(err error) bool {
  177. return strings.Contains(err.Error(), "grpc: timed out trying to connect")
  178. }