Skip to content

Commit f5e9ba1

Browse files
committed
UPSTREAM: <carry>: more kcp tests
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent 83ac60c commit f5e9ba1

File tree

1 file changed

+82
-20
lines changed

1 file changed

+82
-20
lines changed

pkg/kcp/kcp_test.go

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1616
"k8s.io/apimachinery/pkg/types"
17+
"k8s.io/apimachinery/pkg/util/sets"
1718
"k8s.io/client-go/rest"
1819
"sigs.k8s.io/controller-runtime/pkg/cache"
1920
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -47,6 +48,10 @@ var _ = Describe("NewClusterAwareClient", Ordered, func() {
4748
return
4849
}
4950
fallthrough
51+
case "/api/v1/namespaces/default/pods/foo", "/clusters/root/api/v1/namespaces/default/pods/foo":
52+
w.Header().Set("Content-Type", "application/json")
53+
w.WriteHeader(http.StatusOK)
54+
_, _ = w.Write([]byte(`{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"default","resourceVersion":"184126176"}}`))
5055
default:
5156
_, _ = w.Write([]byte(fmt.Sprintf("Not found %q", req.RequestURI)))
5257
w.WriteHeader(http.StatusNotFound)
@@ -78,9 +83,13 @@ var _ = Describe("NewClusterAwareClient", Ordered, func() {
7883
err = cl.List(ctx, pods)
7984
Expect(err).NotTo(HaveOccurred())
8085

86+
pod := &corev1.Pod{}
87+
err = cl.Get(ctx, types.NamespacedName{Namespace: "default", Name: "foo"}, pod)
88+
Expect(err).NotTo(HaveOccurred())
89+
8190
mu.Lock()
8291
defer mu.Unlock()
83-
Expect(paths).To(Equal([]string{"/api/v1", "/api/v1/pods"}))
92+
Expect(paths).To(Equal([]string{"/api/v1", "/api/v1/pods", "/api/v1/namespaces/default/pods/foo"}))
8493
})
8594

8695
It("should work with a cluster in the kontext", func(ctx context.Context) {
@@ -91,9 +100,13 @@ var _ = Describe("NewClusterAwareClient", Ordered, func() {
91100
err = cl.List(kontext.WithCluster(ctx, "root"), pods)
92101
Expect(err).NotTo(HaveOccurred())
93102

103+
pod := &corev1.Pod{}
104+
err = cl.Get(kontext.WithCluster(ctx, "root"), types.NamespacedName{Namespace: "default", Name: "foo"}, pod)
105+
Expect(err).NotTo(HaveOccurred())
106+
94107
mu.Lock()
95108
defer mu.Unlock()
96-
Expect(paths).To(Equal([]string{"/clusters/root/api/v1", "/clusters/root/api/v1/pods"}))
109+
Expect(paths).To(Equal([]string{"/clusters/root/api/v1", "/clusters/root/api/v1/pods", "/clusters/root/api/v1/namespaces/default/pods/foo"}))
97110
})
98111

99112
It("should work with a wildcard cluster in the kontext", func(ctx context.Context) {
@@ -121,9 +134,15 @@ var _ = Describe("NewClusterAwareClient", Ordered, func() {
121134
err = cl.List(ctx, pods)
122135
Expect(err).NotTo(HaveOccurred())
123136

137+
pod := &unstructured.Unstructured{}
138+
pod.SetAPIVersion("v1")
139+
pod.SetKind("Pod")
140+
err = cl.Get(ctx, types.NamespacedName{Namespace: "default", Name: "foo"}, pod)
141+
Expect(err).NotTo(HaveOccurred())
142+
124143
mu.Lock()
125144
defer mu.Unlock()
126-
Expect(paths).To(Equal([]string{"/api/v1", "/api/v1/pods"}))
145+
Expect(paths).To(Equal([]string{"/api/v1", "/api/v1/pods", "/api/v1/namespaces/default/pods/foo"}))
127146
})
128147

129148
It("should work with a cluster in the kontext", func(ctx context.Context) {
@@ -136,9 +155,15 @@ var _ = Describe("NewClusterAwareClient", Ordered, func() {
136155
err = cl.List(kontext.WithCluster(ctx, "root"), pods)
137156
Expect(err).NotTo(HaveOccurred())
138157

158+
pod := &unstructured.Unstructured{}
159+
pod.SetAPIVersion("v1")
160+
pod.SetKind("Pod")
161+
err = cl.Get(kontext.WithCluster(ctx, "root"), types.NamespacedName{Namespace: "default", Name: "foo"}, pod)
162+
Expect(err).NotTo(HaveOccurred())
163+
139164
mu.Lock()
140165
defer mu.Unlock()
141-
Expect(paths).To(Equal([]string{"/clusters/root/api/v1", "/clusters/root/api/v1/pods"}))
166+
Expect(paths).To(Equal([]string{"/clusters/root/api/v1", "/clusters/root/api/v1/pods", "/clusters/root/api/v1/namespaces/default/pods/foo"}))
142167
})
143168

144169
It("should work with a wildcard cluster in the kontext", func(ctx context.Context) {
@@ -201,13 +226,18 @@ var _ = Describe("NewClusterAwareClient", Ordered, func() {
201226

202227
var _ = Describe("NewClusterAwareCache", Ordered, func() {
203228
var (
204-
srv *httptest.Server
205-
mu sync.Mutex
206-
paths []string
207-
cfg *rest.Config
229+
cancelCtx context.CancelFunc
230+
srv *httptest.Server
231+
mu sync.Mutex
232+
paths []string
233+
cfg *rest.Config
234+
c cache.Cache
208235
)
209236

210237
BeforeAll(func() {
238+
var ctx context.Context
239+
ctx, cancelCtx = context.WithCancel(context.Background())
240+
211241
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
212242
mu.Lock()
213243
pth := req.URL.Path
@@ -225,11 +255,16 @@ var _ = Describe("NewClusterAwareCache", Ordered, func() {
225255
case req.URL.Path == "/clusters/*/api/v1/pods" && req.URL.Query().Get("watch") != "true":
226256
w.Header().Set("Content-Type", "application/json")
227257
w.WriteHeader(http.StatusOK)
228-
_, _ = w.Write([]byte(`{"kind": "PodList","apiVersion": "v1","metadata": {"resourceVersion": "184126176"}, "items": [{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"default","resourceVersion":"184126176","annotations":{"kcp.io/cluster":"root"}}}]}`))
258+
_, _ = w.Write([]byte(`{"kind": "PodList","apiVersion": "v1","metadata": {"resourceVersion": "184126176"}, "items": [
259+
{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"default","resourceVersion":"184126176","annotations":{"kcp.io/cluster":"root"}}},
260+
{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"default","resourceVersion":"184126093","annotations":{"kcp.io/cluster":"ws"}}}
261+
]}`))
229262
case req.URL.Path == "/clusters/*/api/v1/pods" && req.URL.Query().Get("watch") == "true":
230263
w.Header().Set("Content-Type", "application/json")
231264
w.Header().Set("Transfer-Encoding", "chunked")
232265
w.WriteHeader(http.StatusOK)
266+
_, _ = w.Write([]byte(`{"type":"ADDED","object":{"kind":"Pod","apiVersion":"v1","metadata":{"name":"bar","namespace":"default","resourceVersion":"184126177","annotations":{"kcp.io/cluster":"root"}}}}`))
267+
_, _ = w.Write([]byte(`{"type":"ADDED","object":{"kind":"Pod","apiVersion":"v1","metadata":{"name":"bar","namespace":"default","resourceVersion":"184126178","annotations":{"kcp.io/cluster":"ws"}}}}`))
233268
if w, ok := w.(http.Flusher); ok {
234269
w.Flush()
235270
}
@@ -239,11 +274,25 @@ var _ = Describe("NewClusterAwareCache", Ordered, func() {
239274
_, _ = w.Write([]byte(fmt.Sprintf("Not found %q", req.RequestURI)))
240275
}
241276
}))
277+
go func() {
278+
<-ctx.Done()
279+
srv.Close()
280+
}()
242281

243282
cfg = &rest.Config{
244283
Host: srv.URL,
245284
}
246285
Expect(rest.SetKubernetesDefaults(cfg)).To(Succeed())
286+
287+
var err error
288+
c, err = NewClusterAwareCache(cfg, cache.Options{})
289+
Expect(err).NotTo(HaveOccurred())
290+
go func() {
291+
if err := c.Start(ctx); err != nil {
292+
Expect(err).NotTo(HaveOccurred())
293+
}
294+
}()
295+
c.WaitForCacheSync(ctx)
247296
})
248297

249298
BeforeEach(func() {
@@ -253,25 +302,38 @@ var _ = Describe("NewClusterAwareCache", Ordered, func() {
253302
})
254303

255304
AfterAll(func() {
256-
srv.Close()
305+
cancelCtx()
257306
})
258307

259308
It("should always access wildcard clusters and serve other clusters from memory", func(ctx context.Context) {
260-
c, err := NewClusterAwareCache(cfg, cache.Options{})
261-
Expect(err).NotTo(HaveOccurred())
262-
go func() {
263-
if err := c.Start(ctx); err != nil {
264-
Expect(err).NotTo(HaveOccurred())
265-
}
266-
}()
267-
c.WaitForCacheSync(ctx)
268-
269309
pod := &corev1.Pod{}
270-
err = c.Get(kontext.WithCluster(ctx, "root"), types.NamespacedName{Namespace: "default", Name: "foo"}, pod)
310+
err := c.Get(kontext.WithCluster(ctx, "root"), types.NamespacedName{Namespace: "default", Name: "foo"}, pod)
271311
Expect(err).NotTo(HaveOccurred())
272312

273313
mu.Lock()
274314
defer mu.Unlock()
275315
Expect(paths).To(Equal([]string{"/clusters/*/api/v1", "/clusters/*/api/v1/pods", "/clusters/*/api/v1/pods?watch=true"}))
276316
})
317+
318+
It("should return only the pods from the requested cluster", func(ctx context.Context) {
319+
pod := &corev1.Pod{}
320+
err := c.Get(kontext.WithCluster(ctx, "root"), types.NamespacedName{Namespace: "default", Name: "foo"}, pod)
321+
Expect(err).NotTo(HaveOccurred())
322+
Expect(pod.Annotations).To(HaveKeyWithValue("kcp.io/cluster", "root"))
323+
324+
pods := &corev1.PodList{}
325+
err = c.List(kontext.WithCluster(ctx, "root"), pods)
326+
Expect(err).NotTo(HaveOccurred())
327+
Expect(pods.Items).To(HaveLen(2))
328+
Expect(pods.Items[0].Annotations).To(HaveKeyWithValue("kcp.io/cluster", "root"))
329+
Expect(pods.Items[1].Annotations).To(HaveKeyWithValue("kcp.io/cluster", "root"))
330+
Expect(sets.New(pods.Items[0].Name, pods.Items[1].Name)).To(Equal(sets.New("foo", "bar")))
331+
})
332+
333+
It("should return all pods from all clusters without cluster in context", func(ctx context.Context) {
334+
pods := &corev1.PodList{}
335+
err := c.List(ctx, pods)
336+
Expect(err).NotTo(HaveOccurred())
337+
Expect(pods.Items).To(HaveLen(4))
338+
})
277339
})

0 commit comments

Comments
 (0)