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

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     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/internal/config"
    18  	"github.com/buildpacks/pack/pkg/buildpack"
    19  	"github.com/buildpacks/pack/pkg/client"
    20  	"github.com/buildpacks/pack/pkg/dist"
    21  	"github.com/buildpacks/pack/pkg/image"
    22  	"github.com/buildpacks/pack/pkg/logging"
    23  	h "github.com/buildpacks/pack/testhelpers"
    24  )
    25  
    26  const extensionOutputSection = `Extension:
    27    ID                           NAME        VERSION        HOMEPAGE
    28    some/single-extension        some        0.0.1          single-extension-homepage`
    29  
    30  const inspectExtensionOutputTemplate = `Inspecting extension: '%s'
    31  
    32  %s
    33  
    34  %s
    35  `
    36  
    37  func TestExtensionInspectCommand(t *testing.T) {
    38  	color.Disable(true)
    39  	defer color.Disable(false)
    40  	spec.Run(t, "ExtensionInspectCommand", testExtensionInspectCommand, spec.Sequential(), spec.Report(report.Terminal{}))
    41  }
    42  
    43  func testExtensionInspectCommand(t *testing.T, when spec.G, it spec.S) {
    44  	var (
    45  		command        *cobra.Command
    46  		logger         logging.Logger
    47  		outBuf         bytes.Buffer
    48  		mockController *gomock.Controller
    49  		mockClient     *testmocks.MockPackClient
    50  		cfg            config.Config
    51  		info           *client.ExtensionInfo
    52  		assert         = h.NewAssertionManager(t)
    53  	)
    54  
    55  	it.Before(func() {
    56  		mockController = gomock.NewController(t)
    57  		mockClient = testmocks.NewMockPackClient(mockController)
    58  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    59  
    60  		info = &client.ExtensionInfo{
    61  			Extension: dist.ModuleInfo{
    62  				ID:       "some/single-extension",
    63  				Version:  "0.0.1",
    64  				Name:     "some",
    65  				Homepage: "single-extension-homepage",
    66  			},
    67  		}
    68  
    69  		command = commands.ExtensionInspect(logger, cfg, mockClient)
    70  	})
    71  
    72  	when("ExtensionInspect", func() {
    73  		when("inspecting an image", func() {
    74  			when("both remote and local image are present", func() {
    75  				it.Before(func() {
    76  					info.Location = buildpack.PackageLocator
    77  
    78  					mockClient.EXPECT().InspectExtension(client.InspectExtensionOptions{
    79  						ExtensionName: "test/extension",
    80  						Daemon:        true,
    81  					}).Return(info, nil)
    82  
    83  					mockClient.EXPECT().InspectExtension(client.InspectExtensionOptions{
    84  						ExtensionName: "test/extension",
    85  						Daemon:        false,
    86  					}).Return(info, nil)
    87  				})
    88  
    89  				it("succeeds", func() {
    90  					command.SetArgs([]string{"test/extension"})
    91  					assert.Nil(command.Execute())
    92  
    93  					localOutputSection := fmt.Sprintf(inspectExtensionOutputTemplate,
    94  						"test/extension",
    95  						"LOCAL IMAGE:",
    96  						extensionOutputSection)
    97  
    98  					remoteOutputSection := fmt.Sprintf("%s\n\n%s",
    99  						"REMOTE IMAGE:",
   100  						extensionOutputSection)
   101  
   102  					assert.AssertTrimmedContains(outBuf.String(), localOutputSection)
   103  					assert.AssertTrimmedContains(outBuf.String(), remoteOutputSection)
   104  				})
   105  			})
   106  
   107  			when("only a local image is present", func() {
   108  				it.Before(func() {
   109  					info.Location = buildpack.PackageLocator
   110  
   111  					mockClient.EXPECT().InspectExtension(client.InspectExtensionOptions{
   112  						ExtensionName: "only-local-test/extension",
   113  						Daemon:        true,
   114  					}).Return(info, nil)
   115  
   116  					mockClient.EXPECT().InspectExtension(client.InspectExtensionOptions{
   117  						ExtensionName: "only-local-test/extension",
   118  						Daemon:        false,
   119  					}).Return(nil, errors.Wrap(image.ErrNotFound, "remote image not found!"))
   120  				})
   121  
   122  				it("displays output for local image", func() {
   123  					command.SetArgs([]string{"only-local-test/extension"})
   124  					assert.Nil(command.Execute())
   125  
   126  					expectedOutput := fmt.Sprintf(inspectExtensionOutputTemplate,
   127  						"only-local-test/extension",
   128  						"LOCAL IMAGE:",
   129  						extensionOutputSection)
   130  
   131  					assert.AssertTrimmedContains(outBuf.String(), expectedOutput)
   132  				})
   133  			})
   134  
   135  			when("only a remote image is present", func() {
   136  				it.Before(func() {
   137  					info.Location = buildpack.PackageLocator
   138  
   139  					mockClient.EXPECT().InspectExtension(client.InspectExtensionOptions{
   140  						ExtensionName: "only-remote-test/extension",
   141  						Daemon:        false,
   142  					}).Return(info, nil)
   143  
   144  					mockClient.EXPECT().InspectExtension(client.InspectExtensionOptions{
   145  						ExtensionName: "only-remote-test/extension",
   146  						Daemon:        true,
   147  					}).Return(nil, errors.Wrap(image.ErrNotFound, "local image not found!"))
   148  				})
   149  
   150  				it("displays output for remote image", func() {
   151  					command.SetArgs([]string{"only-remote-test/extension"})
   152  					assert.Nil(command.Execute())
   153  
   154  					expectedOutput := fmt.Sprintf(inspectExtensionOutputTemplate,
   155  						"only-remote-test/extension",
   156  						"REMOTE IMAGE:",
   157  						extensionOutputSection)
   158  
   159  					assert.AssertTrimmedContains(outBuf.String(), expectedOutput)
   160  				})
   161  			})
   162  		})
   163  	})
   164  
   165  	when("failure cases", func() {
   166  		when("unable to inspect extension image", func() {
   167  			it.Before(func() {
   168  				mockClient.EXPECT().InspectExtension(client.InspectExtensionOptions{
   169  					ExtensionName: "failure-case/extension",
   170  					Daemon:        true,
   171  				}).Return(&client.ExtensionInfo{}, errors.Wrap(image.ErrNotFound, "unable to inspect local failure-case/extension"))
   172  
   173  				mockClient.EXPECT().InspectExtension(client.InspectExtensionOptions{
   174  					ExtensionName: "failure-case/extension",
   175  					Daemon:        false,
   176  				}).Return(&client.ExtensionInfo{}, errors.Wrap(image.ErrNotFound, "unable to inspect remote failure-case/extension"))
   177  			})
   178  
   179  			it("errors", func() {
   180  				command.SetArgs([]string{"failure-case/extension"})
   181  				err := command.Execute()
   182  				assert.Error(err)
   183  			})
   184  		})
   185  	})
   186  }