github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/manifest_rm_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/google/go-containerregistry/pkg/name"
    13  	"github.com/heroku/color"
    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 TestRemoveManifest(t *testing.T) {
    23  	color.Disable(true)
    24  	defer color.Disable(false)
    25  	spec.Run(t, "build", testRemoveManifest, spec.Report(report.Terminal{}))
    26  }
    27  
    28  func testRemoveManifest(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("", "rm-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("#RemoveManifest", func() {
    63  		var (
    64  			indexPath     string
    65  			indexRepoName string
    66  		)
    67  
    68  		when("index exists", func() {
    69  			var digest name.Digest
    70  			var idx imgutil.ImageIndex
    71  
    72  			it.Before(func() {
    73  				indexRepoName = h.NewRandomIndexRepoName()
    74  				indexPath = filepath.Join(tmpDir, imgutil.MakeFileSafeName(indexRepoName))
    75  
    76  				// Initialize the Index with 2 image manifest
    77  				idx, digest = h.RandomCNBIndexAndDigest(t, indexRepoName, 1, 2)
    78  				mockIndexFactory.EXPECT().LoadIndex(gomock.Eq(indexRepoName), gomock.Any()).Return(idx, nil)
    79  			})
    80  
    81  			it("should remove local index", func() {
    82  				err = subject.RemoveManifest(indexRepoName, []string{digest.Name()})
    83  				h.AssertNil(t, err)
    84  
    85  				// We expect one manifest after removing one of them
    86  				index := h.ReadIndexManifest(t, indexPath)
    87  				h.AssertEq(t, len(index.Manifests), 1)
    88  				h.AssertNotEq(t, index.Manifests[0].Digest.String(), digest.Name())
    89  			})
    90  		})
    91  	})
    92  }