etcd_start_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Copyright 2014 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcd
  14. import (
  15. "fmt"
  16. "net/http"
  17. "net/http/httptest"
  18. "net/url"
  19. "strings"
  20. "sync"
  21. "testing"
  22. "time"
  23. )
  24. const (
  25. bootstrapId = 0xBEEF
  26. )
  27. type garbageHandler struct {
  28. t *testing.T
  29. success bool
  30. sync.Mutex
  31. }
  32. func (g *garbageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  33. fmt.Fprintln(w, "Hello, client")
  34. wp := fmt.Sprint("/v2/keys/_etcd/registry/1/", bootstrapId)
  35. if gp := r.URL.String(); gp != wp {
  36. g.t.Fatalf("url = %s, want %s", gp, wp)
  37. }
  38. g.Lock()
  39. defer g.Unlock()
  40. g.success = true
  41. }
  42. func TestBadDiscoveryService(t *testing.T) {
  43. g := garbageHandler{t: t}
  44. ts := httptest.NewServer(&g)
  45. c := newTestConfig()
  46. c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
  47. e, h := newUnstartedTestServer(c, bootstrapId, false)
  48. err := startServer(t, e)
  49. w := `discovery service error`
  50. if err == nil || !strings.HasPrefix(err.Error(), w) {
  51. t.Errorf("err = %v, want %s prefix", err, w)
  52. }
  53. g.Lock()
  54. defer g.Unlock()
  55. if !g.success {
  56. t.Fatal("Discovery server never called")
  57. }
  58. ts.Close()
  59. destroyServer(t, e, h)
  60. afterTest(t)
  61. }
  62. func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
  63. g := garbageHandler{t: t}
  64. ts := httptest.NewServer(&g)
  65. es, hs := buildCluster(1, false)
  66. waitCluster(t, es)
  67. c := newTestConfig()
  68. c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
  69. c.Peers = []string{hs[0].URL}
  70. e, h := newUnstartedTestServer(c, bootstrapId, false)
  71. err := startServer(t, e)
  72. w := `discovery service error`
  73. if err == nil || !strings.HasPrefix(err.Error(), w) {
  74. t.Errorf("err = %v, want %s prefix", err, w)
  75. }
  76. destoryCluster(t, es, hs)
  77. destroyServer(t, e, h)
  78. ts.Close()
  79. afterTest(t)
  80. }
  81. func TestBootstrapByEmptyPeers(t *testing.T) {
  82. c := newTestConfig()
  83. id := genId()
  84. e, h := newUnstartedTestServer(c, id, false)
  85. err := startServer(t, e)
  86. if err != nil {
  87. t.Error(err)
  88. }
  89. if e.p.node.Leader() != id {
  90. t.Errorf("leader = %x, want %x", e.p.node.Leader(), id)
  91. }
  92. destroyServer(t, e, h)
  93. afterTest(t)
  94. }
  95. func TestBootstrapByDiscoveryService(t *testing.T) {
  96. de, dh := newUnstartedTestServer(newTestConfig(), genId(), false)
  97. err := startServer(t, de)
  98. c := newTestConfig()
  99. c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
  100. e, h := newUnstartedTestServer(c, bootstrapId, false)
  101. err = startServer(t, e)
  102. if err != nil {
  103. t.Fatalf("build server err = %v, want nil", err)
  104. }
  105. destroyServer(t, e, h)
  106. destroyServer(t, de, dh)
  107. afterTest(t)
  108. }
  109. func TestRunByAdvisedPeers(t *testing.T) {
  110. es, hs := buildCluster(1, false)
  111. waitCluster(t, es)
  112. c := newTestConfig()
  113. c.Peers = []string{hs[0].URL}
  114. e, h := newUnstartedTestServer(c, bootstrapId, false)
  115. err := startServer(t, e)
  116. if err != nil {
  117. t.Fatalf("build server err = %v, want nil", err)
  118. }
  119. w := es[0].id
  120. if g, _ := waitLeader(append(es, e)); g != w {
  121. t.Errorf("leader = %d, want %d", g, w)
  122. }
  123. destroyServer(t, e, h)
  124. destoryCluster(t, es, hs)
  125. afterTest(t)
  126. }
  127. func TestRunByDiscoveryService(t *testing.T) {
  128. de, dh := newUnstartedTestServer(newTestConfig(), genId(), false)
  129. err := startServer(t, de)
  130. tc := NewTestClient()
  131. v := url.Values{}
  132. v.Set("value", "started")
  133. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", dh.URL, "/v2/keys/_etcd/registry/1/_state"), v)
  134. if g := resp.StatusCode; g != http.StatusCreated {
  135. t.Fatalf("put status = %d, want %d", g, http.StatusCreated)
  136. }
  137. resp.Body.Close()
  138. v.Set("value", dh.URL)
  139. resp, _ = tc.PutForm(fmt.Sprintf("%s%s%d", dh.URL, "/v2/keys/_etcd/registry/1/", de.id), v)
  140. if g := resp.StatusCode; g != http.StatusCreated {
  141. t.Fatalf("put status = %d, want %d", g, http.StatusCreated)
  142. }
  143. resp.Body.Close()
  144. c := newTestConfig()
  145. c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
  146. e, h := newUnstartedTestServer(c, bootstrapId, false)
  147. err = startServer(t, e)
  148. if err != nil {
  149. t.Fatalf("build server err = %v, want nil", err)
  150. }
  151. w := de.id
  152. if g, _ := waitLeader([]*Server{e, de}); g != w {
  153. t.Errorf("leader = %d, want %d", g, w)
  154. }
  155. destroyServer(t, e, h)
  156. destroyServer(t, de, dh)
  157. afterTest(t)
  158. }
  159. func TestRunByDataDir(t *testing.T) {
  160. TestSingleNodeRecovery(t)
  161. }
  162. func startServer(t *testing.T, e *Server) error {
  163. var err error
  164. go func() { err = e.Run() }()
  165. for {
  166. if e.mode.Get() == participantMode {
  167. break
  168. }
  169. if err != nil {
  170. return err
  171. }
  172. time.Sleep(10 * time.Millisecond)
  173. }
  174. return nil
  175. }