etcd_release_upgrade_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "sync"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/pkg/fileutil"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "github.com/coreos/etcd/version"
  24. )
  25. // TestReleaseUpgrade ensures that changes to master branch does not affect
  26. // upgrade from latest etcd releases.
  27. func TestReleaseUpgrade(t *testing.T) {
  28. lastReleaseBinary := binDir + "/etcd-last-release"
  29. if !fileutil.Exist(lastReleaseBinary) {
  30. t.Skipf("%q does not exist", lastReleaseBinary)
  31. }
  32. defer testutil.AfterTest(t)
  33. copiedCfg := configNoTLS
  34. copiedCfg.execPath = lastReleaseBinary
  35. copiedCfg.snapCount = 3
  36. copiedCfg.baseScheme = "unix" // to avoid port conflict
  37. epc, err := newEtcdProcessCluster(&copiedCfg)
  38. if err != nil {
  39. t.Fatalf("could not start etcd process cluster (%v)", err)
  40. }
  41. defer func() {
  42. if errC := epc.Close(); errC != nil {
  43. t.Fatalf("error closing etcd processes (%v)", errC)
  44. }
  45. }()
  46. // 3.0 boots as 2.3 then negotiates up to 3.0
  47. // so there's a window at boot time where it doesn't have V3rpcCapability enabled
  48. // poll /version until etcdcluster is >2.3.x before making v3 requests
  49. for i := 0; i < 7; i++ {
  50. if err = cURLGet(epc, cURLReq{endpoint: "/version", expected: `"etcdcluster":"` + version.Cluster(version.Version)}); err != nil {
  51. t.Logf("#%d: v3 is not ready yet (%v)", i, err)
  52. time.Sleep(time.Second)
  53. continue
  54. }
  55. break
  56. }
  57. if err != nil {
  58. t.Fatalf("cannot pull version (%v)", err)
  59. }
  60. os.Setenv("ETCDCTL_API", "3")
  61. defer os.Unsetenv("ETCDCTL_API")
  62. cx := ctlCtx{
  63. t: t,
  64. cfg: configNoTLS,
  65. dialTimeout: 7 * time.Second,
  66. quorum: true,
  67. epc: epc,
  68. }
  69. var kvs []kv
  70. for i := 0; i < 5; i++ {
  71. kvs = append(kvs, kv{key: fmt.Sprintf("foo%d", i), val: "bar"})
  72. }
  73. for i := range kvs {
  74. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  75. cx.t.Fatalf("#%d: ctlV3Put error (%v)", i, err)
  76. }
  77. }
  78. for i := range epc.procs {
  79. if err := epc.procs[i].Stop(); err != nil {
  80. t.Fatalf("#%d: error closing etcd process (%v)", i, err)
  81. }
  82. epc.procs[i].cfg.execPath = binDir + "/etcd"
  83. epc.procs[i].cfg.keepDataDir = true
  84. if err := epc.procs[i].Restart(); err != nil {
  85. t.Fatalf("error restarting etcd process (%v)", err)
  86. }
  87. for j := range kvs {
  88. if err := ctlV3Get(cx, []string{kvs[j].key}, []kv{kvs[j]}...); err != nil {
  89. cx.t.Fatalf("#%d-%d: ctlV3Get error (%v)", i, j, err)
  90. }
  91. }
  92. }
  93. }
  94. func TestReleaseUpgradeWithRestart(t *testing.T) {
  95. lastReleaseBinary := binDir + "/etcd-last-release"
  96. if !fileutil.Exist(lastReleaseBinary) {
  97. t.Skipf("%q does not exist", lastReleaseBinary)
  98. }
  99. defer testutil.AfterTest(t)
  100. copiedCfg := configNoTLS
  101. copiedCfg.execPath = lastReleaseBinary
  102. copiedCfg.snapCount = 10
  103. copiedCfg.baseScheme = "unix"
  104. epc, err := newEtcdProcessCluster(&copiedCfg)
  105. if err != nil {
  106. t.Fatalf("could not start etcd process cluster (%v)", err)
  107. }
  108. defer func() {
  109. if errC := epc.Close(); errC != nil {
  110. t.Fatalf("error closing etcd processes (%v)", errC)
  111. }
  112. }()
  113. os.Setenv("ETCDCTL_API", "3")
  114. defer os.Unsetenv("ETCDCTL_API")
  115. cx := ctlCtx{
  116. t: t,
  117. cfg: configNoTLS,
  118. dialTimeout: 7 * time.Second,
  119. quorum: true,
  120. epc: epc,
  121. }
  122. var kvs []kv
  123. for i := 0; i < 50; i++ {
  124. kvs = append(kvs, kv{key: fmt.Sprintf("foo%d", i), val: "bar"})
  125. }
  126. for i := range kvs {
  127. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  128. cx.t.Fatalf("#%d: ctlV3Put error (%v)", i, err)
  129. }
  130. }
  131. for i := range epc.procs {
  132. if err := epc.procs[i].Stop(); err != nil {
  133. t.Fatalf("#%d: error closing etcd process (%v)", i, err)
  134. }
  135. }
  136. var wg sync.WaitGroup
  137. wg.Add(len(epc.procs))
  138. for i := range epc.procs {
  139. go func(i int) {
  140. epc.procs[i].cfg.execPath = binDir + "/etcd"
  141. epc.procs[i].cfg.keepDataDir = true
  142. if err := epc.procs[i].Restart(); err != nil {
  143. t.Fatalf("error restarting etcd process (%v)", err)
  144. }
  145. wg.Done()
  146. }(i)
  147. }
  148. wg.Wait()
  149. if err := ctlV3Get(cx, []string{kvs[0].key}, []kv{kvs[0]}...); err != nil {
  150. t.Fatal(err)
  151. }
  152. }