@@ -19,6 +19,7 @@ package download
1919import (
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) {
8082func 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+ }
0 commit comments