forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaching_map_test.go
410 lines (349 loc) · 11.9 KB
/
caching_map_test.go
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (c) 2021 Tigera, Inc. All rights reserved.
//
// 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 cachingmap_test
import (
"fmt"
"testing"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
. "github.com/projectcalico/calico/felix/cachingmap"
"github.com/projectcalico/calico/felix/logutils"
)
func init() {
logutils.ConfigureEarlyLogging()
log.SetLevel(log.DebugLevel)
}
// TestCachingMap_Empty verifies loading of an empty map with no changes queued.
func TestCachingMap_Empty(t *testing.T) {
mockMap, cm := setupCachingMapTest(t)
err := cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(BeEmpty())
}
var ErrFail = fmt.Errorf("fail")
// TestCachingMap_Errors tests returning of errors from the underlying map.
func TestCachingMap_Errors(t *testing.T) {
mockMap, cm := setupCachingMapTest(t)
mockMap.LoadErr = ErrFail
err := cm.ApplyAllChanges()
Expect(err).To(HaveOccurred())
// Failure should have cleared the cache again so next Apply should see this new entry.
mockMap.Contents = map[string]string{
"1, 1": "1, 2, 4, 3",
}
mockMap.LoadErr = nil
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(BeEmpty())
// Now check errors on update
cm.Desired().Set("1, 1", "1, 2, 4, 4")
mockMap.UpdateErr = ErrFail
err = cm.ApplyAllChanges()
Expect(err).To(HaveOccurred())
// And then success
mockMap.UpdateErr = nil
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(Equal(map[string]string{
"1, 1": "1, 2, 4, 4",
}))
// And delete.
mockMap.DeleteErr = ErrFail
cm.Desired().DeleteAll()
err = cm.ApplyAllChanges()
Expect(err).To(HaveOccurred())
mockMap.DeleteErr = nil
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(BeEmpty())
}
// TestCachingMap_CleanUp verifies cleaning up of a whole map.
func TestCachingMap_CleanUp(t *testing.T) {
mockMap, cm := setupCachingMapTest(t)
_ = mockMap.Update("1, 2", "1, 2, 3, 4")
_ = mockMap.Update("1, 3", "1, 2, 4, 4")
err := cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(BeEmpty())
}
// TestCachingMap_ApplyAll mainline test using separate Apply calls for adds and deletes.
func TestCachingMap_SplitUpdateAndDelete(t *testing.T) {
mockMap, cm := setupCachingMapTest(t)
mockMap.Contents = map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 4",
"1, 3": "1, 2, 4, 4",
}
cm.Desired().Set("1, 1", "1, 2, 4, 3") // Same value for existing key.
cm.Desired().Set("1, 2", "1, 2, 3, 6") // New value for existing key.
cm.Desired().Set("1, 4", "1, 2, 3, 5") // New K/V
// Shouldn't do anything until we hit apply.
Expect(mockMap.OpCount()).To(Equal(0))
err := cm.ApplyUpdatesOnly()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(Equal(map[string]string{
"1, 1": "1, 2, 4, 3", // No change
"1, 2": "1, 2, 3, 6", // Updated
"1, 3": "1, 2, 4, 4", // Not desired but should be left alone
"1, 4": "1, 2, 3, 5", // Added
}))
// Two updates and an iteration to load the map initially.
Expect(mockMap.UpdateCount).To(Equal(2))
Expect(mockMap.DeleteCount).To(Equal(0))
Expect(mockMap.GetCount).To(Equal(0))
Expect(mockMap.LoadCount).To(Equal(1))
err = cm.ApplyDeletionsOnly()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(Equal(map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 6",
"1, 4": "1, 2, 3, 5",
}))
// No new updates or iterations but should get one extra deletion.
Expect(mockMap.UpdateCount).To(Equal(2))
Expect(mockMap.GetCount).To(Equal(0))
Expect(mockMap.DeleteCount).To(Equal(1))
Expect(mockMap.LoadCount).To(Equal(1))
// Doing an extra apply should make no changes.
preApplyOpCount := mockMap.OpCount()
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.OpCount()).To(Equal(preApplyOpCount))
}
// TestCachingMap_ApplyAll mainline test using ApplyAll() to update the dataplane.
func TestCachingMap_ApplyAll(t *testing.T) {
mockMap, cm := setupCachingMapTest(t)
mockMap.Contents = map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 4",
"1, 3": "1, 2, 4, 4",
}
cm.Desired().Set("1, 1", "1, 2, 4, 3") // Same value for existing key.
cm.Desired().Set("1, 2", "1, 2, 3, 6") // New value for existing key.
cm.Desired().Set("1, 4", "1, 2, 3, 5") // New K/V
// Shouldn't do anything until we hit apply.
Expect(mockMap.OpCount()).To(Equal(0))
err := cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(Equal(map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 6",
"1, 4": "1, 2, 3, 5",
}))
// Two updates and an iteration to load the map initially.
Expect(mockMap.UpdateCount).To(Equal(2))
Expect(mockMap.DeleteCount).To(Equal(1))
Expect(mockMap.GetCount).To(Equal(0))
Expect(mockMap.LoadCount).To(Equal(1))
// Doing an extra apply should make no changes.
preApplyOpCount := mockMap.OpCount()
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.OpCount()).To(Equal(preApplyOpCount))
// Finish with a DeleteAll()
cm.Desired().DeleteAll()
Expect(mockMap.OpCount()).To(Equal(preApplyOpCount)) // No immediate change
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(BeEmpty())
Expect(mockMap.DeleteCount).To(Equal(4))
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(BeEmpty())
Expect(mockMap.DeleteCount).To(Equal(4))
}
// TestCachingMap_DeleteBeforeLoad does some set and delete calls before loading from
// the dataplane.
func TestCachingMap_DeleteBeforeLoad(t *testing.T) {
mockMap, cm := setupCachingMapTest(t)
mockMap.Contents = map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 4",
"1, 3": "1, 2, 4, 4",
}
cm.Desired().Set("1, 1", "1, 2, 4, 3") // Same value for existing key.
cm.Desired().Set("1, 2", "1, 2, 3, 6") // New value for existing key.
cm.Desired().Set("1, 4", "1, 2, 3, 5") // New K/V
cm.Desired().Delete("1, 2") // Changed my mind.
cm.Desired().Delete("1, 4") // Changed my mind.
cm.Desired().Delete("1, 8") // Delete of nonexistent key is a no-op.
// Shouldn't do anything until we hit apply.
Expect(mockMap.OpCount()).To(Equal(0))
err := cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(Equal(map[string]string{
"1, 1": "1, 2, 4, 3",
}))
// Just the two deletes.
Expect(mockMap.UpdateCount).To(Equal(0))
Expect(mockMap.DeleteCount).To(Equal(2))
Expect(mockMap.GetCount).To(Equal(0))
Expect(mockMap.LoadCount).To(Equal(1))
// Doing an extra apply should make no changes.
preApplyOpCount := mockMap.OpCount()
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.OpCount()).To(Equal(preApplyOpCount))
}
// TestCachingMap_PreLoad verifies calling LoadCacheFromDataplane before setting values.
func TestCachingMap_PreLoad(t *testing.T) {
mockMap, cm := setupCachingMapTest(t)
mockMap.Contents = map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 4",
"1, 3": "1, 2, 4, 4",
}
err := cm.LoadCacheFromDataplane()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.LoadCount).To(Equal(1))
Expect(mockMap.OpCount()).To(Equal(1))
// Check we can query the cache.
v, ok := cm.Dataplane().Get("1, 1")
Expect(ok).To(BeTrue())
Expect(v).To(Equal("1, 2, 4, 3"))
seenValues := make(map[string]string)
cm.Dataplane().Iter(func(k string, v string) {
seenValues[k] = v
})
Expect(seenValues).To(Equal(mockMap.Contents))
cm.Desired().Set("1, 1", "1, 2, 4, 3") // Same value for existing key.
cm.Desired().Set("1, 2", "1, 2, 3, 6") // New value for existing key.
cm.Desired().Set("1, 4", "1, 2, 3, 5") // New K/V
cm.Desired().Delete("1, 8") // Delete of nonexistent key is a no-op.
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(Equal(map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 6",
"1, 4": "1, 2, 3, 5",
}))
// Two updates and an iteration to load the map initially.
Expect(mockMap.UpdateCount).To(Equal(2))
Expect(mockMap.DeleteCount).To(Equal(1))
Expect(mockMap.GetCount).To(Equal(0))
Expect(mockMap.LoadCount).To(Equal(1))
// Doing an extra apply should make no changes.
preApplyOpCount := mockMap.OpCount()
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.OpCount()).To(Equal(preApplyOpCount))
}
// TestCachingMap_Resync verifies handling of a dataplane reload while there are pending
// changes. Pending changes should be dropped if the reload finds that they've already
// been made.
func TestCachingMap_Resync(t *testing.T) {
mockMap, cm := setupCachingMapTest(t)
mockMap.Contents = map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 4",
"1, 3": "1, 2, 4, 4",
}
err := cm.LoadCacheFromDataplane()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.LoadCount).To(Equal(1))
Expect(mockMap.OpCount()).To(Equal(1))
cm.Desired().Set("1, 1", "1, 2, 4, 3") // Same value for existing key.
cm.Desired().Set("1, 2", "1, 2, 3, 6") // New value for existing key.
cm.Desired().Set("1, 4", "1, 2, 3, 5") // New K/V
// At this point we've got some updates and a deletion queued up. Change the contents
// of the map:
// - Remove the key that was already correct.
// - Remove the key that we were about to delete.
// - Correct the value of the other key.
mockMap.Contents = map[string]string{
"1, 2": "1, 2, 3, 6",
}
err = cm.LoadCacheFromDataplane()
Expect(err).NotTo(HaveOccurred())
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.Contents).To(Equal(map[string]string{
"1, 1": "1, 2, 4, 3",
"1, 2": "1, 2, 3, 6",
"1, 4": "1, 2, 3, 5",
}))
// Two updates and an iteration to load the map initially.
Expect(mockMap.UpdateCount).To(Equal(2))
Expect(mockMap.DeleteCount).To(Equal(0))
Expect(mockMap.GetCount).To(Equal(0))
Expect(mockMap.LoadCount).To(Equal(2))
// Doing an extra apply should make no changes.
preApplyOpCount := mockMap.OpCount()
err = cm.ApplyAllChanges()
Expect(err).NotTo(HaveOccurred())
Expect(mockMap.OpCount()).To(Equal(preApplyOpCount))
}
func setupCachingMapTest(t *testing.T) (*Map, *CachingMap[string, string]) {
RegisterTestingT(t)
mockMap := newMockMap()
cm := New[string, string]("mock-map", mockMap)
return mockMap, cm
}
type Map struct {
Contents map[string]string
UpdateCount int
GetCount int
DeleteCount int
LoadCount int
LoadErr error
UpdateErr error
DeleteErr error
}
var errNotExists = fmt.Errorf("does not exist")
func (m *Map) ErrIsNotExists(err error) bool {
return err == errNotExists
}
func (m *Map) Update(k, v string) error {
log.Debugf("Update(\"%s\", \"%s\")", k, v)
m.UpdateCount++
if m.UpdateErr != nil {
return m.UpdateErr
}
m.Contents[string(k)] = string(v)
return nil
}
func (m *Map) Get(k string) (string, error) {
log.Debugf("Get(\"%s\")", k)
m.GetCount++
v, ok := m.Contents[k]
if !ok {
return "", errNotExists
}
return v, nil
}
func (m *Map) Delete(k string) error {
log.Debugf("Delete(\"%s\")", k)
m.DeleteCount++
if m.DeleteErr != nil {
return m.DeleteErr
}
delete(m.Contents, k)
return nil
}
func (m *Map) Load() (map[string]string, error) {
m.LoadCount++
if m.LoadErr != nil {
return nil, m.LoadErr
}
return m.Contents, nil
}
func (m *Map) OpCount() int {
return m.UpdateCount + m.LoadCount + m.GetCount + m.DeleteCount
}
func newMockMap() *Map {
m := &Map{
Contents: map[string]string{},
}
return m
}