|
| 1 | +package porter |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "get.porter.sh/porter/pkg/cache" |
| 7 | + cnabtooci "get.porter.sh/porter/pkg/cnab/cnab-to-oci" |
| 8 | + "get.porter.sh/porter/pkg/config" |
| 9 | + "github.com/cnabio/cnab-go/bundle" |
| 10 | + "github.com/cnabio/cnab-to-oci/relocation" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestBundleResolver_Resolve_ForcePull(t *testing.T) { |
| 15 | + tc := config.NewTestConfig(t) |
| 16 | + testReg := cnabtooci.NewTestRegistry() |
| 17 | + testCache := cache.NewTestCache(cache.New(tc.Config)) |
| 18 | + resolver := BundleResolver{ |
| 19 | + Cache: testCache, |
| 20 | + Registry: testReg, |
| 21 | + } |
| 22 | + |
| 23 | + cacheSearched := false |
| 24 | + testCache.FindBundleMock = func(tag string) (cache.CachedBundle, bool, error) { |
| 25 | + cacheSearched = true |
| 26 | + return cache.CachedBundle{}, true, nil |
| 27 | + } |
| 28 | + |
| 29 | + pulled := false |
| 30 | + testReg.MockPullBundle = func(tag string, insecureRegistry bool) (bundle.Bundle, *relocation.ImageRelocationMap, error) { |
| 31 | + pulled = true |
| 32 | + return bundle.Bundle{}, nil, nil |
| 33 | + } |
| 34 | + |
| 35 | + opts := BundlePullOptions{ |
| 36 | + Force: true, |
| 37 | + } |
| 38 | + resolver.Resolve(opts) |
| 39 | + |
| 40 | + assert.False(t, cacheSearched, "Force should have skipped the cache") |
| 41 | + assert.True(t, pulled, "The bundle should have been re-pulled") |
| 42 | +} |
| 43 | + |
| 44 | +func TestBundleResolver_Resolve_CacheHit(t *testing.T) { |
| 45 | + tc := config.NewTestConfig(t) |
| 46 | + testReg := cnabtooci.NewTestRegistry() |
| 47 | + testCache := cache.NewTestCache(cache.New(tc.Config)) |
| 48 | + resolver := BundleResolver{ |
| 49 | + Cache: testCache, |
| 50 | + Registry: testReg, |
| 51 | + } |
| 52 | + |
| 53 | + cacheSearched := false |
| 54 | + testCache.FindBundleMock = func(tag string) (cache.CachedBundle, bool, error) { |
| 55 | + cacheSearched = true |
| 56 | + return cache.CachedBundle{}, true, nil |
| 57 | + } |
| 58 | + |
| 59 | + pulled := false |
| 60 | + testReg.MockPullBundle = func(tag string, insecureRegistry bool) (bundle.Bundle, *relocation.ImageRelocationMap, error) { |
| 61 | + pulled = true |
| 62 | + return bundle.Bundle{}, nil, nil |
| 63 | + } |
| 64 | + |
| 65 | + opts := BundlePullOptions{} |
| 66 | + resolver.Resolve(opts) |
| 67 | + |
| 68 | + assert.True(t, cacheSearched, "The cache should be searched when force is not specified") |
| 69 | + assert.False(t, pulled, "The bundle should NOT be pulled because it was found in the cache") |
| 70 | +} |
| 71 | + |
| 72 | +func TestBundleResolver_Resolve_CacheMiss(t *testing.T) { |
| 73 | + tc := config.NewTestConfig(t) |
| 74 | + testReg := cnabtooci.NewTestRegistry() |
| 75 | + testCache := cache.NewTestCache(cache.New(tc.Config)) |
| 76 | + resolver := BundleResolver{ |
| 77 | + Cache: testCache, |
| 78 | + Registry: testReg, |
| 79 | + } |
| 80 | + |
| 81 | + cacheSearched := false |
| 82 | + testCache.FindBundleMock = func(tag string) (cache.CachedBundle, bool, error) { |
| 83 | + cacheSearched = true |
| 84 | + return cache.CachedBundle{}, false, nil |
| 85 | + } |
| 86 | + |
| 87 | + pulled := false |
| 88 | + testReg.MockPullBundle = func(tag string, insecureRegistry bool) (bundle.Bundle, *relocation.ImageRelocationMap, error) { |
| 89 | + pulled = true |
| 90 | + return bundle.Bundle{}, nil, nil |
| 91 | + } |
| 92 | + |
| 93 | + opts := BundlePullOptions{} |
| 94 | + resolver.Resolve(opts) |
| 95 | + |
| 96 | + assert.True(t, cacheSearched, "The cache should be searched when force is not specified") |
| 97 | + assert.True(t, pulled, "The bundle should have been pulled because the bundle was not in the cache") |
| 98 | +} |
0 commit comments