Repository navigation
Expand file tree
/
Copy pathhandler_test.go
More file actions
203 lines (186 loc) · 6.18 KB
/
Copy pathhandler_test.go
File metadata and controls
203 lines (186 loc) · 6.18 KB
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
package reprocessor
import (
"testing"
"time"
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/centralsensor"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/expiringcache"
"github.com/stackrox/rox/sensor/common/admissioncontroller/mocks"
"github.com/stackrox/rox/sensor/common/centralcaps"
"github.com/stackrox/rox/sensor/common/image/cache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
type mockImageCacheValue struct {
image *storage.Image
}
func (m *mockImageCacheValue) WaitAndGet() *storage.Image {
return m.image
}
func (m *mockImageCacheValue) GetIfDone() *storage.Image {
return m.image
}
func newTestCache(entries map[cache.Key]string) expiringcache.Cache[cache.Key, cache.Value] {
c := expiringcache.NewExpiringCache[cache.Key, cache.Value](1 * time.Hour)
for key, fullName := range entries {
c.Add(key, &mockImageCacheValue{
image: &storage.Image{
Id: string(key),
Name: &storage.ImageName{FullName: fullName},
},
})
}
return c
}
func TestProcessInvalidateImageCache(t *testing.T) {
cases := []struct {
name string
flatten bool
cache map[cache.Key]string
imageKeys []*central.ImageKey
wantRemoved []cache.Key
wantKept []cache.Key
}{
{
name: "without flatten uses ImageId and ImageFullName",
flatten: false,
cache: map[cache.Key]string{
"sha256:abc123": "docker.io/library/nginx:latest",
"sha256:def456": "quay.io/stackrox/main:4.0.0",
"redis:alpine": "redis:alpine",
"nginx:latest": "nginx:latest",
},
imageKeys: []*central.ImageKey{
{ImageId: "sha256:abc123", ImageFullName: "docker.io/library/nginx:latest"},
{ImageId: "sha256:def456", ImageFullName: "quay.io/stackrox/main:4.0.0"},
{ImageFullName: "redis:alpine"},
},
wantRemoved: []cache.Key{"sha256:abc123", "sha256:def456", "redis:alpine", "docker.io/library/nginx:latest", "quay.io/stackrox/main:4.0.0"},
wantKept: []cache.Key{"nginx:latest"},
},
{
name: "with flatten uses ImageIdV2 and ImageFullName",
flatten: true,
cache: map[cache.Key]string{
"uuid-v5-id-1": "docker.io/library/nginx:latest",
"uuid-v5-id-2": "quay.io/stackrox/main:4.0.0",
"redis:alpine": "redis:alpine",
"nginx:latest": "nginx:latest",
},
imageKeys: []*central.ImageKey{
{ImageIdV2: "uuid-v5-id-1", ImageFullName: "docker.io/library/nginx:latest"},
{ImageIdV2: "uuid-v5-id-2", ImageFullName: "quay.io/stackrox/main:4.0.0"},
{ImageFullName: "redis:alpine"},
},
wantRemoved: []cache.Key{"uuid-v5-id-1", "uuid-v5-id-2", "redis:alpine", "docker.io/library/nginx:latest", "quay.io/stackrox/main:4.0.0"},
wantKept: []cache.Key{"nginx:latest"},
},
{
name: "with flatten removes entry cached by full name when no digest was available",
flatten: true,
cache: map[cache.Key]string{
"docker.io/example/app:v1": "docker.io/example/app:v1",
},
imageKeys: []*central.ImageKey{
{ImageIdV2: "uuid-v5-id-app", ImageFullName: "docker.io/example/app:v1"},
},
wantRemoved: []cache.Key{"uuid-v5-id-app", "docker.io/example/app:v1"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.flatten {
centralcaps.Set([]centralsensor.CentralCapability{centralsensor.FlattenImageData})
} else {
centralcaps.Set([]centralsensor.CentralCapability{})
}
t.Cleanup(func() { centralcaps.Set(nil) })
ctrl := gomock.NewController(t)
mockAdmCtrl := mocks.NewMockSettingsManager(ctrl)
mockAdmCtrl.EXPECT().InvalidateImageCache(gomock.Any()).Times(1)
imgCache := newTestCache(tc.cache)
handler := &handlerImpl{
admCtrlSettingsMgr: mockAdmCtrl,
imageCache: imgCache,
stopSig: concurrency.NewErrorSignal(),
}
err := handler.ProcessInvalidateImageCache(¢ral.InvalidateImageCache{ImageKeys: tc.imageKeys})
require.NoError(t, err)
for _, key := range tc.wantRemoved {
_, found := imgCache.Get(key)
assert.False(t, found, "key %q should be removed", key)
}
for _, key := range tc.wantKept {
_, found := imgCache.Get(key)
assert.True(t, found, "key %q should still be in cache", key)
}
})
}
}
func TestProcessRefreshImageCacheTTL(t *testing.T) {
cases := []struct {
name string
flatten bool
cache map[cache.Key]string
imageKeys []*central.ImageKey
wantKept []cache.Key
}{
{
name: "without flatten uses ImageId",
flatten: false,
cache: map[cache.Key]string{
"sha256:abc123": "docker.io/library/nginx:latest",
"sha256:def456": "quay.io/stackrox/main:4.0.0",
},
imageKeys: []*central.ImageKey{
{ImageId: "sha256:abc123", ImageFullName: "docker.io/library/nginx:latest"},
{ImageId: "sha256:def456", ImageFullName: "quay.io/stackrox/main:4.0.0"},
},
wantKept: []cache.Key{"sha256:abc123", "sha256:def456"},
},
{
name: "with flatten uses ImageIdV2",
flatten: true,
cache: map[cache.Key]string{
"uuid-v5-id-1": "docker.io/library/nginx:latest",
},
imageKeys: []*central.ImageKey{
{ImageIdV2: "uuid-v5-id-1", ImageFullName: "docker.io/library/nginx:latest"},
},
wantKept: []cache.Key{"uuid-v5-id-1"},
},
{
name: "non-existent key does not error",
flatten: false,
cache: map[cache.Key]string{},
imageKeys: []*central.ImageKey{
{ImageId: "sha256:nonexistent", ImageFullName: "does-not-exist:latest"},
},
wantKept: nil,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.flatten {
centralcaps.Set([]centralsensor.CentralCapability{centralsensor.FlattenImageData})
} else {
centralcaps.Set([]centralsensor.CentralCapability{})
}
t.Cleanup(func() { centralcaps.Set(nil) })
imgCache := newTestCache(tc.cache)
handler := &handlerImpl{
imageCache: imgCache,
stopSig: concurrency.NewErrorSignal(),
}
err := handler.ProcessRefreshImageCacheTTL(¢ral.RefreshImageCacheTTL{ImageKeys: tc.imageKeys})
require.NoError(t, err)
for _, key := range tc.wantKept {
_, found := imgCache.Get(key)
assert.True(t, found, "key %q should still be in cache after TTL refresh", key)
}
})
}
}