github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/actor/v7action/application_manifest_test.go (about) 1 package v7action_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/actor/v7action/v7actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 11 "code.cloudfoundry.org/clock" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("Application Manifest Actions", func() { 18 var ( 19 actor *Actor 20 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 21 ) 22 23 BeforeEach(func() { 24 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 25 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, clock.NewClock()) 26 }) 27 28 Describe("ApplyApplicationManifest", func() { 29 var ( 30 fakeParser *v7actionfakes.FakeManifestParser 31 spaceGUID string 32 33 warnings Warnings 34 executeErr error 35 ) 36 37 BeforeEach(func() { 38 fakeParser = new(v7actionfakes.FakeManifestParser) 39 spaceGUID = "some-space-guid" 40 }) 41 42 JustBeforeEach(func() { 43 warnings, executeErr = actor.ApplyApplicationManifest(fakeParser, spaceGUID) 44 }) 45 46 When("given at least one application", func() { 47 BeforeEach(func() { 48 fakeParser.AppNamesReturns([]string{"app-1"}) 49 }) 50 51 When("getting the raw manifest bytes is successful", func() { 52 var manifestContent []byte 53 54 BeforeEach(func() { 55 manifestContent = []byte("some-manifest-contents") 56 fakeParser.RawAppManifestReturns(manifestContent, nil) 57 }) 58 59 When("the app exists", func() { 60 BeforeEach(func() { 61 fakeCloudControllerClient.GetApplicationsReturns( 62 []ccv3.Application{{GUID: "app-1-guid"}}, 63 ccv3.Warnings{"app-1-warning"}, 64 nil, 65 ) 66 }) 67 68 When("applying the manifest succeeds", func() { 69 BeforeEach(func() { 70 fakeCloudControllerClient.UpdateApplicationApplyManifestReturns( 71 "some-job-url", 72 ccv3.Warnings{"apply-manifest-1-warning"}, 73 nil, 74 ) 75 }) 76 77 When("polling finishes successfully", func() { 78 BeforeEach(func() { 79 fakeCloudControllerClient.PollJobReturns( 80 ccv3.Warnings{"poll-1-warning"}, 81 nil, 82 ) 83 }) 84 85 It("uploads the app manifest", func() { 86 Expect(executeErr).ToNot(HaveOccurred()) 87 Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning")) 88 89 Expect(fakeParser.RawAppManifestCallCount()).To(Equal(1)) 90 appName := fakeParser.RawAppManifestArgsForCall(0) 91 Expect(appName).To(Equal("app-1")) 92 93 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1)) 94 queries := fakeCloudControllerClient.GetApplicationsArgsForCall(0) 95 Expect(queries).To(ConsistOf( 96 ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}}, 97 ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}}, 98 )) 99 100 Expect(fakeCloudControllerClient.UpdateApplicationApplyManifestCallCount()).To(Equal(1)) 101 guidInCall, appManifest := fakeCloudControllerClient.UpdateApplicationApplyManifestArgsForCall(0) 102 Expect(guidInCall).To(Equal("app-1-guid")) 103 Expect(appManifest).To(Equal(manifestContent)) 104 105 Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1)) 106 jobURL := fakeCloudControllerClient.PollJobArgsForCall(0) 107 Expect(jobURL).To(Equal(ccv3.JobURL("some-job-url"))) 108 }) 109 }) 110 111 When("polling returns a generic error", func() { 112 var expectedErr error 113 114 BeforeEach(func() { 115 expectedErr = errors.New("poll-1-error") 116 fakeCloudControllerClient.PollJobReturns( 117 ccv3.Warnings{"poll-1-warning"}, 118 expectedErr, 119 ) 120 }) 121 122 It("reports a polling error", func() { 123 Expect(executeErr).To(Equal(expectedErr)) 124 Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning")) 125 }) 126 }) 127 128 When("polling returns an job failed error", func() { 129 var expectedErr error 130 131 BeforeEach(func() { 132 expectedErr = ccerror.V3JobFailedError{Detail: "some-job-failed"} 133 fakeCloudControllerClient.PollJobReturns( 134 ccv3.Warnings{"poll-1-warning"}, 135 expectedErr, 136 ) 137 }) 138 139 It("reports a polling error", func() { 140 Expect(executeErr).To(Equal(actionerror.ApplicationManifestError{Message: "some-job-failed"})) 141 Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning", "poll-1-warning")) 142 }) 143 }) 144 }) 145 146 When("applying the manifest errors", func() { 147 var applyErr error 148 149 BeforeEach(func() { 150 applyErr = errors.New("some-apply-manifest-error") 151 fakeCloudControllerClient.UpdateApplicationApplyManifestReturns( 152 "", 153 ccv3.Warnings{"apply-manifest-1-warning"}, 154 applyErr, 155 ) 156 }) 157 158 It("reports a error trying to apply the manifest", func() { 159 Expect(executeErr).To(Equal(applyErr)) 160 Expect(warnings).To(ConsistOf("app-1-warning", "apply-manifest-1-warning")) 161 }) 162 }) 163 }) 164 165 When("there's an error retrieving the application", func() { 166 var getAppErr error 167 168 BeforeEach(func() { 169 getAppErr = errors.New("get-application-error") 170 171 fakeCloudControllerClient.GetApplicationsReturns( 172 nil, 173 ccv3.Warnings{"app-1-warning"}, 174 getAppErr, 175 ) 176 }) 177 178 It("returns error and warnings", func() { 179 Expect(executeErr).To(Equal(getAppErr)) 180 Expect(warnings).To(ConsistOf("app-1-warning")) 181 }) 182 }) 183 }) 184 185 When("generating the raw manifest errors", func() { 186 getManifestErr := errors.New("get-manifest-error") 187 BeforeEach(func() { 188 fakeParser.RawAppManifestReturns(nil, getManifestErr) 189 }) 190 191 It("returns error", func() { 192 Expect(executeErr).To(Equal(getManifestErr)) 193 Expect(warnings).To(BeEmpty()) 194 }) 195 }) 196 }) 197 198 Context("no applications", func() { 199 It("does nothing", func() { 200 Expect(executeErr).ToNot(HaveOccurred()) 201 Expect(warnings).To(BeEmpty()) 202 }) 203 }) 204 }) 205 206 Describe("GetRawApplicationManifestByNameAndSpace", func() { 207 var ( 208 appName string 209 spaceGUID string 210 211 manifestBytes []byte 212 warnings Warnings 213 executeErr error 214 ) 215 216 BeforeEach(func() { 217 appName = "some-app-name" 218 spaceGUID = "some-space-guid" 219 }) 220 221 JustBeforeEach(func() { 222 manifestBytes, warnings, executeErr = actor.GetRawApplicationManifestByNameAndSpace(appName, spaceGUID) 223 }) 224 225 When("getting the application is successful", func() { 226 BeforeEach(func() { 227 fakeCloudControllerClient.GetApplicationsReturns( 228 []ccv3.Application{ 229 {Name: appName, GUID: "some-app-guid"}, 230 }, 231 ccv3.Warnings{"get-application-warning"}, 232 nil, 233 ) 234 }) 235 236 It("gets the application from the Cloud Controller", func() { 237 Expect(executeErr).ToNot(HaveOccurred()) 238 Expect(warnings).To(ContainElement("get-application-warning")) 239 240 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1)) 241 Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf( 242 ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}}, 243 ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}}, 244 )) 245 }) 246 247 When("getting the manifest is successful", func() { 248 var rawManifest []byte 249 250 BeforeEach(func() { 251 rawManifest = []byte("---\n- potato") 252 fakeCloudControllerClient.GetApplicationManifestReturns( 253 rawManifest, 254 ccv3.Warnings{"get-manifest-warnings"}, 255 nil, 256 ) 257 }) 258 259 It("returns the raw manifest bytes and warnings", func() { 260 Expect(executeErr).ToNot(HaveOccurred()) 261 Expect(warnings).To(ConsistOf("get-application-warning", "get-manifest-warnings")) 262 Expect(manifestBytes).To(Equal(rawManifest)) 263 }) 264 265 It("gets the manifest for the application", func() { 266 Expect(fakeCloudControllerClient.GetApplicationManifestCallCount()).To(Equal(1)) 267 Expect(fakeCloudControllerClient.GetApplicationManifestArgsForCall(0)).To(Equal("some-app-guid")) 268 }) 269 }) 270 271 When("getting the manifest returns an error", func() { 272 var expectedErr error 273 274 BeforeEach(func() { 275 expectedErr = errors.New("some error") 276 fakeCloudControllerClient.GetApplicationManifestReturns( 277 nil, 278 ccv3.Warnings{"get-manifest-warnings"}, 279 expectedErr, 280 ) 281 }) 282 283 It("returns the error and warnings", func() { 284 Expect(executeErr).To(Equal(expectedErr)) 285 Expect(warnings).To(ConsistOf("get-application-warning", "get-manifest-warnings")) 286 }) 287 }) 288 }) 289 290 When("getting the application returns an error", func() { 291 var expectedErr error 292 293 BeforeEach(func() { 294 expectedErr = errors.New("some error") 295 fakeCloudControllerClient.GetApplicationsReturns( 296 nil, 297 ccv3.Warnings{"get-application-warning"}, 298 expectedErr, 299 ) 300 }) 301 302 It("returns the error and warnings", func() { 303 Expect(executeErr).To(Equal(expectedErr)) 304 Expect(warnings).To(ConsistOf("get-application-warning")) 305 _ = manifestBytes //TODO DELETE ME 306 }) 307 }) 308 }) 309 })