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