github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/manifest_push_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/google/go-containerregistry/pkg/authn"
    10  	"github.com/heroku/color"
    11  	"github.com/pkg/errors"
    12  	"github.com/sclevine/spec"
    13  	"github.com/sclevine/spec/report"
    14  
    15  	"github.com/buildpacks/pack/pkg/logging"
    16  	"github.com/buildpacks/pack/pkg/testmocks"
    17  	h "github.com/buildpacks/pack/testhelpers"
    18  )
    19  
    20  func TestPushManifest(t *testing.T) {
    21  	color.Disable(true)
    22  	defer color.Disable(false)
    23  	spec.Run(t, "build", testPushManifest, spec.Report(report.Terminal{}))
    24  }
    25  
    26  func testPushManifest(t *testing.T, when spec.G, it spec.S) {
    27  	var (
    28  		mockController   *gomock.Controller
    29  		mockIndexFactory *testmocks.MockIndexFactory
    30  		out              bytes.Buffer
    31  		logger           logging.Logger
    32  		subject          *Client
    33  		err              error
    34  		tmpDir           string
    35  	)
    36  	it.Before(func() {
    37  		logger = logging.NewLogWithWriters(&out, &out, logging.WithVerbose())
    38  		mockController = gomock.NewController(t)
    39  		mockIndexFactory = testmocks.NewMockIndexFactory(mockController)
    40  
    41  		subject, err = NewClient(
    42  			WithLogger(logger),
    43  			WithIndexFactory(mockIndexFactory),
    44  			WithExperimental(true),
    45  			WithKeychain(authn.DefaultKeychain),
    46  		)
    47  		h.AssertSameInstance(t, mockIndexFactory, subject.indexFactory)
    48  		h.AssertNil(t, err)
    49  	})
    50  	it.After(func() {
    51  		mockController.Finish()
    52  		h.AssertNil(t, os.RemoveAll(tmpDir))
    53  	})
    54  
    55  	when("#PushManifest", func() {
    56  		when("index exists locally", func() {
    57  			var index *h.MockImageIndex
    58  
    59  			it.Before(func() {
    60  				index = h.NewMockImageIndex(t, "some-index", 1, 2)
    61  				mockIndexFactory.EXPECT().LoadIndex(gomock.Eq("some-index"), gomock.Any()).Return(index, nil)
    62  			})
    63  			it("pushes the index to the registry", func() {
    64  				err = subject.PushManifest(PushManifestOptions{
    65  					IndexRepoName: "some-index",
    66  				})
    67  				h.AssertNil(t, err)
    68  				h.AssertTrue(t, index.PushCalled)
    69  			})
    70  		})
    71  
    72  		when("index doesn't exist locally", func() {
    73  			it.Before(func() {
    74  				mockIndexFactory.EXPECT().LoadIndex(gomock.Any(), gomock.Any()).Return(nil, errors.New("ErrNoImageOrIndexFoundWithGivenDigest"))
    75  			})
    76  
    77  			it("errors with a message", func() {
    78  				err = subject.PushManifest(PushManifestOptions{
    79  					IndexRepoName: "some-index",
    80  				})
    81  				h.AssertNotNil(t, err)
    82  			})
    83  		})
    84  	})
    85  }