github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/tasks_command_test.go (about) 1 package v3_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v3action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 11 "code.cloudfoundry.org/cli/command/commandfakes" 12 "code.cloudfoundry.org/cli/command/translatableerror" 13 "code.cloudfoundry.org/cli/command/v3" 14 "code.cloudfoundry.org/cli/command/v3/v3fakes" 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("tasks Command", func() { 23 var ( 24 cmd v3.TasksCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v3fakes.FakeTasksActor 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(v3fakes.FakeTasksActor) 38 39 cmd = v3.TasksCommand{ 40 UI: testUI, 41 Config: fakeConfig, 42 SharedActor: fakeSharedActor, 43 Actor: fakeActor, 44 } 45 46 cmd.RequiredArgs.AppName = "some-app-name" 47 48 binaryName = "faceman" 49 fakeConfig.BinaryNameReturns(binaryName) 50 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionRunTaskV3) 51 }) 52 53 JustBeforeEach(func() { 54 executeErr = cmd.Execute(nil) 55 }) 56 57 Context("when the API version is below the minimum", func() { 58 BeforeEach(func() { 59 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 60 }) 61 62 It("returns a MinimumAPIVersionNotMetError", func() { 63 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 64 CurrentVersion: "0.0.0", 65 MinimumVersion: ccversion.MinVersionRunTaskV3, 66 })) 67 }) 68 }) 69 70 Context("when checking target fails", func() { 71 BeforeEach(func() { 72 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 73 }) 74 75 It("returns an error", func() { 76 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 77 78 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 79 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 80 Expect(checkTargetedOrg).To(BeTrue()) 81 Expect(checkTargetedSpace).To(BeTrue()) 82 }) 83 }) 84 85 Context("when the user is logged in, and a space and org are targeted", func() { 86 BeforeEach(func() { 87 fakeConfig.HasTargetedOrganizationReturns(true) 88 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 89 GUID: "some-org-guid", 90 Name: "some-org", 91 }) 92 fakeConfig.HasTargetedSpaceReturns(true) 93 fakeConfig.TargetedSpaceReturns(configv3.Space{ 94 GUID: "some-space-guid", 95 Name: "some-space", 96 }) 97 }) 98 99 Context("when getting the current user returns an error", func() { 100 var expectedErr error 101 102 BeforeEach(func() { 103 expectedErr = errors.New("get current user error") 104 fakeConfig.CurrentUserReturns( 105 configv3.User{}, 106 expectedErr) 107 }) 108 109 It("returns the error", func() { 110 Expect(executeErr).To(MatchError(expectedErr)) 111 }) 112 }) 113 114 Context("when getting the current user does not return an error", func() { 115 BeforeEach(func() { 116 fakeConfig.CurrentUserReturns( 117 configv3.User{Name: "some-user"}, 118 nil) 119 }) 120 121 Context("when provided a valid application name", func() { 122 BeforeEach(func() { 123 fakeActor.GetApplicationByNameAndSpaceReturns( 124 v3action.Application{GUID: "some-app-guid"}, 125 v3action.Warnings{"get-application-warning-1", "get-application-warning-2"}, 126 nil) 127 fakeActor.GetApplicationTasksReturns( 128 []v3action.Task{ 129 { 130 GUID: "task-3-guid", 131 SequenceID: 3, 132 Name: "task-3", 133 State: constant.TaskRunning, 134 CreatedAt: "2016-11-08T22:26:02Z", 135 Command: "some-command", 136 }, 137 { 138 GUID: "task-2-guid", 139 SequenceID: 2, 140 Name: "task-2", 141 State: constant.TaskFailed, 142 CreatedAt: "2016-11-08T22:26:02Z", 143 Command: "some-command", 144 }, 145 { 146 GUID: "task-1-guid", 147 SequenceID: 1, 148 Name: "task-1", 149 State: constant.TaskSucceeded, 150 CreatedAt: "2016-11-08T22:26:02Z", 151 Command: "some-command", 152 }, 153 }, 154 v3action.Warnings{"get-tasks-warning-1"}, 155 nil) 156 }) 157 158 It("outputs all tasks associated with the application and all warnings", func() { 159 Expect(executeErr).ToNot(HaveOccurred()) 160 161 Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1)) 162 appName, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0) 163 Expect(appName).To(Equal("some-app-name")) 164 Expect(spaceGUID).To(Equal("some-space-guid")) 165 166 Expect(fakeActor.GetApplicationTasksCallCount()).To(Equal(1)) 167 guid, order := fakeActor.GetApplicationTasksArgsForCall(0) 168 Expect(guid).To(Equal("some-app-guid")) 169 Expect(order).To(Equal(v3action.Descending)) 170 171 Expect(testUI.Out).To(Say("Getting tasks for app some-app-name in org some-org / space some-space as some-user...")) 172 Expect(testUI.Out).To(Say("OK")) 173 174 Expect(testUI.Out).To(Say("id\\s+name\\s+state\\s+start time\\s+command")) 175 Expect(testUI.Out).To(Say("3\\s+task-3\\s+RUNNING\\s+Tue, 08 Nov 2016 22:26:02 UTC\\s+some-command")) 176 Expect(testUI.Out).To(Say("2\\s+task-2\\s+FAILED\\s+Tue, 08 Nov 2016 22:26:02 UTC\\s+some-command")) 177 Expect(testUI.Out).To(Say("1\\s+task-1\\s+SUCCEEDED\\s+Tue, 08 Nov 2016 22:26:02 UTC\\s+some-command")) 178 Expect(testUI.Err).To(Say("get-application-warning-1")) 179 Expect(testUI.Err).To(Say("get-application-warning-2")) 180 Expect(testUI.Err).To(Say("get-tasks-warning-1")) 181 }) 182 183 Context("when the tasks' command fields are returned as empty strings", func() { 184 BeforeEach(func() { 185 fakeActor.GetApplicationTasksReturns( 186 []v3action.Task{ 187 { 188 GUID: "task-2-guid", 189 SequenceID: 2, 190 Name: "task-2", 191 State: constant.TaskFailed, 192 CreatedAt: "2016-11-08T22:26:02Z", 193 Command: "", 194 }, 195 { 196 GUID: "task-1-guid", 197 SequenceID: 1, 198 Name: "task-1", 199 State: constant.TaskSucceeded, 200 CreatedAt: "2016-11-08T22:26:02Z", 201 Command: "", 202 }, 203 }, 204 v3action.Warnings{"get-tasks-warning-1"}, 205 nil) 206 }) 207 208 It("outputs [hidden] for the tasks' commands", func() { 209 Expect(executeErr).ToNot(HaveOccurred()) 210 211 Expect(testUI.Out).To(Say("2\\s+task-2\\s+FAILED\\s+Tue, 08 Nov 2016 22:26:02 UTC\\s+\\[hidden\\]")) 212 Expect(testUI.Out).To(Say("1\\s+task-1\\s+SUCCEEDED\\s+Tue, 08 Nov 2016 22:26:02 UTC\\s+\\[hidden\\]")) 213 }) 214 }) 215 216 Context("when there are no tasks associated with the application", func() { 217 BeforeEach(func() { 218 fakeActor.GetApplicationTasksReturns([]v3action.Task{}, nil, nil) 219 }) 220 221 It("outputs an empty table", func() { 222 Expect(executeErr).ToNot(HaveOccurred()) 223 224 Expect(testUI.Out).To(Say("id\\s+name\\s+state\\s+start time\\s+command")) 225 Expect(testUI.Out).NotTo(Say("1")) 226 }) 227 }) 228 }) 229 230 Context("when there are errors", func() { 231 Context("when the error is translatable", func() { 232 Context("when getting the application returns the error", func() { 233 var ( 234 returnedErr error 235 expectedErr error 236 ) 237 238 BeforeEach(func() { 239 expectedErr = errors.New("request-error") 240 returnedErr = ccerror.RequestError{Err: expectedErr} 241 fakeActor.GetApplicationByNameAndSpaceReturns( 242 v3action.Application{GUID: "some-app-guid"}, 243 nil, 244 returnedErr) 245 }) 246 247 It("returns a translatable error", func() { 248 Expect(executeErr).To(MatchError(ccerror.RequestError{Err: expectedErr})) 249 }) 250 }) 251 252 Context("when getting the app's tasks returns the error", func() { 253 var returnedErr error 254 255 BeforeEach(func() { 256 returnedErr = ccerror.UnverifiedServerError{URL: "some-url"} 257 fakeActor.GetApplicationByNameAndSpaceReturns( 258 v3action.Application{GUID: "some-app-guid"}, 259 nil, 260 nil) 261 fakeActor.GetApplicationTasksReturns( 262 []v3action.Task{}, 263 nil, 264 returnedErr) 265 }) 266 267 It("returns a translatable error", func() { 268 Expect(executeErr).To(MatchError(returnedErr)) 269 }) 270 }) 271 }) 272 273 Context("when the error is not translatable", func() { 274 Context("when getting the app returns the error", func() { 275 var expectedErr error 276 277 BeforeEach(func() { 278 expectedErr = errors.New("bananapants") 279 fakeActor.GetApplicationByNameAndSpaceReturns( 280 v3action.Application{GUID: "some-app-guid"}, 281 v3action.Warnings{"get-application-warning-1", "get-application-warning-2"}, 282 expectedErr) 283 }) 284 285 It("return the error and outputs all warnings", func() { 286 Expect(executeErr).To(MatchError(expectedErr)) 287 288 Expect(testUI.Err).To(Say("get-application-warning-1")) 289 Expect(testUI.Err).To(Say("get-application-warning-2")) 290 }) 291 }) 292 293 Context("when getting the app's tasks returns the error", func() { 294 var expectedErr error 295 296 BeforeEach(func() { 297 expectedErr = errors.New("bananapants??") 298 fakeActor.GetApplicationByNameAndSpaceReturns( 299 v3action.Application{GUID: "some-app-guid"}, 300 v3action.Warnings{"get-application-warning-1", "get-application-warning-2"}, 301 nil) 302 fakeActor.GetApplicationTasksReturns( 303 nil, 304 v3action.Warnings{"get-tasks-warning-1", "get-tasks-warning-2"}, 305 expectedErr) 306 }) 307 308 It("returns the error and outputs all warnings", func() { 309 Expect(executeErr).To(MatchError(expectedErr)) 310 311 Expect(testUI.Err).To(Say("get-application-warning-1")) 312 Expect(testUI.Err).To(Say("get-application-warning-2")) 313 Expect(testUI.Err).To(Say("get-tasks-warning-1")) 314 Expect(testUI.Err).To(Say("get-tasks-warning-2")) 315 }) 316 }) 317 }) 318 }) 319 }) 320 }) 321 })