-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathhandler_test.go
More file actions
200 lines (175 loc) · 6.67 KB
/
handler_test.go
File metadata and controls
200 lines (175 loc) · 6.67 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
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 TestProcessInvalidateImageCache_WithoutFlattenImageData(t *testing.T) {
centralcaps.Set([]centralsensor.CentralCapability{})
ctrl := gomock.NewController(t)
mockAdmCtrlSettingsMgr := mocks.NewMockSettingsManager(ctrl)
mockAdmCtrlSettingsMgr.EXPECT().InvalidateImageCache(gomock.Any()).Times(1)
imageCache := expiringcache.NewExpiringCache[cache.Key, cache.Value](1 * time.Hour)
imageCache.Add(cache.Key("sha256:abc123"), &mockImageCacheValue{
image: &storage.Image{
Id: "sha256:abc123",
Name: &storage.ImageName{FullName: "docker.io/library/nginx:latest"},
},
})
imageCache.Add(cache.Key("sha256:def456"), &mockImageCacheValue{
image: &storage.Image{
Id: "sha256:def456",
Name: &storage.ImageName{FullName: "quay.io/stackrox/main:4.0.0"},
},
})
// Images without IDs (cached by full name)
imageCache.Add(cache.Key("redis:alpine"), &mockImageCacheValue{
image: &storage.Image{
Id: "",
Name: &storage.ImageName{FullName: "redis:alpine"},
},
})
imageCache.Add(cache.Key("nginx:latest"), &mockImageCacheValue{
image: &storage.Image{
Id: "",
Name: &storage.ImageName{FullName: "nginx:latest"},
},
})
// Verify images are in cache
_, found := imageCache.Get(cache.Key("sha256:abc123"))
require.True(t, found, "Image sha256:abc123 should be in cache")
_, found = imageCache.Get(cache.Key("sha256:def456"))
require.True(t, found, "Image sha256:def456 should be in cache")
_, found = imageCache.Get(cache.Key("redis:alpine"))
require.True(t, found, "Image redis:alpine should be in cache")
_, found = imageCache.Get(cache.Key("nginx:latest"))
require.True(t, found, "Image nginx:latest should be in cache")
handler := &handlerImpl{
admCtrlSettingsMgr: mockAdmCtrlSettingsMgr,
imageCache: imageCache,
stopSig: concurrency.NewErrorSignal(),
}
// Create invalidate request (without capability, uses ImageId or full name)
req := ¢ral.InvalidateImageCache{
ImageKeys: []*central.InvalidateImageCache_ImageKey{
{
ImageId: "sha256:abc123",
ImageFullName: "docker.io/library/nginx:latest",
},
{
ImageId: "sha256:def456",
ImageFullName: "quay.io/stackrox/main:4.0.0",
},
{
ImageFullName: "redis:alpine",
},
},
}
err := handler.ProcessInvalidateImageCache(req)
require.NoError(t, err)
// Verify specified images are removed from cache
_, found = imageCache.Get(cache.Key("sha256:abc123"))
assert.False(t, found, "Image sha256:abc123 should be removed from cache")
_, found = imageCache.Get(cache.Key("sha256:def456"))
assert.False(t, found, "Image sha256:def456 should be removed from cache")
_, found = imageCache.Get(cache.Key("redis:alpine"))
assert.False(t, found, "Image redis:alpine should be removed from cache (fallback to full name)")
// Verify image not in request is still in cache
_, found = imageCache.Get(cache.Key("nginx:latest"))
assert.True(t, found, "Image nginx:latest should still be in cache")
}
func TestProcessInvalidateImageCache_WithFlattenImageData(t *testing.T) {
centralcaps.Set([]centralsensor.CentralCapability{centralsensor.FlattenImageData})
ctrl := gomock.NewController(t)
mockAdmCtrlSettingsMgr := mocks.NewMockSettingsManager(ctrl)
mockAdmCtrlSettingsMgr.EXPECT().InvalidateImageCache(gomock.Any()).Times(1)
imageCache := expiringcache.NewExpiringCache[cache.Key, cache.Value](1 * time.Hour)
imageCache.Add(cache.Key("uuid-v5-id-1"), &mockImageCacheValue{
image: &storage.Image{
Id: "sha256:abc123",
Name: &storage.ImageName{FullName: "docker.io/library/nginx:latest"},
},
})
imageCache.Add(cache.Key("uuid-v5-id-2"), &mockImageCacheValue{
image: &storage.Image{
Id: "sha256:def456",
Name: &storage.ImageName{FullName: "quay.io/stackrox/main:4.0.0"},
},
})
// Images without IDs (cached by full name)
imageCache.Add(cache.Key("redis:alpine"), &mockImageCacheValue{
image: &storage.Image{
Id: "",
Name: &storage.ImageName{FullName: "redis:alpine"},
},
})
imageCache.Add(cache.Key("nginx:latest"), &mockImageCacheValue{
image: &storage.Image{
Id: "",
Name: &storage.ImageName{FullName: "nginx:latest"},
},
})
// Verify images are in cache
_, found := imageCache.Get(cache.Key("uuid-v5-id-1"))
require.True(t, found, "Image uuid-v5-id-1 should be in cache")
_, found = imageCache.Get(cache.Key("uuid-v5-id-2"))
require.True(t, found, "Image uuid-v5-id-2 should be in cache")
_, found = imageCache.Get(cache.Key("redis:alpine"))
require.True(t, found, "Image redis:alpine should be in cache")
_, found = imageCache.Get(cache.Key("nginx:latest"))
require.True(t, found, "Image nginx:latest should be in cache")
handler := &handlerImpl{
admCtrlSettingsMgr: mockAdmCtrlSettingsMgr,
imageCache: imageCache,
stopSig: concurrency.NewErrorSignal(),
}
// Create invalidate request (with capability, uses ImageIdV2 or full name)
req := ¢ral.InvalidateImageCache{
ImageKeys: []*central.InvalidateImageCache_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",
},
{
// No ID, fallback to full name
ImageFullName: "redis:alpine",
},
},
}
// Call ProcessInvalidateImageCache
err := handler.ProcessInvalidateImageCache(req)
require.NoError(t, err)
// Verify specified images are removed from cache
_, found = imageCache.Get(cache.Key("uuid-v5-id-1"))
assert.False(t, found, "Image uuid-v5-id-1 should be removed from cache")
_, found = imageCache.Get(cache.Key("uuid-v5-id-2"))
assert.False(t, found, "Image uuid-v5-id-2 should be removed from cache")
_, found = imageCache.Get(cache.Key("redis:alpine"))
assert.False(t, found, "Image redis:alpine should be removed from cache (fallback to full name)")
// Verify image not in request is still in cache
_, found = imageCache.Get(cache.Key("nginx:latest"))
assert.True(t, found, "Image nginx:latest should still be in cache")
}