github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/command/v3/terminate_task_command_test.go (about) 1 package v3_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/sharedaction" 7 "code.cloudfoundry.org/cli/actor/v3action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller" 9 "code.cloudfoundry.org/cli/command" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 "code.cloudfoundry.org/cli/command/v3" 12 "code.cloudfoundry.org/cli/command/v3/v3fakes" 13 "code.cloudfoundry.org/cli/util/configv3" 14 "code.cloudfoundry.org/cli/util/ui" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("terminate-task Command", func() { 21 var ( 22 cmd v3.TerminateTaskCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v3fakes.FakeTerminateTaskActor 27 binaryName string 28 executeErr error 29 ) 30 31 BeforeEach(func() { 32 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 33 fakeConfig = new(commandfakes.FakeConfig) 34 fakeSharedActor = new(commandfakes.FakeSharedActor) 35 fakeActor = new(v3fakes.FakeTerminateTaskActor) 36 37 cmd = v3.TerminateTaskCommand{ 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 Actor: fakeActor, 42 } 43 44 cmd.RequiredArgs.AppName = "some-app-name" 45 cmd.RequiredArgs.SequenceID = "1" 46 47 binaryName = "faceman" 48 fakeConfig.BinaryNameReturns(binaryName) 49 fakeActor.CloudControllerAPIVersionReturns("3.0.0") 50 }) 51 52 JustBeforeEach(func() { 53 executeErr = cmd.Execute(nil) 54 }) 55 56 Context("when the API version is below the minimum", func() { 57 BeforeEach(func() { 58 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 59 }) 60 61 It("returns a MinimumAPIVersionNotMetError", func() { 62 Expect(executeErr).To(MatchError(command.MinimumAPIVersionNotMetError{ 63 CurrentVersion: "0.0.0", 64 MinimumVersion: "3.0.0", 65 })) 66 }) 67 }) 68 69 Context("when the task id argument is not an integer", func() { 70 BeforeEach(func() { 71 cmd.RequiredArgs.SequenceID = "not-an-integer" 72 }) 73 74 It("returns an ParseArgumentError", func() { 75 Expect(executeErr).To(MatchError(command.ParseArgumentError{ 76 ArgumentName: "TASK_ID", 77 ExpectedType: "integer", 78 })) 79 }) 80 }) 81 82 Context("when checking target fails", func() { 83 BeforeEach(func() { 84 fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName}) 85 }) 86 87 It("returns an error", func() { 88 Expect(executeErr).To(MatchError(command.NotLoggedInError{BinaryName: binaryName})) 89 90 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 91 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 92 Expect(checkTargetedOrg).To(BeTrue()) 93 Expect(checkTargetedSpace).To(BeTrue()) 94 }) 95 }) 96 97 Context("when the user is logged in, and a space and org are targeted", func() { 98 BeforeEach(func() { 99 fakeConfig.HasTargetedOrganizationReturns(true) 100 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 101 GUID: "some-org-guid", 102 Name: "some-org", 103 }) 104 fakeConfig.HasTargetedSpaceReturns(true) 105 fakeConfig.TargetedSpaceReturns(configv3.Space{ 106 GUID: "some-space-guid", 107 Name: "some-space", 108 }) 109 }) 110 111 Context("when getting the current user returns an error", func() { 112 var expectedErr error 113 114 BeforeEach(func() { 115 expectedErr = errors.New("get current user error") 116 fakeConfig.CurrentUserReturns( 117 configv3.User{}, 118 expectedErr) 119 }) 120 121 It("returns the error", func() { 122 Expect(executeErr).To(MatchError(expectedErr)) 123 }) 124 }) 125 126 Context("when getting the current user does not return an error", func() { 127 BeforeEach(func() { 128 fakeConfig.CurrentUserReturns( 129 configv3.User{Name: "some-user"}, 130 nil) 131 }) 132 133 Context("when provided a valid application name and task sequence ID", func() { 134 BeforeEach(func() { 135 fakeActor.GetApplicationByNameAndSpaceReturns( 136 v3action.Application{GUID: "some-app-guid"}, 137 v3action.Warnings{"get-application-warning"}, 138 nil) 139 fakeActor.GetTaskBySequenceIDAndApplicationReturns( 140 v3action.Task{GUID: "some-task-guid"}, 141 v3action.Warnings{"get-task-warning"}, 142 nil) 143 fakeActor.TerminateTaskReturns( 144 v3action.Task{}, 145 v3action.Warnings{"terminate-task-warning"}, 146 nil) 147 }) 148 149 It("cancels the task and displays all warnings", func() { 150 Expect(executeErr).ToNot(HaveOccurred()) 151 152 Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1)) 153 appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0) 154 Expect(appName).To(Equal("some-app-name")) 155 Expect(spaceGUID).To(Equal("some-space-guid")) 156 157 Expect(fakeActor.GetTaskBySequenceIDAndApplicationCallCount()).To(Equal(1)) 158 sequenceID, applicationGUID := fakeActor.GetTaskBySequenceIDAndApplicationArgsForCall(0) 159 Expect(sequenceID).To(Equal(1)) 160 Expect(applicationGUID).To(Equal("some-app-guid")) 161 162 Expect(fakeActor.TerminateTaskCallCount()).To(Equal(1)) 163 taskGUID := fakeActor.TerminateTaskArgsForCall(0) 164 Expect(taskGUID).To(Equal("some-task-guid")) 165 166 Expect(testUI.Err).To(Say("get-application-warning")) 167 Expect(testUI.Err).To(Say("get-task-warning")) 168 Expect(testUI.Out).To(Say("Terminating task 1 of app some-app-name in org some-org / space some-space as some-user...")) 169 Expect(testUI.Err).To(Say("terminate-task-warning")) 170 Expect(testUI.Out).To(Say("OK")) 171 }) 172 }) 173 174 Context("when there are errors", func() { 175 Context("when the error is translatable", func() { 176 var ( 177 returnedErr error 178 expectedErr error 179 ) 180 181 BeforeEach(func() { 182 expectedErr = errors.New("request-error") 183 returnedErr = cloudcontroller.RequestError{Err: expectedErr} 184 }) 185 186 Context("when getting the app returns the error", func() { 187 BeforeEach(func() { 188 fakeActor.GetApplicationByNameAndSpaceReturns( 189 v3action.Application{GUID: "some-app-guid"}, 190 nil, 191 returnedErr) 192 }) 193 194 It("returns a translatable error", func() { 195 Expect(executeErr).To(MatchError(command.APIRequestError{Err: expectedErr})) 196 }) 197 }) 198 199 Context("when getting the task returns the error", func() { 200 BeforeEach(func() { 201 fakeActor.GetApplicationByNameAndSpaceReturns( 202 v3action.Application{GUID: "some-app-guid"}, 203 nil, 204 nil) 205 fakeActor.GetTaskBySequenceIDAndApplicationReturns( 206 v3action.Task{}, 207 nil, 208 returnedErr) 209 }) 210 211 It("returns a translatable error", func() { 212 Expect(executeErr).To(MatchError(command.APIRequestError{Err: expectedErr})) 213 }) 214 }) 215 216 Context("when terminating the task returns the error", func() { 217 BeforeEach(func() { 218 fakeActor.GetApplicationByNameAndSpaceReturns( 219 v3action.Application{GUID: "some-app-guid"}, 220 nil, 221 nil) 222 fakeActor.GetTaskBySequenceIDAndApplicationReturns( 223 v3action.Task{GUID: "some-task-guid"}, 224 nil, 225 nil) 226 fakeActor.TerminateTaskReturns( 227 v3action.Task{GUID: "some-task-guid"}, 228 nil, 229 returnedErr) 230 }) 231 232 It("returns a translatable error", func() { 233 Expect(executeErr).To(MatchError(command.APIRequestError{Err: expectedErr})) 234 }) 235 }) 236 }) 237 238 Context("when the error is not translatable", func() { 239 var expectedErr error 240 241 BeforeEach(func() { 242 expectedErr = errors.New("bananapants") 243 }) 244 245 Context("when getting the app returns the error", func() { 246 BeforeEach(func() { 247 fakeActor.GetApplicationByNameAndSpaceReturns( 248 v3action.Application{GUID: "some-app-guid"}, 249 v3action.Warnings{"get-application-warning-1", "get-application-warning-2"}, 250 expectedErr) 251 }) 252 253 It("return the error and outputs all warnings", func() { 254 Expect(executeErr).To(MatchError(expectedErr)) 255 256 Expect(testUI.Err).To(Say("get-application-warning-1")) 257 Expect(testUI.Err).To(Say("get-application-warning-2")) 258 }) 259 }) 260 261 Context("when getting the task returns the error", func() { 262 BeforeEach(func() { 263 fakeActor.GetApplicationByNameAndSpaceReturns( 264 v3action.Application{GUID: "some-app-guid"}, 265 nil, 266 nil) 267 fakeActor.GetTaskBySequenceIDAndApplicationReturns( 268 v3action.Task{}, 269 v3action.Warnings{"get-task-warning-1", "get-task-warning-2"}, 270 expectedErr) 271 }) 272 273 It("return the error and outputs all warnings", func() { 274 Expect(executeErr).To(MatchError(expectedErr)) 275 276 Expect(testUI.Err).To(Say("get-task-warning-1")) 277 Expect(testUI.Err).To(Say("get-task-warning-2")) 278 }) 279 }) 280 281 Context("when terminating the task returns the error", func() { 282 BeforeEach(func() { 283 fakeActor.GetApplicationByNameAndSpaceReturns( 284 v3action.Application{GUID: "some-app-guid"}, 285 nil, 286 nil) 287 fakeActor.GetTaskBySequenceIDAndApplicationReturns( 288 v3action.Task{GUID: "some-task-guid"}, 289 nil, 290 nil) 291 fakeActor.TerminateTaskReturns( 292 v3action.Task{}, 293 v3action.Warnings{"terminate-task-warning-1", "terminate-task-warning-2"}, 294 expectedErr) 295 }) 296 297 It("returns the error and outputs all warnings", func() { 298 Expect(executeErr).To(MatchError(expectedErr)) 299 300 Expect(testUI.Err).To(Say("terminate-task-warning-1")) 301 Expect(testUI.Err).To(Say("terminate-task-warning-2")) 302 }) 303 }) 304 }) 305 }) 306 }) 307 }) 308 })