Skip to content

Commit aa99d88

Browse files
authored
Merge pull request #15256 from spowelljr/checkForPreloadSize
Ensure downloading preload images when size is zero
2 parents 8bd593b + 0f08d6d commit aa99d88

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

pkg/minikube/download/download_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package download
1919
import (
2020
"fmt"
2121
"io/fs"
22+
"os"
2223
"sync"
2324
"testing"
2425
"time"
@@ -35,6 +36,7 @@ func TestDownload(t *testing.T) {
3536
t.Run("PreloadNotExists", testPreloadNotExists)
3637
t.Run("PreloadChecksumMismatch", testPreloadChecksumMismatch)
3738
t.Run("PreloadExistsCaching", testPreloadExistsCaching)
39+
t.Run("PreloadWithCachedSizeZero", testPreloadWithCachedSizeZero)
3840
}
3941

4042
// Returns a mock function that sleeps before incrementing `downloadsCounter` and creates the requested file.
@@ -80,12 +82,20 @@ func testBinaryDownloadPreventsMultipleDownload(t *testing.T) {
8082
func testPreloadDownloadPreventsMultipleDownload(t *testing.T) {
8183
downloadNum := 0
8284
DownloadMock = mockSleepDownload(&downloadNum)
85+
f, err := os.CreateTemp("", "preload")
86+
if err != nil {
87+
t.Fatalf("failed to create temp file: %v", err)
88+
}
89+
defer os.Remove(f.Name())
90+
if _, err := f.Write([]byte("data")); err != nil {
91+
t.Fatalf("failed to write to temp file: %v", err)
92+
}
8393

8494
checkCache = func(file string) (fs.FileInfo, error) {
8595
if downloadNum == 0 {
8696
return nil, fmt.Errorf("some error")
8797
}
88-
return nil, nil
98+
return os.Stat(f.Name())
8999
}
90100
checkPreloadExists = func(k8sVersion, containerRuntime, driverName string, forcePreload ...bool) bool { return true }
91101
getChecksum = func(k8sVersion, containerRuntime string) ([]byte, error) { return []byte("check"), nil }
@@ -236,3 +246,25 @@ func testPreloadExistsCaching(t *testing.T) {
236246
t.Errorf("Expected preload to exist and no check to be performed. Existence: %v, Check: %v", existence, checkCalled)
237247
}
238248
}
249+
250+
func testPreloadWithCachedSizeZero(t *testing.T) {
251+
downloadNum := 0
252+
DownloadMock = mockSleepDownload(&downloadNum)
253+
f, err := os.CreateTemp("", "preload")
254+
if err != nil {
255+
t.Fatalf("failed to create temp file: %v", err)
256+
}
257+
258+
checkCache = func(file string) (fs.FileInfo, error) { return os.Stat(f.Name()) }
259+
checkPreloadExists = func(k8sVersion, containerRuntime, driverName string, forcePreload ...bool) bool { return true }
260+
getChecksum = func(k8sVersion, containerRuntime string) ([]byte, error) { return []byte("check"), nil }
261+
ensureChecksumValid = func(k8sVersion, containerRuntime, path string, checksum []byte) error { return nil }
262+
263+
if err := Preload(constants.DefaultKubernetesVersion, constants.Docker, "docker"); err != nil {
264+
t.Errorf("Expected no error with cached preload of size zero")
265+
}
266+
267+
if downloadNum != 1 {
268+
t.Errorf("Expected only 1 download attempt but got %v!", downloadNum)
269+
}
270+
}

pkg/minikube/download/preload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func PreloadExists(k8sVersion, containerRuntime, driverName string, forcePreload
144144

145145
// Omit remote check if tarball exists locally
146146
targetPath := TarballPath(k8sVersion, containerRuntime)
147-
if _, err := checkCache(targetPath); err == nil {
147+
if f, err := checkCache(targetPath); err == nil && f.Size() != 0 {
148148
klog.Infof("Found local preload: %s", targetPath)
149149
setPreloadState(k8sVersion, containerRuntime, true)
150150
return true
@@ -170,7 +170,7 @@ func Preload(k8sVersion, containerRuntime, driverName string) error {
170170
return err
171171
}
172172

173-
if _, err := checkCache(targetPath); err == nil {
173+
if f, err := checkCache(targetPath); err == nil && f.Size() != 0 {
174174
klog.Infof("Found %s in cache, skipping download", targetPath)
175175
return nil
176176
}

0 commit comments

Comments
 (0)