etcd_release_upgrade_test.go 5.0 KB

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