github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/job_url_test.go (about) 1 package ccv3_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/ghttp" 11 ) 12 13 var _ = Describe("Job URL", func() { 14 var client *Client 15 var fakedMultiErrResponse string 16 var expectedMultiErr ccerror.MultiError 17 18 BeforeEach(func() { 19 client, _ = NewTestClient() 20 21 expectedMultiErr = ccerror.MultiError{ 22 ResponseCode: http.StatusTeapot, 23 Errors: []ccerror.V3Error{ 24 { 25 Code: 1001, 26 Detail: "Request invalid due to parse error: invalid request body", 27 Title: "CF-MessageParseError", 28 }, 29 { 30 Code: 10010, 31 Detail: "App not found", 32 Title: "CF-ResourceNotFound", 33 }, 34 }, 35 } 36 fakedMultiErrResponse = `{ 37 "errors": [ 38 { 39 "code": 1001, 40 "detail": "Request invalid due to parse error: invalid request body", 41 "title": "CF-MessageParseError" 42 }, 43 { 44 "code": 10010, 45 "detail": "App not found", 46 "title": "CF-ResourceNotFound" 47 } 48 ] 49 }` 50 51 }) 52 53 Describe("DeleteApplication", func() { 54 var ( 55 jobLocation JobURL 56 warnings Warnings 57 executeErr error 58 ) 59 60 JustBeforeEach(func() { 61 jobLocation, warnings, executeErr = client.DeleteApplication("some-app-guid") 62 }) 63 64 When("the application is deleted successfully", func() { 65 BeforeEach(func() { 66 server.AppendHandlers( 67 CombineHandlers( 68 VerifyRequest(http.MethodDelete, "/v3/apps/some-app-guid"), 69 RespondWith(http.StatusAccepted, ``, 70 http.Header{ 71 "X-Cf-Warnings": {"some-warning"}, 72 "Location": {"/v3/jobs/some-location"}, 73 }, 74 ), 75 ), 76 ) 77 }) 78 79 It("returns all warnings", func() { 80 Expect(executeErr).ToNot(HaveOccurred()) 81 Expect(jobLocation).To(Equal(JobURL("/v3/jobs/some-location"))) 82 Expect(warnings).To(ConsistOf("some-warning")) 83 }) 84 }) 85 86 When("deleting the application returns an error", func() { 87 BeforeEach(func() { 88 response := `{ 89 "errors": [ 90 { 91 "code": 1001, 92 "detail": "Request invalid due to parse error: invalid request body", 93 "title": "CF-MessageParseError" 94 }, 95 { 96 "code": 10010, 97 "detail": "App not found", 98 "title": "CF-ResourceNotFound" 99 } 100 ] 101 }` 102 server.AppendHandlers( 103 CombineHandlers( 104 VerifyRequest(http.MethodDelete, "/v3/apps/some-app-guid"), 105 RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"some-warning"}}), 106 ), 107 ) 108 }) 109 110 It("returns all warnings", func() { 111 Expect(executeErr).To(MatchError(ccerror.MultiError{ 112 ResponseCode: http.StatusBadRequest, 113 Errors: []ccerror.V3Error{ 114 { 115 Code: 1001, 116 Detail: "Request invalid due to parse error: invalid request body", 117 Title: "CF-MessageParseError", 118 }, 119 { 120 Code: 10010, 121 Detail: "App not found", 122 Title: "CF-ResourceNotFound", 123 }, 124 }, 125 })) 126 Expect(warnings).To(ConsistOf("some-warning")) 127 }) 128 }) 129 }) 130 131 Describe("UpdateApplicationApplyManifest", func() { 132 var ( 133 manifestBody []byte 134 135 jobURL JobURL 136 warnings Warnings 137 executeErr error 138 ) 139 140 JustBeforeEach(func() { 141 jobURL, warnings, executeErr = client.UpdateApplicationApplyManifest( 142 "some-app-guid", 143 manifestBody, 144 ) 145 }) 146 147 When("the manifest application is successful", func() { 148 var expectedJobURL string 149 150 BeforeEach(func() { 151 manifestBody = []byte("fake-yaml-body") 152 expectedJobURL = "i-am-a-job-url" 153 154 server.AppendHandlers( 155 CombineHandlers( 156 VerifyRequest(http.MethodPost, "/v3/apps/some-app-guid/actions/apply_manifest"), 157 VerifyHeaderKV("Content-type", "application/x-yaml"), 158 VerifyBody(manifestBody), 159 RespondWith(http.StatusAccepted, "", http.Header{ 160 "X-Cf-Warnings": {"this is a warning"}, 161 "Location": {expectedJobURL}, 162 }), 163 ), 164 ) 165 }) 166 167 It("returns the job URL and warnings", func() { 168 Expect(executeErr).NotTo(HaveOccurred()) 169 Expect(warnings).To(ConsistOf("this is a warning")) 170 171 Expect(jobURL).To(Equal(JobURL(expectedJobURL))) 172 }) 173 }) 174 175 When("the manifest application fails", func() { 176 BeforeEach(func() { 177 server.AppendHandlers( 178 CombineHandlers( 179 VerifyRequest(http.MethodPost, "/v3/apps/some-app-guid/actions/apply_manifest"), 180 VerifyHeaderKV("Content-type", "application/x-yaml"), 181 RespondWith(http.StatusTeapot, fakedMultiErrResponse, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 182 ), 183 ) 184 }) 185 186 It("returns the error and all warnings", func() { 187 Expect(executeErr).To(MatchError(expectedMultiErr)) 188 Expect(warnings).To(ConsistOf("this is a warning")) 189 }) 190 }) 191 }) 192 193 Describe("UpdateSpaceApplyManifest", func() { 194 var ( 195 manifestBody []byte 196 197 query Query 198 199 responseJobURL JobURL 200 warnings Warnings 201 executeErr error 202 203 expectedJobURL string 204 ) 205 206 JustBeforeEach(func() { 207 responseJobURL, warnings, executeErr = client.UpdateSpaceApplyManifest( 208 "some-space-guid", 209 manifestBody, 210 query, 211 ) 212 }) 213 214 BeforeEach(func() { 215 manifestBody = []byte("fake-manifest-yml-body") 216 expectedJobURL = "apply-manifest-job-url" 217 }) 218 219 When("applying the manifest to the space succeeds", func() { 220 BeforeEach(func() { 221 query = Query{Key: NoRouteFilter, Values: []string{"true"}} 222 server.AppendHandlers( 223 CombineHandlers( 224 VerifyRequest(http.MethodPost, "/v3/spaces/some-space-guid/actions/apply_manifest", "no_route=true"), 225 VerifyHeaderKV("Content-type", "application/x-yaml"), 226 VerifyBody(manifestBody), 227 RespondWith(http.StatusAccepted, "", http.Header{ 228 "X-Cf-Warnings": {"some-ccv3-warning"}, 229 "Location": {expectedJobURL}, 230 }), 231 ), 232 ) 233 }) 234 235 It("returns the job URL and warnings", func() { 236 Expect(executeErr).To(Not(HaveOccurred())) 237 Expect(warnings).To(ConsistOf("some-ccv3-warning")) 238 239 Expect(responseJobURL).To(Equal(JobURL(expectedJobURL))) 240 }) 241 }) 242 243 When("applying the manifest to the space fails", func() { 244 BeforeEach(func() { 245 server.AppendHandlers( 246 CombineHandlers( 247 VerifyRequest(http.MethodPost, "/v3/spaces/some-space-guid/actions/apply_manifest"), 248 VerifyHeaderKV("Content-type", "application/x-yaml"), 249 RespondWith( 250 http.StatusTeapot, fakedMultiErrResponse, http.Header{"X-Cf-Warnings": {"some warning"}}), 251 ), 252 ) 253 }) 254 255 It("returns an error and warnings", func() { 256 Expect(executeErr).To(HaveOccurred()) 257 Expect(executeErr).To(MatchError(expectedMultiErr)) 258 }) 259 }) 260 }) 261 })