github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv3/isolation_segment_test.go (about) 1 package ccv3_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/ghttp" 13 ) 14 15 var _ = Describe("Isolation Segments", func() { 16 var ( 17 client *Client 18 name string 19 ) 20 21 BeforeEach(func() { 22 client = NewTestClient() 23 name = "an_isolation_segment" 24 }) 25 26 Describe("CreateIsolationSegment", func() { 27 Context("when the segment does not exist", func() { 28 BeforeEach(func() { 29 response := `{ 30 "guid": "some-guid", 31 "name": "an_isolation_segment" 32 }` 33 34 requestBody := map[string]string{ 35 "name": name, 36 } 37 38 server.AppendHandlers( 39 CombineHandlers( 40 VerifyRequest(http.MethodPost, "/v3/isolation_segments"), 41 VerifyJSONRepresenting(requestBody), 42 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 43 ), 44 ) 45 }) 46 47 It("returns the queried applications and all warnings", func() { 48 isolationSegment, warnings, err := client.CreateIsolationSegment(IsolationSegment{Name: name}) 49 Expect(err).NotTo(HaveOccurred()) 50 51 Expect(isolationSegment).To(Equal(IsolationSegment{ 52 Name: name, 53 GUID: "some-guid", 54 })) 55 Expect(warnings).To(ConsistOf("this is a warning")) 56 }) 57 }) 58 59 Context("when the cloud controller returns errors and warnings", func() { 60 BeforeEach(func() { 61 response := `{ 62 "errors": [ 63 { 64 "code": 10008, 65 "detail": "The request is semantically invalid: command presence", 66 "title": "CF-UnprocessableEntity" 67 } 68 ] 69 }` 70 server.AppendHandlers( 71 CombineHandlers( 72 VerifyRequest(http.MethodPost, "/v3/isolation_segments"), 73 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 74 ), 75 ) 76 }) 77 78 It("returns the error and all warnings", func() { 79 _, warnings, err := client.CreateIsolationSegment(IsolationSegment{Name: name}) 80 Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ 81 ResponseCode: http.StatusTeapot, 82 V3ErrorResponse: ccerror.V3ErrorResponse{ 83 Errors: []ccerror.V3Error{ 84 { 85 Code: 10008, 86 Detail: "The request is semantically invalid: command presence", 87 Title: "CF-UnprocessableEntity", 88 }, 89 }, 90 }, 91 })) 92 Expect(warnings).To(ConsistOf("this is a warning")) 93 }) 94 }) 95 }) 96 97 Describe("GetIsolationSegments", func() { 98 Context("when the isolation segments exist", func() { 99 BeforeEach(func() { 100 response1 := fmt.Sprintf(`{ 101 "pagination": { 102 "next": { 103 "href": "%s/v3/isolation_segments?organization_guids=some-org-guid&names=iso1,iso2,iso3&page=2&per_page=2" 104 } 105 }, 106 "resources": [ 107 { 108 "name": "iso-name-1", 109 "guid": "iso-guid-1" 110 }, 111 { 112 "name": "iso-name-2", 113 "guid": "iso-guid-2" 114 } 115 ] 116 }`, server.URL()) 117 response2 := `{ 118 "pagination": { 119 "next": null 120 }, 121 "resources": [ 122 { 123 "name": "iso-name-3", 124 "guid": "iso-guid-3" 125 } 126 ] 127 }` 128 server.AppendHandlers( 129 CombineHandlers( 130 VerifyRequest(http.MethodGet, "/v3/isolation_segments", "organization_guids=some-org-guid&names=iso1,iso2,iso3"), 131 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 132 ), 133 ) 134 server.AppendHandlers( 135 CombineHandlers( 136 VerifyRequest(http.MethodGet, "/v3/isolation_segments", "organization_guids=some-org-guid&names=iso1,iso2,iso3&page=2&per_page=2"), 137 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 138 ), 139 ) 140 }) 141 142 It("returns the queried applications and all warnings", func() { 143 segments, warnings, err := client.GetIsolationSegments(url.Values{ 144 "organization_guids": []string{"some-org-guid"}, 145 "names": []string{"iso1,iso2,iso3"}, 146 }) 147 Expect(err).NotTo(HaveOccurred()) 148 149 Expect(segments).To(ConsistOf( 150 IsolationSegment{Name: "iso-name-1", GUID: "iso-guid-1"}, 151 IsolationSegment{Name: "iso-name-2", GUID: "iso-guid-2"}, 152 IsolationSegment{Name: "iso-name-3", GUID: "iso-guid-3"}, 153 )) 154 Expect(warnings).To(ConsistOf("this is a warning", "this is another warning")) 155 }) 156 }) 157 158 Context("when the cloud controller returns errors and warnings", func() { 159 BeforeEach(func() { 160 response := `{ 161 "errors": [ 162 { 163 "code": 10008, 164 "detail": "The request is semantically invalid: command presence", 165 "title": "CF-UnprocessableEntity" 166 }, 167 { 168 "code": 10010, 169 "detail": "App not found", 170 "title": "CF-ResourceNotFound" 171 } 172 ] 173 }` 174 server.AppendHandlers( 175 CombineHandlers( 176 VerifyRequest(http.MethodGet, "/v3/isolation_segments"), 177 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 178 ), 179 ) 180 }) 181 182 It("returns the error and all warnings", func() { 183 _, warnings, err := client.GetIsolationSegments(url.Values{}) 184 Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ 185 ResponseCode: http.StatusTeapot, 186 V3ErrorResponse: ccerror.V3ErrorResponse{ 187 Errors: []ccerror.V3Error{ 188 { 189 Code: 10008, 190 Detail: "The request is semantically invalid: command presence", 191 Title: "CF-UnprocessableEntity", 192 }, 193 { 194 Code: 10010, 195 Detail: "App not found", 196 Title: "CF-ResourceNotFound", 197 }, 198 }, 199 }, 200 })) 201 Expect(warnings).To(ConsistOf("this is a warning")) 202 }) 203 }) 204 }) 205 206 Describe("GetIsolationSegment", func() { 207 Context("when the isolation segment exists", func() { 208 BeforeEach(func() { 209 response := `{ 210 "guid": "some-iso-guid", 211 "name": "an_isolation_segment" 212 }` 213 server.AppendHandlers( 214 CombineHandlers( 215 VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-guid"), 216 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 217 ), 218 ) 219 }) 220 221 It("returns the isolation segment and all warnings", func() { 222 isolationSegment, warnings, err := client.GetIsolationSegment("some-iso-guid") 223 Expect(err).NotTo(HaveOccurred()) 224 Expect(warnings).To(ConsistOf("this is a warning")) 225 Expect(isolationSegment).To(Equal(IsolationSegment{ 226 Name: "an_isolation_segment", 227 GUID: "some-iso-guid", 228 })) 229 }) 230 }) 231 232 Context("when the isolation segment does not exist", func() { 233 BeforeEach(func() { 234 response := ` 235 { 236 "errors": [ 237 { 238 "detail": "Isolation segment not found", 239 "title": "CF-ResourceNotFound", 240 "code": 10010 241 } 242 ] 243 }` 244 server.AppendHandlers( 245 CombineHandlers( 246 VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-guid"), 247 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 248 ), 249 ) 250 }) 251 252 It("returns a ResourceNotFoundError", func() { 253 _, warnings, err := client.GetIsolationSegment("some-iso-guid") 254 Expect(warnings).To(ConsistOf("this is a warning")) 255 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{Message: "Isolation segment not found"})) 256 }) 257 }) 258 259 Context("when the cloud controller returns errors and warnings", func() { 260 BeforeEach(func() { 261 response := `{ 262 "errors": [ 263 { 264 "code": 10008, 265 "detail": "The request is semantically invalid: command presence", 266 "title": "CF-UnprocessableEntity" 267 }, 268 { 269 "code": 10010, 270 "detail": "Isolation Segment not found", 271 "title": "CF-ResourceNotFound" 272 } 273 ] 274 }` 275 server.AppendHandlers( 276 CombineHandlers( 277 VerifyRequest(http.MethodGet, "/v3/isolation_segments/some-iso-guid"), 278 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 279 ), 280 ) 281 }) 282 283 It("returns the error and all warnings", func() { 284 _, warnings, err := client.GetIsolationSegment("some-iso-guid") 285 Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ 286 ResponseCode: http.StatusTeapot, 287 V3ErrorResponse: ccerror.V3ErrorResponse{ 288 Errors: []ccerror.V3Error{ 289 { 290 Code: 10008, 291 Detail: "The request is semantically invalid: command presence", 292 Title: "CF-UnprocessableEntity", 293 }, 294 { 295 Code: 10010, 296 Detail: "Isolation Segment not found", 297 Title: "CF-ResourceNotFound", 298 }, 299 }, 300 }, 301 })) 302 Expect(warnings).To(ConsistOf("this is a warning")) 303 }) 304 }) 305 }) 306 307 Describe("DeleteIsolationSegment", func() { 308 Context("when the delete is successful", func() { 309 BeforeEach(func() { 310 server.AppendHandlers( 311 CombineHandlers( 312 VerifyRequest(http.MethodDelete, "/v3/isolation_segments/some-iso-guid"), 313 RespondWith(http.StatusOK, "", http.Header{"X-Cf-Warnings": {"this is a warning"}}), 314 ), 315 ) 316 }) 317 318 It("returns the queried applications and all warnings", func() { 319 warnings, err := client.DeleteIsolationSegment("some-iso-guid") 320 Expect(err).NotTo(HaveOccurred()) 321 Expect(warnings).To(ConsistOf("this is a warning")) 322 }) 323 }) 324 325 Context("when the cloud controller returns errors and warnings", func() { 326 BeforeEach(func() { 327 response := `{ 328 "errors": [ 329 { 330 "code": 10008, 331 "detail": "The request is semantically invalid: command presence", 332 "title": "CF-UnprocessableEntity" 333 }, 334 { 335 "code": 10010, 336 "detail": "App not found", 337 "title": "CF-ResourceNotFound" 338 } 339 ] 340 }` 341 server.AppendHandlers( 342 CombineHandlers( 343 VerifyRequest(http.MethodDelete, "/v3/isolation_segments/some-iso-guid"), 344 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 345 ), 346 ) 347 }) 348 349 It("returns the error and all warnings", func() { 350 warnings, err := client.DeleteIsolationSegment("some-iso-guid") 351 Expect(err).To(MatchError(ccerror.V3UnexpectedResponseError{ 352 ResponseCode: http.StatusTeapot, 353 V3ErrorResponse: ccerror.V3ErrorResponse{ 354 Errors: []ccerror.V3Error{ 355 { 356 Code: 10008, 357 Detail: "The request is semantically invalid: command presence", 358 Title: "CF-UnprocessableEntity", 359 }, 360 { 361 Code: 10010, 362 Detail: "App not found", 363 Title: "CF-ResourceNotFound", 364 }, 365 }, 366 }, 367 })) 368 Expect(warnings).To(ConsistOf("this is a warning")) 369 }) 370 }) 371 }) 372 })