github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/manifest_remove_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/buildpacks/imgutil" 10 "github.com/golang/mock/gomock" 11 "github.com/google/go-containerregistry/pkg/authn" 12 "github.com/heroku/color" 13 "github.com/pkg/errors" 14 "github.com/sclevine/spec" 15 "github.com/sclevine/spec/report" 16 17 "github.com/buildpacks/pack/pkg/logging" 18 "github.com/buildpacks/pack/pkg/testmocks" 19 h "github.com/buildpacks/pack/testhelpers" 20 ) 21 22 func TestDeleteManifest(t *testing.T) { 23 color.Disable(true) 24 defer color.Disable(false) 25 spec.Run(t, "build", testDeleteManifest, spec.Report(report.Terminal{})) 26 } 27 28 func testDeleteManifest(t *testing.T, when spec.G, it spec.S) { 29 var ( 30 mockController *gomock.Controller 31 mockIndexFactory *testmocks.MockIndexFactory 32 out bytes.Buffer 33 logger logging.Logger 34 subject *Client 35 err error 36 tmpDir string 37 ) 38 39 it.Before(func() { 40 logger = logging.NewLogWithWriters(&out, &out, logging.WithVerbose()) 41 mockController = gomock.NewController(t) 42 mockIndexFactory = testmocks.NewMockIndexFactory(mockController) 43 44 tmpDir, err = os.MkdirTemp("", "remove-manifest-test") 45 h.AssertNil(t, err) 46 os.Setenv("XDG_RUNTIME_DIR", tmpDir) 47 48 subject, err = NewClient( 49 WithLogger(logger), 50 WithIndexFactory(mockIndexFactory), 51 WithExperimental(true), 52 WithKeychain(authn.DefaultKeychain), 53 ) 54 h.AssertSameInstance(t, mockIndexFactory, subject.indexFactory) 55 h.AssertNil(t, err) 56 }) 57 it.After(func() { 58 mockController.Finish() 59 h.AssertNil(t, os.RemoveAll(tmpDir)) 60 }) 61 62 when("#DeleteManifest", func() { 63 var ( 64 indexPath string 65 indexRepoName string 66 ) 67 68 when("index doesn't exists", func() { 69 it.Before(func() { 70 mockIndexFactory.EXPECT().LoadIndex(gomock.Any(), gomock.Any()).Return(nil, errors.New("index not found locally")) 71 }) 72 it("should return an error when index is already deleted", func() { 73 err = subject.DeleteManifest([]string{"pack/none-existent-index"}) 74 h.AssertNotNil(t, err) 75 }) 76 }) 77 78 when("index exists", func() { 79 var idx imgutil.ImageIndex 80 81 it.Before(func() { 82 indexRepoName = h.NewRandomIndexRepoName() 83 indexPath = filepath.Join(tmpDir, imgutil.MakeFileSafeName(indexRepoName)) 84 idx = h.RandomCNBIndex(t, indexRepoName, 1, 1) 85 mockIndexFactory.EXPECT().LoadIndex(gomock.Eq(indexRepoName), gomock.Any()).Return(idx, nil) 86 87 // Let's write the index on disk 88 h.AssertNil(t, idx.SaveDir()) 89 }) 90 91 it("should delete local index", func() { 92 err = subject.DeleteManifest([]string{indexRepoName}) 93 h.AssertNil(t, err) 94 h.AssertContains(t, out.String(), "Successfully deleted manifest list(s) from local storage") 95 h.AssertPathDoesNotExists(t, indexPath) 96 }) 97 }) 98 }) 99 }