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