github.com/buildpack/pack@v0.5.0/internal/fakes/fake_image_fetcher.go (about)

     1  package fakes
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/buildpack/imgutil"
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/buildpack/pack/image"
    10  )
    11  
    12  type FetchArgs struct {
    13  	Daemon bool
    14  	Pull   bool
    15  }
    16  
    17  type FakeImageFetcher struct {
    18  	LocalImages  map[string]imgutil.Image
    19  	RemoteImages map[string]imgutil.Image
    20  	FetchCalls   map[string]*FetchArgs
    21  }
    22  
    23  func NewFakeImageFetcher() *FakeImageFetcher {
    24  	return &FakeImageFetcher{
    25  		LocalImages:  map[string]imgutil.Image{},
    26  		RemoteImages: map[string]imgutil.Image{},
    27  		FetchCalls:   map[string]*FetchArgs{},
    28  	}
    29  }
    30  
    31  func (f *FakeImageFetcher) Fetch(ctx context.Context, name string, daemon, pull bool) (imgutil.Image, error) {
    32  	f.FetchCalls[name] = &FetchArgs{Daemon: daemon, Pull: pull}
    33  
    34  	ri, remoteFound := f.RemoteImages[name]
    35  
    36  	if daemon {
    37  		if remoteFound && pull {
    38  			f.LocalImages[name] = ri
    39  		}
    40  		li, localFound := f.LocalImages[name]
    41  		if !localFound {
    42  			return nil, errors.Wrapf(image.ErrNotFound, "image '%s' does not exist on the daemon", name)
    43  		}
    44  		return li, nil
    45  	}
    46  
    47  	if !remoteFound {
    48  		return nil, errors.Wrapf(image.ErrNotFound, "image '%s' does not exist in registry", name)
    49  	}
    50  
    51  	return ri, nil
    52  }