peers_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package etcdhttp
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "reflect"
  6. "sort"
  7. "strings"
  8. "testing"
  9. "github.com/coreos/etcd/raft/raftpb"
  10. )
  11. func TestPeers(t *testing.T) {
  12. tests := []struct {
  13. in string
  14. wids []int64
  15. wep []string
  16. wstring string
  17. }{
  18. {
  19. "1=1.1.1.1",
  20. []int64{1},
  21. []string{"http://1.1.1.1"},
  22. "1=1.1.1.1",
  23. },
  24. {
  25. "2=2.2.2.2",
  26. []int64{2},
  27. []string{"http://2.2.2.2"},
  28. "2=2.2.2.2",
  29. },
  30. {
  31. "1=1.1.1.1&1=1.1.1.2&2=2.2.2.2",
  32. []int64{1, 2},
  33. []string{"http://1.1.1.1", "http://1.1.1.2", "http://2.2.2.2"},
  34. "1=1.1.1.1&1=1.1.1.2&2=2.2.2.2",
  35. },
  36. {
  37. "3=3.3.3.3&4=4.4.4.4&1=1.1.1.1&1=1.1.1.2&2=2.2.2.2",
  38. []int64{1, 2, 3, 4},
  39. []string{"http://1.1.1.1", "http://1.1.1.2", "http://2.2.2.2",
  40. "http://3.3.3.3", "http://4.4.4.4"},
  41. "1=1.1.1.1&1=1.1.1.2&2=2.2.2.2&3=3.3.3.3&4=4.4.4.4",
  42. },
  43. }
  44. for i, tt := range tests {
  45. p := &Peers{}
  46. err := p.Set(tt.in)
  47. if err != nil {
  48. t.Errorf("#%d: err=%v, want nil", i, err)
  49. }
  50. ids := int64Slice(p.IDs())
  51. sort.Sort(ids)
  52. if !reflect.DeepEqual([]int64(ids), tt.wids) {
  53. t.Errorf("#%d: IDs=%#v, want %#v", i, []int64(ids), tt.wids)
  54. }
  55. ep := p.Endpoints()
  56. if !reflect.DeepEqual(ep, tt.wep) {
  57. t.Errorf("#%d: Endpoints=%#v, want %#v", i, ep, tt.wep)
  58. }
  59. s := p.String()
  60. if s != tt.wstring {
  61. t.Errorf("#%d: string=%q, want %q", i, s, tt.wstring)
  62. }
  63. }
  64. }
  65. type int64Slice []int64
  66. func (p int64Slice) Len() int { return len(p) }
  67. func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
  68. func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  69. func TestPeersSetBad(t *testing.T) {
  70. tests := []string{
  71. // garbage URL
  72. "asdf%%",
  73. // non-int64 keys
  74. "a=1.2.3.4",
  75. "-1-23=1.2.3.4",
  76. }
  77. for i, tt := range tests {
  78. p := &Peers{}
  79. if err := p.Set(tt); err == nil {
  80. t.Errorf("#%d: err=nil unexpectedly", i)
  81. }
  82. }
  83. }
  84. func TestPeersPick(t *testing.T) {
  85. ps := &Peers{
  86. 1: []string{"abc", "def", "ghi", "jkl", "mno", "pqr", "stu"},
  87. 2: []string{"xyz"},
  88. 3: []string{},
  89. }
  90. ids := map[string]bool{
  91. "http://abc": true,
  92. "http://def": true,
  93. "http://ghi": true,
  94. "http://jkl": true,
  95. "http://mno": true,
  96. "http://pqr": true,
  97. "http://stu": true,
  98. }
  99. for i := 0; i < 1000; i++ {
  100. a := ps.Pick(1)
  101. if _, ok := ids[a]; !ok {
  102. t.Errorf("returned ID %q not in expected range!", a)
  103. break
  104. }
  105. }
  106. if b := ps.Pick(2); b != "http://xyz" {
  107. t.Errorf("id=%q, want %q", b, "http://xyz")
  108. }
  109. if c := ps.Pick(3); c != "" {
  110. t.Errorf("id=%q, want \"\"", c)
  111. }
  112. }
  113. func TestHttpPost(t *testing.T) {
  114. var tr *http.Request
  115. tests := []struct {
  116. h http.HandlerFunc
  117. w bool
  118. }{
  119. {
  120. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  121. tr = r
  122. w.WriteHeader(http.StatusNoContent)
  123. }),
  124. true,
  125. },
  126. {
  127. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  128. tr = r
  129. w.WriteHeader(http.StatusNotFound)
  130. }),
  131. false,
  132. },
  133. {
  134. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  135. tr = r
  136. w.WriteHeader(http.StatusInternalServerError)
  137. }),
  138. false,
  139. },
  140. }
  141. for i, tt := range tests {
  142. ts := httptest.NewServer(tt.h)
  143. if g := httpPost(http.DefaultClient, ts.URL, []byte("adsf")); g != tt.w {
  144. t.Errorf("#%d: httpPost()=%t, want %t", i, g, tt.w)
  145. }
  146. if tr.Method != "POST" {
  147. t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
  148. }
  149. if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
  150. t.Errorf("#%d: Content-Type=%q, want %q", i, ct, "application/protobuf")
  151. }
  152. tr = nil
  153. ts.Close()
  154. }
  155. if httpPost(http.DefaultClient, "garbage url", []byte("data")) {
  156. t.Errorf("httpPost with bad URL returned true unexpectedly!")
  157. }
  158. }
  159. func TestSend(t *testing.T) {
  160. var tr *http.Request
  161. var rc int
  162. h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  163. tr = r
  164. w.WriteHeader(rc)
  165. })
  166. tests := []struct {
  167. m raftpb.Message
  168. code int
  169. ok bool
  170. }{
  171. {
  172. // all good
  173. raftpb.Message{
  174. To: 42,
  175. Type: 4,
  176. },
  177. http.StatusNoContent,
  178. true,
  179. },
  180. {
  181. // bad response from server should be silently ignored
  182. raftpb.Message{
  183. To: 42,
  184. Type: 2,
  185. },
  186. http.StatusInternalServerError,
  187. true,
  188. },
  189. {
  190. // unknown destination!
  191. raftpb.Message{
  192. To: 3,
  193. Type: 2,
  194. },
  195. 0,
  196. false,
  197. },
  198. }
  199. for i, tt := range tests {
  200. tr = nil
  201. rc = tt.code
  202. ts := httptest.NewServer(h)
  203. ps := Peers{
  204. 42: []string{strings.TrimPrefix(ts.URL, "http://")},
  205. }
  206. send(http.DefaultClient, ps, tt.m)
  207. if !tt.ok {
  208. if tr != nil {
  209. t.Errorf("#%d: got request=%#v, want nil", i, tr)
  210. }
  211. ts.Close()
  212. continue
  213. }
  214. if tr.Method != "POST" {
  215. t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
  216. }
  217. if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
  218. t.Errorf("#%d: Content-Type=%q, want %q", i, ct, "application/protobuf")
  219. }
  220. if tr.URL.String() != "/raft" {
  221. t.Errorf("#%d: URL=%q, want %q", i, tr.URL.String(), "/raft")
  222. }
  223. ts.Close()
  224. }
  225. }