This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
controler_test.go
151 lines (117 loc) · 3.72 KB
/
controler_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
package main
import (
"context"
"testing"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kubeinformers "k8s.io/client-go/informers"
k8sfake "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
)
var (
alwaysReady = func() bool { return true }
noResyncPeriodFunc = func() time.Duration { return 0 }
)
type fixture struct {
t *testing.T
kubeclient *k8sfake.Clientset
// Objects to put in the store.
nodesLister []*corev1.Node
actions []string
// Objects from here preloaded into NewSimpleFake.
kubeobjects []runtime.Object
}
func newFixture(t *testing.T) *fixture {
f := &fixture{}
f.t = t
f.kubeobjects = []runtime.Object{}
return f
}
func (f *fixture) newController(ipUpdater *MockIPUpdater) (*NodeController, kubeinformers.SharedInformerFactory) {
f.kubeclient = k8sfake.NewSimpleClientset(f.kubeobjects...)
k8sI := kubeinformers.NewSharedInformerFactory(f.kubeclient, noResyncPeriodFunc())
c := NewNodeController(f.kubeclient, k8sI.Core().V1().Nodes(), ipUpdater)
c.nodesSynced = alwaysReady
c.recorder = &record.FakeRecorder{}
for _, d := range f.nodesLister {
k8sI.Core().V1().Nodes().Informer().GetIndexer().Add(d)
}
return c, k8sI
}
func (f *fixture) run(name string) {
f.runController(name, true, false)
}
func getKey(node *corev1.Node, t *testing.T) string {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(node)
if err != nil {
t.Errorf("Unexpected error getting key for node %v: %v", node.Name, err)
return ""
}
return key
}
type MockIPUpdater struct {
actions []string
}
func (m *MockIPUpdater) CreateOrUpdateVMPulicIP(ctx context.Context, vmName string, ipName string) error {
m.actions = append(m.actions, "IP_CREATE")
return nil
}
func (m *MockIPUpdater) DeletePublicIP(ctx context.Context, ipName string) error {
m.actions = append(m.actions, "IP_DELETE")
return nil
}
func (m *MockIPUpdater) DisassociatePublicIPForNode(ctx context.Context, nodeName string) error {
m.actions = append(m.actions, "IP_DISASSOCIATE")
return nil
}
func TestAddNode(t *testing.T) {
f := newFixture(t)
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "testNode"}}
f.nodesLister = append(f.nodesLister, node)
f.kubeobjects = append(f.kubeobjects, node)
f.expectCreateIPAction()
f.run(getKey(node, t))
}
func TestDeleteNode(t *testing.T) {
f := newFixture(t)
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "testNode"}}
f.expectDeleteIPAction()
f.run(getKey(node, t))
}
func (f *fixture) expectCreateIPAction() {
f.actions = append(f.actions, "IP_CREATE")
}
func (f *fixture) expectDeleteIPAction() {
f.actions = append(f.actions, "IP_DELETE")
}
func (f *fixture) runController(nodeName string, startInformers bool, expectError bool) {
ipUpdater := &MockIPUpdater{actions: []string{}}
c, k8sI := f.newController(ipUpdater)
if startInformers {
stopCh := make(chan struct{})
defer close(stopCh)
k8sI.Start(stopCh)
}
err := c.syncHandler(nodeName)
if !expectError && err != nil {
f.t.Errorf("error syncing node: %v", err)
} else if expectError && err == nil {
f.t.Error("expected error syncing node, got nil")
}
for i, action := range ipUpdater.actions {
if len(f.actions) < i+1 {
f.t.Errorf("%d unexpected actions: %+v", len(ipUpdater.actions)-len(f.actions), ipUpdater.actions[i:])
break
}
expectedAction := f.actions[i]
if action != expectedAction {
f.t.Errorf("Different expected actions, action %v vs expectedAction %v", action, expectedAction)
}
}
if len(f.actions) > len(ipUpdater.actions) {
f.t.Errorf("%d additional expected actions:%+v", len(f.actions)-len(ipUpdater.actions), f.actions[len(ipUpdater.actions):])
}
}