snapshotter_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2015 CoreOS, Inc.
  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. if f, err := os.Create(path.Join(dir, fmt.Sprintf("%d.snap", i))); err != nil {
  115. t.Fatal(err)
  116. } else {
  117. f.Close()
  118. }
  119. }
  120. ss := New(dir)
  121. names, err := ss.snapNames()
  122. if err != nil {
  123. t.Errorf("err = %v, want nil", err)
  124. }
  125. if len(names) != 5 {
  126. t.Errorf("len = %d, want 10", len(names))
  127. }
  128. w := []string{"5.snap", "4.snap", "3.snap", "2.snap", "1.snap"}
  129. if !reflect.DeepEqual(names, w) {
  130. t.Errorf("names = %v, want %v", names, w)
  131. }
  132. }
  133. func TestLoadNewestSnap(t *testing.T) {
  134. dir := path.Join(os.TempDir(), "snapshot")
  135. err := os.Mkdir(dir, 0700)
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. defer os.RemoveAll(dir)
  140. ss := New(dir)
  141. err = ss.save(testSnap)
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. newSnap := *testSnap
  146. newSnap.Metadata.Index = 5
  147. err = ss.save(&newSnap)
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. g, err := ss.Load()
  152. if err != nil {
  153. t.Errorf("err = %v, want nil", err)
  154. }
  155. if !reflect.DeepEqual(g, &newSnap) {
  156. t.Errorf("snap = %#v, want %#v", g, &newSnap)
  157. }
  158. }
  159. func TestNoSnapshot(t *testing.T) {
  160. dir := path.Join(os.TempDir(), "snapshot")
  161. err := os.Mkdir(dir, 0700)
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. defer os.RemoveAll(dir)
  166. ss := New(dir)
  167. _, err = ss.Load()
  168. if err != ErrNoSnapshot {
  169. t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
  170. }
  171. }
  172. func TestEmptySnapshot(t *testing.T) {
  173. dir := path.Join(os.TempDir(), "snapshot")
  174. err := os.Mkdir(dir, 0700)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. defer os.RemoveAll(dir)
  179. err = ioutil.WriteFile(path.Join(dir, "1.snap"), []byte(""), 0x700)
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. _, err = Read(path.Join(dir, "1.snap"))
  184. if err != ErrEmptySnapshot {
  185. t.Errorf("err = %v, want %v", err, ErrEmptySnapshot)
  186. }
  187. }
  188. // TestAllSnapshotBroken ensures snapshotter returens
  189. // ErrNoSnapshot if all the snapshots are broken.
  190. func TestAllSnapshotBroken(t *testing.T) {
  191. dir := path.Join(os.TempDir(), "snapshot")
  192. err := os.Mkdir(dir, 0700)
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. defer os.RemoveAll(dir)
  197. err = ioutil.WriteFile(path.Join(dir, "1.snap"), []byte("bad"), 0x700)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. ss := New(dir)
  202. _, err = ss.Load()
  203. if err != ErrNoSnapshot {
  204. t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
  205. }
  206. }