github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/revisions_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "strings" 6 7 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror" 8 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 9 "github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccv3/constant" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 11 . "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 12 v7 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 13 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 14 "github.com/LukasHeimann/cloudfoundrycli/v8/resources" 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("revisions Command", func() { 23 var ( 24 cmd RevisionsCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v7fakes.FakeActor 29 binaryName string 30 executeErr error 31 appName string 32 33 out *Buffer 34 ) 35 36 BeforeEach(func() { 37 out = NewBuffer() 38 testUI = ui.NewTestUI(nil, out, NewBuffer()) 39 fakeConfig = new(commandfakes.FakeConfig) 40 fakeSharedActor = new(commandfakes.FakeSharedActor) 41 fakeActor = new(v7fakes.FakeActor) 42 43 cmd = v7.RevisionsCommand{ 44 BaseCommand: v7.BaseCommand{ 45 UI: testUI, 46 Config: fakeConfig, 47 SharedActor: fakeSharedActor, 48 Actor: fakeActor, 49 }, 50 } 51 binaryName = "faceman" 52 fakeConfig.BinaryNameReturns(binaryName) 53 appName = "some-app" 54 55 cmd.RequiredArgs.AppName = appName 56 }) 57 58 JustBeforeEach(func() { 59 executeErr = cmd.Execute(nil) 60 }) 61 62 It("displays the experimental warning", func() { 63 Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice")) 64 }) 65 66 When("checking target fails", func() { 67 BeforeEach(func() { 68 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 69 }) 70 71 It("returns an error", func() { 72 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 73 74 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 75 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 76 Expect(checkTargetedOrg).To(BeTrue()) 77 Expect(checkTargetedSpace).To(BeTrue()) 78 }) 79 }) 80 81 When("the user is logged in, an org is targeted and a space is targeted", func() { 82 BeforeEach(func() { 83 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 84 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 85 }) 86 87 When("getting the current user returns an error", func() { 88 BeforeEach(func() { 89 fakeActor.GetCurrentUserReturns(configv3.User{}, errors.New("some-error")) 90 }) 91 92 It("returns the error", func() { 93 Expect(executeErr).To(MatchError("some-error")) 94 }) 95 }) 96 97 When("getting the current user succeeds", func() { 98 BeforeEach(func() { 99 fakeActor.GetCurrentUserReturns(configv3.User{Name: "banana"}, nil) 100 }) 101 102 When("when revisions are available", func() { 103 BeforeEach(func() { 104 revisions := []resources.Revision{ 105 { 106 Version: 3, 107 GUID: "A68F13F7-7E5E-4411-88E8-1FAC54F73F50", 108 Description: "On a different note", 109 CreatedAt: "2020-03-10T17:11:58Z", 110 Deployable: true, 111 }, 112 { 113 Version: 2, 114 GUID: "A89F8259-D32B-491A-ABD6-F100AC42D74C", 115 Description: "Something else", 116 CreatedAt: "2020-03-08T12:43:30Z", 117 Deployable: true, 118 }, 119 { 120 Version: 1, 121 GUID: "17E0E587-0E53-4A6E-B6AE-82073159F910", 122 Description: "Something", 123 CreatedAt: "2020-03-04T13:23:32Z", 124 Deployable: false, 125 }, 126 } 127 fakeActor.GetRevisionsByApplicationNameAndSpaceReturns(revisions, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 128 129 fakeApp := resources.Application{ 130 GUID: "fake-guid", 131 } 132 fakeActor.GetApplicationByNameAndSpaceReturns(fakeApp, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 133 134 deployedRevisions := []resources.Revision{ 135 { 136 Version: 3, 137 GUID: "A68F13F7-7E5E-4411-88E8-1FAC54F73F50", 138 Description: "On a different note", 139 CreatedAt: "2020-03-10T17:11:58Z", 140 Deployable: true, 141 }, 142 } 143 fakeActor.GetApplicationRevisionsDeployedReturns(deployedRevisions, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 144 }) 145 146 It("displays the revisions", func() { 147 Expect(executeErr).ToNot(HaveOccurred()) 148 149 Expect(fakeActor.GetApplicationRevisionsDeployedCallCount()).To(Equal(1)) 150 appGUID := fakeActor.GetApplicationRevisionsDeployedArgsForCall(0) 151 Expect(appGUID).To(Equal("fake-guid")) 152 153 Expect(testUI.Out).To(Say(`Getting revisions for app some-app in org some-org / space some-space as banana\.\.\.`)) 154 Expect(testUI.Out).To(Say("revision description deployable revision guid created at")) 155 Expect(testUI.Out).To(Say("3\\(deployed\\) On a different note true A68F13F7-7E5E-4411-88E8-1FAC54F73F50 2020-03-10T17:11:58Z")) 156 Expect(testUI.Out).To(Say("2 Something else true A89F8259-D32B-491A-ABD6-F100AC42D74C 2020-03-08T12:43:30Z")) 157 Expect(testUI.Out).To(Say("1 Something false 17E0E587-0E53-4A6E-B6AE-82073159F910 2020-03-04T13:23:32Z")) 158 159 Expect(testUI.Err).To(Say("get-warning-1")) 160 Expect(testUI.Err).To(Say("get-warning-2")) 161 162 Expect(fakeActor.GetRevisionsByApplicationNameAndSpaceCallCount()).To(Equal(1)) 163 appName, spaceGUID := fakeActor.GetRevisionsByApplicationNameAndSpaceArgsForCall(0) 164 Expect(appName).To(Equal("some-app")) 165 Expect(spaceGUID).To(Equal("some-space-guid")) 166 }) 167 168 It("does not display an informative message", func() { 169 Expect(testUI.Out).NotTo(Say("Info: this app is in the middle of a rolling deployment. More than one revision is deployed.")) 170 }) 171 172 When("there is more than one revision deployed", func() { 173 BeforeEach(func() { 174 deployedRevisions := []resources.Revision{ 175 { 176 Version: 2, 177 GUID: "A89F8259-D32B-491A-ABD6-F100AC42D74C", 178 Description: "Something else", 179 CreatedAt: "2020-03-08T12:43:30Z", 180 Deployable: true, 181 }, 182 { 183 Version: 3, 184 GUID: "A68F13F7-7E5E-4411-88E8-1FAC54F73F50", 185 Description: "On a different note", 186 CreatedAt: "2020-03-10T17:11:58Z", 187 Deployable: true, 188 }, 189 } 190 fakeActor.GetApplicationRevisionsDeployedReturns(deployedRevisions, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 191 }) 192 193 It("marks both as deployed", func() { 194 Expect(testUI.Out).To(Say("3\\(deployed\\) On a different note true A68F13F7-7E5E-4411-88E8-1FAC54F73F50 2020-03-10T17:11:58Z")) 195 Expect(testUI.Out).To(Say("2\\(deployed\\) Something else true A89F8259-D32B-491A-ABD6-F100AC42D74C 2020-03-08T12:43:30Z")) 196 }) 197 It("displays an informative message", func() { 198 Expect(testUI.Out).To(Say("Info: this app is in the middle of a rolling deployment. More than one revision is deployed.")) 199 }) 200 }) 201 202 When("the revisions feature is disabled on the app", func() { 203 BeforeEach(func() { 204 revisionsFeature := resources.ApplicationFeature{ 205 Name: "revisions", 206 Enabled: false, 207 } 208 fakeActor.GetAppFeatureReturns(revisionsFeature, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 209 }) 210 211 It("displays the revisions with a warning", func() { 212 Expect(executeErr).ToNot(HaveOccurred()) 213 214 Expect(testUI.Out).To(Say(`Getting revisions for app some-app in org some-org / space some-space as banana\.\.\.`)) 215 Expect(testUI.Err).To(Say(`Warning: Revisions for app 'some-app' are disabled. Updates to the app will not create new revisions.`)) 216 217 Expect(testUI.Out).To(Say("revision description deployable revision guid created at")) 218 Expect(testUI.Out).To(Say("3\\(deployed\\) On a different note true A68F13F7-7E5E-4411-88E8-1FAC54F73F50 2020-03-10T17:11:58Z")) 219 Expect(testUI.Out).To(Say("2 Something else true A89F8259-D32B-491A-ABD6-F100AC42D74C 2020-03-08T12:43:30Z")) 220 Expect(testUI.Out).To(Say("1 Something false 17E0E587-0E53-4A6E-B6AE-82073159F910 2020-03-04T13:23:32Z")) 221 222 Expect(testUI.Err).To(Say("get-warning-1")) 223 Expect(testUI.Err).To(Say("get-warning-2")) 224 225 Expect(fakeActor.GetRevisionsByApplicationNameAndSpaceCallCount()).To(Equal(1)) 226 appName, spaceGUID := fakeActor.GetRevisionsByApplicationNameAndSpaceArgsForCall(0) 227 Expect(appName).To(Equal("some-app")) 228 Expect(spaceGUID).To(Equal("some-space-guid")) 229 }) 230 231 When("the app is in the STOPPED state", func() { 232 BeforeEach(func() { 233 fakeApp := resources.Application{ 234 GUID: "fake-guid", 235 Name: "app-name", 236 State: constant.ApplicationStopped, 237 } 238 fakeActor.GetApplicationByNameAndSpaceReturns(fakeApp, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 239 }) 240 241 It("displays the revisions with an info message about being unable to determine the deployed revision", func() { 242 Expect(executeErr).ToNot(HaveOccurred()) 243 244 Expect(testUI.Out).To(Say(`Getting revisions for app some-app in org some-org / space some-space as banana\.\.\.`)) 245 Expect(testUI.Out).To(Say(`Info: this app is in a stopped state. It is not possible to determine which revision is currently deployed.`)) 246 }) 247 248 }) 249 }) 250 251 When("Application Revisions deployed call fails", func() { 252 var expectedErr error 253 BeforeEach(func() { 254 expectedErr = errors.New("some-error") 255 fakeActor.GetApplicationRevisionsDeployedReturns( 256 []resources.Revision{}, 257 v7action.Warnings{"get-warning-1", "get-warning-2"}, 258 expectedErr, 259 ) 260 }) 261 262 It("returns the error", func() { 263 Expect(executeErr).To(Equal(expectedErr)) 264 Expect(testUI.Out).To(Say(`Getting revisions for app some-app in org some-org / space some-space as banana\.\.\.`)) 265 266 Expect(testUI.Err).To(Say("get-warning-1")) 267 Expect(testUI.Err).To(Say("get-warning-2")) 268 269 }) 270 }) 271 }) 272 273 When("there are no revisions available", func() { 274 BeforeEach(func() { 275 fakeActor.GetRevisionsByApplicationNameAndSpaceReturns( 276 []resources.Revision{}, 277 v7action.Warnings{"get-warning-1", "get-warning-2"}, 278 nil, 279 ) 280 }) 281 282 It("returns 'no revisions found'", func() { 283 Expect(executeErr).ToNot(HaveOccurred()) 284 285 Expect(strings.TrimSpace(string(out.Contents()))).To(Equal(strings.TrimSpace(` 286 Getting revisions for app some-app in org some-org / space some-space as banana... 287 288 No revisions found 289 `))) 290 Expect(testUI.Err).To(Say("get-warning-1")) 291 Expect(testUI.Err).To(Say("get-warning-2")) 292 }) 293 }) 294 295 When("revisions variables returns an unknown error", func() { 296 var expectedErr error 297 BeforeEach(func() { 298 expectedErr = errors.New("some-error") 299 fakeActor.GetRevisionsByApplicationNameAndSpaceReturns([]resources.Revision{}, v7action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr) 300 }) 301 302 It("returns the error", func() { 303 Expect(executeErr).To(Equal(expectedErr)) 304 Expect(testUI.Out).To(Say(`Getting revisions for app some-app in org some-org / space some-space as banana\.\.\.`)) 305 306 Expect(testUI.Err).To(Say("get-warning-1")) 307 Expect(testUI.Err).To(Say("get-warning-2")) 308 }) 309 }) 310 }) 311 }) 312 })