snapshotter_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2015 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 snap
  15. import (
  16. "fmt"
  17. "hash/crc32"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "reflect"
  22. "testing"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. )
  25. var testSnap = &raftpb.Snapshot{
  26. Data: []byte("some snapshot"),
  27. Metadata: raftpb.SnapshotMetadata{
  28. ConfState: raftpb.ConfState{
  29. Nodes: []uint64{1, 2, 3},
  30. },
  31. Index: 1,
  32. Term: 1,
  33. },
  34. }
  35. func TestSaveAndLoad(t *testing.T) {
  36. dir := path.Join(os.TempDir(), "snapshot")
  37. err := os.Mkdir(dir, 0700)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. defer os.RemoveAll(dir)
  42. ss := New(dir)
  43. err = ss.save(testSnap)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. g, err := ss.Load()
  48. if err != nil {
  49. t.Errorf("err = %v, want nil", err)
  50. }
  51. if !reflect.DeepEqual(g, testSnap) {
  52. t.Errorf("snap = %#v, want %#v", g, testSnap)
  53. }
  54. }
  55. func TestBadCRC(t *testing.T) {
  56. dir := path.Join(os.TempDir(), "snapshot")
  57. err := os.Mkdir(dir, 0700)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. defer os.RemoveAll(dir)
  62. ss := New(dir)
  63. err = ss.save(testSnap)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. defer func() { crcTable = crc32.MakeTable(crc32.Castagnoli) }()
  68. // switch to use another crc table
  69. // fake a crc mismatch
  70. crcTable = crc32.MakeTable(crc32.Koopman)
  71. _, err = Read(path.Join(dir, fmt.Sprintf("%016x-%016x.snap", 1, 1)))
  72. if err == nil || err != ErrCRCMismatch {
  73. t.Errorf("err = %v, want %v", err, ErrCRCMismatch)
  74. }
  75. }
  76. func TestFailback(t *testing.T) {
  77. dir := path.Join(os.TempDir(), "snapshot")
  78. err := os.Mkdir(dir, 0700)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. defer os.RemoveAll(dir)
  83. large := fmt.Sprintf("%016x-%016x-%016x.snap", 0xFFFF, 0xFFFF, 0xFFFF)
  84. err = ioutil.WriteFile(path.Join(dir, large), []byte("bad data"), 0666)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. ss := New(dir)
  89. err = ss.save(testSnap)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. g, err := ss.Load()
  94. if err != nil {
  95. t.Errorf("err = %v, want nil", err)
  96. }
  97. if !reflect.DeepEqual(g, testSnap) {
  98. t.Errorf("snap = %#v, want %#v", g, testSnap)
  99. }
  100. if f, err := os.Open(path.Join(dir, large) + ".broken"); err != nil {
  101. t.Fatal("broken snapshot does not exist")
  102. } else {
  103. f.Close()
  104. }
  105. }
  106. func TestSnapNames(t *testing.T) {
  107. dir := path.Join(os.TempDir(), "snapshot")
  108. err := os.Mkdir(dir, 0700)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. defer os.RemoveAll(dir)
  113. for i := 1; i <= 5; i++ {
  114. var f *os.File
  115. if f, err = os.Create(path.Join(dir, fmt.Sprintf("%d.snap", i))); err != nil {
  116. t.Fatal(err)
  117. } else {
  118. f.Close()
  119. }
  120. }
  121. ss := New(dir)
  122. names, err := ss.snapNames()
  123. if err != nil {
  124. t.Errorf("err = %v, want nil", err)
  125. }
  126. if len(names) != 5 {
  127. t.Errorf("len = %d, want 10", len(names))
  128. }
  129. w := []string{"5.snap", "4.snap", "3.snap", "2.snap", "1.snap"}
  130. if !reflect.DeepEqual(names, w) {
  131. t.Errorf("names = %v, want %v", names, w)
  132. }
  133. }
  134. func TestLoadNewestSnap(t *testing.T) {
  135. dir := path.Join(os.TempDir(), "snapshot")
  136. err := os.Mkdir(dir, 0700)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. defer os.RemoveAll(dir)
  141. ss := New(dir)
  142. err = ss.save(testSnap)
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. newSnap := *testSnap
  147. newSnap.Metadata.Index = 5
  148. err = ss.save(&newSnap)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. g, err := ss.Load()
  153. if err != nil {
  154. t.Errorf("err = %v, want nil", err)
  155. }
  156. if !reflect.DeepEqual(g, &newSnap) {
  157. t.Errorf("snap = %#v, want %#v", g, &newSnap)
  158. }
  159. }
  160. func TestNoSnapshot(t *testing.T) {
  161. dir := path.Join(os.TempDir(), "snapshot")
  162. err := os.Mkdir(dir, 0700)
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. defer os.RemoveAll(dir)
  167. ss := New(dir)
  168. _, err = ss.Load()
  169. if err != ErrNoSnapshot {
  170. t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
  171. }
  172. }
  173. func TestEmptySnapshot(t *testing.T) {
  174. dir := path.Join(os.TempDir(), "snapshot")
  175. err := os.Mkdir(dir, 0700)
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. defer os.RemoveAll(dir)
  180. err = ioutil.WriteFile(path.Join(dir, "1.snap"), []byte(""), 0x700)
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. _, err = Read(path.Join(dir, "1.snap"))
  185. if err != ErrEmptySnapshot {
  186. t.Errorf("err = %v, want %v", err, ErrEmptySnapshot)
  187. }
  188. }
  189. // TestAllSnapshotBroken ensures snapshotter returns
  190. // ErrNoSnapshot if all the snapshots are broken.
  191. func TestAllSnapshotBroken(t *testing.T) {
  192. dir := path.Join(os.TempDir(), "snapshot")
  193. err := os.Mkdir(dir, 0700)
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. defer os.RemoveAll(dir)
  198. err = ioutil.WriteFile(path.Join(dir, "1.snap"), []byte("bad"), 0x700)
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. ss := New(dir)
  203. _, err = ss.Load()
  204. if err != ErrNoSnapshot {
  205. t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
  206. }
  207. }