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

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/heroku/color"
     9  	"github.com/sclevine/spec"
    10  	"github.com/sclevine/spec/report"
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/buildpacks/pack/internal/commands"
    14  	"github.com/buildpacks/pack/internal/commands/testmocks"
    15  	"github.com/buildpacks/pack/pkg/client"
    16  	"github.com/buildpacks/pack/pkg/logging"
    17  	h "github.com/buildpacks/pack/testhelpers"
    18  )
    19  
    20  func TestManifestAnnotationsCommand(t *testing.T) {
    21  	color.Disable(true)
    22  	defer color.Disable(false)
    23  
    24  	spec.Run(t, "Commands", testManifestAnnotateCommand, spec.Random(), spec.Report(report.Terminal{}))
    25  }
    26  
    27  func testManifestAnnotateCommand(t *testing.T, when spec.G, it spec.S) {
    28  	var (
    29  		command        *cobra.Command
    30  		logger         *logging.LogWithWriters
    31  		outBuf         bytes.Buffer
    32  		mockController *gomock.Controller
    33  		mockClient     *testmocks.MockPackClient
    34  	)
    35  
    36  	it.Before(func() {
    37  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    38  		mockController = gomock.NewController(t)
    39  		mockClient = testmocks.NewMockPackClient(mockController)
    40  
    41  		command = commands.ManifestAnnotate(logger, mockClient)
    42  	})
    43  
    44  	when("args are valid", func() {
    45  		var (
    46  			indexRepoName string
    47  			repoName      string
    48  		)
    49  		it.Before(func() {
    50  			indexRepoName = h.NewRandomIndexRepoName()
    51  			repoName = "busybox@sha256:6457d53fb065d6f250e1504b9bc42d5b6c65941d57532c072d929dd0628977d0"
    52  		})
    53  
    54  		when("index exists", func() {
    55  			when("--os is provided", func() {
    56  				it.Before(func() {
    57  					mockClient.EXPECT().
    58  						AnnotateManifest(
    59  							gomock.Any(),
    60  							gomock.Eq(client.ManifestAnnotateOptions{
    61  								IndexRepoName: indexRepoName,
    62  								RepoName:      repoName,
    63  								OS:            "linux",
    64  								Annotations:   map[string]string{},
    65  							}),
    66  						).
    67  						Return(nil)
    68  				})
    69  
    70  				it("should annotate images with given flags", func() {
    71  					command.SetArgs([]string{indexRepoName, repoName, "--os", "linux"})
    72  					h.AssertNilE(t, command.Execute())
    73  				})
    74  			})
    75  
    76  			when("--arch is provided", func() {
    77  				it.Before(func() {
    78  					mockClient.EXPECT().
    79  						AnnotateManifest(
    80  							gomock.Any(),
    81  							gomock.Eq(client.ManifestAnnotateOptions{
    82  								IndexRepoName: indexRepoName,
    83  								RepoName:      repoName,
    84  								OSArch:        "amd64",
    85  								Annotations:   map[string]string{},
    86  							}),
    87  						).
    88  						Return(nil)
    89  				})
    90  
    91  				it("should annotate images with given flags", func() {
    92  					command.SetArgs([]string{indexRepoName, repoName, "--arch", "amd64"})
    93  					h.AssertNilE(t, command.Execute())
    94  				})
    95  			})
    96  
    97  			when("--variant is provided", func() {
    98  				it.Before(func() {
    99  					mockClient.EXPECT().
   100  						AnnotateManifest(
   101  							gomock.Any(),
   102  							gomock.Eq(client.ManifestAnnotateOptions{
   103  								IndexRepoName: indexRepoName,
   104  								RepoName:      repoName,
   105  								OSVariant:     "V6",
   106  								Annotations:   map[string]string{},
   107  							}),
   108  						).
   109  						Return(nil)
   110  				})
   111  
   112  				it("should annotate images with given flags", func() {
   113  					command.SetArgs([]string{indexRepoName, repoName, "--variant", "V6"})
   114  					h.AssertNilE(t, command.Execute())
   115  				})
   116  			})
   117  
   118  			when("--annotations are provided", func() {
   119  				it.Before(func() {
   120  					mockClient.EXPECT().
   121  						AnnotateManifest(
   122  							gomock.Any(),
   123  							gomock.Eq(client.ManifestAnnotateOptions{
   124  								IndexRepoName: indexRepoName,
   125  								RepoName:      repoName,
   126  								Annotations:   map[string]string{"foo": "bar"},
   127  							}),
   128  						).
   129  						Return(nil)
   130  				})
   131  
   132  				it("should annotate images with given flags", func() {
   133  					command.SetArgs([]string{indexRepoName, repoName, "--annotations", "foo=bar"})
   134  					h.AssertNilE(t, command.Execute())
   135  				})
   136  			})
   137  
   138  			when("--help", func() {
   139  				it("should have help flag", func() {
   140  					command.SetArgs([]string{"--help"})
   141  					h.AssertNilE(t, command.Execute())
   142  				})
   143  			})
   144  		})
   145  	})
   146  
   147  	when("args are invalid", func() {
   148  		it("errors a message when no options are provided", func() {
   149  			command.SetArgs([]string{"foo", "bar"})
   150  			h.AssertError(t, command.Execute(), "one of --os, --arch, or --variant is required")
   151  		})
   152  
   153  		it("errors when missing mandatory arguments", func() {
   154  			command.SetArgs([]string{"some-index"})
   155  			err := command.Execute()
   156  			h.AssertNotNil(t, err)
   157  			h.AssertError(t, err, "accepts 2 arg(s), received 1")
   158  		})
   159  
   160  		it("errors when annotations are invalid", func() {
   161  			command.SetArgs([]string{"some-index", "some-manifest", "--annotations", "some-key"})
   162  			err := command.Execute()
   163  			h.AssertEq(t, err.Error(), `invalid argument "some-key" for "--annotations" flag: some-key must be formatted as key=value`)
   164  		})
   165  	})
   166  }