github.com/sleungcy-sap/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/info_test.go (about) 1 package ccv3_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "strings" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 . "github.com/onsi/gomega/ghttp" 15 ) 16 17 var _ = Describe("Info", func() { 18 var ( 19 client *Client 20 rootRespondWith http.HandlerFunc 21 v3RespondWith http.HandlerFunc 22 23 apis Info 24 resources ResourceLinks 25 warnings Warnings 26 executeErr error 27 ) 28 29 BeforeEach(func() { 30 rootRespondWith = nil 31 v3RespondWith = nil 32 }) 33 34 JustBeforeEach(func() { 35 client, _ = NewTestClient() 36 37 server.AppendHandlers( 38 CombineHandlers( 39 VerifyRequest(http.MethodGet, "/"), 40 rootRespondWith, 41 ), 42 CombineHandlers( 43 VerifyRequest(http.MethodGet, "/v3"), 44 v3RespondWith, 45 )) 46 47 apis, resources, warnings, executeErr = client.GetInfo() 48 }) 49 50 Describe("when all requests are successful", func() { 51 BeforeEach(func() { 52 rootResponse := strings.Replace(`{ 53 "links": { 54 "self": { 55 "href": "SERVER_URL" 56 }, 57 "cloud_controller_v2": { 58 "href": "SERVER_URL/v2", 59 "meta": { 60 "version": "2.64.0" 61 } 62 }, 63 "cloud_controller_v3": { 64 "href": "SERVER_URL/v3", 65 "meta": { 66 "version": "3.0.0-alpha.5" 67 } 68 }, 69 "network_policy_v1": { 70 "href": "SERVER_URL/networking/v1/external" 71 }, 72 "uaa": { 73 "href": "https://uaa.bosh-lite.com" 74 }, 75 "logging": { 76 "href": "wss://doppler.bosh-lite.com:443" 77 }, 78 "app_ssh": { 79 "href": "ssh.bosh-lite.com:2222", 80 "meta": { 81 "host_key_fingerprint": "some-fingerprint", 82 "oath_client": "some-client" 83 } 84 } 85 } 86 }`, "SERVER_URL", server.URL(), -1) 87 88 rootRespondWith = RespondWith( 89 http.StatusOK, 90 rootResponse, 91 http.Header{"X-Cf-Warnings": {"warning 1"}}) 92 93 v3Response := strings.Replace(`{ 94 "links": { 95 "self": { 96 "href": "SERVER_URL/v3" 97 }, 98 "apps": { 99 "href": "SERVER_URL/v3/apps" 100 }, 101 "tasks": { 102 "href": "SERVER_URL/v3/tasks" 103 } 104 } 105 }`, "SERVER_URL", server.URL(), -1) 106 107 v3RespondWith = RespondWith( 108 http.StatusOK, 109 v3Response, 110 http.Header{"X-Cf-Warnings": {"warning 2"}}) 111 }) 112 113 It("returns the CC Information", func() { 114 Expect(executeErr).NotTo(HaveOccurred()) 115 Expect(apis.UAA()).To(Equal("https://uaa.bosh-lite.com")) 116 Expect(apis.Logging()).To(Equal("wss://doppler.bosh-lite.com:443")) 117 Expect(apis.NetworkPolicyV1()).To(Equal(fmt.Sprintf("%s/networking/v1/external", server.URL()))) 118 Expect(apis.AppSSHHostKeyFingerprint()).To(Equal("some-fingerprint")) 119 Expect(apis.AppSSHEndpoint()).To(Equal("ssh.bosh-lite.com:2222")) 120 Expect(apis.OAuthClient()).To(Equal("some-client")) 121 }) 122 123 It("returns back the resource links", func() { 124 Expect(executeErr).NotTo(HaveOccurred()) 125 Expect(resources[internal.AppsResource].HREF).To(Equal(server.URL() + "/v3/apps")) 126 Expect(resources[internal.TasksResource].HREF).To(Equal(server.URL() + "/v3/tasks")) 127 }) 128 129 It("returns all warnings", func() { 130 Expect(executeErr).NotTo(HaveOccurred()) 131 Expect(warnings).To(ConsistOf("warning 1", "warning 2")) 132 }) 133 }) 134 135 When("the cloud controller encounters an error", func() { 136 When("the root response is invalid", func() { 137 BeforeEach(func() { 138 rootRespondWith = RespondWith( 139 http.StatusNotFound, 140 `i am google, bow down`, 141 http.Header{"X-Cf-Warnings": {"warning 2"}}, 142 ) 143 }) 144 145 It("returns an APINotFoundError and no warnings", func() { 146 Expect(executeErr).To(MatchError(ccerror.APINotFoundError{URL: server.URL()})) 147 Expect(warnings).To(BeNil()) 148 }) 149 }) 150 151 When("the error occurs making a request to '/'", func() { 152 BeforeEach(func() { 153 rootRespondWith = RespondWith( 154 http.StatusNotFound, 155 `{"errors": [{}]}`, 156 http.Header{"X-Cf-Warnings": {"this is a warning"}}) 157 }) 158 159 It("returns the same error and all warnings", func() { 160 Expect(executeErr).To(MatchError(ccerror.ResourceNotFoundError{})) 161 Expect(warnings).To(ConsistOf("this is a warning")) 162 }) 163 }) 164 165 When("the error occurs making a request to '/v3'", func() { 166 BeforeEach(func() { 167 rootResponse := fmt.Sprintf(`{ 168 "links": { 169 "self": { 170 "href": "%s" 171 }, 172 "cloud_controller_v2": { 173 "href": "%s/v2", 174 "meta": { 175 "version": "2.64.0" 176 } 177 }, 178 "cloud_controller_v3": { 179 "href": "%s/v3", 180 "meta": { 181 "version": "3.0.0-alpha.5" 182 } 183 }, 184 "uaa": { 185 "href": "https://uaa.bosh-lite.com" 186 }, 187 "logging": { 188 "href": "wss://doppler.bosh-lite.com:443" 189 } 190 } 191 } 192 `, server.URL(), server.URL(), server.URL()) 193 194 rootRespondWith = RespondWith( 195 http.StatusOK, 196 rootResponse, 197 http.Header{"X-Cf-Warnings": {"warning 1"}}) 198 v3RespondWith = RespondWith( 199 http.StatusNotFound, 200 `{"errors": [{ 201 "code": 10010, 202 "title": "CF-ResourceNotFound", 203 "detail": "Not found, lol" 204 }] 205 }`, 206 http.Header{"X-Cf-Warnings": {"this is a warning"}}) 207 }) 208 209 It("returns the same error and all warnings", func() { 210 Expect(executeErr).To(MatchError(ccerror.ResourceNotFoundError{Message: "Not found, lol"})) 211 Expect(warnings).To(ConsistOf("warning 1", "this is a warning")) 212 }) 213 }) 214 }) 215 })