Skip to content

Commit 88a9156

Browse files
committed
Addressed some of pwittrock comments
1 parent 5c37148 commit 88a9156

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

pkg/cache/cache_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
const testNamespaceOne = "test-namespace-1"
3636
const testNamespaceTwo = "test-namespace-2"
3737

38+
// TODO(community): Pull these helper functions into testenv.
39+
// Restart policy is included to allow indexing on that field.
3840
func createPod(name, namespace string, restartPolicy kcorev1.RestartPolicy) runtime.Object {
3941
three := int64(3)
4042
pod := &kcorev1.Pod{
@@ -80,6 +82,7 @@ var _ = Describe("Informer Cache", func() {
8082
Expect(cfg).NotTo(BeNil())
8183

8284
By("creating three pods")
85+
// Includes restart policy since these objects are indexed on this field.
8386
knownPod1 = createPod("test-pod-1", testNamespaceOne, kcorev1.RestartPolicyNever)
8487
knownPod2 = createPod("test-pod-2", testNamespaceTwo, kcorev1.RestartPolicyAlways)
8588
knownPod3 = createPod("test-pod-3", testNamespaceTwo, kcorev1.RestartPolicyOnFailure)
@@ -91,9 +94,9 @@ var _ = Describe("Informer Cache", func() {
9194
By("running the cache and waiting for it to sync")
9295
go func() {
9396
defer GinkgoRecover()
94-
Expect(informerCache.Start(stop)).ToNot(HaveOccurred())
97+
Expect(informerCache.Start(stop)).To(Succeed())
9598
}()
96-
Expect(informerCache.WaitForCacheSync(stop)).NotTo(BeFalse())
99+
Expect(informerCache.WaitForCacheSync(stop)).To(BeTrue())
97100
})
98101

99102
AfterEach(func() {
@@ -109,10 +112,10 @@ var _ = Describe("Informer Cache", func() {
109112
It("should be able to list objects that haven't been watched previously", func() {
110113
By("listing all services in the cluster")
111114
listObj := &kcorev1.ServiceList{}
112-
Expect(informerCache.List(context.Background(), nil, listObj)).NotTo(HaveOccurred())
115+
Expect(informerCache.List(context.Background(), nil, listObj)).To(Succeed())
113116

114117
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.
116119
Expect(listObj.Items).NotTo(BeEmpty())
117120
hasKubeService := false
118121
for _, svc := range listObj.Items {
@@ -128,7 +131,7 @@ var _ = Describe("Informer Cache", func() {
128131
By("getting the Kubernetes service")
129132
svc := &kcorev1.Service{}
130133
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())
132135

133136
By("verifying that the returned service looks reasonable")
134137
Expect(svc.Name).To(Equal("kubernetes"))
@@ -140,7 +143,7 @@ var _ = Describe("Informer Cache", func() {
140143
// NB: each pod has a "test-label" equal to the pod name
141144
out := kcorev1.PodList{}
142145
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())
144147

145148
By("verifying the returned pods have the correct label")
146149
Expect(out.Items).NotTo(BeEmpty())
@@ -154,7 +157,7 @@ var _ = Describe("Informer Cache", func() {
154157
listObj := &kcorev1.PodList{}
155158
Expect(informerCache.List(context.Background(),
156159
client.InNamespace(testNamespaceOne),
157-
listObj)).NotTo(HaveOccurred())
160+
listObj)).To(Succeed())
158161

159162
By("verifying that the returned pods are in test-namespace-1")
160163
Expect(listObj.Items).NotTo(BeEmpty())
@@ -167,7 +170,7 @@ var _ = Describe("Informer Cache", func() {
167170
By("retrieving a specific pod from the cache")
168171
out := &kcorev1.Pod{}
169172
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())
171174

172175
By("verifying the retrieved pod is equal to a known pod")
173176
Expect(out).To(Equal(knownPod2))
@@ -179,7 +182,7 @@ var _ = Describe("Informer Cache", func() {
179182
Expect(out).NotTo(Equal(knownPod2))
180183
})
181184

182-
It("should error out for missing objects", func() {
185+
It("should return an error if the object is not found", func() {
183186
By("getting a service that does not exists")
184187
svc := &kcorev1.Service{}
185188
svcKey := client.ObjectKey{Namespace: "unknown", Name: "unknown"}
@@ -230,6 +233,7 @@ var _ = Describe("Informer Cache", func() {
230233
close(done)
231234
})
232235

236+
// TODO: Add a test for when GVK is not in Scheme. Does code support informer for unstructured object?
233237
It("should be able to get an informer by group/version/kind", func(done Done) {
234238
By("getting an shared index informer for gvk = core/v1/pod")
235239
gvk := schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"}
@@ -279,20 +283,20 @@ var _ = Describe("Informer Cache", func() {
279283
indexFunc := func(obj runtime.Object) []string {
280284
return []string{string(obj.(*kcorev1.Pod).Spec.RestartPolicy)}
281285
}
282-
Expect(informer.IndexField(pod, "spec.restartPolicy", indexFunc)).ToNot(HaveOccurred())
286+
Expect(informer.IndexField(pod, "spec.restartPolicy", indexFunc)).To(Succeed())
283287

284288
By("running the cache and waiting for it to sync")
285289
go func() {
286290
defer GinkgoRecover()
287-
Expect(informer.Start(stop)).ToNot(HaveOccurred())
291+
Expect(informer.Start(stop)).To(Succeed())
288292
}()
289293
Expect(informer.WaitForCacheSync(stop)).NotTo(BeFalse())
290294

291295
By("listing Pods with restartPolicyOnFailure")
292296
listObj := &kcorev1.PodList{}
293297
Expect(informer.List(context.Background(),
294298
client.MatchingField("spec.restartPolicy", "OnFailure"),
295-
listObj)).NotTo(HaveOccurred())
299+
listObj)).To(Succeed())
296300

297301
By("verifying that the returned pods have correct restart policy")
298302
Expect(listObj.Items).NotTo(BeEmpty())

0 commit comments

Comments
 (0)