github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/fakes/fake_image_fetcher.go (about) 1 package fakes 2 3 import ( 4 "context" 5 6 "github.com/buildpacks/imgutil" 7 "github.com/pkg/errors" 8 9 "github.com/buildpacks/pack/pkg/image" 10 ) 11 12 type FetchArgs struct { 13 Daemon bool 14 PullPolicy image.PullPolicy 15 Platform string 16 LayoutOption image.LayoutOption 17 } 18 19 type FakeImageFetcher struct { 20 LocalImages map[string]imgutil.Image 21 RemoteImages map[string]imgutil.Image 22 FetchCalls map[string]*FetchArgs 23 } 24 25 func NewFakeImageFetcher() *FakeImageFetcher { 26 return &FakeImageFetcher{ 27 LocalImages: map[string]imgutil.Image{}, 28 RemoteImages: map[string]imgutil.Image{}, 29 FetchCalls: map[string]*FetchArgs{}, 30 } 31 } 32 33 func (f *FakeImageFetcher) Fetch(ctx context.Context, name string, options image.FetchOptions) (imgutil.Image, error) { 34 f.FetchCalls[name] = &FetchArgs{Daemon: options.Daemon, PullPolicy: options.PullPolicy, Platform: options.Platform, LayoutOption: options.LayoutOption} 35 36 ri, remoteFound := f.RemoteImages[name] 37 38 if options.Daemon { 39 li, localFound := f.LocalImages[name] 40 41 if shouldPull(localFound, remoteFound, options.PullPolicy) { 42 f.LocalImages[name] = ri 43 li = ri 44 } 45 if !localFound { 46 return nil, errors.Wrapf(image.ErrNotFound, "image '%s' does not exist on the daemon", name) 47 } 48 return li, nil 49 } 50 51 if !remoteFound { 52 return nil, errors.Wrapf(image.ErrNotFound, "image '%s' does not exist in registry", name) 53 } 54 55 return ri, nil 56 } 57 58 func (f *FakeImageFetcher) CheckReadAccess(_ string, _ image.FetchOptions) bool { 59 return true 60 } 61 62 func shouldPull(localFound, remoteFound bool, policy image.PullPolicy) bool { 63 if remoteFound && !localFound && policy == image.PullIfNotPresent { 64 return true 65 } 66 67 return remoteFound && policy == image.PullAlways 68 }