etcd/raftsnap/snapshotter_test.go

233 lines
5.2 KiB
Go
Raw Normal View History

2016-05-13 06:51:48 +03:00
// 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 raftsnap
2014-08-19 22:13:25 +04:00
import (
"fmt"
"hash/crc32"
"io/ioutil"
"os"
2017-03-16 05:31:10 +03:00
"path/filepath"
2014-08-19 22:13:25 +04:00
"reflect"
"testing"
"github.com/coreos/etcd/raft/raftpb"
"go.uber.org/zap"
2014-08-19 22:13:25 +04:00
)
var testSnap = &raftpb.Snapshot{
Data: []byte("some snapshot"),
Metadata: raftpb.SnapshotMetadata{
ConfState: raftpb.ConfState{
Nodes: []uint64{1, 2, 3},
},
Index: 1,
Term: 1,
},
2014-08-19 22:13:25 +04:00
}
func TestSaveAndLoad(t *testing.T) {
2017-03-16 05:31:10 +03:00
dir := filepath.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(zap.NewExample(), dir)
2014-09-17 05:18:45 +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) {
2017-03-16 05:31:10 +03:00
dir := filepath.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(zap.NewExample(), dir)
2014-09-17 05:18:45 +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)
2017-03-16 05:31:10 +03:00
_, err = Read(filepath.Join(dir, fmt.Sprintf("%016x-%016x.snap", 1, 1)))
2014-08-19 22:13:25 +04:00
if err == nil || err != ErrCRCMismatch {
t.Errorf("err = %v, want %v", err, ErrCRCMismatch)
}
}
func TestFailback(t *testing.T) {
2017-03-16 05:31:10 +03:00
dir := filepath.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)
large := fmt.Sprintf("%016x-%016x-%016x.snap", 0xFFFF, 0xFFFF, 0xFFFF)
2017-03-16 05:31:10 +03:00
err = ioutil.WriteFile(filepath.Join(dir, large), []byte("bad data"), 0666)
2014-08-19 22:13:25 +04:00
if err != nil {
t.Fatal(err)
}
ss := New(zap.NewExample(), dir)
2014-09-17 05:18:45 +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)
}
2017-03-16 05:31:10 +03:00
if f, err := os.Open(filepath.Join(dir, large) + ".broken"); err != nil {
t.Fatal("broken snapshot does not exist")
} else {
f.Close()
}
2014-08-19 22:13:25 +04:00
}
func TestSnapNames(t *testing.T) {
2017-03-16 05:31:10 +03:00
dir := filepath.Join(os.TempDir(), "snapshot")
2014-08-20 08:28:43 +04:00
err := os.Mkdir(dir, 0700)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
for i := 1; i <= 5; i++ {
2015-08-27 23:24:47 +03:00
var f *os.File
2017-03-16 05:31:10 +03:00
if f, err = os.Create(filepath.Join(dir, fmt.Sprintf("%d.snap", i))); err != nil {
t.Fatal(err)
} else {
f.Close()
}
}
ss := New(zap.NewExample(), 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)
}
}
2014-08-19 22:13:25 +04:00
func TestLoadNewestSnap(t *testing.T) {
2017-03-16 05:31:10 +03:00
dir := filepath.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(zap.NewExample(), dir)
2014-09-17 05:18:45 +04:00
err = ss.save(testSnap)
2014-08-19 22:13:25 +04:00
if err != nil {
t.Fatal(err)
}
newSnap := *testSnap
newSnap.Metadata.Index = 5
2014-09-17 05:18:45 +04:00
err = ss.save(&newSnap)
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, &newSnap) {
t.Errorf("snap = %#v, want %#v", g, &newSnap)
}
}
func TestNoSnapshot(t *testing.T) {
2017-03-16 05:31:10 +03:00
dir := filepath.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(zap.NewExample(), dir)
2014-08-20 08:28:43 +04:00
_, err = ss.Load()
if err != ErrNoSnapshot {
2014-08-19 22:13:25 +04:00
t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
}
}
2014-12-09 07:39:12 +03:00
func TestEmptySnapshot(t *testing.T) {
2017-03-16 05:31:10 +03:00
dir := filepath.Join(os.TempDir(), "snapshot")
2014-12-09 07:39:12 +03:00
err := os.Mkdir(dir, 0700)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
2017-03-16 05:31:10 +03:00
err = ioutil.WriteFile(filepath.Join(dir, "1.snap"), []byte(""), 0x700)
if err != nil {
t.Fatal(err)
}
2017-03-16 05:31:10 +03:00
_, err = Read(filepath.Join(dir, "1.snap"))
if err != ErrEmptySnapshot {
t.Errorf("err = %v, want %v", err, ErrEmptySnapshot)
}
}
2016-02-01 08:42:39 +03:00
// TestAllSnapshotBroken ensures snapshotter returns
// ErrNoSnapshot if all the snapshots are broken.
func TestAllSnapshotBroken(t *testing.T) {
2017-03-16 05:31:10 +03:00
dir := filepath.Join(os.TempDir(), "snapshot")
err := os.Mkdir(dir, 0700)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
2017-03-16 05:31:10 +03:00
err = ioutil.WriteFile(filepath.Join(dir, "1.snap"), []byte("bad"), 0x700)
2014-12-09 07:39:12 +03:00
if err != nil {
t.Fatal(err)
}
ss := New(zap.NewExample(), dir)
2014-12-09 07:39:12 +03:00
_, err = ss.Load()
if err != ErrNoSnapshot {
t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
2014-12-09 07:39:12 +03:00
}
}