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

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/google/go-containerregistry/pkg/v1/types"
     9  	"github.com/heroku/color"
    10  	"github.com/pkg/errors"
    11  	"github.com/sclevine/spec"
    12  	"github.com/sclevine/spec/report"
    13  	"github.com/spf13/cobra"
    14  
    15  	"github.com/buildpacks/pack/internal/commands"
    16  	"github.com/buildpacks/pack/internal/commands/testmocks"
    17  	"github.com/buildpacks/pack/pkg/client"
    18  	"github.com/buildpacks/pack/pkg/logging"
    19  	h "github.com/buildpacks/pack/testhelpers"
    20  )
    21  
    22  func TestManifestPushCommand(t *testing.T) {
    23  	color.Disable(true)
    24  	defer color.Disable(false)
    25  
    26  	spec.Run(t, "Commands", testManifestPushCommand, spec.Random(), spec.Report(report.Terminal{}))
    27  }
    28  
    29  func testManifestPushCommand(t *testing.T, when spec.G, it spec.S) {
    30  	var (
    31  		command        *cobra.Command
    32  		logger         *logging.LogWithWriters
    33  		outBuf         bytes.Buffer
    34  		mockController *gomock.Controller
    35  		mockClient     *testmocks.MockPackClient
    36  	)
    37  
    38  	it.Before(func() {
    39  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    40  		mockController = gomock.NewController(t)
    41  		mockClient = testmocks.NewMockPackClient(mockController)
    42  
    43  		command = commands.ManifestPush(logger, mockClient)
    44  	})
    45  
    46  	when("args are valid", func() {
    47  		var indexRepoName string
    48  		it.Before(func() {
    49  			indexRepoName = h.NewRandomIndexRepoName()
    50  		})
    51  
    52  		when("index exists", func() {
    53  			when("no extra flag is provided", func() {
    54  				it.Before(func() {
    55  					mockClient.EXPECT().
    56  						PushManifest(gomock.Eq(client.PushManifestOptions{
    57  							IndexRepoName: indexRepoName,
    58  							Format:        types.OCIImageIndex,
    59  							Insecure:      false,
    60  							Purge:         false,
    61  						})).Return(nil)
    62  				})
    63  
    64  				it("should call push operation with default configuration", func() {
    65  					command.SetArgs([]string{indexRepoName})
    66  					h.AssertNil(t, command.Execute())
    67  				})
    68  			})
    69  
    70  			when("--format is docker", func() {
    71  				it.Before(func() {
    72  					mockClient.EXPECT().
    73  						PushManifest(gomock.Eq(client.PushManifestOptions{
    74  							IndexRepoName: indexRepoName,
    75  							Format:        types.DockerManifestList,
    76  							Insecure:      false,
    77  							Purge:         false,
    78  						})).Return(nil)
    79  				})
    80  
    81  				it("should call push operation with docker media type", func() {
    82  					command.SetArgs([]string{indexRepoName, "-f", "docker"})
    83  					h.AssertNil(t, command.Execute())
    84  				})
    85  			})
    86  
    87  			when("--purge", func() {
    88  				it.Before(func() {
    89  					mockClient.EXPECT().
    90  						PushManifest(gomock.Eq(client.PushManifestOptions{
    91  							IndexRepoName: indexRepoName,
    92  							Format:        types.OCIImageIndex,
    93  							Insecure:      false,
    94  							Purge:         true,
    95  						})).Return(nil)
    96  				})
    97  
    98  				it("should call push operation with purge enabled", func() {
    99  					command.SetArgs([]string{indexRepoName, "--purge"})
   100  					h.AssertNil(t, command.Execute())
   101  				})
   102  			})
   103  
   104  			when("--insecure", func() {
   105  				it.Before(func() {
   106  					mockClient.EXPECT().
   107  						PushManifest(gomock.Eq(client.PushManifestOptions{
   108  							IndexRepoName: indexRepoName,
   109  							Format:        types.OCIImageIndex,
   110  							Insecure:      true,
   111  							Purge:         false,
   112  						})).Return(nil)
   113  				})
   114  
   115  				it("should call push operation with insecure enabled", func() {
   116  					command.SetArgs([]string{indexRepoName, "--insecure"})
   117  					h.AssertNil(t, command.Execute())
   118  				})
   119  			})
   120  
   121  			when("--help", func() {
   122  				it("should have help flag", func() {
   123  					command.SetArgs([]string{"--help"})
   124  					h.AssertNilE(t, command.Execute())
   125  					h.AssertEq(t, outBuf.String(), "")
   126  				})
   127  			})
   128  		})
   129  
   130  		when("index doesn't exist", func() {
   131  			it.Before(func() {
   132  				mockClient.
   133  					EXPECT().
   134  					PushManifest(
   135  						gomock.Any(),
   136  					).
   137  					AnyTimes().
   138  					Return(errors.New("unable to push Image"))
   139  			})
   140  
   141  			it("should return an error when index not exists locally", func() {
   142  				command.SetArgs([]string{"some-index"})
   143  				err := command.Execute()
   144  				h.AssertNotNil(t, err)
   145  			})
   146  		})
   147  	})
   148  
   149  	when("args are invalid", func() {
   150  		when("--format is invalid", func() {
   151  			it("should return an error when index not exists locally", func() {
   152  				command.SetArgs([]string{"some-index", "-f", "bad-media-type"})
   153  				err := command.Execute()
   154  				h.AssertNotNil(t, err)
   155  				h.AssertError(t, err, "invalid media type format")
   156  			})
   157  		})
   158  	})
   159  }