snapshot_test.go 3.5 KB

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