@@ -25,6 +25,7 @@ import (
25
25
"time"
26
26
27
27
"k8s.io/minikube/pkg/minikube/constants"
28
+ "k8s.io/minikube/pkg/minikube/localpath"
28
29
)
29
30
30
31
// Force download tests to run in serial.
@@ -33,7 +34,6 @@ func TestDownload(t *testing.T) {
33
34
t .Run ("PreloadDownloadPreventsMultipleDownload" , testPreloadDownloadPreventsMultipleDownload )
34
35
t .Run ("ImageToCache" , testImageToCache )
35
36
t .Run ("PreloadNotExists" , testPreloadNotExists )
36
- t .Run ("PreloadChecksumMismatch" , testPreloadChecksumMismatch )
37
37
t .Run ("PreloadExistsCaching" , testPreloadExistsCaching )
38
38
t .Run ("PreloadWithCachedSizeZero" , testPreloadWithCachedSizeZero )
39
39
}
@@ -48,7 +48,31 @@ func mockSleepDownload(downloadsCounter *int) func(src, dst string) error {
48
48
}
49
49
}
50
50
51
+ // point each subtest at an isolated MINIKUBE_HOME, pre-create the preload cache directory,
52
+ //
53
+ // and automatically restore the global download/preload mocks after each run.
54
+ // Applied the helper across all download-related tests
55
+ func setupTestMiniHome (t * testing.T ) {
56
+ t .Helper ()
57
+ tmpHome := t .TempDir ()
58
+ t .Setenv (localpath .MinikubeHome , tmpHome )
59
+ if err := os .MkdirAll (targetDir (), 0o755 ); err != nil {
60
+ t .Fatalf ("failed to create preload cache dir: %v" , err )
61
+ }
62
+ origDownloadMock := DownloadMock
63
+ origCheckCache := checkCache
64
+ origCheckPreloadExists := checkPreloadExists
65
+ origGetChecksumGCS := getChecksumGCS
66
+ t .Cleanup (func () {
67
+ DownloadMock = origDownloadMock
68
+ checkCache = origCheckCache
69
+ checkPreloadExists = origCheckPreloadExists
70
+ getChecksumGCS = origGetChecksumGCS
71
+ })
72
+ }
73
+
51
74
func testBinaryDownloadPreventsMultipleDownload (t * testing.T ) {
75
+ setupTestMiniHome (t )
52
76
downloadNum := 0
53
77
DownloadMock = mockSleepDownload (& downloadNum )
54
78
@@ -79,6 +103,8 @@ func testBinaryDownloadPreventsMultipleDownload(t *testing.T) {
79
103
}
80
104
81
105
func testPreloadDownloadPreventsMultipleDownload (t * testing.T ) {
106
+ setupTestMiniHome (t )
107
+
82
108
downloadNum := 0
83
109
DownloadMock = mockSleepDownload (& downloadNum )
84
110
f , err := os .CreateTemp ("" , "preload" )
@@ -97,8 +123,7 @@ func testPreloadDownloadPreventsMultipleDownload(t *testing.T) {
97
123
return os .Stat (f .Name ())
98
124
}
99
125
checkPreloadExists = func (_ , _ , _ string , _ ... bool ) bool { return true }
100
- getChecksum = func (_ , _ string ) ([]byte , error ) { return []byte ("check" ), nil }
101
- ensureChecksumValid = func (_ , _ , _ string , _ []byte ) error { return nil }
126
+ getChecksumGCS = func (_ , _ string ) ([]byte , error ) { return []byte ("check" ), nil }
102
127
103
128
var group sync.WaitGroup
104
129
group .Add (2 )
@@ -120,13 +145,13 @@ func testPreloadDownloadPreventsMultipleDownload(t *testing.T) {
120
145
}
121
146
122
147
func testPreloadNotExists (t * testing.T ) {
148
+ setupTestMiniHome (t )
123
149
downloadNum := 0
124
150
DownloadMock = mockSleepDownload (& downloadNum )
125
151
126
152
checkCache = func (_ string ) (fs.FileInfo , error ) { return nil , fmt .Errorf ("cache not found" ) }
127
153
checkPreloadExists = func (_ , _ , _ string , _ ... bool ) bool { return false }
128
- getChecksum = func (_ , _ string ) ([]byte , error ) { return []byte ("check" ), nil }
129
- ensureChecksumValid = func (_ , _ , _ string , _ []byte ) error { return nil }
154
+ getChecksumGCS = func (_ , _ string ) ([]byte , error ) { return []byte ("check" ), nil }
130
155
131
156
err := Preload (constants .DefaultKubernetesVersion , constants .Docker , "docker" )
132
157
if err != nil {
@@ -138,27 +163,8 @@ func testPreloadNotExists(t *testing.T) {
138
163
}
139
164
}
140
165
141
- func testPreloadChecksumMismatch (t * testing.T ) {
142
- downloadNum := 0
143
- DownloadMock = mockSleepDownload (& downloadNum )
144
-
145
- checkCache = func (_ string ) (fs.FileInfo , error ) { return nil , fmt .Errorf ("cache not found" ) }
146
- checkPreloadExists = func (_ , _ , _ string , _ ... bool ) bool { return true }
147
- getChecksum = func (_ , _ string ) ([]byte , error ) { return []byte ("check" ), nil }
148
- ensureChecksumValid = func (_ , _ , _ string , _ []byte ) error {
149
- return fmt .Errorf ("checksum mismatch" )
150
- }
151
-
152
- err := Preload (constants .DefaultKubernetesVersion , constants .Docker , "docker" )
153
- expectedErrMsg := "checksum mismatch"
154
- if err == nil {
155
- t .Errorf ("Expected error when checksum mismatches" )
156
- } else if err .Error () != expectedErrMsg {
157
- t .Errorf ("Expected error to be %s, got %s" , expectedErrMsg , err .Error ())
158
- }
159
- }
160
-
161
166
func testImageToCache (t * testing.T ) {
167
+ setupTestMiniHome (t )
162
168
downloadNum := 0
163
169
DownloadMock = mockSleepDownload (& downloadNum )
164
170
@@ -184,44 +190,72 @@ func testImageToCache(t *testing.T) {
184
190
}
185
191
186
192
// Validates that preload existence checks correctly caches values retrieved by remote checks.
193
+ // testPreloadExistsCaching verifies the caching semantics of PreloadExists when
194
+ // the local cache is absent and remote existence checks are required.
195
+ // In summary, this test enforces that:
196
+ // - PreloadExists performs remote checks only on cache misses.
197
+ // - Negative and positive results are cached per (k8sVersion, containerVersion, runtime) key.
198
+ // - GitHub is only consulted when GCS reports the preload as not existing.
199
+ // - Global state is correctly restored after the test.
187
200
func testPreloadExistsCaching (t * testing.T ) {
201
+ setupTestMiniHome (t )
188
202
checkCache = func (_ string ) (fs.FileInfo , error ) {
189
203
return nil , fmt .Errorf ("cache not found" )
190
204
}
191
205
doesPreloadExist := false
192
- checkCalled := false
193
- checkRemotePreloadExists = func (_ , _ string ) bool {
194
- checkCalled = true
206
+ gcsCheckCalls := 0
207
+ ghCheckCalls := 0
208
+ savedGCSCheck := checkRemotePreloadExistsGCS
209
+ savedGHCheck := checkRemotePreloadExistsGitHub
210
+ preloadStates = make (map [string ]map [string ]preloadState )
211
+ checkRemotePreloadExistsGCS = func (_ , _ string ) bool {
212
+ gcsCheckCalls ++
195
213
return doesPreloadExist
196
214
}
215
+ checkRemotePreloadExistsGitHub = func (_ , _ string ) bool {
216
+ ghCheckCalls ++
217
+ return false
218
+ }
219
+ t .Cleanup (func () {
220
+ checkRemotePreloadExistsGCS = savedGCSCheck
221
+ checkRemotePreloadExistsGitHub = savedGHCheck
222
+ preloadStates = make (map [string ]map [string ]preloadState )
223
+ })
224
+
197
225
existence := PreloadExists ("v1" , "c1" , "docker" , true )
198
- if existence || ! checkCalled {
199
- t .Errorf ("Expected preload not to exist and a check to be performed. Existence: %v, Check : %v " , existence , checkCalled )
226
+ if existence || gcsCheckCalls != 1 || ghCheckCalls != 1 {
227
+ t .Errorf ("Expected preload not to exist and checks to be performed. Existence: %v, GCS Calls : %d, GH Calls: %d " , existence , gcsCheckCalls , ghCheckCalls )
200
228
}
201
- checkCalled = false
229
+ gcsCheckCalls = 0
230
+ ghCheckCalls = 0
202
231
existence = PreloadExists ("v1" , "c1" , "docker" , true )
203
- if existence || checkCalled {
204
- t .Errorf ("Expected preload not to exist and no check to be performed. Existence: %v, Check : %v " , existence , checkCalled )
232
+ if existence || gcsCheckCalls != 0 || ghCheckCalls != 0 {
233
+ t .Errorf ("Expected preload not to exist and no checks to be performed. Existence: %v, GCS Calls : %d, GH Calls: %d " , existence , gcsCheckCalls , ghCheckCalls )
205
234
}
206
235
doesPreloadExist = true
207
- checkCalled = false
236
+ gcsCheckCalls = 0
237
+ ghCheckCalls = 0
208
238
existence = PreloadExists ("v2" , "c1" , "docker" , true )
209
- if ! existence || ! checkCalled {
210
- t .Errorf ("Expected preload to exist and a check to be performed . Existence: %v, Check : %v " , existence , checkCalled )
239
+ if ! existence || gcsCheckCalls != 1 || ghCheckCalls != 0 {
240
+ t .Errorf ("Expected preload to exist via GCS . Existence: %v, GCS Calls : %d, GH Calls: %d " , existence , gcsCheckCalls , ghCheckCalls )
211
241
}
212
- checkCalled = false
242
+ gcsCheckCalls = 0
243
+ ghCheckCalls = 0
213
244
existence = PreloadExists ("v2" , "c2" , "docker" , true )
214
- if ! existence || ! checkCalled {
215
- t .Errorf ("Expected preload to exist and a check to be performed . Existence: %v, Check : %v " , existence , checkCalled )
245
+ if ! existence || gcsCheckCalls != 1 || ghCheckCalls != 0 {
246
+ t .Errorf ("Expected preload to exist via GCS for new runtime . Existence: %v, GCS Calls : %d, GH Calls: %d " , existence , gcsCheckCalls , ghCheckCalls )
216
247
}
217
- checkCalled = false
248
+ gcsCheckCalls = 0
249
+ ghCheckCalls = 0
218
250
existence = PreloadExists ("v2" , "c2" , "docker" , true )
219
- if ! existence || checkCalled {
220
- t .Errorf ("Expected preload to exist and no check to be performed. Existence: %v, Check : %v " , existence , checkCalled )
251
+ if ! existence || gcsCheckCalls != 0 || ghCheckCalls != 0 {
252
+ t .Errorf ("Expected preload to exist and no checks to be performed. Existence: %v, GCS Calls : %d, GH Calls: %d " , existence , gcsCheckCalls , ghCheckCalls )
221
253
}
222
254
}
223
255
224
256
func testPreloadWithCachedSizeZero (t * testing.T ) {
257
+ setupTestMiniHome (t )
258
+
225
259
downloadNum := 0
226
260
DownloadMock = mockSleepDownload (& downloadNum )
227
261
f , err := os .CreateTemp ("" , "preload" )
@@ -231,8 +265,7 @@ func testPreloadWithCachedSizeZero(t *testing.T) {
231
265
232
266
checkCache = func (_ string ) (fs.FileInfo , error ) { return os .Stat (f .Name ()) }
233
267
checkPreloadExists = func (_ , _ , _ string , _ ... bool ) bool { return true }
234
- getChecksum = func (_ , _ string ) ([]byte , error ) { return []byte ("check" ), nil }
235
- ensureChecksumValid = func (_ , _ , _ string , _ []byte ) error { return nil }
268
+ getChecksumGCS = func (_ , _ string ) ([]byte , error ) { return []byte ("check" ), nil }
236
269
237
270
if err := Preload (constants .DefaultKubernetesVersion , constants .Docker , "docker" ); err != nil {
238
271
t .Errorf ("Expected no error with cached preload of size zero" )
0 commit comments