ctl_v3_test.go 3.5 KB

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