github.com/loafoe/cli@v7.1.0+incompatible/command/v7/run_task_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 "code.cloudfoundry.org/cli/command/flag" 12 . "code.cloudfoundry.org/cli/command/v7" 13 "code.cloudfoundry.org/cli/command/v7/v7fakes" 14 "code.cloudfoundry.org/cli/resources" 15 "code.cloudfoundry.org/cli/types" 16 "code.cloudfoundry.org/cli/util/configv3" 17 "code.cloudfoundry.org/cli/util/ui" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 . "github.com/onsi/gomega/gbytes" 21 ) 22 23 var _ = Describe("run-task Command", func() { 24 var ( 25 cmd RunTaskCommand 26 testUI *ui.UI 27 fakeConfig *commandfakes.FakeConfig 28 fakeSharedActor *commandfakes.FakeSharedActor 29 fakeActor *v7fakes.FakeActor 30 binaryName string 31 executeErr error 32 ) 33 34 BeforeEach(func() { 35 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v7fakes.FakeActor) 39 40 cmd = RunTaskCommand{ 41 BaseCommand: BaseCommand{ 42 UI: testUI, 43 Config: fakeConfig, 44 SharedActor: fakeSharedActor, 45 Actor: fakeActor, 46 }, 47 } 48 49 cmd.RequiredArgs.AppName = "some-app-name" 50 cmd.Command = "some command" 51 52 binaryName = "faceman" 53 fakeConfig.BinaryNameReturns(binaryName) 54 }) 55 56 JustBeforeEach(func() { 57 executeErr = cmd.Execute(nil) 58 }) 59 60 When("checking target fails", func() { 61 BeforeEach(func() { 62 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 63 }) 64 65 It("returns an error", func() { 66 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 67 68 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 69 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 70 Expect(checkTargetedOrg).To(BeTrue()) 71 Expect(checkTargetedSpace).To(BeTrue()) 72 }) 73 }) 74 75 When("the user is logged in, and a space and org are targeted", func() { 76 BeforeEach(func() { 77 fakeConfig.HasTargetedOrganizationReturns(true) 78 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 79 GUID: "some-org-guid", 80 Name: "some-org", 81 }) 82 fakeConfig.HasTargetedSpaceReturns(true) 83 fakeConfig.TargetedSpaceReturns(configv3.Space{ 84 GUID: "some-space-guid", 85 Name: "some-space", 86 }) 87 }) 88 89 When("getting the current user returns an error", func() { 90 var expectedErr error 91 92 BeforeEach(func() { 93 expectedErr = errors.New("got bananapants??") 94 fakeConfig.CurrentUserReturns( 95 configv3.User{}, 96 expectedErr) 97 }) 98 99 It("returns the error", func() { 100 Expect(executeErr).To(MatchError(expectedErr)) 101 }) 102 }) 103 104 When("getting the current user does not return an error", func() { 105 BeforeEach(func() { 106 fakeConfig.CurrentUserReturns( 107 configv3.User{Name: "some-user"}, 108 nil) 109 }) 110 111 When("provided a valid application name", func() { 112 BeforeEach(func() { 113 fakeActor.GetApplicationByNameAndSpaceReturns( 114 resources.Application{GUID: "some-app-guid"}, 115 v7action.Warnings{"get-application-warning-1", "get-application-warning-2"}, 116 nil) 117 }) 118 119 When("the task name is not provided", func() { 120 BeforeEach(func() { 121 fakeActor.RunTaskReturns( 122 v7action.Task{ 123 Name: "31337ddd", 124 SequenceID: 3, 125 }, 126 v7action.Warnings{"get-application-warning-3"}, 127 nil) 128 }) 129 130 It("creates a new task and displays all warnings", func() { 131 Expect(executeErr).ToNot(HaveOccurred()) 132 133 Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1)) 134 appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0) 135 Expect(appName).To(Equal("some-app-name")) 136 Expect(spaceGUID).To(Equal("some-space-guid")) 137 138 Expect(fakeActor.RunTaskCallCount()).To(Equal(1)) 139 appGUID, task := fakeActor.RunTaskArgsForCall(0) 140 Expect(appGUID).To(Equal("some-app-guid")) 141 Expect(task).To(Equal(v7action.Task{Command: "some command"})) 142 143 Expect(testUI.Out).To(Say("Creating task for app some-app-name in org some-org / space some-space as some-user...")) 144 Expect(testUI.Out).To(Say("Task has been submitted successfully for execution.")) 145 Expect(testUI.Out).To(Say("OK")) 146 147 Expect(testUI.Out).To(Say(`task name:\s+31337ddd`)) 148 Expect(testUI.Out).To(Say(`task id:\s+3`)) 149 150 Expect(testUI.Err).To(Say("get-application-warning-1")) 151 Expect(testUI.Err).To(Say("get-application-warning-2")) 152 Expect(testUI.Err).To(Say("get-application-warning-3")) 153 }) 154 }) 155 156 When("task disk space is provided", func() { 157 BeforeEach(func() { 158 cmd.Name = "some-task-name" 159 cmd.Disk = flag.Megabytes{NullUint64: types.NullUint64{Value: 321, IsSet: true}} 160 cmd.Memory = flag.Megabytes{NullUint64: types.NullUint64{Value: 123, IsSet: true}} 161 fakeActor.RunTaskReturns( 162 v7action.Task{ 163 Name: "some-task-name", 164 SequenceID: 3, 165 }, 166 v7action.Warnings{"get-application-warning-3"}, 167 nil) 168 }) 169 170 It("creates a new task and outputs all warnings", func() { 171 Expect(executeErr).ToNot(HaveOccurred()) 172 173 Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1)) 174 appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0) 175 Expect(appName).To(Equal("some-app-name")) 176 Expect(spaceGUID).To(Equal("some-space-guid")) 177 178 Expect(fakeActor.RunTaskCallCount()).To(Equal(1)) 179 appGUID, task := fakeActor.RunTaskArgsForCall(0) 180 Expect(appGUID).To(Equal("some-app-guid")) 181 Expect(task).To(Equal(v7action.Task{ 182 Command: "some command", 183 Name: "some-task-name", 184 DiskInMB: 321, 185 MemoryInMB: 123, 186 })) 187 188 Expect(testUI.Out).To(Say("Creating task for app some-app-name in org some-org / space some-space as some-user...")) 189 Expect(testUI.Out).To(Say("Task has been submitted successfully for execution.")) 190 Expect(testUI.Out).To(Say("OK")) 191 192 Expect(testUI.Out).To(Say(`task name:\s+some-task-name`)) 193 Expect(testUI.Out).To(Say(`task id:\s+3`)) 194 195 Expect(testUI.Err).To(Say("get-application-warning-1")) 196 Expect(testUI.Err).To(Say("get-application-warning-2")) 197 Expect(testUI.Err).To(Say("get-application-warning-3")) 198 }) 199 }) 200 201 When("process is provided", func() { 202 BeforeEach(func() { 203 cmd.Name = "some-task-name" 204 cmd.Process = "process-template-name" 205 cmd.Command = "" 206 fakeActor.RunTaskReturns( 207 v7action.Task{ 208 Name: "some-task-name", 209 SequenceID: 3, 210 }, 211 v7action.Warnings{"get-application-warning-3"}, 212 nil) 213 fakeActor.GetProcessByTypeAndApplicationReturns( 214 v7action.Process{ 215 GUID: "some-process-guid", 216 }, 217 v7action.Warnings{"get-process-warning"}, 218 nil) 219 }) 220 221 It("creates a new task and outputs all warnings", func() { 222 Expect(executeErr).ToNot(HaveOccurred()) 223 224 Expect(fakeActor.RunTaskCallCount()).To(Equal(1)) 225 appGUID, task := fakeActor.RunTaskArgsForCall(0) 226 Expect(appGUID).To(Equal("some-app-guid")) 227 Expect(task).To(Equal(v7action.Task{ 228 Command: "", 229 Name: "some-task-name", 230 Template: &ccv3.TaskTemplate{ 231 Process: ccv3.TaskProcessTemplate{Guid: "some-process-guid"}, 232 }, 233 })) 234 235 Expect(testUI.Out).To(Say("Creating task for app some-app-name in org some-org / space some-space as some-user...")) 236 Expect(testUI.Out).To(Say("Task has been submitted successfully for execution.")) 237 Expect(testUI.Out).To(Say("OK")) 238 239 Expect(testUI.Out).To(Say(`task name:\s+some-task-name`)) 240 Expect(testUI.Out).To(Say(`task id:\s+3`)) 241 242 Expect(testUI.Err).To(Say("get-application-warning-1")) 243 Expect(testUI.Err).To(Say("get-application-warning-2")) 244 Expect(testUI.Err).To(Say("get-process-warning")) 245 Expect(testUI.Err).To(Say("get-application-warning-3")) 246 }) 247 }) 248 249 When("neither command nor process template are provided", func() { 250 BeforeEach(func() { 251 cmd.Name = "some-task-name" 252 cmd.Command = "" 253 fakeActor.RunTaskReturns( 254 v7action.Task{ 255 Name: "some-task-name", 256 SequenceID: 3, 257 }, 258 v7action.Warnings{"get-application-warning-3"}, 259 nil) 260 fakeActor.GetProcessByTypeAndApplicationReturns( 261 v7action.Process{ 262 GUID: "some-process-guid", 263 }, 264 v7action.Warnings{"get-process-warning"}, 265 nil) 266 }) 267 268 It("creates a new task using 'task' as the template process type and outputs all warnings", func() { 269 Expect(executeErr).ToNot(HaveOccurred()) 270 271 Expect(fakeActor.GetProcessByTypeAndApplicationCallCount()).To(Equal(1)) 272 processType, _ := fakeActor.GetProcessByTypeAndApplicationArgsForCall(0) 273 Expect(processType).To(Equal("task")) 274 275 Expect(fakeActor.RunTaskCallCount()).To(Equal(1)) 276 appGUID, task := fakeActor.RunTaskArgsForCall(0) 277 Expect(appGUID).To(Equal("some-app-guid")) 278 Expect(task).To(Equal(v7action.Task{ 279 Command: "", 280 Name: "some-task-name", 281 Template: &ccv3.TaskTemplate{ 282 Process: ccv3.TaskProcessTemplate{Guid: "some-process-guid"}, 283 }, 284 })) 285 286 Expect(testUI.Out).To(Say("Creating task for app some-app-name in org some-org / space some-space as some-user...")) 287 Expect(testUI.Out).To(Say("Task has been submitted successfully for execution.")) 288 Expect(testUI.Out).To(Say("OK")) 289 290 Expect(testUI.Out).To(Say(`task name:\s+some-task-name`)) 291 Expect(testUI.Out).To(Say(`task id:\s+3`)) 292 293 Expect(testUI.Err).To(Say("get-application-warning-1")) 294 Expect(testUI.Err).To(Say("get-application-warning-2")) 295 Expect(testUI.Err).To(Say("get-process-warning")) 296 Expect(testUI.Err).To(Say("get-application-warning-3")) 297 }) 298 }) 299 }) 300 301 When("there are errors", func() { 302 When("the error is translatable", func() { 303 When("getting the app returns the error", func() { 304 var ( 305 returnedErr error 306 expectedErr error 307 ) 308 309 BeforeEach(func() { 310 expectedErr = errors.New("request-error") 311 returnedErr = ccerror.RequestError{Err: expectedErr} 312 fakeActor.GetApplicationByNameAndSpaceReturns( 313 resources.Application{GUID: "some-app-guid"}, 314 nil, 315 returnedErr) 316 }) 317 318 It("returns a translatable error", func() { 319 Expect(executeErr).To(MatchError(ccerror.RequestError{Err: expectedErr})) 320 }) 321 }) 322 323 When("running the task returns the error", func() { 324 var returnedErr error 325 326 BeforeEach(func() { 327 returnedErr = ccerror.UnverifiedServerError{URL: "some-url"} 328 fakeActor.GetApplicationByNameAndSpaceReturns( 329 resources.Application{GUID: "some-app-guid"}, 330 nil, 331 nil) 332 fakeActor.RunTaskReturns( 333 v7action.Task{}, 334 nil, 335 returnedErr) 336 }) 337 338 It("returns a translatable error", func() { 339 Expect(executeErr).To(MatchError(returnedErr)) 340 }) 341 }) 342 }) 343 344 When("the error is not translatable", func() { 345 When("getting the app returns the error", func() { 346 var expectedErr error 347 348 BeforeEach(func() { 349 expectedErr = errors.New("got bananapants??") 350 fakeActor.GetApplicationByNameAndSpaceReturns( 351 resources.Application{GUID: "some-app-guid"}, 352 v7action.Warnings{"get-application-warning-1", "get-application-warning-2"}, 353 expectedErr) 354 }) 355 356 It("return the error and all warnings", func() { 357 Expect(executeErr).To(MatchError(expectedErr)) 358 359 Expect(testUI.Err).To(Say("get-application-warning-1")) 360 Expect(testUI.Err).To(Say("get-application-warning-2")) 361 }) 362 }) 363 364 When("running the task returns an error", func() { 365 var expectedErr error 366 367 BeforeEach(func() { 368 expectedErr = errors.New("got bananapants??") 369 fakeActor.GetApplicationByNameAndSpaceReturns( 370 resources.Application{GUID: "some-app-guid"}, 371 v7action.Warnings{"get-application-warning-1", "get-application-warning-2"}, 372 nil) 373 fakeActor.RunTaskReturns( 374 v7action.Task{}, 375 v7action.Warnings{"run-task-warning-1", "run-task-warning-2"}, 376 expectedErr) 377 }) 378 379 It("returns the error and all warnings", func() { 380 Expect(executeErr).To(MatchError(expectedErr)) 381 382 Expect(testUI.Err).To(Say("get-application-warning-1")) 383 Expect(testUI.Err).To(Say("get-application-warning-2")) 384 Expect(testUI.Err).To(Say("run-task-warning-1")) 385 Expect(testUI.Err).To(Say("run-task-warning-2")) 386 }) 387 }) 388 }) 389 }) 390 }) 391 }) 392 })