Skip to content

Commit 0c800d8

Browse files
committed
Adds test for label filtering across multiple namespaces
1 parent 88a9156 commit 0c800d8

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

pkg/cache/cache_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var _ = Describe("Informer Cache", func() {
109109
})
110110

111111
Describe("as a Reader", func() {
112+
112113
It("should be able to list objects that haven't been watched previously", func() {
113114
By("listing all services in the cluster")
114115
listObj := &kcorev1.ServiceList{}
@@ -138,9 +139,9 @@ var _ = Describe("Informer Cache", func() {
138139
Expect(svc.Namespace).To(Equal("default"))
139140
})
140141

141-
It("should support filtering by labels", func() {
142+
It("should support filtering by labels in a single namespace", func() {
142143
By("listing pods with a particular label")
143-
// NB: each pod has a "test-label" equal to the pod name
144+
// NB: each pod has a "test-label": <pod-name>
144145
out := kcorev1.PodList{}
145146
Expect(informerCache.List(context.Background(), client.InNamespace(testNamespaceTwo).
146147
MatchingLabels(map[string]string{"test-label": "test-pod-2"}), &out)).To(Succeed())
@@ -152,6 +153,27 @@ var _ = Describe("Informer Cache", func() {
152153
Expect(actual.Labels["test-label"]).To(Equal("test-pod-2"))
153154
})
154155

156+
It("should support filtering by labels from multiple namespaces", func() {
157+
By("creating another pod with the same label but different namespace")
158+
anotherPod := createPod("test-pod-2", testNamespaceOne, kcorev1.RestartPolicyAlways)
159+
160+
By("listing pods with a particular label")
161+
// NB: each pod has a "test-label": <pod-name>
162+
out := kcorev1.PodList{}
163+
labels := map[string]string{"test-label": "test-pod-2"}
164+
Expect(informerCache.List(context.Background(),
165+
client.MatchingLabels(labels), &out)).To(Succeed())
166+
167+
By("verifying multiple pods with the same label in different namespaces are returned")
168+
Expect(out.Items).NotTo(BeEmpty())
169+
Expect(out.Items).Should(HaveLen(2))
170+
for _, actual := range out.Items {
171+
Expect(actual.Labels["test-label"]).To(Equal("test-pod-2"))
172+
}
173+
174+
deletePod(anotherPod)
175+
})
176+
155177
It("should be able to list objects by namespace", func() {
156178
By("listing pods in test-namespace-1")
157179
listObj := &kcorev1.PodList{}
@@ -195,6 +217,7 @@ var _ = Describe("Informer Cache", func() {
195217
})
196218

197219
Describe("as an Informer", func() {
220+
198221
It("should be able to get informer for the object", func(done Done) {
199222
By("getting a shared index informer for a pod")
200223
pod := &kcorev1.Pod{

0 commit comments

Comments
 (0)