Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alevenberg committed Nov 15, 2023
1 parent ac52976 commit da533e6
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion pkg/image/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
)

// mockImage mocks the v1.Image interface
type mockImage struct{}
type mockImage struct{
}

func (m *mockImage) ConfigFile() (*v1.ConfigFile, error) {
return nil, nil
Expand Down Expand Up @@ -140,3 +141,49 @@ func Test_RetrieveRemoteImage_skipFallback(t *testing.T) {
t.Fatal("Expected call to fail because fallback to default registry is skipped")
}
}

func Test_RetryRetrieveRemoteImageSucceeds(t *testing.T) {
image := "debian"

opts := config.RegistryOptions{
ImageDownloadRetry: 2,
}
attempts := 0
remoteImageFunc = func(ref name.Reference, options ...remote.Option) (v1.Image, error) {
if attempts < 2{
attempts ++
return nil, errors.New("no image found")
}
return &mockImage{}, nil
}

// Clean cached image
manifestCache = make(map[string]v1.Image)

if _, err := RetrieveRemoteImage(image, opts, ""); err != nil {
t.Fatal("Expected call to succeed because of retry")
}
}

func Test_NoRetryRetrieveRemoteImageFails(t *testing.T) {
image := "debian"

opts := config.RegistryOptions{
ImageDownloadRetry: 0,
}
attempts := 0
remoteImageFunc = func(ref name.Reference, options ...remote.Option) (v1.Image, error) {
if attempts < 1 {
attempts ++
return nil, errors.New("no image found")
}
return &mockImage{}, nil
}

// Clean cached image
manifestCache = make(map[string]v1.Image)

if _, err := RetrieveRemoteImage(image, opts, ""); err == nil {
t.Fatal("Expected call to fail because there is no retry")
}
}

0 comments on commit da533e6

Please sign in to comment.