github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/manifest_inspect_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "testing" 7 8 "github.com/buildpacks/imgutil" 9 "github.com/golang/mock/gomock" 10 "github.com/google/go-containerregistry/pkg/authn" 11 v1 "github.com/google/go-containerregistry/pkg/v1" 12 "github.com/google/go-containerregistry/pkg/v1/random" 13 "github.com/heroku/color" 14 "github.com/pkg/errors" 15 "github.com/sclevine/spec" 16 "github.com/sclevine/spec/report" 17 18 "github.com/buildpacks/pack/pkg/logging" 19 "github.com/buildpacks/pack/pkg/testmocks" 20 h "github.com/buildpacks/pack/testhelpers" 21 ) 22 23 func TestInspectManifest(t *testing.T) { 24 color.Disable(true) 25 defer color.Disable(false) 26 spec.Run(t, "build", testInspectManifest, spec.Report(report.Terminal{})) 27 } 28 29 func testInspectManifest(t *testing.T, when spec.G, it spec.S) { 30 var ( 31 mockController *gomock.Controller 32 mockIndexFactory *testmocks.MockIndexFactory 33 stdout bytes.Buffer 34 stderr bytes.Buffer 35 logger logging.Logger 36 subject *Client 37 err error 38 ) 39 40 it.Before(func() { 41 logger = logging.NewLogWithWriters(&stdout, &stderr, logging.WithVerbose()) 42 mockController = gomock.NewController(t) 43 mockIndexFactory = testmocks.NewMockIndexFactory(mockController) 44 45 subject, err = NewClient( 46 WithLogger(logger), 47 WithIndexFactory(mockIndexFactory), 48 WithExperimental(true), 49 WithKeychain(authn.DefaultKeychain), 50 ) 51 h.AssertSameInstance(t, mockIndexFactory, subject.indexFactory) 52 h.AssertSameInstance(t, subject.logger, logger) 53 h.AssertNil(t, err) 54 }) 55 it.After(func() { 56 mockController.Finish() 57 }) 58 59 when("#InspectManifest", func() { 60 var indexRepoName string 61 62 when("index doesn't exits", func() { 63 it.Before(func() { 64 indexRepoName = h.NewRandomIndexRepoName() 65 mockIndexFactory. 66 EXPECT(). 67 FindIndex(gomock.Eq(indexRepoName), gomock.Any()).Return(nil, errors.New("index not found")) 68 }) 69 70 it("should return an error when index not found", func() { 71 err = subject.InspectManifest(indexRepoName) 72 h.AssertEq(t, err.Error(), "index not found") 73 }) 74 }) 75 76 when("index exists", func() { 77 var indexManifest *v1.IndexManifest 78 79 it.Before(func() { 80 indexRepoName = h.NewRandomIndexRepoName() 81 idx := setUpIndex(t, indexRepoName, *mockIndexFactory) 82 indexManifest, err = idx.IndexManifest() 83 h.AssertNil(t, err) 84 }) 85 86 it("should return formatted IndexManifest", func() { 87 err = subject.InspectManifest(indexRepoName) 88 h.AssertNil(t, err) 89 90 printedIndex := &v1.IndexManifest{} 91 err = json.Unmarshal(stdout.Bytes(), printedIndex) 92 h.AssertEq(t, indexManifest, printedIndex) 93 }) 94 }) 95 }) 96 } 97 98 func setUpIndex(t *testing.T, indexRepoName string, mockIndexFactory testmocks.MockIndexFactory) v1.ImageIndex { 99 randomUnderlyingIndex, err := random.Index(1024, 1, 2) 100 h.AssertNil(t, err) 101 102 options := &imgutil.IndexOptions{ 103 BaseIndex: randomUnderlyingIndex, 104 } 105 idx, err := imgutil.NewCNBIndex(indexRepoName, *options) 106 h.AssertNil(t, err) 107 108 mockIndexFactory.EXPECT().FindIndex(gomock.Eq(indexRepoName), gomock.Any()).Return(idx, nil) 109 return randomUnderlyingIndex 110 }