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