| 1 |
/* |
| 2 |
Copyright 2023 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package aes |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
clocktesting "k8s.io/utils/clock/testing" |
| 24 |
) |
| 25 |
|
| 26 |
type dataString string |
| 27 |
|
| 28 |
func (d dataString) AuthenticatedData() []byte { return []byte(d) } |
| 29 |
|
| 30 |
func Test_simpleCache(t *testing.T) { |
| 31 |
info1 := []byte{1} |
| 32 |
info2 := []byte{2} |
| 33 |
key1 := dataString("1") |
| 34 |
key2 := dataString("2") |
| 35 |
twi1 := &transformerWithInfo{info: info1} |
| 36 |
twi2 := &transformerWithInfo{info: info2} |
| 37 |
|
| 38 |
tests := []struct { |
| 39 |
name string |
| 40 |
test func(*testing.T, *simpleCache, *clocktesting.FakeClock) |
| 41 |
}{ |
| 42 |
{ |
| 43 |
name: "get from empty", |
| 44 |
test: func(t *testing.T, cache *simpleCache, clock *clocktesting.FakeClock) { |
| 45 |
got := cache.get(info1, key1) |
| 46 |
twiPtrEquals(t, nil, got) |
| 47 |
cacheLenEquals(t, cache, 0) |
| 48 |
}, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "get after set", |
| 52 |
test: func(t *testing.T, cache *simpleCache, clock *clocktesting.FakeClock) { |
| 53 |
cache.set(key1, twi1) |
| 54 |
got := cache.get(info1, key1) |
| 55 |
twiPtrEquals(t, twi1, got) |
| 56 |
cacheLenEquals(t, cache, 1) |
| 57 |
}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "get after set but with different info", |
| 61 |
test: func(t *testing.T, cache *simpleCache, clock *clocktesting.FakeClock) { |
| 62 |
cache.set(key1, twi1) |
| 63 |
got := cache.get(info2, key1) |
| 64 |
twiPtrEquals(t, nil, got) |
| 65 |
cacheLenEquals(t, cache, 1) |
| 66 |
}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "expired get after set", |
| 70 |
test: func(t *testing.T, cache *simpleCache, clock *clocktesting.FakeClock) { |
| 71 |
cache.set(key1, twi1) |
| 72 |
clock.Step(time.Hour) |
| 73 |
got := cache.get(info1, key1) |
| 74 |
twiPtrEquals(t, twi1, got) |
| 75 |
cacheLenEquals(t, cache, 1) |
| 76 |
}, |
| 77 |
}, |
| 78 |
{ |
| 79 |
name: "expired get after GC", |
| 80 |
test: func(t *testing.T, cache *simpleCache, clock *clocktesting.FakeClock) { |
| 81 |
cache.set(key1, twi1) |
| 82 |
clock.Step(time.Hour) |
| 83 |
cacheLenEquals(t, cache, 1) |
| 84 |
cache.set(key2, twi2) // unrelated set to make GC run |
| 85 |
got := cache.get(info1, key1) |
| 86 |
twiPtrEquals(t, nil, got) |
| 87 |
cacheLenEquals(t, cache, 1) |
| 88 |
}, |
| 89 |
}, |
| 90 |
{ |
| 91 |
name: "multiple sets for same key", |
| 92 |
test: func(t *testing.T, cache *simpleCache, clock *clocktesting.FakeClock) { |
| 93 |
cache.set(key1, twi1) |
| 94 |
cacheLenEquals(t, cache, 1) |
| 95 |
cache.set(key1, twi2) |
| 96 |
cacheLenEquals(t, cache, 1) |
| 97 |
|
| 98 |
got11 := cache.get(info1, key1) |
| 99 |
twiPtrEquals(t, nil, got11) |
| 100 |
|
| 101 |
got21 := cache.get(info2, key1) |
| 102 |
twiPtrEquals(t, twi2, got21) |
| 103 |
|
| 104 |
got12 := cache.get(info1, key2) |
| 105 |
twiPtrEquals(t, nil, got12) |
| 106 |
|
| 107 |
got22 := cache.get(info2, key2) |
| 108 |
twiPtrEquals(t, nil, got22) |
| 109 |
}, |
| 110 |
}, |
| 111 |
} |
| 112 |
for _, tt := range tests { |
| 113 |
tt := tt |
| 114 |
t.Run(tt.name, func(t *testing.T) { |
| 115 |
t.Parallel() |
| 116 |
clock := clocktesting.NewFakeClock(time.Now()) |
| 117 |
cache := newSimpleCache(clock, 10*time.Second) |
| 118 |
tt.test(t, cache, clock) |
| 119 |
}) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func twiPtrEquals(t *testing.T, want, got *transformerWithInfo) { |
| 124 |
t.Helper() |
| 125 |
|
| 126 |
if want != got { |
| 127 |
t.Errorf("transformerWithInfo structs are not pointer equivalent") |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
func cacheLenEquals(t *testing.T, cache *simpleCache, want int) { |
| 132 |
t.Helper() |
| 133 |
|
| 134 |
if got := cache.cache.Len(); want != got { |
| 135 |
t.Errorf("unexpected cache len: want %d, got %d", want, got) |
| 136 |
} |
| 137 |
} |