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