github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/common_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/buildpacks/lifecycle/auth" 8 "github.com/golang/mock/gomock" 9 "github.com/google/go-containerregistry/pkg/authn" 10 "github.com/heroku/color" 11 "github.com/sclevine/spec" 12 "github.com/sclevine/spec/report" 13 14 "github.com/buildpacks/pack/internal/builder" 15 "github.com/buildpacks/pack/pkg/image" 16 "github.com/buildpacks/pack/pkg/logging" 17 "github.com/buildpacks/pack/pkg/testmocks" 18 h "github.com/buildpacks/pack/testhelpers" 19 ) 20 21 func TestCommon(t *testing.T) { 22 color.Disable(true) 23 defer color.Disable(false) 24 spec.Run(t, "build", testCommon, spec.Report(report.Terminal{})) 25 } 26 27 func testCommon(t *testing.T, when spec.G, it spec.S) { 28 when("#resolveRunImage", func() { 29 var ( 30 subject *Client 31 outBuf bytes.Buffer 32 logger logging.Logger 33 keychain authn.Keychain 34 runImageName string 35 defaultRegistry string 36 defaultMirror string 37 gcrRegistry string 38 gcrRunMirror string 39 stackInfo builder.StackMetadata 40 assert = h.NewAssertionManager(t) 41 publish bool 42 err error 43 ) 44 45 it.Before(func() { 46 logger = logging.NewLogWithWriters(&outBuf, &outBuf) 47 48 keychain, err = auth.DefaultKeychain("pack-test/dummy") 49 h.AssertNil(t, err) 50 51 subject, err = NewClient(WithLogger(logger), WithKeychain(keychain)) 52 assert.Nil(err) 53 54 defaultRegistry = "default.registry.io" 55 runImageName = "stack/run" 56 defaultMirror = defaultRegistry + "/" + runImageName 57 gcrRegistry = "gcr.io" 58 gcrRunMirror = gcrRegistry + "/" + runImageName 59 stackInfo = builder.StackMetadata{ 60 RunImage: builder.RunImageMetadata{ 61 Image: runImageName, 62 Mirrors: []string{ 63 defaultMirror, gcrRunMirror, 64 }, 65 }, 66 } 67 }) 68 69 when("passed specific run image", func() { 70 it.Before(func() { 71 publish = false 72 }) 73 74 it("selects that run image", func() { 75 runImgFlag := "flag/passed-run-image" 76 runImageName = subject.resolveRunImage(runImgFlag, defaultRegistry, "", stackInfo.RunImage, nil, publish, image.FetchOptions{Daemon: !publish, PullPolicy: image.PullAlways}) 77 assert.Equal(runImageName, runImgFlag) 78 }) 79 }) 80 81 when("desirable run-image are accessible", func() { 82 it.Before(func() { 83 publish = true 84 mockController := gomock.NewController(t) 85 mockFetcher := testmocks.NewMockImageFetcher(mockController) 86 mockFetcher.EXPECT().CheckReadAccessValidator(gomock.Any(), gomock.Any()).Return(true).AnyTimes() 87 subject, err = NewClient(WithLogger(logger), WithKeychain(keychain), WithFetcher(mockFetcher)) 88 h.AssertNil(t, err) 89 }) 90 91 it("defaults to run-image in registry publishing to", func() { 92 runImageName = subject.resolveRunImage("", gcrRegistry, defaultRegistry, stackInfo.RunImage, nil, publish, image.FetchOptions{}) 93 assert.Equal(runImageName, gcrRunMirror) 94 }) 95 96 it("prefers config defined run image mirror to stack defined run image mirror", func() { 97 configMirrors := map[string][]string{ 98 runImageName: {defaultRegistry + "/unique-run-img"}, 99 } 100 runImageName = subject.resolveRunImage("", defaultRegistry, "", stackInfo.RunImage, configMirrors, publish, image.FetchOptions{}) 101 assert.NotEqual(runImageName, defaultMirror) 102 assert.Equal(runImageName, defaultRegistry+"/unique-run-img") 103 }) 104 105 it("returns a config mirror if no match to target registry", func() { 106 configMirrors := map[string][]string{ 107 runImageName: {defaultRegistry + "/unique-run-img"}, 108 } 109 runImageName = subject.resolveRunImage("", "test.registry.io", "", stackInfo.RunImage, configMirrors, publish, image.FetchOptions{}) 110 assert.NotEqual(runImageName, defaultMirror) 111 assert.Equal(runImageName, defaultRegistry+"/unique-run-img") 112 }) 113 }) 114 115 when("desirable run-images are not accessible", func() { 116 it.Before(func() { 117 publish = true 118 119 mockController := gomock.NewController(t) 120 mockFetcher := testmocks.NewMockImageFetcher(mockController) 121 mockFetcher.EXPECT().CheckReadAccessValidator(gcrRunMirror, gomock.Any()).Return(false) 122 mockFetcher.EXPECT().CheckReadAccessValidator(stackInfo.RunImage.Image, gomock.Any()).Return(false) 123 mockFetcher.EXPECT().CheckReadAccessValidator(defaultMirror, gomock.Any()).Return(true) 124 125 subject, err = NewClient(WithLogger(logger), WithKeychain(keychain), WithFetcher(mockFetcher)) 126 h.AssertNil(t, err) 127 }) 128 129 it("selects the first accessible run-image", func() { 130 runImageName = subject.resolveRunImage("", gcrRegistry, defaultRegistry, stackInfo.RunImage, nil, publish, image.FetchOptions{}) 131 assert.Equal(runImageName, defaultMirror) 132 }) 133 }) 134 135 when("desirable run-image are empty", func() { 136 it.Before(func() { 137 publish = false 138 stackInfo = builder.StackMetadata{ 139 RunImage: builder.RunImageMetadata{ 140 Image: "stack/run-image", 141 }, 142 } 143 }) 144 145 it("selects the builder run-image", func() { 146 // issue: https://github.com/buildpacks/pack/issues/2078 147 runImageName = subject.resolveRunImage("", "", "", stackInfo.RunImage, nil, publish, image.FetchOptions{}) 148 assert.Equal(runImageName, "stack/run-image") 149 }) 150 }) 151 }) 152 }