@@ -35,6 +35,8 @@ import (
35
35
const testNamespaceOne = "test-namespace-1"
36
36
const testNamespaceTwo = "test-namespace-2"
37
37
38
+ // TODO(community): Pull these helper functions into testenv.
39
+ // Restart policy is included to allow indexing on that field.
38
40
func createPod (name , namespace string , restartPolicy kcorev1.RestartPolicy ) runtime.Object {
39
41
three := int64 (3 )
40
42
pod := & kcorev1.Pod {
@@ -80,6 +82,7 @@ var _ = Describe("Informer Cache", func() {
80
82
Expect (cfg ).NotTo (BeNil ())
81
83
82
84
By ("creating three pods" )
85
+ // Includes restart policy since these objects are indexed on this field.
83
86
knownPod1 = createPod ("test-pod-1" , testNamespaceOne , kcorev1 .RestartPolicyNever )
84
87
knownPod2 = createPod ("test-pod-2" , testNamespaceTwo , kcorev1 .RestartPolicyAlways )
85
88
knownPod3 = createPod ("test-pod-3" , testNamespaceTwo , kcorev1 .RestartPolicyOnFailure )
@@ -91,9 +94,9 @@ var _ = Describe("Informer Cache", func() {
91
94
By ("running the cache and waiting for it to sync" )
92
95
go func () {
93
96
defer GinkgoRecover ()
94
- Expect (informerCache .Start (stop )).ToNot ( HaveOccurred ())
97
+ Expect (informerCache .Start (stop )).To ( Succeed ())
95
98
}()
96
- Expect (informerCache .WaitForCacheSync (stop )).NotTo ( BeFalse ())
99
+ Expect (informerCache .WaitForCacheSync (stop )).To ( BeTrue ())
97
100
})
98
101
99
102
AfterEach (func () {
@@ -109,10 +112,10 @@ var _ = Describe("Informer Cache", func() {
109
112
It ("should be able to list objects that haven't been watched previously" , func () {
110
113
By ("listing all services in the cluster" )
111
114
listObj := & kcorev1.ServiceList {}
112
- Expect (informerCache .List (context .Background (), nil , listObj )).NotTo ( HaveOccurred ())
115
+ Expect (informerCache .List (context .Background (), nil , listObj )).To ( Succeed ())
113
116
114
117
By ("verifying that the returned list contains the Kubernetes service" )
115
- // NB: there has to be at least the kubernetes service in the cluster
118
+ // NB: kubernetes default service is automatically created in testenv.
116
119
Expect (listObj .Items ).NotTo (BeEmpty ())
117
120
hasKubeService := false
118
121
for _ , svc := range listObj .Items {
@@ -128,7 +131,7 @@ var _ = Describe("Informer Cache", func() {
128
131
By ("getting the Kubernetes service" )
129
132
svc := & kcorev1.Service {}
130
133
svcKey := client.ObjectKey {Namespace : "default" , Name : "kubernetes" }
131
- Expect (informerCache .Get (context .Background (), svcKey , svc )).NotTo ( HaveOccurred ())
134
+ Expect (informerCache .Get (context .Background (), svcKey , svc )).To ( Succeed ())
132
135
133
136
By ("verifying that the returned service looks reasonable" )
134
137
Expect (svc .Name ).To (Equal ("kubernetes" ))
@@ -140,7 +143,7 @@ var _ = Describe("Informer Cache", func() {
140
143
// NB: each pod has a "test-label" equal to the pod name
141
144
out := kcorev1.PodList {}
142
145
Expect (informerCache .List (context .Background (), client .InNamespace (testNamespaceTwo ).
143
- MatchingLabels (map [string ]string {"test-label" : "test-pod-2" }), & out )).NotTo ( HaveOccurred ())
146
+ MatchingLabels (map [string ]string {"test-label" : "test-pod-2" }), & out )).To ( Succeed ())
144
147
145
148
By ("verifying the returned pods have the correct label" )
146
149
Expect (out .Items ).NotTo (BeEmpty ())
@@ -154,7 +157,7 @@ var _ = Describe("Informer Cache", func() {
154
157
listObj := & kcorev1.PodList {}
155
158
Expect (informerCache .List (context .Background (),
156
159
client .InNamespace (testNamespaceOne ),
157
- listObj )).NotTo ( HaveOccurred ())
160
+ listObj )).To ( Succeed ())
158
161
159
162
By ("verifying that the returned pods are in test-namespace-1" )
160
163
Expect (listObj .Items ).NotTo (BeEmpty ())
@@ -167,7 +170,7 @@ var _ = Describe("Informer Cache", func() {
167
170
By ("retrieving a specific pod from the cache" )
168
171
out := & kcorev1.Pod {}
169
172
podKey := client.ObjectKey {Name : "test-pod-2" , Namespace : testNamespaceTwo }
170
- Expect (informerCache .Get (context .Background (), podKey , out )).NotTo ( HaveOccurred ())
173
+ Expect (informerCache .Get (context .Background (), podKey , out )).To ( Succeed ())
171
174
172
175
By ("verifying the retrieved pod is equal to a known pod" )
173
176
Expect (out ).To (Equal (knownPod2 ))
@@ -179,7 +182,7 @@ var _ = Describe("Informer Cache", func() {
179
182
Expect (out ).NotTo (Equal (knownPod2 ))
180
183
})
181
184
182
- It ("should error out for missing objects " , func () {
185
+ It ("should return an error if the object is not found " , func () {
183
186
By ("getting a service that does not exists" )
184
187
svc := & kcorev1.Service {}
185
188
svcKey := client.ObjectKey {Namespace : "unknown" , Name : "unknown" }
@@ -230,6 +233,7 @@ var _ = Describe("Informer Cache", func() {
230
233
close (done )
231
234
})
232
235
236
+ // TODO: Add a test for when GVK is not in Scheme. Does code support informer for unstructured object?
233
237
It ("should be able to get an informer by group/version/kind" , func (done Done ) {
234
238
By ("getting an shared index informer for gvk = core/v1/pod" )
235
239
gvk := schema.GroupVersionKind {Group : "" , Version : "v1" , Kind : "Pod" }
@@ -279,20 +283,20 @@ var _ = Describe("Informer Cache", func() {
279
283
indexFunc := func (obj runtime.Object ) []string {
280
284
return []string {string (obj .(* kcorev1.Pod ).Spec .RestartPolicy )}
281
285
}
282
- Expect (informer .IndexField (pod , "spec.restartPolicy" , indexFunc )).ToNot ( HaveOccurred ())
286
+ Expect (informer .IndexField (pod , "spec.restartPolicy" , indexFunc )).To ( Succeed ())
283
287
284
288
By ("running the cache and waiting for it to sync" )
285
289
go func () {
286
290
defer GinkgoRecover ()
287
- Expect (informer .Start (stop )).ToNot ( HaveOccurred ())
291
+ Expect (informer .Start (stop )).To ( Succeed ())
288
292
}()
289
293
Expect (informer .WaitForCacheSync (stop )).NotTo (BeFalse ())
290
294
291
295
By ("listing Pods with restartPolicyOnFailure" )
292
296
listObj := & kcorev1.PodList {}
293
297
Expect (informer .List (context .Background (),
294
298
client .MatchingField ("spec.restartPolicy" , "OnFailure" ),
295
- listObj )).NotTo ( HaveOccurred ())
299
+ listObj )).To ( Succeed ())
296
300
297
301
By ("verifying that the returned pods have correct restart policy" )
298
302
Expect (listObj .Items ).NotTo (BeEmpty ())
0 commit comments