-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_test.go
183 lines (161 loc) · 4.01 KB
/
controller_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"testing"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
)
type testWriter struct {
rendered chan string
stopOn int
}
func newTestWriter(stopOn int) *testWriter {
if stopOn <= 0 {
panic("stopOn must be >= 0")
}
return &testWriter{
rendered: make(chan string, 1),
stopOn: stopOn,
}
}
func (tw *testWriter) Write(p []byte) (int, error) {
tw.rendered <- string(p)
tw.stopOn--
if tw.stopOn == 0 {
close(tw.rendered)
}
return len(p), nil
}
func createInternalNode(name string, addr string) *corev1.Node {
return createNode(name, []corev1.NodeAddress{
{
Type: corev1.NodeInternalIP,
Address: addr,
},
})
}
func TestRun(t *testing.T) {
tests := []struct {
name string
nodeMutator func(cs kubernetes.Interface) error
stopOn int
wantHosts string
}{
{
name: "add nodes",
nodeMutator: func(cs kubernetes.Interface) error {
node1 := createInternalNode("host1", "1.1.1.1")
node2 := createInternalNode("host2", "2.2.2.2")
for i, node := range []*corev1.Node{node1, node2} {
i++
if _, err := cs.CoreV1().Nodes().Create(node); err != nil {
return fmt.Errorf("failed to create node #%d: %s", i, err)
}
}
return nil
},
stopOn: 2,
wantHosts: hostsFile{
"host1": "1.1.1.1",
"host2": "2.2.2.2",
}.String(),
},
{
name: "delete node",
nodeMutator: func(cs kubernetes.Interface) error {
node1 := createInternalNode("host1", "1.1.1.1")
node2 := createInternalNode("host2", "2.2.2.2")
for i, node := range []*corev1.Node{node1, node2} {
i++
if _, err := cs.CoreV1().Nodes().Create(node); err != nil {
return fmt.Errorf("failed to create node #%d: %s", i, err)
}
}
err := cs.CoreV1().Nodes().Delete("host2", &metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete node2: %s", err)
}
return nil
},
stopOn: 3,
wantHosts: hostsFile{
"host1": "1.1.1.1",
}.String(),
},
{
name: "no update",
nodeMutator: func(cs kubernetes.Interface) error {
node1 := createInternalNode("host1", "1.1.1.1")
node2 := createInternalNode("host2", "2.2.2.2")
_, err := cs.CoreV1().Nodes().Create(node1)
if err != nil {
return fmt.Errorf("failed to create node1: %s", err)
}
_, err = cs.CoreV1().Nodes().Update(node1)
if err != nil {
return fmt.Errorf("failed to update node1: %s", err)
}
node1.ResourceVersion = "42"
_, err = cs.CoreV1().Nodes().Update(node1)
if err != nil {
return fmt.Errorf("failed to update node1 with different resource version: %s", err)
}
_, err = cs.CoreV1().Nodes().Create(node2)
if err != nil {
return fmt.Errorf("failed to create node2: %s", err)
}
return nil
},
stopOn: 2,
wantHosts: hostsFile{
"host1": "1.1.1.1",
"host2": "2.2.2.2",
}.String(),
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cs := fake.NewSimpleClientset()
informerFactory := informers.NewSharedInformerFactory(cs, 0)
writer := newTestWriter(test.stopOn)
stopCh := make(chan struct{}, 1)
con := newController(cs, informerFactory.Core().V1().Nodes(), writer)
informerFactory.Start(stopCh)
syncDoneCh := make(chan struct{}, 1)
go func() {
con.Run(stopCh, syncDoneCh)
}()
defer func() {
close(stopCh)
}()
// Wait for caches to sync so that no injected objects are skipped.
<-syncDoneCh
if err := test.nodeMutator(cs); err != nil {
t.Fatal(err)
}
timeout := time.After(3 * time.Second)
var gotHosts string
Loop:
for {
select {
case hosts, ok := <-writer.rendered:
if !ok {
break Loop
}
gotHosts = hosts
case <-timeout:
t.Fatal("timeout fired")
}
}
if gotHosts != test.wantHosts {
t.Error(hostsFileDiff(gotHosts, test.wantHosts))
}
})
}
}