github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v6/create_buildpack_command_test.go (about) 1 package v6_test 2 3 import ( 4 "errors" 5 6 . "github.com/onsi/ginkgo" 7 . "github.com/onsi/gomega" 8 . "github.com/onsi/gomega/gbytes" 9 10 "code.cloudfoundry.org/cli/actor/actionerror" 11 "code.cloudfoundry.org/cli/actor/v2action" 12 "code.cloudfoundry.org/cli/command/commandfakes" 13 "code.cloudfoundry.org/cli/command/translatableerror" 14 . "code.cloudfoundry.org/cli/command/v6" 15 "code.cloudfoundry.org/cli/command/v6/v6fakes" 16 "code.cloudfoundry.org/cli/util/configv3" 17 "code.cloudfoundry.org/cli/util/ui" 18 ) 19 20 var _ = Describe("CreateBuildpackCommand", func() { 21 var ( 22 cmd CreateBuildpackCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v6fakes.FakeCreateBuildpackActor 27 input *Buffer 28 binaryName string 29 30 executeErr error 31 ) 32 33 BeforeEach(func() { 34 input = NewBuffer() 35 testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v6fakes.FakeCreateBuildpackActor) 39 40 cmd = CreateBuildpackCommand{ 41 UI: testUI, 42 Config: fakeConfig, 43 SharedActor: fakeSharedActor, 44 Actor: fakeActor, 45 } 46 47 cmd.RequiredArgs.Buildpack = "bp-name" 48 cmd.RequiredArgs.Position = 3 49 50 binaryName = "faceman" 51 fakeConfig.BinaryNameReturns(binaryName) 52 }) 53 54 JustBeforeEach(func() { 55 executeErr = cmd.Execute(nil) 56 }) 57 58 When("an error is encountered checking if the environment is setup correctly", func() { 59 BeforeEach(func() { 60 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 61 }) 62 63 It("returns an error", func() { 64 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 65 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 66 checkTargetedOrgArg, checkTargetedSpaceArg := fakeSharedActor.CheckTargetArgsForCall(0) 67 Expect(checkTargetedOrgArg).To(BeFalse()) 68 Expect(checkTargetedSpaceArg).To(BeFalse()) 69 }) 70 }) 71 72 When("the user is logged in", func() { 73 When("getting the current user fails", func() { 74 var expectedErr error 75 76 BeforeEach(func() { 77 expectedErr = errors.New("some-error that happened") 78 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 79 }) 80 81 It("returns the error", func() { 82 Expect(executeErr).To(MatchError(expectedErr)) 83 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 84 }) 85 }) 86 87 When("getting the current user succeeds", func() { 88 var fakeUser configv3.User 89 90 BeforeEach(func() { 91 fakeUser = configv3.User{Name: "some-user"} 92 fakeConfig.CurrentUserReturns(fakeUser, nil) 93 }) 94 95 When("creating the buildpack fails because a buildpack already exists", func() { 96 BeforeEach(func() { 97 fakeActor.CreateBuildpackReturns(v2action.Buildpack{}, v2action.Warnings{"some-create-bp-warning"}, actionerror.BuildpackNameTakenError{Name: "bp-name"}) 98 }) 99 100 It("prints the error message as a warning but does not return it", func() { 101 Expect(executeErr).ToNot(HaveOccurred()) 102 Expect(testUI.Err).To(Say("some-create-bp-warning")) 103 Expect(testUI.Err).To(Say("Buildpack bp-name already exists")) 104 Expect(testUI.Out).To(Say("TIP: use 'faceman update-buildpack' to update this buildpack")) 105 }) 106 }) 107 108 When("creating the buildpack fails because a buildpack with the nil stack already exists", func() { 109 BeforeEach(func() { 110 fakeActor.CreateBuildpackReturns(v2action.Buildpack{}, v2action.Warnings{"some-create-bp-warning"}, actionerror.BuildpackAlreadyExistsWithoutStackError{BuildpackName: "bp-name"}) 111 cmd.RequiredArgs.Buildpack = "bp-name" 112 }) 113 114 It("prints the error message as a warning but does not return it", func() { 115 Expect(executeErr).ToNot(HaveOccurred()) 116 Expect(testUI.Err).To(Say("some-create-bp-warning")) 117 Expect(testUI.Err).To(Say("Buildpack bp-name already exists without a stack")) 118 Expect(testUI.Out).To(Say("TIP: use 'faceman buildpacks' and 'faceman delete-buildpack' to delete buildpack bp-name without a stack")) 119 }) 120 }) 121 122 When("the path specified is an empty directory", func() { 123 var emptyDirectoryError error 124 BeforeEach(func() { 125 emptyDirectoryError = actionerror.EmptyBuildpackDirectoryError{Path: "some-directory"} 126 fakeActor.PrepareBuildpackBitsReturns("", emptyDirectoryError) 127 cmd.RequiredArgs.Path = "some empty directory" 128 }) 129 130 It("exits without updating if the path points to an empty directory", func() { 131 Expect(executeErr).To(MatchError(emptyDirectoryError)) 132 Expect(fakeActor.CreateBuildpackCallCount()).To(Equal(0)) 133 }) 134 }) 135 136 When("creating the buildpack fails with a generic error", func() { 137 BeforeEach(func() { 138 fakeActor.CreateBuildpackReturns(v2action.Buildpack{}, v2action.Warnings{"some-create-bp-warning"}, errors.New("some-create-bp-error")) 139 }) 140 141 It("returns an error and warnings", func() { 142 Expect(executeErr).To(MatchError("some-create-bp-error")) 143 Expect(testUI.Err).To(Say("some-create-bp-warning")) 144 }) 145 }) 146 147 When("creating the buildpack succeeds", func() { 148 BeforeEach(func() { 149 fakeActor.CreateBuildpackReturns(v2action.Buildpack{GUID: "some-guid"}, v2action.Warnings{"some-create-bp-warning"}, nil) 150 BeforeEach(func() { 151 cmd.RequiredArgs.Path = "some-path/to/buildpack.zip" 152 }) 153 154 It("displays that the buildpack was created successfully", func() { 155 Expect(executeErr).ToNot(HaveOccurred()) 156 Expect(testUI.Out).To(Say("OK")) 157 158 Expect(fakeActor.CreateBuildpackCallCount()).To(Equal(1)) 159 bpName, bpPosition, enabled := fakeActor.CreateBuildpackArgsForCall(0) 160 Expect(bpName).To(Equal("bp-name")) 161 Expect(bpPosition).To(Equal(3)) 162 Expect(enabled).To(Equal(true)) 163 }) 164 165 When("preparing the buildpack bits fails", func() { 166 BeforeEach(func() { 167 fakeActor.PrepareBuildpackBitsReturns("some/invalid/path", errors.New("some-prepare-bp-error")) 168 }) 169 170 It("returns an error", func() { 171 Expect(executeErr).To(MatchError("some-prepare-bp-error")) 172 Expect(fakeActor.PrepareBuildpackBitsCallCount()).To(Equal(1)) 173 }) 174 }) 175 176 When("preparing the buildpack bits succeeds", func() { 177 BeforeEach(func() { 178 fakeActor.PrepareBuildpackBitsReturns("buildpack.zip", nil) 179 }) 180 181 It("displays that upload is starting", func() { 182 Expect(executeErr).ToNot(HaveOccurred()) 183 Expect(testUI.Out).To(Say("Uploading buildpack bp-name as some-user")) 184 185 Expect(fakeActor.PrepareBuildpackBitsCallCount()).To(Equal(1)) 186 path, _, _ := fakeActor.PrepareBuildpackBitsArgsForCall(0) 187 Expect(path).To(Equal("some-path/to/buildpack.zip")) 188 }) 189 190 PWhen("uploading the buildpack fails because a buildpack with that stack already exists", func() { 191 BeforeEach(func() { 192 fakeActor.UploadBuildpackReturns(v2action.Warnings{"some-upload-bp-warning"}, actionerror.BuildpackAlreadyExistsForStackError{Message: "The buildpack name bp-name is already in use with stack stack-name"}) 193 }) 194 195 It("prints the error message as a warning but does not return it", func() { 196 Expect(executeErr).ToNot(HaveOccurred()) 197 Expect(testUI.Err).To(Say("some-upload-bp-warning")) 198 Expect(testUI.Err).To(Say("The buildpack name bp-name is already in use with stack stack-name")) 199 Expect(testUI.Out).To(Say("TIP: use 'faceman update-buildpack' to update this buildpack")) 200 }) 201 }) 202 203 When("uploading the buildpack fails with a generic error", func() { 204 BeforeEach(func() { 205 fakeActor.UploadBuildpackReturns(v2action.Warnings{"some-upload-bp-warning"}, errors.New("some-upload-bp-error")) 206 }) 207 208 It("returns an error and warnings", func() { 209 Expect(executeErr).To(MatchError("some-upload-bp-error")) 210 Expect(testUI.Err).To(Say("some-create-bp-warning")) 211 Expect(testUI.Err).To(Say("some-upload-bp-warning")) 212 }) 213 214 }) 215 216 When("uploading the buildpack succeeds", func() { 217 BeforeEach(func() { 218 fakeActor.UploadBuildpackReturns(v2action.Warnings{"some-upload-bp-warning"}, nil) 219 }) 220 221 It("displays that the buildpack was uploaded successfully", func() { 222 Expect(executeErr).ToNot(HaveOccurred()) 223 Expect(testUI.Out).To(Say("Done uploading")) 224 Expect(testUI.Out).To(Say("OK")) 225 Expect(testUI.Err).To(Say("some-upload-bp-warning")) 226 227 Expect(fakeActor.UploadBuildpackCallCount()).To(Equal(1)) 228 guid, path, _ := fakeActor.UploadBuildpackArgsForCall(0) 229 Expect(guid).To(Equal("some-guid")) 230 Expect(path).To(Equal("buildpack.zip")) 231 }) 232 }) 233 }) 234 }) 235 }) 236 237 When("both --enable and --disable are provided", func() { 238 BeforeEach(func() { 239 cmd.Enable = true 240 cmd.Disable = true 241 }) 242 243 It("returns an argument combination error", func() { 244 argumentCombinationError := translatableerror.ArgumentCombinationError{ 245 Args: []string{"--enable", "--disable"}, 246 } 247 Expect(executeErr).To(MatchError(argumentCombinationError)) 248 }) 249 }) 250 251 When("--enable is provided", func() { 252 BeforeEach(func() { 253 cmd.Enable = true 254 fakeActor.CreateBuildpackReturns(v2action.Buildpack{GUID: "some-guid"}, v2action.Warnings{"some-create-bp-warning"}, nil) 255 }) 256 257 It("successfully creates a buildpack with enabled set to true", func() { 258 Expect(executeErr).ToNot(HaveOccurred()) 259 Expect(testUI.Out).To(Say("OK")) 260 Expect(testUI.Out).To(Say("Uploading buildpack bp-name as some-user")) 261 Expect(testUI.Out).To(Say("Done uploading")) 262 Expect(testUI.Out).To(Say("OK")) 263 264 Expect(fakeActor.CreateBuildpackCallCount()).To(Equal(1)) 265 _, _, enabled := fakeActor.CreateBuildpackArgsForCall(0) 266 Expect(enabled).To(BeTrue()) 267 }) 268 }) 269 270 When("--disable is provided", func() { 271 BeforeEach(func() { 272 cmd.Disable = true 273 fakeActor.CreateBuildpackReturns(v2action.Buildpack{GUID: "some-guid"}, v2action.Warnings{"some-create-bp-warning"}, nil) 274 }) 275 276 It("successfully creates a buildpack with enabled set to false", func() { 277 Expect(executeErr).ToNot(HaveOccurred()) 278 Expect(testUI.Out).To(Say("OK")) 279 Expect(testUI.Out).To(Say("Uploading buildpack bp-name as some-user")) 280 Expect(testUI.Out).To(Say("Done uploading")) 281 Expect(testUI.Out).To(Say("OK")) 282 283 Expect(fakeActor.CreateBuildpackCallCount()).To(Equal(1)) 284 _, _, enabled := fakeActor.CreateBuildpackArgsForCall(0) 285 Expect(enabled).To(BeFalse()) 286 }) 287 }) 288 289 }) 290 }) 291 })