2014-08-19 22:13:25 +04:00
|
|
|
package snap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash/crc32"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/raft"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testSnap = &raft.Snapshot{
|
|
|
|
ClusterId: 0xBEEF,
|
|
|
|
Data: []byte("some snapshot"),
|
|
|
|
Nodes: []int64{1, 2, 3},
|
|
|
|
Index: 1,
|
|
|
|
Term: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveAndLoad(t *testing.T) {
|
|
|
|
dir := path.Join(os.TempDir(), "snapshot")
|
2014-08-20 08:28:43 +04:00
|
|
|
err := os.Mkdir(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-19 22:13:25 +04:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
ss := New(dir)
|
2014-08-20 08:28:43 +04:00
|
|
|
err = ss.Save(testSnap)
|
2014-08-19 22:13:25 +04:00
|
|
|
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")
|
2014-08-20 08:28:43 +04:00
|
|
|
err := os.Mkdir(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-19 22:13:25 +04:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
ss := New(dir)
|
2014-08-20 08:28:43 +04:00
|
|
|
err = ss.Save(testSnap)
|
2014-08-19 22:13:25 +04:00
|
|
|
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 = ss.Load()
|
|
|
|
if err == nil || err != ErrCRCMismatch {
|
|
|
|
t.Errorf("err = %v, want %v", err, ErrCRCMismatch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailback(t *testing.T) {
|
|
|
|
dir := path.Join(os.TempDir(), "snapshot")
|
2014-08-20 08:28:43 +04:00
|
|
|
err := os.Mkdir(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-19 22:13:25 +04:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2014-08-20 04:03:21 +04:00
|
|
|
large := fmt.Sprintf("%016x-%016x-%016x.snap", 0xFFFF, 0xFFFF, 0xFFFF)
|
2014-08-20 08:28:43 +04:00
|
|
|
err = ioutil.WriteFile(path.Join(dir, large), []byte("bad data"), 0666)
|
2014-08-19 22:13:25 +04:00
|
|
|
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)
|
|
|
|
}
|
2014-08-21 00:15:08 +04:00
|
|
|
if f, err := os.Open(path.Join(dir, large) + ".broken"); err != nil {
|
|
|
|
t.Fatal("broken snapshot does not exist")
|
|
|
|
} else {
|
|
|
|
f.Close()
|
|
|
|
}
|
2014-08-19 22:13:25 +04:00
|
|
|
}
|
|
|
|
|
2014-08-20 08:08:29 +04:00
|
|
|
func TestSnapNames(t *testing.T) {
|
|
|
|
dir := path.Join(os.TempDir(), "snapshot")
|
2014-08-20 08:28:43 +04:00
|
|
|
err := os.Mkdir(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-20 08:08:29 +04:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
for i := 1; i <= 5; i++ {
|
2014-08-21 00:15:08 +04:00
|
|
|
if f, err := os.Create(path.Join(dir, fmt.Sprintf("%d.snap", i))); err != nil {
|
2014-08-20 08:08:29 +04:00
|
|
|
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))
|
|
|
|
}
|
2014-08-21 00:15:08 +04:00
|
|
|
w := []string{"5.snap", "4.snap", "3.snap", "2.snap", "1.snap"}
|
2014-08-20 08:08:29 +04:00
|
|
|
if !reflect.DeepEqual(names, w) {
|
|
|
|
t.Errorf("names = %v, want %v", names, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 22:13:25 +04:00
|
|
|
func TestLoadNewestSnap(t *testing.T) {
|
|
|
|
dir := path.Join(os.TempDir(), "snapshot")
|
2014-08-20 08:28:43 +04:00
|
|
|
err := os.Mkdir(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-19 22:13:25 +04:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
ss := New(dir)
|
2014-08-20 08:28:43 +04:00
|
|
|
err = ss.Save(testSnap)
|
2014-08-19 22:13:25 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newSnap := *testSnap
|
|
|
|
newSnap.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")
|
2014-08-20 08:28:43 +04:00
|
|
|
err := os.Mkdir(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-19 22:13:25 +04:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
ss := New(dir)
|
2014-08-20 08:28:43 +04:00
|
|
|
_, err = ss.Load()
|
2014-08-19 22:13:25 +04:00
|
|
|
if err == nil || err != ErrNoSnapshot {
|
|
|
|
t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
|
|
|
|
}
|
|
|
|
}
|