ctl_v3_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. "go.etcd.io/etcd/pkg/flags"
  22. "go.etcd.io/etcd/pkg/testutil"
  23. "go.etcd.io/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.EndpointsV3()), "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. apiPrefix string
  48. cfg etcdProcessClusterConfig
  49. quotaBackendBytes int64
  50. corruptFunc func(string) error
  51. noStrictReconfig bool
  52. epc *etcdProcessCluster
  53. envMap map[string]struct{}
  54. dialTimeout time.Duration
  55. quorum bool // if true, set up 3-node cluster and linearizable read
  56. interactive bool
  57. user string
  58. pass string
  59. initialCorruptCheck bool
  60. // for compaction
  61. compactPhysical bool
  62. }
  63. type ctlOption func(*ctlCtx)
  64. func (cx *ctlCtx) applyOpts(opts []ctlOption) {
  65. for _, opt := range opts {
  66. opt(cx)
  67. }
  68. cx.initialCorruptCheck = true
  69. }
  70. func withCfg(cfg etcdProcessClusterConfig) ctlOption {
  71. return func(cx *ctlCtx) { cx.cfg = cfg }
  72. }
  73. func withDialTimeout(timeout time.Duration) ctlOption {
  74. return func(cx *ctlCtx) { cx.dialTimeout = timeout }
  75. }
  76. func withQuorum() ctlOption {
  77. return func(cx *ctlCtx) { cx.quorum = true }
  78. }
  79. func withInteractive() ctlOption {
  80. return func(cx *ctlCtx) { cx.interactive = true }
  81. }
  82. func withQuota(b int64) ctlOption {
  83. return func(cx *ctlCtx) { cx.quotaBackendBytes = b }
  84. }
  85. func withCompactPhysical() ctlOption {
  86. return func(cx *ctlCtx) { cx.compactPhysical = true }
  87. }
  88. func withInitialCorruptCheck() ctlOption {
  89. return func(cx *ctlCtx) { cx.initialCorruptCheck = true }
  90. }
  91. func withCorruptFunc(f func(string) error) ctlOption {
  92. return func(cx *ctlCtx) { cx.corruptFunc = f }
  93. }
  94. func withNoStrictReconfig() ctlOption {
  95. return func(cx *ctlCtx) { cx.noStrictReconfig = true }
  96. }
  97. func withApiPrefix(p string) ctlOption {
  98. return func(cx *ctlCtx) { cx.apiPrefix = p }
  99. }
  100. func withFlagByEnv() ctlOption {
  101. return func(cx *ctlCtx) { cx.envMap = make(map[string]struct{}) }
  102. }
  103. func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
  104. defer testutil.AfterTest(t)
  105. ret := ctlCtx{
  106. t: t,
  107. cfg: configAutoTLS,
  108. dialTimeout: 7 * time.Second,
  109. }
  110. ret.applyOpts(opts)
  111. mustEtcdctl(t)
  112. if !ret.quorum {
  113. ret.cfg = *configStandalone(ret.cfg)
  114. }
  115. if ret.quotaBackendBytes > 0 {
  116. ret.cfg.quotaBackendBytes = ret.quotaBackendBytes
  117. }
  118. ret.cfg.noStrictReconfig = ret.noStrictReconfig
  119. if ret.initialCorruptCheck {
  120. ret.cfg.initialCorruptCheck = ret.initialCorruptCheck
  121. }
  122. epc, err := newEtcdProcessCluster(&ret.cfg)
  123. if err != nil {
  124. t.Fatalf("could not start etcd process cluster (%v)", err)
  125. }
  126. ret.epc = epc
  127. defer func() {
  128. if ret.envMap != nil {
  129. for k := range ret.envMap {
  130. os.Unsetenv(k)
  131. }
  132. }
  133. if errC := ret.epc.Close(); errC != nil {
  134. t.Fatalf("error closing etcd processes (%v)", errC)
  135. }
  136. }()
  137. donec := make(chan struct{})
  138. go func() {
  139. defer close(donec)
  140. testFunc(ret)
  141. }()
  142. timeout := 2*ret.dialTimeout + time.Second
  143. if ret.dialTimeout == 0 {
  144. timeout = 30 * time.Second
  145. }
  146. select {
  147. case <-time.After(timeout):
  148. testutil.FatalStack(t, fmt.Sprintf("test timed out after %v", timeout))
  149. case <-donec:
  150. }
  151. }
  152. func (cx *ctlCtx) prefixArgs(eps []string) []string {
  153. fmap := make(map[string]string)
  154. fmap["endpoints"] = strings.Join(eps, ",")
  155. fmap["dial-timeout"] = cx.dialTimeout.String()
  156. if cx.epc.cfg.clientTLS == clientTLS {
  157. if cx.epc.cfg.isClientAutoTLS {
  158. fmap["insecure-transport"] = "false"
  159. fmap["insecure-skip-tls-verify"] = "true"
  160. } else if cx.epc.cfg.isClientCRL {
  161. fmap["cacert"] = caPath
  162. fmap["cert"] = revokedCertPath
  163. fmap["key"] = revokedPrivateKeyPath
  164. } else {
  165. fmap["cacert"] = caPath
  166. fmap["cert"] = certPath
  167. fmap["key"] = privateKeyPath
  168. }
  169. }
  170. if cx.user != "" {
  171. fmap["user"] = cx.user + ":" + cx.pass
  172. }
  173. useEnv := cx.envMap != nil
  174. cmdArgs := []string{ctlBinPath + "3"}
  175. for k, v := range fmap {
  176. if useEnv {
  177. ek := flags.FlagToEnv("ETCDCTL", k)
  178. os.Setenv(ek, v)
  179. cx.envMap[ek] = struct{}{}
  180. } else {
  181. cmdArgs = append(cmdArgs, fmt.Sprintf("--%s=%s", k, v))
  182. }
  183. }
  184. return cmdArgs
  185. }
  186. // PrefixArgs prefixes etcdctl command.
  187. // Make sure to unset environment variables after tests.
  188. func (cx *ctlCtx) PrefixArgs() []string {
  189. return cx.prefixArgs(cx.epc.EndpointsV3())
  190. }
  191. func isGRPCTimedout(err error) bool {
  192. return strings.Contains(err.Error(), "grpc: timed out trying to connect")
  193. }
  194. func (cx *ctlCtx) memberToRemove() (ep string, memberID string, clusterID string) {
  195. n1 := cx.cfg.clusterSize
  196. if n1 < 2 {
  197. cx.t.Fatalf("%d-node is too small to test 'member remove'", n1)
  198. }
  199. resp, err := getMemberList(*cx)
  200. if err != nil {
  201. cx.t.Fatal(err)
  202. }
  203. if n1 != len(resp.Members) {
  204. cx.t.Fatalf("expected %d, got %d", n1, len(resp.Members))
  205. }
  206. ep = resp.Members[0].ClientURLs[0]
  207. clusterID = fmt.Sprintf("%x", resp.Header.ClusterId)
  208. memberID = fmt.Sprintf("%x", resp.Members[1].ID)
  209. return ep, memberID, clusterID
  210. }