123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- // Copyright 2015 The etcd Authors
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package snap
- import (
- "fmt"
- "hash/crc32"
- "io/ioutil"
- "os"
- "path"
- "reflect"
- "testing"
- "github.com/coreos/etcd/raft/raftpb"
- )
- var testSnap = &raftpb.Snapshot{
- Data: []byte("some snapshot"),
- Metadata: raftpb.SnapshotMetadata{
- ConfState: raftpb.ConfState{
- Nodes: []uint64{1, 2, 3},
- },
- Index: 1,
- Term: 1,
- },
- }
- func TestSaveAndLoad(t *testing.T) {
- dir := path.Join(os.TempDir(), "snapshot")
- err := os.Mkdir(dir, 0700)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dir)
- ss := New(dir)
- err = ss.save(testSnap)
- if err != nil {
- t.Fatal(err)
- }
- g, err := ss.Load()
- if err != nil {
- t.Errorf("err = %v, want nil", err)
- }
- if !reflect.DeepEqual(g, testSnap) {
- t.Errorf("snap = %#v, want %#v", g, testSnap)
- }
- }
- func TestBadCRC(t *testing.T) {
- dir := path.Join(os.TempDir(), "snapshot")
- err := os.Mkdir(dir, 0700)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dir)
- ss := New(dir)
- err = ss.save(testSnap)
- if err != nil {
- t.Fatal(err)
- }
- defer func() { crcTable = crc32.MakeTable(crc32.Castagnoli) }()
- // switch to use another crc table
- // fake a crc mismatch
- crcTable = crc32.MakeTable(crc32.Koopman)
- _, err = Read(path.Join(dir, fmt.Sprintf("%016x-%016x.snap", 1, 1)))
- if err == nil || err != ErrCRCMismatch {
- t.Errorf("err = %v, want %v", err, ErrCRCMismatch)
- }
- }
- func TestFailback(t *testing.T) {
- dir := path.Join(os.TempDir(), "snapshot")
- err := os.Mkdir(dir, 0700)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dir)
- large := fmt.Sprintf("%016x-%016x-%016x.snap", 0xFFFF, 0xFFFF, 0xFFFF)
- err = ioutil.WriteFile(path.Join(dir, large), []byte("bad data"), 0666)
- if err != nil {
- t.Fatal(err)
- }
- ss := New(dir)
- err = ss.save(testSnap)
- if err != nil {
- t.Fatal(err)
- }
- g, err := ss.Load()
- if err != nil {
- t.Errorf("err = %v, want nil", err)
- }
- if !reflect.DeepEqual(g, testSnap) {
- t.Errorf("snap = %#v, want %#v", g, testSnap)
- }
- if f, err := os.Open(path.Join(dir, large) + ".broken"); err != nil {
- t.Fatal("broken snapshot does not exist")
- } else {
- f.Close()
- }
- }
- func TestSnapNames(t *testing.T) {
- dir := path.Join(os.TempDir(), "snapshot")
- err := os.Mkdir(dir, 0700)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dir)
- for i := 1; i <= 5; i++ {
- var f *os.File
- if f, err = os.Create(path.Join(dir, fmt.Sprintf("%d.snap", i))); err != nil {
- t.Fatal(err)
- } else {
- f.Close()
- }
- }
- ss := New(dir)
- names, err := ss.snapNames()
- if err != nil {
- t.Errorf("err = %v, want nil", err)
- }
- if len(names) != 5 {
- t.Errorf("len = %d, want 10", len(names))
- }
- w := []string{"5.snap", "4.snap", "3.snap", "2.snap", "1.snap"}
- if !reflect.DeepEqual(names, w) {
- t.Errorf("names = %v, want %v", names, w)
- }
- }
- func TestLoadNewestSnap(t *testing.T) {
- dir := path.Join(os.TempDir(), "snapshot")
- err := os.Mkdir(dir, 0700)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dir)
- ss := New(dir)
- err = ss.save(testSnap)
- if err != nil {
- t.Fatal(err)
- }
- newSnap := *testSnap
- newSnap.Metadata.Index = 5
- err = ss.save(&newSnap)
- if err != nil {
- t.Fatal(err)
- }
- g, err := ss.Load()
- if err != nil {
- t.Errorf("err = %v, want nil", err)
- }
- if !reflect.DeepEqual(g, &newSnap) {
- t.Errorf("snap = %#v, want %#v", g, &newSnap)
- }
- }
- func TestNoSnapshot(t *testing.T) {
- dir := path.Join(os.TempDir(), "snapshot")
- err := os.Mkdir(dir, 0700)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dir)
- ss := New(dir)
- _, err = ss.Load()
- if err != ErrNoSnapshot {
- t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
- }
- }
- func TestEmptySnapshot(t *testing.T) {
- dir := path.Join(os.TempDir(), "snapshot")
- err := os.Mkdir(dir, 0700)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dir)
- err = ioutil.WriteFile(path.Join(dir, "1.snap"), []byte(""), 0x700)
- if err != nil {
- t.Fatal(err)
- }
- _, err = Read(path.Join(dir, "1.snap"))
- if err != ErrEmptySnapshot {
- t.Errorf("err = %v, want %v", err, ErrEmptySnapshot)
- }
- }
- // TestAllSnapshotBroken ensures snapshotter returns
- // ErrNoSnapshot if all the snapshots are broken.
- func TestAllSnapshotBroken(t *testing.T) {
- dir := path.Join(os.TempDir(), "snapshot")
- err := os.Mkdir(dir, 0700)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dir)
- err = ioutil.WriteFile(path.Join(dir, "1.snap"), []byte("bad"), 0x700)
- if err != nil {
- t.Fatal(err)
- }
- ss := New(dir)
- _, err = ss.Load()
- if err != ErrNoSnapshot {
- t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
- }
- }
|