github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/cse/csev2/serviceendpoints_test.go (about) 1 package csev2 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 8 "github.com/IBM-Cloud/bluemix-go" 9 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 10 11 "github.com/IBM-Cloud/bluemix-go/client" 12 "github.com/IBM-Cloud/bluemix-go/session" 13 "github.com/onsi/gomega/ghttp" 14 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("ServiceEndpoints", func() { 20 var server *ghttp.Server 21 srvID := "test-srv-id" 22 23 AfterEach(func() { 24 server.Close() 25 }) 26 27 Describe("GetServiceEndpoint", func() { 28 Context("When service with srvid being srvID exists", func() { 29 BeforeEach(func() { 30 server = ghttp.NewServer() 31 server.AppendHandlers( 32 ghttp.CombineHandlers( 33 ghttp.VerifyRequest(http.MethodGet, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 34 ghttp.RespondWith(http.StatusOK, `{"service":{"srvid":"test-srv-id","service":"test-terraform","customer":"test-customer","serviceAddresses":["10.102.33.133"],"estadoProto":"http","estadoPort":8080,"estadoPath":"/service","estadoResultCode":0,"tcpports":[8080,8081],"udpports":null,"tcpportrange":"","udpportrange":"","region":"us-south","dataCenters":["dal13"],"maxSpeed":"1g","url":"test-terraform.test.cloud.ibm.com","hostname":"","dedicated":1,"multitenant":1,"acl":null,"creationTime":"2019-06-05T05:55:40Z","owner":"IBM","bss":"IBM"},"endpoints":[{"seid":"test-srv-id-dal13","srvid":"test-srv-id","mbid":"test-mbid","crn":"crn:v1:bluemix:public:serviceendpoint:dal13:a/xxxxxx:test-srv-id-dal13::","staticAddress":"166.9.1.144","netmask":"25","dnsStatus":"Y","region":"us-south","dataCenter":"dal13","vlanid":2288443,"status":"Ready","serverGroup":"groupA","serviceStatus":null,"statusDetails":[{"address":"10.102.33.133","ping":1,"estado":1,"ports":["8080:1","8081:1"]}],"heartbeatTime":"2019-06-14T01:32:39Z"}]}`), 35 ), 36 ) 37 }) 38 It("should return the service info", func() { 39 srvObj, err := newTestCseAPI(server.URL()).GetServiceEndpoint(srvID) 40 41 Expect(err).ShouldNot(HaveOccurred()) 42 Expect(srvObj.Service.ServiceName).Should(Equal("test-terraform")) 43 Expect(srvObj.Service.CustomerName).Should(Equal("test-customer")) 44 }) 45 }) 46 47 Context("When service with srvid being srvID does not exist", func() { 48 BeforeEach(func() { 49 server = ghttp.NewServer() 50 server.AppendHandlers( 51 ghttp.CombineHandlers( 52 ghttp.VerifyRequest(http.MethodGet, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 53 ghttp.RespondWith(http.StatusNotFound, 54 fmt.Sprintf(`{{"message":"Not found service endpoint with id: %s"}}`, srvID)), 55 ), 56 ) 57 }) 58 It("should return nil and an error", func() { 59 srvObj, err := newTestCseAPI(server.URL()).GetServiceEndpoint(srvID) 60 Expect(err).Should(HaveOccurred()) 61 Expect(srvObj).Should(BeNil()) 62 }) 63 }) 64 65 Context("When srvID is empty", func() { 66 BeforeEach(func() { 67 server = ghttp.NewServer() 68 server.AppendHandlers( 69 ghttp.CombineHandlers( 70 ghttp.VerifyRequest(http.MethodGet, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 71 ghttp.RespondWith(http.StatusNotFound, 72 fmt.Sprintf(`{{"message":"Not found service endpoint with id: %s"}}`, srvID)), 73 ), 74 ) 75 }) 76 It("should return nil and an error", func() { 77 srvObj, err := newTestCseAPI(server.URL()).GetServiceEndpoint("") 78 Expect(err).Should(HaveOccurred()) 79 Expect(srvObj).Should(BeNil()) 80 }) 81 }) 82 }) 83 84 Describe("ListServiceEndpoints", func() { 85 Context("When list executes successfuly", func() { 86 BeforeEach(func() { 87 server = ghttp.NewServer() 88 server.AppendHandlers( 89 ghttp.CombineHandlers( 90 ghttp.VerifyRequest(http.MethodGet, "/v2/serviceendpoints"), 91 ghttp.RespondWith(http.StatusOK, `[{"service":{"srvid":"test-srv-id","service":"test-terraform","customer":"test-customer","serviceAddresses":["10.102.33.133"],"estadoProto":"http","estadoPort":8080,"estadoPath":"/service","estadoResultCode":0,"tcpports":[8080,8081],"udpports":null,"tcpportrange":"","udpportrange":"","region":"us-south","dataCenters":["dal13"],"maxSpeed":"1g","url":"test-terraform.test.cloud.ibm.com","hostname":"","dedicated":1,"multitenant":1,"acl":null,"creationTime":"2019-06-05T05:55:40Z","owner":"IBM","bss":"IBM"},"endpoints":[{"seid":"test-srv-id-dal13","srvid":"test-srv-id","mbid":"test-mbid","crn":"crn:v1:bluemix:public:serviceendpoint:dal13:a/xxxxxx:test-srv-id-dal13::","staticAddress":"166.9.1.144","netmask":"25","dnsStatus":"Y","region":"us-south","dataCenter":"dal13","vlanid":2288443,"status":"Ready","serverGroup":"groupA","serviceStatus":null,"statusDetails":[{"address":"10.102.33.133","ping":1,"estado":1,"ports":["8080:1","8081:1"]}],"heartbeatTime":"2019-06-14T01:32:39Z"}]}]`), 92 ), 93 ) 94 }) 95 It("should return the service info as an array", func() { 96 srvObj, err := newTestCseAPI(server.URL()).GetServiceEndpoints() 97 98 Expect(err).ShouldNot(HaveOccurred()) 99 Expect((*srvObj)[0].Service.ServiceName).Should(Equal("test-terraform")) 100 Expect((*srvObj)[0].Service.CustomerName).Should(Equal("test-customer")) 101 }) 102 }) 103 104 Context("When list fails", func() { 105 BeforeEach(func() { 106 server = ghttp.NewServer() 107 server.AppendHandlers( 108 ghttp.CombineHandlers( 109 ghttp.VerifyRequest(http.MethodGet, "/v2/serviceendpoints"), 110 ghttp.RespondWith(http.StatusNotFound, 111 fmt.Sprintf(`{{"message":"Cannot list service endpoints"}}`)), 112 ), 113 ) 114 }) 115 It("should return nil and an error", func() { 116 srvObj, err := newTestCseAPI(server.URL()).GetServiceEndpoints() 117 Expect(err).Should(HaveOccurred()) 118 Expect(srvObj).Should(BeNil()) 119 }) 120 }) 121 }) 122 123 Describe("CreateServiceEndpoint", func() { 124 Context("When creation is successful", func() { 125 BeforeEach(func() { 126 server = ghttp.NewServer() 127 server.AppendHandlers( 128 ghttp.CombineHandlers( 129 ghttp.VerifyRequest(http.MethodPost, "/v2/serviceendpoint"), 130 ghttp.RespondWith(http.StatusOK, `{"serviceid":"test-srv-id"}`), 131 ), 132 ) 133 }) 134 It("should return the srvid of newly created service endpoint", func() { 135 payload := SeCreateData{ 136 ServiceName: "test-terrafor-11", 137 CustomerName: "test-customer-11", 138 ServiceAddresses: []string{"10.102.33.131", "10.102.33.133"}, 139 Region: "us-south", 140 DataCenters: []string{"dal10"}, 141 TCPPorts: []int{8080, 80}, 142 } 143 srvID, err := newTestCseAPI(server.URL()).CreateServiceEndpoint(payload) 144 145 Expect(err).ShouldNot(HaveOccurred()) 146 Expect(srvID).Should(Equal("test-srv-id")) 147 }) 148 }) 149 150 Context("When creation is failed", func() { 151 BeforeEach(func() { 152 server = ghttp.NewServer() 153 server.AppendHandlers( 154 ghttp.CombineHandlers( 155 ghttp.VerifyRequest(http.MethodPost, "/v2/serviceendpoint"), 156 ghttp.RespondWith(http.StatusBadRequest, `{"message":"The port 0 in tcpports is invalid"}`), 157 ), 158 ) 159 }) 160 It("should return empty srvid and an error", func() { 161 payload := SeCreateData{ 162 ServiceName: "test-terrafor-11", 163 CustomerName: "test-customer-11", 164 ServiceAddresses: []string{"10.102.33.131", "10.102.33.133"}, 165 Region: "us-south", 166 DataCenters: []string{"dal10"}, 167 TCPPorts: []int{0, 8080, 80}, 168 } 169 srvID, err := newTestCseAPI(server.URL()).CreateServiceEndpoint(payload) 170 171 Expect(err).Should(HaveOccurred()) 172 Expect(srvID).Should(Equal("")) 173 }) 174 }) 175 }) 176 177 Describe("UpdateServiceEndpoint", func() { 178 Context("When update is successful", func() { 179 BeforeEach(func() { 180 server = ghttp.NewServer() 181 server.AppendHandlers( 182 ghttp.CombineHandlers( 183 ghttp.VerifyRequest(http.MethodPut, fmt.Sprintf("/v2/serviceendpointtf/%s", srvID)), 184 ghttp.RespondWith(http.StatusOK, ` 185 {"message": "Update the service endpoint and view its status to show update result."} 186 `), 187 ), 188 ) 189 }) 190 It("should return nil", func() { 191 payload := SeUpdateData{ 192 DataCenters: []string{"dal10", "dal13"}, 193 TCPPorts: []int{8080, 80, 8081}, 194 } 195 err := newTestCseAPI(server.URL()).UpdateServiceEndpoint(srvID, payload) 196 197 Expect(err).ShouldNot(HaveOccurred()) 198 }) 199 }) 200 Context("When update is failed", func() { 201 BeforeEach(func() { 202 server = ghttp.NewServer() 203 server.AppendHandlers( 204 ghttp.CombineHandlers( 205 ghttp.VerifyRequest(http.MethodPut, fmt.Sprintf("/v2/serviceendpointtf/%s", srvID)), 206 ghttp.RespondWith(http.StatusNotFound, 207 fmt.Sprintf(`{"message": "Not found service endpoint with id: %s"}`, srvID)), 208 ), 209 ) 210 }) 211 It("should return an error", func() { 212 payload := SeUpdateData{ 213 DataCenters: []string{"dal10", "dal13"}, 214 TCPPorts: []int{8080, 80, 8081}, 215 } 216 err := newTestCseAPI(server.URL()).UpdateServiceEndpoint(srvID, payload) 217 Expect(err).Should(HaveOccurred()) 218 }) 219 }) 220 221 Context("When srvID is empty", func() { 222 BeforeEach(func() { 223 server = ghttp.NewServer() 224 server.AppendHandlers( 225 ghttp.CombineHandlers( 226 ghttp.VerifyRequest(http.MethodPut, fmt.Sprintf("/v2/serviceendpointtf/%s", srvID)), 227 ghttp.RespondWith(http.StatusOK, ` 228 {"message": "Update the service endpoint and view its status to show update result."} 229 `), 230 ), 231 ) 232 }) 233 It("should return an error", func() { 234 payload := SeUpdateData{ 235 DataCenters: []string{"dal10", "dal13"}, 236 TCPPorts: []int{8080, 80, 8081}, 237 } 238 err := newTestCseAPI(server.URL()).UpdateServiceEndpoint("", payload) 239 240 Expect(err).Should(HaveOccurred()) 241 }) 242 }) 243 }) 244 245 Describe("PatchServiceEndpoint", func() { 246 Context("When patch is successful", func() { 247 BeforeEach(func() { 248 server = ghttp.NewServer() 249 server.AppendHandlers( 250 ghttp.CombineHandlers( 251 ghttp.VerifyRequest(http.MethodPatch, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 252 ghttp.RespondWith(http.StatusOK, 253 fmt.Sprintf(`{"message": "Success to patch service endpoint: %s"}`, srvID)), 254 ), 255 ) 256 }) 257 It("should return nil", func() { 258 payload := SePatchData{ 259 SeUpdateData: SeUpdateData{ 260 DataCenters: []string{"dal10", "dal13"}, 261 TCPPorts: []int{8080, 80, 8081}, 262 }, 263 Action: "add", 264 } 265 err := newTestCseAPI(server.URL()).PatchServiceEndpoint(srvID, payload) 266 Expect(err).ShouldNot(HaveOccurred()) 267 }) 268 }) 269 Context("When patch is failed", func() { 270 BeforeEach(func() { 271 server = ghttp.NewServer() 272 server.AppendHandlers( 273 ghttp.CombineHandlers( 274 ghttp.VerifyRequest(http.MethodPatch, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 275 ghttp.RespondWith(http.StatusNotFound, 276 fmt.Sprintf(`{"message": "Not found service endpoint with id: %s"}`, srvID)), 277 ), 278 ) 279 }) 280 It("should return an error", func() { 281 payload := SePatchData{ 282 SeUpdateData: SeUpdateData{ 283 DataCenters: []string{"dal10", "dal13"}, 284 TCPPorts: []int{8080, 80, 8081}, 285 }, 286 Action: "add", 287 } 288 err := newTestCseAPI(server.URL()).PatchServiceEndpoint(srvID, payload) 289 Expect(err).Should(HaveOccurred()) 290 }) 291 }) 292 293 Context("When srvID is empty", func() { 294 BeforeEach(func() { 295 server = ghttp.NewServer() 296 server.AppendHandlers( 297 ghttp.CombineHandlers( 298 ghttp.VerifyRequest(http.MethodPatch, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 299 ghttp.RespondWith(http.StatusOK, 300 fmt.Sprintf(`{"message": "Success to delete service endpoint: %s"}`, srvID)), 301 ), 302 ) 303 }) 304 It("should return an error", func() { 305 payload := SePatchData{ 306 SeUpdateData: SeUpdateData{ 307 DataCenters: []string{"dal10", "dal13"}, 308 TCPPorts: []int{8080, 80, 8081}, 309 }, 310 Action: "add", 311 } 312 err := newTestCseAPI(server.URL()).PatchServiceEndpoint("", payload) 313 Expect(err).Should(HaveOccurred()) 314 }) 315 }) 316 }) 317 318 Describe("DeleteServiceEndpoint", func() { 319 Context("When delete is successful", func() { 320 BeforeEach(func() { 321 server = ghttp.NewServer() 322 server.AppendHandlers( 323 ghttp.CombineHandlers( 324 ghttp.VerifyRequest(http.MethodDelete, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 325 ghttp.RespondWith(http.StatusOK, 326 fmt.Sprintf(`{"message": "Success to delete service endpoint: %s"}`, srvID)), 327 ), 328 ) 329 }) 330 It("should return nil", func() { 331 err := newTestCseAPI(server.URL()).DeleteServiceEndpoint(srvID) 332 Expect(err).ShouldNot(HaveOccurred()) 333 }) 334 }) 335 Context("When delete is failed", func() { 336 BeforeEach(func() { 337 server = ghttp.NewServer() 338 server.AppendHandlers( 339 ghttp.CombineHandlers( 340 ghttp.VerifyRequest(http.MethodDelete, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 341 ghttp.RespondWith(http.StatusNotFound, 342 fmt.Sprintf(`{"message": "Not found service endpoint with id: %s"}`, srvID)), 343 ), 344 ) 345 }) 346 It("should return an error", func() { 347 err := newTestCseAPI(server.URL()).DeleteServiceEndpoint(srvID) 348 Expect(err).Should(HaveOccurred()) 349 }) 350 }) 351 352 Context("When srvID is empty", func() { 353 BeforeEach(func() { 354 server = ghttp.NewServer() 355 server.AppendHandlers( 356 ghttp.CombineHandlers( 357 ghttp.VerifyRequest(http.MethodDelete, fmt.Sprintf("/v2/serviceendpoint/%s", srvID)), 358 ghttp.RespondWith(http.StatusOK, 359 fmt.Sprintf(`{"message": "Success to delete service endpoint: %s"}`, srvID)), 360 ), 361 ) 362 }) 363 It("should return an error", func() { 364 err := newTestCseAPI(server.URL()).DeleteServiceEndpoint("") 365 Expect(err).Should(HaveOccurred()) 366 }) 367 }) 368 }) 369 370 }) 371 372 func newTestCseAPI(url string) ServiceEndpoints { 373 sess, err := session.New() 374 if err != nil { 375 log.Fatal(err) 376 } 377 conf := sess.Config.Copy() 378 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 379 conf.Endpoint = &url 380 381 client := client.Client{ 382 Config: conf, 383 ServiceName: bluemix.CseService, 384 } 385 return newServiceEndpointsAPI(&client) 386 }