forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 100
/
publisher_test.go
161 lines (145 loc) · 4.5 KB
/
publisher_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
package servicecacertpublisher
import (
"reflect"
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/kubernetes/pkg/controller"
)
func TestConfigMapCreation(t *testing.T) {
ns := metav1.NamespaceDefault
caConfigMap := defaultCrtConfigMapPtr()
addAnnotationCM := defaultCrtConfigMapPtr()
addAnnotationCM.Annotations["test"] = "test"
modifyAnnotationCM := defaultCrtConfigMapPtr()
modifyAnnotationCM.Annotations["service.beta.openshift.io/inject-cabundle"] = "no"
otherConfigMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "other",
Namespace: ns,
ResourceVersion: "1",
},
}
updateOtherConfigMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "other",
Namespace: ns,
ResourceVersion: "1",
Annotations: map[string]string{"test": "true"},
},
}
existNS := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: ns},
Status: v1.NamespaceStatus{
Phase: v1.NamespaceActive,
},
}
newNs := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "new"},
Status: v1.NamespaceStatus{
Phase: v1.NamespaceActive,
},
}
terminatingNS := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: ns},
Status: v1.NamespaceStatus{
Phase: v1.NamespaceTerminating,
},
}
type action struct {
verb string
name string
}
testcases := map[string]struct {
ExistingConfigMaps []*v1.ConfigMap
AddedNamespace *v1.Namespace
UpdatedNamespace *v1.Namespace
DeletedConfigMap *v1.ConfigMap
UpdatedConfigMap *v1.ConfigMap
ExpectActions []action
}{
"create new namespace": {
AddedNamespace: newNs,
ExpectActions: []action{{verb: "create", name: ServiceCACertConfigMapName}},
},
"delete other configmap": {
ExistingConfigMaps: []*v1.ConfigMap{otherConfigMap, caConfigMap},
DeletedConfigMap: otherConfigMap,
},
"delete ca configmap": {
ExistingConfigMaps: []*v1.ConfigMap{otherConfigMap, caConfigMap},
DeletedConfigMap: caConfigMap,
ExpectActions: []action{{verb: "create", name: ServiceCACertConfigMapName}},
},
"update ca configmap with adding annotation": {
ExistingConfigMaps: []*v1.ConfigMap{caConfigMap},
UpdatedConfigMap: addAnnotationCM,
ExpectActions: []action{{verb: "update", name: ServiceCACertConfigMapName}},
},
"update ca configmap with modifying annotation": {
ExistingConfigMaps: []*v1.ConfigMap{caConfigMap},
UpdatedConfigMap: modifyAnnotationCM,
ExpectActions: []action{{verb: "update", name: ServiceCACertConfigMapName}},
},
"update with other configmap": {
ExistingConfigMaps: []*v1.ConfigMap{caConfigMap, otherConfigMap},
UpdatedConfigMap: updateOtherConfigMap,
},
"update namespace with terminating state": {
UpdatedNamespace: terminatingNS,
},
}
for k, tc := range testcases {
t.Run(k, func(t *testing.T) {
client := fake.NewSimpleClientset(caConfigMap, existNS)
informers := informers.NewSharedInformerFactory(fake.NewSimpleClientset(), controller.NoResyncPeriodFunc())
cmInformer := informers.Core().V1().ConfigMaps()
nsInformer := informers.Core().V1().Namespaces()
controller, err := NewPublisher(cmInformer, nsInformer, client)
if err != nil {
t.Fatalf("error creating controller: %v", err)
}
cmStore := cmInformer.Informer().GetStore()
controller.syncHandler = controller.syncNamespace
for _, s := range tc.ExistingConfigMaps {
cmStore.Add(s)
}
if tc.AddedNamespace != nil {
controller.namespaceAdded(tc.AddedNamespace)
}
if tc.UpdatedNamespace != nil {
controller.namespaceUpdated(nil, tc.UpdatedNamespace)
}
if tc.DeletedConfigMap != nil {
cmStore.Delete(tc.DeletedConfigMap)
controller.configMapDeleted(tc.DeletedConfigMap)
}
if tc.UpdatedConfigMap != nil {
cmStore.Add(tc.UpdatedConfigMap)
controller.configMapUpdated(nil, tc.UpdatedConfigMap)
}
for controller.queue.Len() != 0 {
controller.processNextWorkItem()
}
actions := client.Actions()
if reflect.DeepEqual(actions, tc.ExpectActions) {
t.Errorf("Unexpected actions:\n%s", diff.ObjectGoPrintDiff(actions, tc.ExpectActions))
}
})
}
}
func defaultCrtConfigMapPtr() *v1.ConfigMap {
tmp := v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: ServiceCACertConfigMapName,
Annotations: map[string]string{
"service.beta.openshift.io/inject-cabundle": "true",
},
},
}
tmp.Namespace = metav1.NamespaceDefault
return &tmp
}