ctl_v3_test.go 3.9 KB

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