snapshot_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 rafthttp
  15. import (
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "net/http"
  20. "net/http/httptest"
  21. "os"
  22. "strings"
  23. "testing"
  24. "time"
  25. "github.com/coreos/etcd/pkg/types"
  26. "github.com/coreos/etcd/raft/raftpb"
  27. "github.com/coreos/etcd/snap"
  28. )
  29. type strReaderCloser struct{ *strings.Reader }
  30. func (s strReaderCloser) Close() error { return nil }
  31. func TestSnapshotSend(t *testing.T) {
  32. tests := []struct {
  33. m raftpb.Message
  34. rc io.ReadCloser
  35. size int64
  36. wsent bool
  37. wfiles int
  38. }{
  39. // sent and receive with no errors
  40. {
  41. m: raftpb.Message{Type: raftpb.MsgSnap, To: 1},
  42. rc: strReaderCloser{strings.NewReader("hello")},
  43. size: 5,
  44. wsent: true,
  45. wfiles: 1,
  46. },
  47. // error when reading snapshot for send
  48. {
  49. m: raftpb.Message{Type: raftpb.MsgSnap, To: 1},
  50. rc: &errReadCloser{fmt.Errorf("snapshot error")},
  51. size: 1,
  52. wsent: false,
  53. wfiles: 0,
  54. },
  55. // sends less than the given snapshot length
  56. {
  57. m: raftpb.Message{Type: raftpb.MsgSnap, To: 1},
  58. rc: strReaderCloser{strings.NewReader("hello")},
  59. size: 10000,
  60. wsent: false,
  61. wfiles: 0,
  62. },
  63. // sends less than actual snapshot length
  64. {
  65. m: raftpb.Message{Type: raftpb.MsgSnap, To: 1},
  66. rc: strReaderCloser{strings.NewReader("hello")},
  67. size: 1,
  68. wsent: false,
  69. wfiles: 0,
  70. },
  71. }
  72. for i, tt := range tests {
  73. sent, files := testSnapshotSend(t, snap.NewMessage(tt.m, tt.rc, tt.size))
  74. if tt.wsent != sent {
  75. t.Errorf("#%d: snapshot expected %v, got %v", i, tt.wsent, sent)
  76. }
  77. if tt.wfiles != len(files) {
  78. t.Fatalf("#%d: expected %d files, got %d files", i, tt.wfiles, len(files))
  79. }
  80. }
  81. }
  82. func testSnapshotSend(t *testing.T, sm *snap.Message) (bool, []os.FileInfo) {
  83. d, err := ioutil.TempDir(os.TempDir(), "snapdir")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. defer os.RemoveAll(d)
  88. r := &fakeRaft{}
  89. tr := &Transport{pipelineRt: &http.Transport{}, ClusterID: types.ID(1), Raft: r}
  90. ch := make(chan struct{}, 1)
  91. h := &syncHandler{newSnapshotHandler(tr, r, snap.New(d), types.ID(1)), ch}
  92. srv := httptest.NewServer(h)
  93. defer srv.Close()
  94. picker := mustNewURLPicker(t, []string{srv.URL})
  95. snapsend := newSnapshotSender(tr, picker, types.ID(1), newPeerStatus(types.ID(1)))
  96. defer snapsend.stop()
  97. snapsend.send(*sm)
  98. sent := false
  99. select {
  100. case <-time.After(time.Second):
  101. t.Fatalf("timed out sending snapshot")
  102. case sent = <-sm.CloseNotify():
  103. }
  104. // wait for handler to finish accepting snapshot
  105. <-ch
  106. files, rerr := ioutil.ReadDir(d)
  107. if rerr != nil {
  108. t.Fatal(rerr)
  109. }
  110. return sent, files
  111. }
  112. type errReadCloser struct{ err error }
  113. func (s *errReadCloser) Read(p []byte) (int, error) { return 0, s.err }
  114. func (s *errReadCloser) Close() error { return s.err }
  115. type syncHandler struct {
  116. h http.Handler
  117. ch chan<- struct{}
  118. }
  119. func (sh *syncHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  120. sh.h.ServeHTTP(w, r)
  121. sh.ch <- struct{}{}
  122. }