-
Notifications
You must be signed in to change notification settings - Fork 30
/
restore_test.go
111 lines (91 loc) · 3.23 KB
/
restore_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"reflect"
"sync"
"testing"
"github.com/coreos/go-etcd/etcd"
)
func TestLoadDataSet(t *testing.T) {
dataSet := LoadDataSet("fixtures/etcd-dump.json")
value := "testValue"
expectedDataSet := []BackupKey{BackupKey{Key: "/test", Value: &value}}
if !reflect.DeepEqual(*dataSet, expectedDataSet) {
t.Fatal("Unexpected dataSet, expected ", expectedDataSet, "got", *dataSet)
}
}
func TestRestoreDataSet(t *testing.T) {
etcdClientTest, _ := initTestClient()
etcdClientTest.On("SetDir", "/test", 0).Return(&etcd.Response{}, nil)
backupKeys := []BackupKey{BackupKey{Key: "/test"}}
RestoreDataSet(backupKeys, config, etcdClientTest)
etcdClientTest.Mock.AssertExpectations(t)
}
func TestNewRestoreStatistics(t *testing.T) {
value := "testValue"
backupKeys := []BackupKey{BackupKey{Key: "/test"}, BackupKey{Key: "/test1", Value: &value}}
result := NewRestoreStatistics(backupKeys)
if *result["DataSetSize"] != int32(2) {
t.Fatal("Unexpected DataSetSize, expected 2 got", result["DataSetSize"])
}
}
func TestRestoreKey(t *testing.T) {
backupKey := &BackupKey{Key: "/test"}
keyFunc := func(backupKey *BackupKey,
statistics map[string]*int32,
wg *sync.WaitGroup,
throttle chan int,
etcdClientTest *MockedEtcdClient) {
throttle <- 1
etcdClientTest.On("SetDir", "/test", 0).Return(&etcd.Response{}, nil)
RestoreKey(backupKey, statistics, wg, throttle, etcdClientTest)
}
checkRestoreKeys(t, backupKey, keyFunc)
}
func TestRestoreDir(t *testing.T) {
value := "testValue"
backupKey := &BackupKey{Key: "/test1", Value: &value}
keyFunc := func(backupKey *BackupKey,
statistics map[string]*int32,
wg *sync.WaitGroup,
throttle chan int,
etcdClientTest *MockedEtcdClient) {
throttle <- 1
etcdClientTest.On("Set", "/test1", value, 0).Return(&etcd.Response{}, nil)
RestoreKey(backupKey, statistics, wg, throttle, etcdClientTest)
}
checkRestoreKeys(t, backupKey, keyFunc)
}
func checkRestoreKeys(t *testing.T,
backupKey *BackupKey,
function func(*BackupKey, map[string]*int32, *sync.WaitGroup, chan int, *MockedEtcdClient)) {
etcdClientTest, _ := initTestClient()
emptyDirectoriesNbr := int32(0)
keysInsertedNbr := int32(0)
statistics := map[string]*int32{"EmptyDirectories": &emptyDirectoriesNbr, "KeysInserted": &keysInsertedNbr}
throttle := make(chan int, 1)
var wg sync.WaitGroup
wg.Add(1)
go function(backupKey, statistics, &wg, throttle, etcdClientTest)
wg.Wait()
if backupKey.Value == nil {
if *statistics["EmptyDirectories"] != int32(1) {
t.Fatal("Unexpected EmptyDirectories number, expected 1 got", *statistics["EmptyDirectories"])
}
}
if *statistics["KeysInserted"] != int32(1) {
t.Fatal("Unexpected KeysInserted number, expected 1 got", *statistics["KeysInserted"])
}
}
func TestSetKey(t *testing.T) {
etcdClientTest, _ := initTestClient()
value := "testValue"
etcdClientTest.On("Set", "/test", value, 0).Return(&etcd.Response{}, nil)
setKey(&BackupKey{Key: "/test", Value: &value}, etcdClientTest)
etcdClientTest.Mock.AssertExpectations(t)
}
func TestSetDirectory(t *testing.T) {
etcdClientTest, _ := initTestClient()
etcdClientTest.On("SetDir", "/test", 0).Return(&etcd.Response{}, nil)
setDirectory(&BackupKey{Key: "/test"}, etcdClientTest)
etcdClientTest.Mock.AssertExpectations(t)
}