wangpengfei
2023-06-02 064c0874e5fd041c4641ef873d1bf72ac98a184d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Copyright 2017 The Kubernetes Authors.
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
package watch
 
import (
    "context"
    "reflect"
    goruntime "runtime"
    "sort"
    "testing"
    "time"
 
    "github.com/davecgh/go-spew/spew"
 
    corev1 "k8s.io/api/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/runtime"
    "k8s.io/apimachinery/pkg/runtime/schema"
    "k8s.io/apimachinery/pkg/util/diff"
    "k8s.io/apimachinery/pkg/watch"
    fakeclientset "k8s.io/client-go/kubernetes/fake"
    testcore "k8s.io/client-go/testing"
    "k8s.io/client-go/tools/cache"
)
 
// TestEventProcessorExit is expected to timeout if the event processor fails
// to exit when stopped.
func TestEventProcessorExit(t *testing.T) {
    event := watch.Event{}
 
    tests := []struct {
        name  string
        write func(e *eventProcessor)
    }{
        {
            name: "exit on blocked read",
            write: func(e *eventProcessor) {
                e.push(event)
            },
        },
        {
            name: "exit on blocked write",
            write: func(e *eventProcessor) {
                e.push(event)
                e.push(event)
            },
        },
    }
    for _, test := range tests {
        t.Run(test.name, func(t *testing.T) {
            out := make(chan watch.Event)
            e := newEventProcessor(out)
 
            test.write(e)
 
            exited := make(chan struct{})
            go func() {
                e.run()
                close(exited)
            }()
 
            <-out
            e.stop()
            goruntime.Gosched()
            <-exited
        })
    }
}
 
type apiInt int
 
func (apiInt) GetObjectKind() schema.ObjectKind { return nil }
func (apiInt) DeepCopyObject() runtime.Object   { return nil }
 
func TestEventProcessorOrdersEvents(t *testing.T) {
    out := make(chan watch.Event)
    e := newEventProcessor(out)
    go e.run()
 
    numProcessed := 0
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    go func() {
        for i := 0; i < 1000; i++ {
            e := <-out
            if got, want := int(e.Object.(apiInt)), i; got != want {
                t.Errorf("unexpected event: got=%d, want=%d", got, want)
            }
            numProcessed++
        }
        cancel()
    }()
 
    for i := 0; i < 1000; i++ {
        e.push(watch.Event{Object: apiInt(i)})
    }
 
    <-ctx.Done()
    e.stop()
 
    if numProcessed != 1000 {
        t.Errorf("unexpected number of events processed: %d", numProcessed)
    }
 
}
 
type byEventTypeAndName []watch.Event
 
func (a byEventTypeAndName) Len() int      { return len(a) }
func (a byEventTypeAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byEventTypeAndName) Less(i, j int) bool {
    if a[i].Type < a[j].Type {
        return true
    }
 
    if a[i].Type > a[j].Type {
        return false
    }
 
    return a[i].Object.(*corev1.Secret).Name < a[j].Object.(*corev1.Secret).Name
}
 
func TestNewInformerWatcher(t *testing.T) {
    // Make sure there are no 2 same types of events on a secret with the same name or that might be flaky.
    tt := []struct {
        name    string
        objects []runtime.Object
        events  []watch.Event
    }{
        {
            name: "basic test",
            objects: []runtime.Object{
                &corev1.Secret{
                    ObjectMeta: metav1.ObjectMeta{
                        Name: "pod-1",
                    },
                    StringData: map[string]string{
                        "foo-1": "initial",
                    },
                },
                &corev1.Secret{
                    ObjectMeta: metav1.ObjectMeta{
                        Name: "pod-2",
                    },
                    StringData: map[string]string{
                        "foo-2": "initial",
                    },
                },
                &corev1.Secret{
                    ObjectMeta: metav1.ObjectMeta{
                        Name: "pod-3",
                    },
                    StringData: map[string]string{
                        "foo-3": "initial",
                    },
                },
            },
            events: []watch.Event{
                {
                    Type: watch.Added,
                    Object: &corev1.Secret{
                        ObjectMeta: metav1.ObjectMeta{
                            Name: "pod-4",
                        },
                        StringData: map[string]string{
                            "foo-4": "initial",
                        },
                    },
                },
                {
                    Type: watch.Modified,
                    Object: &corev1.Secret{
                        ObjectMeta: metav1.ObjectMeta{
                            Name: "pod-2",
                        },
                        StringData: map[string]string{
                            "foo-2": "new",
                        },
                    },
                },
                {
                    Type: watch.Deleted,
                    Object: &corev1.Secret{
                        ObjectMeta: metav1.ObjectMeta{
                            Name: "pod-3",
                        },
                    },
                },
            },
        },
    }
 
    for _, tc := range tt {
        t.Run(tc.name, func(t *testing.T) {
            var expected []watch.Event
            for _, o := range tc.objects {
                expected = append(expected, watch.Event{
                    Type:   watch.Added,
                    Object: o.DeepCopyObject(),
                })
            }
            for _, e := range tc.events {
                expected = append(expected, *e.DeepCopy())
            }
 
            fake := fakeclientset.NewSimpleClientset(tc.objects...)
            fakeWatch := watch.NewFakeWithChanSize(len(tc.events), false)
            fake.PrependWatchReactor("secrets", testcore.DefaultWatchReactor(fakeWatch, nil))
 
            for _, e := range tc.events {
                fakeWatch.Action(e.Type, e.Object)
            }
 
            lw := &cache.ListWatch{
                ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
                    return fake.CoreV1().Secrets("").List(context.TODO(), options)
                },
                WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
                    return fake.CoreV1().Secrets("").Watch(context.TODO(), options)
                },
            }
            _, _, w, done := NewIndexerInformerWatcher(lw, &corev1.Secret{})
 
            var result []watch.Event
        loop:
            for {
                var event watch.Event
                var ok bool
                select {
                case event, ok = <-w.ResultChan():
                    if !ok {
                        t.Errorf("Failed to read event: channel is already closed!")
                        return
                    }
 
                    result = append(result, *event.DeepCopy())
                case <-time.After(time.Second * 1):
                    // All the events are buffered -> this means we are done
                    // Also the one sec will make sure that we would detect RetryWatcher's incorrect behaviour after last event
                    break loop
                }
            }
 
            // Informers don't guarantee event order so we need to sort these arrays to compare them
            sort.Sort(byEventTypeAndName(expected))
            sort.Sort(byEventTypeAndName(result))
 
            if !reflect.DeepEqual(expected, result) {
                t.Error(spew.Errorf("\nexpected: %#v,\ngot:      %#v,\ndiff: %s", expected, result, diff.ObjectReflectDiff(expected, result)))
                return
            }
 
            // Fill in some data to test watch closing while there are some events to be read
            for _, e := range tc.events {
                fakeWatch.Action(e.Type, e.Object)
            }
 
            // Stop before reading all the data to make sure the informer can deal with closed channel
            w.Stop()
 
            <-done
        })
    }
 
}