-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
77 lines (60 loc) · 1.95 KB
/
main_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
package main
import (
"testing"
"github.com/coreos/go-etcd/etcd"
"github.com/stretchr/testify/mock"
)
type MockedEtcdClient struct {
mock.Mock
}
func (c *MockedEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, error) {
args := c.Mock.Called(key, sort, recursive)
return args.Get(0).(*etcd.Response), args.Error(1)
}
func (c *MockedEtcdClient) Set(key string, value string, ttl uint64) (*etcd.Response, error) {
args := c.Mock.Called(key, value, ttl)
return args.Get(0).(*etcd.Response), args.Error(1)
}
func (c *MockedEtcdClient) SetDir(key string, ttl uint64) (*etcd.Response, error) {
args := c.Mock.Called(key, ttl)
return args.Get(0).(*etcd.Response), args.Error(1)
}
func init() {
config = &Config{
ConcurentRequest: 1,
Retries: 1,
EtcdConfigPath: "none",
DumpFilePath: "fixtures/etcd-dump.json",
BackupStrategy: &BackupStrategy{[]string{"/"}, true, true},
}
failures = 0
config.LogPrintln = func(v ...interface{}) {}
config.LogFatal = func(v ...interface{}) { failures += 1 }
}
var failures int
func initTestClient() (*MockedEtcdClient, *etcd.Response) {
etcdClientTest := new(MockedEtcdClient)
node := etcd.Node{Key: "/test", Value: "testValue"}
response := etcd.Response{Node: &node}
return etcdClientTest, &response
}
func TestExecuteActionDump(t *testing.T) {
etcdClientTest, response := initTestClient()
etcdClientTest.On("Get", "/", true, true).Return(response, nil)
ExecuteAction("dump", etcdClientTest)
etcdClientTest.Mock.AssertExpectations(t)
}
func TestExecuteActionRestore(t *testing.T) {
etcdClientTest, response := initTestClient()
etcdClientTest.On("Set", "/test", "testValue", 0).Return(response, nil)
ExecuteAction("restore", etcdClientTest)
etcdClientTest.Mock.AssertExpectations(t)
}
func TestExecuteActionBreaking(t *testing.T) {
failures = 0
etcdClientTest, _ := initTestClient()
ExecuteAction("break", etcdClientTest)
if failures != 1 {
t.Fatal("Action is not breaking!")
}
}