etcd_start_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "github.com/coreos/etcd/config"
  24. )
  25. const (
  26. bootstrapId = 0xBEEF
  27. )
  28. type garbageHandler struct {
  29. t *testing.T
  30. success bool
  31. sync.Mutex
  32. }
  33. func (g *garbageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  34. fmt.Fprintln(w, "Hello, client")
  35. wp := fmt.Sprint("/v2/keys/_etcd/registry/1/", bootstrapId)
  36. if gp := r.URL.String(); gp != wp {
  37. g.t.Fatalf("url = %s, want %s", gp, wp)
  38. }
  39. g.Lock()
  40. defer g.Unlock()
  41. g.success = true
  42. }
  43. func TestBadDiscoveryService(t *testing.T) {
  44. g := garbageHandler{t: t}
  45. ts := httptest.NewServer(&g)
  46. c := config.New()
  47. c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
  48. _, _, err := buildServer(t, c, bootstrapId)
  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. afterTest(t)
  60. }
  61. func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
  62. g := garbageHandler{t: t}
  63. ts := httptest.NewServer(&g)
  64. es, hs := buildCluster(1, false)
  65. waitCluster(t, es)
  66. c := config.New()
  67. c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
  68. c.Peers = []string{hs[0].URL}
  69. _, _, err := buildServer(t, c, bootstrapId)
  70. w := `discovery service error`
  71. if err == nil || !strings.HasPrefix(err.Error(), w) {
  72. t.Errorf("err = %v, want %s prefix", err, w)
  73. }
  74. destoryCluster(t, es, hs)
  75. ts.Close()
  76. afterTest(t)
  77. }
  78. func TestBootstrapByEmptyPeers(t *testing.T) {
  79. c := config.New()
  80. id := genId()
  81. e, h, err := buildServer(t, c, id)
  82. if err != nil {
  83. t.Error(err)
  84. }
  85. if e.p.node.Leader() != id {
  86. t.Error("leader = %x, want %x", e.p.node.Leader(), id)
  87. }
  88. destroyServer(t, e, h)
  89. afterTest(t)
  90. }
  91. func TestBootstrapByDiscoveryService(t *testing.T) {
  92. de, dh, _ := buildServer(t, config.New(), genId())
  93. c := config.New()
  94. c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
  95. e, h, err := buildServer(t, c, bootstrapId)
  96. if err != nil {
  97. t.Fatalf("build server err = %v, want nil", err)
  98. }
  99. destroyServer(t, e, h)
  100. destroyServer(t, de, dh)
  101. afterTest(t)
  102. }
  103. func TestRunByAdvisedPeers(t *testing.T) {
  104. es, hs := buildCluster(1, false)
  105. waitCluster(t, es)
  106. c := config.New()
  107. c.Peers = []string{hs[0].URL}
  108. e, h, err := buildServer(t, c, bootstrapId)
  109. if err != nil {
  110. t.Fatalf("build server err = %v, want nil", err)
  111. }
  112. w := es[0].id
  113. if g, _ := waitLeader(append(es, e)); g != w {
  114. t.Errorf("leader = %d, want %d", g, w)
  115. }
  116. destroyServer(t, e, h)
  117. destoryCluster(t, es, hs)
  118. afterTest(t)
  119. }
  120. func TestRunByDiscoveryService(t *testing.T) {
  121. de, dh, _ := buildServer(t, config.New(), genId())
  122. tc := NewTestClient()
  123. v := url.Values{}
  124. v.Set("value", "started")
  125. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", dh.URL, "/v2/keys/_etcd/registry/1/_state"), v)
  126. if g := resp.StatusCode; g != http.StatusCreated {
  127. t.Fatalf("put status = %d, want %d", g, http.StatusCreated)
  128. }
  129. resp.Body.Close()
  130. v.Set("value", dh.URL)
  131. resp, _ = tc.PutForm(fmt.Sprintf("%s%s%d", dh.URL, "/v2/keys/_etcd/registry/1/", de.id), v)
  132. if g := resp.StatusCode; g != http.StatusCreated {
  133. t.Fatalf("put status = %d, want %d", g, http.StatusCreated)
  134. }
  135. resp.Body.Close()
  136. c := config.New()
  137. c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
  138. e, h, err := buildServer(t, c, bootstrapId)
  139. if err != nil {
  140. t.Fatalf("build server err = %v, want nil", err)
  141. }
  142. w := de.id
  143. if g, _ := waitLeader([]*Server{e, de}); g != w {
  144. t.Errorf("leader = %d, want %d", g, w)
  145. }
  146. destroyServer(t, e, h)
  147. destroyServer(t, de, dh)
  148. afterTest(t)
  149. }
  150. func TestRunByDataDir(t *testing.T) {
  151. TestSingleNodeRecovery(t)
  152. }
  153. func buildServer(t *testing.T, c *config.Config, id int64) (e *Server, h *httptest.Server, err error) {
  154. e, h = initTestServer(c, id, false)
  155. go func() { err = e.Run() }()
  156. for {
  157. if e.mode.Get() == participantMode {
  158. break
  159. }
  160. if err != nil {
  161. h.Close()
  162. return nil, nil, err
  163. }
  164. time.Sleep(10 * time.Millisecond)
  165. }
  166. return e, h, nil
  167. }