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