-
Notifications
You must be signed in to change notification settings - Fork 2
/
watcher_test.go
112 lines (102 loc) · 2.03 KB
/
watcher_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
112
package disco_test
import (
"reflect"
"testing"
"github.com/deixis/spine/disco"
)
func TestDiff(t *testing.T) {
// Add first node
diff := disco.Diff{}
a := []*disco.Instance{
&disco.Instance{
ID: "alpha",
Name: "Instance Alpha",
Host: "127.0.0.1",
Port: 1001,
},
}
expect := []*disco.Event{
&disco.Event{
Op: disco.Add,
Instance: a[0],
},
}
res := diff.Apply(a)
if !reflect.DeepEqual(expect, res) {
t.Errorf("expect state A to be %v, but got %v", expect, res)
}
// Add second node
b := []*disco.Instance{
&disco.Instance{
ID: "alpha",
Name: "Instance Alpha",
Host: "127.0.0.1",
Port: 1001,
},
&disco.Instance{
ID: "beta",
Name: "Instance Beta",
Host: "127.0.0.1",
Port: 1002,
},
}
expect = []*disco.Event{
&disco.Event{
Op: disco.Update,
Instance: b[0], // TODO: Remove once diff tests when updates are needed
},
&disco.Event{
Op: disco.Add,
Instance: b[1],
},
}
res = diff.Apply(b)
if !reflect.DeepEqual(expect, res) {
t.Errorf("expect state B to be %v, but got %v", expect, res)
}
// Remove first node
c := []*disco.Instance{
&disco.Instance{
ID: "beta",
Name: "Instance Beta",
Host: "127.0.0.1",
Port: 1002,
},
}
expect = []*disco.Event{
&disco.Event{
Op: disco.Update,
Instance: c[0], // TODO: Remove once diff tests when updates are needed
},
&disco.Event{
Op: disco.Delete,
Instance: &disco.Instance{
ID: "alpha",
Name: "Instance Alpha",
Host: "127.0.0.1",
Port: 1001,
},
},
}
res = diff.Apply(c)
if !reflect.DeepEqual(expect, res) {
t.Errorf("expect state C to be %v, but got %v", expect, res)
}
// Remove second node
d := []*disco.Instance{}
expect = []*disco.Event{
&disco.Event{
Op: disco.Delete,
Instance: &disco.Instance{
ID: "beta",
Name: "Instance Beta",
Host: "127.0.0.1",
Port: 1002,
},
},
}
res = diff.Apply(d)
if !reflect.DeepEqual(expect, res) {
t.Errorf("expect state D to be %v, but got %v", expect, res)
}
}