etcd_start_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 etcdserver
  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. bootstrapName = "BEEF"
  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/", bootstrapName)
  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. defer afterTest(t)
  44. g := garbageHandler{t: t}
  45. httpts := httptest.NewServer(&g)
  46. defer httpts.Close()
  47. c := newTestConfig()
  48. c.Name = bootstrapName
  49. c.Discovery = httpts.URL + "/v2/keys/_etcd/registry/1"
  50. ts := testServer{Config: c}
  51. ts.Start()
  52. err := ts.Destroy()
  53. w := `discovery service error`
  54. if err == nil || !strings.HasPrefix(err.Error(), w) {
  55. t.Errorf("err = %v, want %s prefix", err, w)
  56. }
  57. g.Lock()
  58. defer g.Unlock()
  59. if !g.success {
  60. t.Fatal("Discovery server never called")
  61. }
  62. }
  63. func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
  64. defer afterTest(t)
  65. g := garbageHandler{t: t}
  66. httpts := httptest.NewServer(&g)
  67. defer httpts.Close()
  68. c := newTestConfig()
  69. c.Name = bootstrapName
  70. c.Discovery = httpts.URL + "/v2/keys/_etcd/registry/1"
  71. c.Peers = []string{"a peer"}
  72. ts := testServer{Config: c}
  73. ts.Start()
  74. err := ts.Destroy()
  75. w := `discovery service error`
  76. if err == nil || !strings.HasPrefix(err.Error(), w) {
  77. t.Errorf("err = %v, want %s prefix", err, w)
  78. }
  79. }
  80. func TestBootstrapByEmptyPeers(t *testing.T) {
  81. defer afterTest(t)
  82. ts := testServer{}
  83. ts.Start()
  84. defer ts.Destroy()
  85. ts.WaitMode(participantMode)
  86. if ts.Participant().node.Leader() != ts.Participant().id {
  87. t.Errorf("leader = %x, want %x", ts.Participant().node.Leader(), ts.Participant().id)
  88. }
  89. }
  90. func TestBootstrapByDiscoveryService(t *testing.T) {
  91. defer afterTest(t)
  92. discoverService := testCluster{Size: 1}
  93. discoverService.Start()
  94. defer discoverService.Destroy()
  95. c := newTestConfig()
  96. c.Name = bootstrapName
  97. c.Discovery = discoverService.URL(0) + "/v2/keys/_etcd/registry/1"
  98. ts := testServer{Config: c}
  99. ts.Start()
  100. ts.WaitMode(participantMode)
  101. err := ts.Destroy()
  102. if err != nil {
  103. t.Fatalf("server stop err = %v, want nil", err)
  104. }
  105. }
  106. func TestRunByAdvisedPeers(t *testing.T) {
  107. t.Skip("test covered by TestMultipleNodes")
  108. }
  109. func TestRunByDiscoveryService(t *testing.T) {
  110. ds := testCluster{Size: 1}
  111. ds.Start()
  112. defer ds.Destroy()
  113. tc := NewTestClient()
  114. v := url.Values{}
  115. v.Set("value", "started")
  116. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", ds.URL(0), "/v2/keys/_etcd/registry/1/_state"), v)
  117. if g := resp.StatusCode; g != http.StatusCreated {
  118. t.Fatalf("put status = %d, want %d", g, http.StatusCreated)
  119. }
  120. resp.Body.Close()
  121. v.Set("value", ds.URL(0))
  122. resp, _ = tc.PutForm(fmt.Sprintf("%s%s%d", ds.URL(0), "/v2/keys/_etcd/registry/1/", ds.Participant(0).id), v)
  123. if g := resp.StatusCode; g != http.StatusCreated {
  124. t.Fatalf("put status = %d, want %d", g, http.StatusCreated)
  125. }
  126. resp.Body.Close()
  127. c := newTestConfig()
  128. c.Name = bootstrapName
  129. c.Discovery = ds.URL(0) + "/v2/keys/_etcd/registry/1"
  130. ts := &testServer{Config: c}
  131. ds.Add(ts)
  132. // wait for the leader to do a heartbeat
  133. // it will update the lead field of the follower
  134. time.Sleep(ds.Node(0).e.tickDuration * defaultHeartbeat * 2)
  135. w := ds.Participant(0).id
  136. if g := ts.Lead().lead; g != w {
  137. t.Errorf("leader = %d, want %d", g, w)
  138. }
  139. }
  140. func TestRunByDataDir(t *testing.T) {
  141. t.Skip("test covered by TestSingleNodeRecovery")
  142. }