github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/sidecar_test.go (about) 1 package ccv3_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 8 "code.cloudfoundry.org/cli/resources" 9 "code.cloudfoundry.org/cli/types" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/ghttp" 13 . "github.com/onsi/gomega/gstruct" 14 ) 15 16 var _ = Describe("Sidecar", func() { 17 var client *Client 18 19 BeforeEach(func() { 20 client, _ = NewTestClient() 21 }) 22 23 Describe("GetProcessSidecars", func() { 24 var ( 25 processSidecars []resources.Sidecar 26 warnings []string 27 err error 28 ) 29 30 JustBeforeEach(func() { 31 processSidecars, warnings, err = client.GetProcessSidecars("some-process-guid") 32 }) 33 34 When("the process has sidecars", func() { 35 BeforeEach(func() { 36 response := `{ 37 "resources": [ 38 { 39 "guid": "process-1-guid", 40 "name": "auth-sidecar", 41 "command": "bundle exec rackup", 42 "process_types": ["web", "worker"], 43 "memory_in_mb": 300, 44 "relationships": { 45 "app": { 46 "data": { 47 "guid": "process-1-guid" 48 } 49 } 50 }, 51 "created_at": "2017-02-01T01:33:58Z", 52 "updated_at": "2017-02-01T01:33:58Z" 53 }, 54 { 55 "guid": "process-2-guid", 56 "name": "echo-sidecar", 57 "command": "start-echo-server", 58 "process_types": ["web"], 59 "memory_in_mb": 300, 60 "relationships": { 61 "app": { 62 "data": { 63 "guid": "process-2-guid" 64 } 65 } 66 }, 67 "created_at": "2017-02-01T01:33:59Z", 68 "updated_at": "2017-02-01T01:33:59Z" 69 } 70 ] 71 } 72 }` 73 server.AppendHandlers( 74 CombineHandlers( 75 VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/sidecars"), 76 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 77 ), 78 ) 79 }) 80 81 It("returns the sidecars and all warnings", func() { 82 Expect(err).NotTo(HaveOccurred()) 83 Expect(warnings).To(ConsistOf("this is a warning")) 84 Expect(warnings).To(ConsistOf("this is a warning")) 85 Expect(len(processSidecars)).To(Equal(2)) 86 Expect(processSidecars[0]).To(MatchAllFields(Fields{ 87 "GUID": Equal("process-1-guid"), 88 "Name": Equal("auth-sidecar"), 89 "Command": Equal(types.FilteredString{IsSet: true, Value: "bundle exec rackup"}), 90 })) 91 Expect(processSidecars[1]).To(MatchAllFields(Fields{ 92 "GUID": Equal("process-2-guid"), 93 "Name": Equal("echo-sidecar"), 94 "Command": Equal(types.FilteredString{IsSet: true, Value: "start-echo-server"}), 95 })) 96 }) 97 }) 98 99 When("the process has no sidecars", func() { 100 BeforeEach(func() { 101 response := `{ 102 "resources": [] 103 }` 104 server.AppendHandlers( 105 CombineHandlers( 106 VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/sidecars"), 107 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 108 ), 109 ) 110 }) 111 112 It("does not error", func() { 113 Expect(err).NotTo(HaveOccurred()) 114 }) 115 }) 116 117 When("the process does not exist", func() { 118 BeforeEach(func() { 119 response := `{ 120 "errors": [ 121 { 122 "detail": "Process not found", 123 "title": "CF-ResourceNotFound", 124 "code": 10010 125 } 126 ] 127 }` 128 129 server.AppendHandlers( 130 CombineHandlers( 131 VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/sidecars"), 132 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 133 ), 134 ) 135 136 }) 137 138 It("returns an error and warnings", func() { 139 Expect(err).To(MatchError(ccerror.ProcessNotFoundError{})) 140 Expect(warnings).To(ConsistOf("this is a warning")) 141 }) 142 }) 143 144 When("the cloud controller returns errors and warnings", func() { 145 BeforeEach(func() { 146 response := `{ 147 "errors": [ 148 { 149 "code": 10008, 150 "detail": "The request is semantically invalid: command presence", 151 "title": "CF-UnprocessableEntity" 152 }, 153 { 154 "code": 10009, 155 "detail": "Some CC Error", 156 "title": "CF-SomeNewError" 157 } 158 ] 159 }` 160 server.AppendHandlers( 161 CombineHandlers( 162 VerifyRequest(http.MethodGet, "/v3/processes/some-process-guid/sidecars"), 163 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 164 ), 165 ) 166 }) 167 168 It("returns the error and all warnings", func() { 169 Expect(err).To(MatchError(ccerror.MultiError{ 170 ResponseCode: http.StatusTeapot, 171 Errors: []ccerror.V3Error{ 172 { 173 Code: 10008, 174 Detail: "The request is semantically invalid: command presence", 175 Title: "CF-UnprocessableEntity", 176 }, 177 { 178 Code: 10009, 179 Detail: "Some CC Error", 180 Title: "CF-SomeNewError", 181 }, 182 }, 183 })) 184 Expect(warnings).To(ConsistOf("this is a warning")) 185 }) 186 }) 187 }) 188 })