github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/stack_test.go (about) 1 package ccv3_test 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("Stacks", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client, _ = NewTestClient() 19 }) 20 21 Describe("GetStacks", func() { 22 var ( 23 query Query 24 25 stacks []Stack 26 warnings Warnings 27 executeErr error 28 ) 29 30 JustBeforeEach(func() { 31 stacks, warnings, executeErr = client.GetStacks(query) 32 }) 33 34 When("stacks exist", func() { 35 BeforeEach(func() { 36 response1 := fmt.Sprintf(`{ 37 "pagination": { 38 "next": { 39 "href": "%s/v3/stacks?names=some-stack-name&page=2&per_page=2" 40 } 41 }, 42 "resources": [ 43 { 44 "name": "stack-name-1", 45 "guid": "stack-guid-1", 46 "description": "stack desc 1" 47 }, 48 { 49 "name": "stack-name-2", 50 "guid": "stack-guid-2", 51 "description": "stack desc 2" 52 } 53 ] 54 }`, server.URL()) 55 response2 := `{ 56 "pagination": { 57 "next": null 58 }, 59 "resources": [ 60 { 61 "name": "stack-name-3", 62 "guid": "stack-guid-3", 63 "description": "stack desc 3" 64 } 65 ] 66 }` 67 68 server.AppendHandlers( 69 CombineHandlers( 70 VerifyRequest(http.MethodGet, "/v3/stacks", "names=some-stack-name"), 71 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 72 ), 73 ) 74 server.AppendHandlers( 75 CombineHandlers( 76 VerifyRequest(http.MethodGet, "/v3/stacks", "names=some-stack-name&page=2&per_page=2"), 77 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 78 ), 79 ) 80 81 query = Query{ 82 Key: NameFilter, 83 Values: []string{"some-stack-name"}, 84 } 85 }) 86 87 It("returns the queried stacks and all warnings", func() { 88 Expect(executeErr).NotTo(HaveOccurred()) 89 90 Expect(stacks).To(ConsistOf( 91 Stack{Name: "stack-name-1", GUID: "stack-guid-1", Description: "stack desc 1"}, 92 Stack{Name: "stack-name-2", GUID: "stack-guid-2", Description: "stack desc 2"}, 93 Stack{Name: "stack-name-3", GUID: "stack-guid-3", Description: "stack desc 3"}, 94 )) 95 Expect(warnings).To(ConsistOf("this is a warning", "this is another warning")) 96 }) 97 }) 98 99 When("the cloud controller returns errors and warnings", func() { 100 BeforeEach(func() { 101 response := `{ 102 "errors": [ 103 { 104 "code": 10008, 105 "detail": "The request is semantically invalid: command presence", 106 "title": "CF-UnprocessableEntity" 107 }, 108 { 109 "code": 10010, 110 "detail": "stack not found", 111 "title": "CF-stackNotFound" 112 } 113 ] 114 }` 115 server.AppendHandlers( 116 CombineHandlers( 117 VerifyRequest(http.MethodGet, "/v3/stacks"), 118 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 119 ), 120 ) 121 }) 122 123 It("returns the error and all warnings", func() { 124 Expect(executeErr).To(MatchError(ccerror.MultiError{ 125 ResponseCode: http.StatusTeapot, 126 Errors: []ccerror.V3Error{ 127 { 128 Code: 10008, 129 Detail: "The request is semantically invalid: command presence", 130 Title: "CF-UnprocessableEntity", 131 }, 132 { 133 Code: 10010, 134 Detail: "stack not found", 135 Title: "CF-stackNotFound", 136 }, 137 }, 138 })) 139 Expect(warnings).To(ConsistOf("this is a warning")) 140 }) 141 }) 142 }) 143 })