github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/ghttp" 14 ) 15 16 var _ = Describe("Info", func() { 17 var ( 18 client *Client 19 rootRespondWith http.HandlerFunc 20 21 info Info 22 warnings Warnings 23 executeErr error 24 ) 25 26 BeforeEach(func() { 27 rootRespondWith = nil 28 }) 29 30 JustBeforeEach(func() { 31 client, _ = NewTestClient() 32 33 server.AppendHandlers( 34 CombineHandlers( 35 VerifyRequest(http.MethodGet, "/"), 36 rootRespondWith, 37 ), 38 ) 39 40 info, warnings, executeErr = client.GetInfo() 41 }) 42 43 Describe("when all requests are successful", func() { 44 BeforeEach(func() { 45 rootResponse := strings.Replace(`{ 46 "links": { 47 "self": { 48 "href": "SERVER_URL" 49 }, 50 "cloud_controller_v2": { 51 "href": "SERVER_URL/v2", 52 "meta": { 53 "version": "2.64.0" 54 } 55 }, 56 "cloud_controller_v3": { 57 "href": "SERVER_URL/v3", 58 "meta": { 59 "version": "3.0.0-alpha.5" 60 } 61 }, 62 "network_policy_v1": { 63 "href": "SERVER_URL/networking/v1/external" 64 }, 65 "uaa": { 66 "href": "https://uaa.bosh-lite.com" 67 }, 68 "logging": { 69 "href": "wss://doppler.bosh-lite.com:443" 70 }, 71 "app_ssh": { 72 "href": "ssh.bosh-lite.com:2222", 73 "meta": { 74 "host_key_fingerprint": "some-fingerprint", 75 "oath_client": "some-client" 76 } 77 } 78 } 79 }`, "SERVER_URL", server.URL(), -1) 80 81 rootRespondWith = RespondWith( 82 http.StatusOK, 83 rootResponse, 84 http.Header{"X-Cf-Warnings": {"warning 1"}}) 85 }) 86 87 It("returns the CC Information", func() { 88 Expect(executeErr).NotTo(HaveOccurred()) 89 Expect(info.UAA()).To(Equal("https://uaa.bosh-lite.com")) 90 Expect(info.Logging()).To(Equal("wss://doppler.bosh-lite.com:443")) 91 Expect(info.NetworkPolicyV1()).To(Equal(fmt.Sprintf("%s/networking/v1/external", server.URL()))) 92 Expect(info.AppSSHHostKeyFingerprint()).To(Equal("some-fingerprint")) 93 Expect(info.AppSSHEndpoint()).To(Equal("ssh.bosh-lite.com:2222")) 94 Expect(info.OAuthClient()).To(Equal("some-client")) 95 Expect(info.CFOnK8s).To(BeFalse()) 96 }) 97 98 It("returns all warnings", func() { 99 Expect(executeErr).NotTo(HaveOccurred()) 100 Expect(warnings).To(ConsistOf("warning 1")) 101 }) 102 103 When("CF-on-K8s", func() { 104 BeforeEach(func() { 105 rootRespondWith = RespondWith(http.StatusOK, `{ "cf_on_k8s": true }`) 106 }) 107 108 It("sets the CFOnK8s", func() { 109 Expect(info.CFOnK8s).To(BeTrue()) 110 }) 111 }) 112 }) 113 114 When("the cloud controller encounters an error", func() { 115 When("the root response is invalid", func() { 116 BeforeEach(func() { 117 rootRespondWith = RespondWith( 118 http.StatusNotFound, 119 `i am google, bow down`, 120 http.Header{"X-Cf-Warnings": {"warning 2"}}, 121 ) 122 }) 123 124 It("returns an APINotFoundError and no warnings", func() { 125 Expect(executeErr).To(MatchError(ccerror.APINotFoundError{URL: server.URL()})) 126 Expect(warnings).To(BeNil()) 127 }) 128 }) 129 130 When("the error occurs making a request to '/'", func() { 131 BeforeEach(func() { 132 rootRespondWith = RespondWith( 133 http.StatusNotFound, 134 `{"errors": [{}]}`, 135 http.Header{"X-Cf-Warnings": {"this is a warning"}}) 136 }) 137 138 It("returns the same error and all warnings", func() { 139 Expect(executeErr).To(MatchError(ccerror.ResourceNotFoundError{})) 140 Expect(warnings).To(ConsistOf("this is a warning")) 141 }) 142 }) 143 }) 144 })