github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/set_default_builder_test.go (about) 1 package commands_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/golang/mock/gomock" 11 "github.com/heroku/color" 12 "github.com/sclevine/spec" 13 "github.com/sclevine/spec/report" 14 "github.com/spf13/cobra" 15 16 "github.com/buildpacks/pack/internal/commands" 17 "github.com/buildpacks/pack/internal/commands/testmocks" 18 "github.com/buildpacks/pack/internal/config" 19 "github.com/buildpacks/pack/pkg/client" 20 "github.com/buildpacks/pack/pkg/logging" 21 h "github.com/buildpacks/pack/testhelpers" 22 ) 23 24 func TestSetDefaultBuilderCommand(t *testing.T) { 25 color.Disable(true) 26 defer color.Disable(false) 27 spec.Run(t, "Commands", testSetDefaultBuilderCommand, spec.Random(), spec.Report(report.Terminal{})) 28 } 29 30 func testSetDefaultBuilderCommand(t *testing.T, when spec.G, it spec.S) { 31 var ( 32 command *cobra.Command 33 logger logging.Logger 34 outBuf bytes.Buffer 35 mockController *gomock.Controller 36 mockClient *testmocks.MockPackClient 37 tempPackHome string 38 ) 39 40 it.Before(func() { 41 mockController = gomock.NewController(t) 42 mockClient = testmocks.NewMockPackClient(mockController) 43 logger = logging.NewLogWithWriters(&outBuf, &outBuf) 44 45 var err error 46 tempPackHome, err = os.MkdirTemp("", "pack-home") 47 h.AssertNil(t, err) 48 command = commands.SetDefaultBuilder(logger, config.Config{}, filepath.Join(tempPackHome, "config.toml"), mockClient) 49 }) 50 51 it.After(func() { 52 mockController.Finish() 53 h.AssertNil(t, os.RemoveAll(tempPackHome)) 54 }) 55 56 when("#SetDefaultBuilder", func() { 57 when("no builder provided", func() { 58 it.Before(func() { 59 mockClient.EXPECT().InspectBuilder(gomock.Any(), false).Return(&client.BuilderInfo{}, nil).AnyTimes() 60 }) 61 62 it("display suggested builders", func() { 63 command.SetArgs([]string{}) 64 h.AssertNil(t, command.Execute()) 65 h.AssertContains(t, outBuf.String(), "Suggested builders:") 66 }) 67 }) 68 69 when("empty builder name is provided", func() { 70 it.Before(func() { 71 mockClient.EXPECT().InspectBuilder(gomock.Any(), false).Return(&client.BuilderInfo{}, nil).AnyTimes() 72 }) 73 74 it("display suggested builders", func() { 75 command.SetArgs([]string{}) 76 h.AssertNil(t, command.Execute()) 77 h.AssertContains(t, outBuf.String(), "Suggested builders:") 78 }) 79 }) 80 81 when("valid builder is provider", func() { 82 when("in local", func() { 83 it("sets default builder", func() { 84 imageName := "some/image" 85 mockClient.EXPECT().InspectBuilder(imageName, true).Return(&client.BuilderInfo{ 86 Stack: "test.stack.id", 87 }, nil) 88 89 command.SetArgs([]string{imageName}) 90 h.AssertNil(t, command.Execute()) 91 h.AssertContains(t, outBuf.String(), fmt.Sprintf("Builder '%s' is now the default builder", imageName)) 92 }) 93 }) 94 95 when("in remote", func() { 96 it("sets default builder", func() { 97 imageName := "some/image" 98 99 localCall := mockClient.EXPECT().InspectBuilder(imageName, true).Return(nil, nil) 100 101 mockClient.EXPECT().InspectBuilder(imageName, false).Return(&client.BuilderInfo{ 102 Stack: "test.stack.id", 103 }, nil).After(localCall) 104 105 command.SetArgs([]string{imageName}) 106 h.AssertNil(t, command.Execute()) 107 h.AssertContains(t, outBuf.String(), fmt.Sprintf("Builder '%s' is now the default builder", imageName)) 108 }) 109 }) 110 }) 111 112 when("invalid builder is provided", func() { 113 it("error is presented", func() { 114 imageName := "nonbuilder/image" 115 116 mockClient.EXPECT().InspectBuilder(imageName, true).Return( 117 nil, 118 fmt.Errorf("failed to inspect image %s", imageName)) 119 120 command.SetArgs([]string{imageName}) 121 122 h.AssertNotNil(t, command.Execute()) 123 h.AssertContains(t, outBuf.String(), "ERROR: failed to inspect image nonbuilder/image") 124 }) 125 }) 126 127 when("non-existent builder is provided", func() { 128 it("error is present", func() { 129 imageName := "nonexisting/image" 130 131 localCall := mockClient.EXPECT().InspectBuilder(imageName, true).Return( 132 nil, 133 nil) 134 135 mockClient.EXPECT().InspectBuilder(imageName, false).Return( 136 nil, 137 nil).After(localCall) 138 139 command.SetArgs([]string{imageName}) 140 141 h.AssertNotNil(t, command.Execute()) 142 h.AssertContains(t, outBuf.String(), "ERROR: builder 'nonexisting/image' not found") 143 }) 144 }) 145 }) 146 }