...
1
16
17 package internal
18
19 import (
20 "fmt"
21
22 . "github.com/onsi/ginkgo/v2"
23 . "github.com/onsi/gomega"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/runtime"
26 "k8s.io/apimachinery/pkg/runtime/schema"
27 "k8s.io/apimachinery/pkg/watch"
28 )
29
30
31
32
33
34 var _ = Describe("gvkFixupWatcher", func() {
35 It("behaves like watch.FakeWatcher", func() {
36 newTestType := func(name string) runtime.Object {
37 return &metav1.PartialObjectMetadata{
38 ObjectMeta: metav1.ObjectMeta{
39 Name: name,
40 },
41 }
42 }
43
44 f := watch.NewFake()
45
46 expectedGVK := schema.GroupVersionKind{
47 Group: "testgroup",
48 Version: "v1test2",
49 Kind: "TestKind",
50 }
51 gvkfw := newGVKFixupWatcher(expectedGVK, f)
52
53 table := []struct {
54 t watch.EventType
55 s runtime.Object
56 }{
57 {watch.Added, newTestType("foo")},
58 {watch.Modified, newTestType("qux")},
59 {watch.Modified, newTestType("bar")},
60 {watch.Deleted, newTestType("bar")},
61 {watch.Error, newTestType("error: blah")},
62 }
63
64 consumer := func(w watch.Interface) {
65 for _, expect := range table {
66 By(fmt.Sprintf("Fixing up watch.EventType: %v and passing it on", expect.t))
67 got, ok := <-w.ResultChan()
68 Expect(ok).To(BeTrue(), "closed early")
69 Expect(expect.t).To(Equal(got.Type), "unexpected Event.Type or out-of-order Event")
70 Expect(got.Object).To(BeAssignableToTypeOf(&metav1.PartialObjectMetadata{}), "unexpected Event.Object type")
71 a := got.Object.(*metav1.PartialObjectMetadata)
72 Expect(got.Object.GetObjectKind().GroupVersionKind()).To(Equal(expectedGVK), "GVK was not fixed up")
73 expected := expect.s.DeepCopyObject()
74 expected.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
75 actual := a.DeepCopyObject()
76 actual.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
77 Expect(actual).To(Equal(expected), "unexpected change to the Object")
78 }
79 Eventually(w.ResultChan()).Should(BeClosed())
80 }
81
82 sender := func() {
83 f.Add(newTestType("foo"))
84 f.Action(watch.Modified, newTestType("qux"))
85 f.Modify(newTestType("bar"))
86 f.Delete(newTestType("bar"))
87 f.Error(newTestType("error: blah"))
88 f.Stop()
89 }
90
91 go sender()
92 consumer(gvkfw)
93 })
94 })
95
View as plain text