github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv1/clusters_test.go (about) 1 package containerv1 2 3 import ( 4 "log" 5 "net/http" 6 7 bluemix "github.com/IBM-Cloud/bluemix-go" 8 "github.com/IBM-Cloud/bluemix-go/client" 9 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 10 "github.com/IBM-Cloud/bluemix-go/session" 11 12 "github.com/onsi/gomega/ghttp" 13 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Clusters", func() { 19 var server *ghttp.Server 20 AfterEach(func() { 21 server.Close() 22 }) 23 Describe("Create", func() { 24 Context("When creation is successful", func() { 25 BeforeEach(func() { 26 server = ghttp.NewServer() 27 server.AppendHandlers( 28 ghttp.CombineHandlers( 29 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters"), 30 ghttp.VerifyJSON(`{"GatewayEnabled": false,"defaultWorkerPoolName": "","disableAutoUpdate": false,"podSubnet": "","serviceSubnet": "","dataCenter":"dal10","isolation":"","machineType":"b2c.4x16","name":"testservice","privateVlan":"vlan","publicVlan":"vlan","workerNum":1,"noSubnet":false,"masterVersion":"1.8.1","prefix":"worker","diskEncryption": true,"privateSeviceEndpoint": false,"publicServiceEndpoint": false,"defaultWorkerPoolEntitlement": ""} 31 `), 32 ghttp.RespondWith(http.StatusCreated, `{ 33 "id": "f91adfe2-76c9-4649-939e-b01c37a3704c" 34 }`), 35 ), 36 ) 37 }) 38 39 It("should return cluster created", func() { 40 params := ClusterCreateRequest{ 41 Name: "testservice", Datacenter: "dal10", MachineType: "b2c.4x16", PublicVlan: "vlan", PrivateVlan: "vlan", MasterVersion: "1.8.1", Prefix: "worker", WorkerNum: 1, DiskEncryption: true, 42 } 43 target := ClusterTargetHeader{ 44 OrgID: "abc", 45 SpaceID: "def", 46 AccountID: "ghi", 47 } 48 myCluster, err := newCluster(server.URL()).Create(params, target) 49 Expect(err).NotTo(HaveOccurred()) 50 Expect(myCluster).ShouldNot(BeNil()) 51 Expect(myCluster.ID).Should(Equal("f91adfe2-76c9-4649-939e-b01c37a3704c")) 52 }) 53 }) 54 Context("When creation with OS is successful", func() { 55 BeforeEach(func() { 56 server = ghttp.NewServer() 57 server.AppendHandlers( 58 ghttp.CombineHandlers( 59 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters"), 60 ghttp.VerifyJSON(`{"GatewayEnabled": false,"defaultWorkerPoolName": "","disableAutoUpdate": false,"podSubnet": "","serviceSubnet": "","dataCenter":"dal10","isolation":"","machineType":"b2c.4x16","name":"testservice","privateVlan":"vlan","publicVlan":"vlan","workerNum":1,"noSubnet":false,"masterVersion":"1.8.1","prefix":"worker","diskEncryption": true,"privateSeviceEndpoint": false,"publicServiceEndpoint": false,"defaultWorkerPoolEntitlement": "","operatingSystem":"REDHAT_7_64"} 61 `), 62 ghttp.RespondWith(http.StatusCreated, `{ 63 "id": "f91adfe2-76c9-4649-939e-b01c37a3704c" 64 }`), 65 ), 66 ) 67 }) 68 69 It("should return cluster created", func() { 70 params := ClusterCreateRequest{ 71 Name: "testservice", 72 Datacenter: "dal10", 73 MachineType: "b2c.4x16", 74 PublicVlan: "vlan", 75 PrivateVlan: "vlan", 76 MasterVersion: "1.8.1", 77 Prefix: "worker", 78 WorkerNum: 1, 79 DiskEncryption: true, 80 OperatingSystem: "REDHAT_7_64", 81 } 82 target := ClusterTargetHeader{ 83 OrgID: "abc", 84 SpaceID: "def", 85 AccountID: "ghi", 86 } 87 myCluster, err := newCluster(server.URL()).Create(params, target) 88 Expect(err).NotTo(HaveOccurred()) 89 Expect(myCluster).ShouldNot(BeNil()) 90 Expect(myCluster.ID).Should(Equal("f91adfe2-76c9-4649-939e-b01c37a3704c")) 91 }) 92 }) 93 Context("When creation is unsuccessful", func() { 94 BeforeEach(func() { 95 server = ghttp.NewServer() 96 server.SetAllowUnhandledRequests(true) 97 server.AppendHandlers( 98 ghttp.CombineHandlers( 99 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters"), 100 ghttp.VerifyJSON(`{"GatewayEnabled": false,"defaultWorkerPoolName": "","disableAutoUpdate": false,"podSubnet": "","serviceSubnet": "","dataCenter":"dal10","isolation":"","machineType":"u2c.2x4","name":"testservice","privateVlan":"vlan","publicVlan":"vlan","workerNum":1,"noSubnet":false,"masterVersion":"1.8.1","prefix":"worker","diskEncryption": false,"privateSeviceEndpoint": false,"publicServiceEndpoint": false,"defaultWorkerPoolEntitlement": ""} 101 `), 102 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create cluster`), 103 ), 104 ) 105 }) 106 107 It("should return error during cluster creation", func() { 108 params := ClusterCreateRequest{ 109 Name: "testservice", Datacenter: "dal10", MachineType: "u2c.2x4", PublicVlan: "vlan", PrivateVlan: "vlan", MasterVersion: "1.8.1", Prefix: "worker", WorkerNum: 1, 110 } 111 target := ClusterTargetHeader{ 112 OrgID: "abc", 113 SpaceID: "def", 114 AccountID: "ghi", 115 } 116 myCluster, err := newCluster(server.URL()).Create(params, target) 117 Expect(err).To(HaveOccurred()) 118 Expect(myCluster.ID).Should(Equal("")) 119 }) 120 }) 121 }) 122 //List 123 Describe("List", func() { 124 Context("When read of clusters is successful", func() { 125 BeforeEach(func() { 126 server = ghttp.NewServer() 127 server.AppendHandlers( 128 ghttp.CombineHandlers( 129 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters"), 130 ghttp.RespondWith(http.StatusOK, `[{ 131 "CreatedDate": "", 132 "DataCenter": "dal10", 133 "ID": "f91adfe2-76c9-4649-939e-b01c37a3704", 134 "IngressHostname": "", 135 "IngressSecretName": "", 136 "Location": "", 137 "MasterKubeVersion": "1.8.1", 138 "Prefix": "worker", 139 "ModifiedDate": "", 140 "Name": "test", 141 "Region": "abc", 142 "ServerURL": "", 143 "State": "normal", 144 "IsPaid": false, 145 "IsTrusted": true, 146 "WorkerCount": 1 147 }]`), 148 ), 149 ) 150 }) 151 152 It("should return cluster list", func() { 153 target := ClusterTargetHeader{ 154 OrgID: "abc", 155 SpaceID: "def", 156 AccountID: "ghi", 157 } 158 myCluster, err := newCluster(server.URL()).List(target) 159 Expect(myCluster).ShouldNot(BeNil()) 160 for _, cluster := range myCluster { 161 Expect(err).NotTo(HaveOccurred()) 162 Expect(cluster.ID).Should(Equal("f91adfe2-76c9-4649-939e-b01c37a3704")) 163 Expect(cluster.WorkerCount).Should(Equal(1)) 164 Expect(cluster.MasterKubeVersion).Should(Equal("1.8.1")) 165 } 166 }) 167 }) 168 Context("When read of clusters is unsuccessful", func() { 169 BeforeEach(func() { 170 server = ghttp.NewServer() 171 server.SetAllowUnhandledRequests(true) 172 server.AppendHandlers( 173 ghttp.CombineHandlers( 174 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters"), 175 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve clusters`), 176 ), 177 ) 178 }) 179 180 It("should return error when cluster are retrieved", func() { 181 target := ClusterTargetHeader{ 182 OrgID: "abc", 183 SpaceID: "def", 184 AccountID: "ghi", 185 } 186 myCluster, err := newCluster(server.URL()).List(target) 187 Expect(err).To(HaveOccurred()) 188 Expect(myCluster).Should(BeNil()) 189 }) 190 }) 191 }) 192 //RefreshAPIServers 193 Describe("RefreshAPIServers", func() { 194 Context("When refresh of api servers of cluster is successful", func() { 195 BeforeEach(func() { 196 server = ghttp.NewServer() 197 server.AppendHandlers( 198 ghttp.CombineHandlers( 199 ghttp.VerifyRequest(http.MethodPut, "/v1/clusters/test/masters"), 200 ghttp.RespondWith(http.StatusOK, `{ 201 }`), 202 ), 203 ) 204 }) 205 206 It("should refresh api servers", func() { 207 target := ClusterTargetHeader{ 208 OrgID: "abc", 209 SpaceID: "def", 210 AccountID: "ghi", 211 } 212 err := newCluster(server.URL()).RefreshAPIServers("test", target) 213 Expect(err).NotTo(HaveOccurred()) 214 }) 215 }) 216 Context("When refresh of api servers of cluster is failed", func() { 217 BeforeEach(func() { 218 server = ghttp.NewServer() 219 server.SetAllowUnhandledRequests(true) 220 server.AppendHandlers( 221 ghttp.CombineHandlers( 222 ghttp.VerifyRequest(http.MethodPut, "/v1/clusters/test/masters"), 223 ghttp.RespondWith(http.StatusInternalServerError, `Failed to refresh api servers`), 224 ), 225 ) 226 }) 227 228 It("should return error failed to refresh api servers", func() { 229 target := ClusterTargetHeader{ 230 OrgID: "abc", 231 SpaceID: "def", 232 AccountID: "ghi", 233 } 234 err := newCluster(server.URL()).RefreshAPIServers("test", target) 235 Expect(err).To(HaveOccurred()) 236 }) 237 }) 238 }) 239 //Delete 240 Describe("Delete", func() { 241 Context("When delete of cluster is successful", func() { 242 BeforeEach(func() { 243 server = ghttp.NewServer() 244 server.AppendHandlers( 245 ghttp.CombineHandlers( 246 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test"), 247 ghttp.RespondWith(http.StatusOK, `{ 248 }`), 249 ), 250 ) 251 }) 252 253 It("should delete cluster", func() { 254 target := ClusterTargetHeader{ 255 OrgID: "abc", 256 SpaceID: "def", 257 AccountID: "ghi", 258 } 259 err := newCluster(server.URL()).Delete("test", target) 260 Expect(err).NotTo(HaveOccurred()) 261 }) 262 }) 263 Context("When cluster delete is failed", func() { 264 BeforeEach(func() { 265 server = ghttp.NewServer() 266 server.SetAllowUnhandledRequests(true) 267 server.AppendHandlers( 268 ghttp.CombineHandlers( 269 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test"), 270 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete service key`), 271 ), 272 ) 273 }) 274 275 It("should return error service key delete", func() { 276 target := ClusterTargetHeader{ 277 OrgID: "abc", 278 SpaceID: "def", 279 AccountID: "ghi", 280 } 281 err := newCluster(server.URL()).Delete("test", target) 282 Expect(err).To(HaveOccurred()) 283 }) 284 }) 285 }) 286 //Find 287 Describe("Find", func() { 288 Context("When read of cluster is successful", func() { 289 BeforeEach(func() { 290 server = ghttp.NewServer() 291 server.AppendHandlers( 292 ghttp.CombineHandlers( 293 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test"), 294 ghttp.RespondWith(http.StatusOK, `{ 295 "CreatedDate": "", 296 "DataCenter": "dal10", 297 "ID": "f91adfe2-76c9-4649-939e-b01c37a3704", 298 "IngressHostname": "", 299 "IngressSecretName": "", 300 "Location": "", 301 "MasterKubeVersion": "", 302 "ModifiedDate": "", 303 "Name": "test", 304 "Region": "abc", 305 "ServerURL": "", 306 "State": "normal", 307 "IsPaid": false, 308 "IsTrusted": true, 309 "ResourceGroup": "abcd", 310 "ResourceGroupName": "abcdefgh", 311 "WorkerCount": 1, 312 "workerZones": [ 313 "zone" 314 ], 315 "Vlans": [{ 316 "ID": "177453", 317 "Subnets": [ 318 { 319 "Cidr": "159.8.226.208/29", 320 "ID": "1541737", 321 "Ips": ["159.8.226.210"], 322 "Is_ByOIP": false, 323 "Is_Public": true 324 }] 325 }]}`), 326 ), 327 ) 328 }) 329 330 It("should return cluster", func() { 331 target := ClusterTargetHeader{ 332 OrgID: "abc", 333 SpaceID: "def", 334 AccountID: "ghi", 335 } 336 myCluster, err := newCluster(server.URL()).Find("test", target) 337 Expect(err).NotTo(HaveOccurred()) 338 Expect(myCluster).ShouldNot(BeNil()) 339 Expect(myCluster.Vlans[0].ID).Should(Equal("177453")) 340 Expect(myCluster.Vlans[0].Subnets[0].ID).Should(Equal("1541737")) 341 Expect(myCluster.Vlans[0].Subnets[0].Cidr).Should(Equal("159.8.226.208/29")) 342 Expect(myCluster.Vlans[0].Subnets[0].IsPublic).Should(Equal(true)) 343 Expect(myCluster.ResourceGroupID).Should(Equal("abcd")) 344 }) 345 }) 346 Context("When cluster retrieve is failed", func() { 347 BeforeEach(func() { 348 server = ghttp.NewServer() 349 server.SetAllowUnhandledRequests(true) 350 server.AppendHandlers( 351 ghttp.CombineHandlers( 352 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test"), 353 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve cluster`), 354 ), 355 ) 356 }) 357 358 It("should return error when cluster is retrieved", func() { 359 target := ClusterTargetHeader{ 360 OrgID: "abc", 361 SpaceID: "def", 362 AccountID: "ghi", 363 } 364 myCluster, err := newCluster(server.URL()).Find("test", target) 365 Expect(err).To(HaveOccurred()) 366 Expect(myCluster.ID).Should(Equal("")) 367 }) 368 }) 369 }) 370 //FindWithOutShowResourcesCompatible 371 Describe("FindWithOutShowResourcesCompatible", func() { 372 Context("When read of cluster v1 is successful", func() { 373 BeforeEach(func() { 374 server = ghttp.NewServer() 375 server.AppendHandlers( 376 ghttp.CombineHandlers( 377 ghttp.VerifyRequest(http.MethodGet, "/v2/getCluster"), 378 ghttp.RespondWith(http.StatusOK, `{ 379 "CreatedDate": "", 380 "DataCenter": "dal10", 381 "ID": "f91adfe2-76c9-4649-939e-b01c37a3704", 382 "IngressHostname": "", 383 "IngressSecretName": "", 384 "Location": "", 385 "MasterKubeVersion": "", 386 "ModifiedDate": "", 387 "Name": "test", 388 "Region": "abc", 389 "ServerURL": "https://test.com", 390 "State": "normal", 391 "IsPaid": false, 392 "IsTrusted": true, 393 "ResourceGroup": "abcd", 394 "ResourceGroupName": "abcdefgh", 395 "WorkerCount": 1 396 }`), 397 ), 398 ) 399 }) 400 401 It("should return cluster", func() { 402 target := ClusterTargetHeader{ 403 OrgID: "abc", 404 SpaceID: "def", 405 AccountID: "ghi", 406 } 407 myCluster, err := newCluster(server.URL()).FindWithOutShowResourcesCompatible("test", target) 408 Expect(err).NotTo(HaveOccurred()) 409 Expect(myCluster).ShouldNot(BeNil()) 410 Expect(myCluster.ServerURL).Should(Equal("https://test.com")) 411 }) 412 }) 413 Context("When read of cluster v2 is successful", func() { 414 BeforeEach(func() { 415 server = ghttp.NewServer() 416 server.AppendHandlers( 417 ghttp.CombineHandlers( 418 ghttp.VerifyRequest(http.MethodGet, "/v2/getCluster"), 419 ghttp.RespondWith(http.StatusOK, `{ 420 "CreatedDate": "", 421 "DataCenter": "dal10", 422 "ID": "f91adfe2-76c9-4649-939e-b01c37a3704", 423 "IngressHostname": "", 424 "IngressSecretName": "", 425 "Location": "", 426 "MasterKubeVersion": "", 427 "ModifiedDate": "", 428 "Name": "test", 429 "Region": "abc", 430 "MasterURL": "https://test.master.com", 431 "State": "normal", 432 "IsPaid": false, 433 "IsTrusted": true, 434 "ResourceGroup": "abcd", 435 "ResourceGroupName": "abcdefgh", 436 "WorkerCount": 1 437 }`), 438 ), 439 ) 440 }) 441 442 It("should return cluster with masterURL", func() { 443 target := ClusterTargetHeader{ 444 OrgID: "abc", 445 SpaceID: "def", 446 AccountID: "ghi", 447 } 448 myCluster, err := newCluster(server.URL()).FindWithOutShowResourcesCompatible("test", target) 449 Expect(err).NotTo(HaveOccurred()) 450 Expect(myCluster).ShouldNot(BeNil()) 451 Expect(myCluster.ServerURL).Should(Equal("https://test.master.com")) 452 }) 453 }) 454 Context("When cluster v1 retrieve is failed", func() { 455 BeforeEach(func() { 456 server = ghttp.NewServer() 457 server.SetAllowUnhandledRequests(true) 458 server.AppendHandlers( 459 ghttp.CombineHandlers( 460 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test"), 461 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve cluster`), 462 ), 463 ) 464 }) 465 466 It("should return error when cluster is retrieved", func() { 467 target := ClusterTargetHeader{ 468 OrgID: "abc", 469 SpaceID: "def", 470 AccountID: "ghi", 471 } 472 myCluster, err := newCluster(server.URL()).Find("test", target) 473 Expect(err).To(HaveOccurred()) 474 Expect(myCluster.ID).Should(Equal("")) 475 }) 476 }) 477 }) 478 //set credentials 479 Describe("set credentials", func() { 480 Context("When credential set is successful", func() { 481 BeforeEach(func() { 482 server = ghttp.NewServer() 483 server.AppendHandlers( 484 ghttp.CombineHandlers( 485 ghttp.VerifyRequest(http.MethodPost, "/v1/credentials"), 486 ghttp.RespondWith(http.StatusOK, `{}`), 487 ), 488 ) 489 }) 490 491 It("should set credentials", func() { 492 target := ClusterTargetHeader{ 493 OrgID: "abc", 494 SpaceID: "def", 495 AccountID: "ghi", 496 } 497 err := newCluster(server.URL()).SetCredentials("test", "abcdef-df-fg", target) 498 Expect(err).NotTo(HaveOccurred()) 499 500 }) 501 }) 502 Context("When credential set is unsuccessful", func() { 503 BeforeEach(func() { 504 server = ghttp.NewServer() 505 server.SetAllowUnhandledRequests(true) 506 server.AppendHandlers( 507 ghttp.CombineHandlers( 508 ghttp.VerifyRequest(http.MethodPost, "/v1/credentials"), 509 ghttp.RespondWith(http.StatusInternalServerError, `Failed to set credentials`), 510 ), 511 ) 512 }) 513 514 It("should throw error when setting credentials", func() { 515 target := ClusterTargetHeader{ 516 OrgID: "abc", 517 SpaceID: "def", 518 AccountID: "ghi", 519 } 520 err := newCluster(server.URL()).SetCredentials("test", "abcdef-df-fg", target) 521 Expect(err).To(HaveOccurred()) 522 523 }) 524 }) 525 }) 526 //Unset credentials 527 Describe("unset credentials", func() { 528 Context("When unset credential is successful", func() { 529 BeforeEach(func() { 530 server = ghttp.NewServer() 531 server.AppendHandlers( 532 ghttp.CombineHandlers( 533 ghttp.VerifyRequest(http.MethodDelete, "/v1/credentials"), 534 ghttp.RespondWith(http.StatusOK, `{}`), 535 ), 536 ) 537 }) 538 539 It("should set credentials", func() { 540 target := ClusterTargetHeader{ 541 OrgID: "abc", 542 SpaceID: "def", 543 AccountID: "ghi", 544 } 545 err := newCluster(server.URL()).UnsetCredentials(target) 546 Expect(err).NotTo(HaveOccurred()) 547 548 }) 549 }) 550 Context("When unset credential is unsuccessful", func() { 551 BeforeEach(func() { 552 server = ghttp.NewServer() 553 server.SetAllowUnhandledRequests(true) 554 server.AppendHandlers( 555 ghttp.CombineHandlers( 556 ghttp.VerifyRequest(http.MethodDelete, "/v1/credentials"), 557 ghttp.RespondWith(http.StatusInternalServerError, `Failed to unset credentials`), 558 ), 559 ) 560 }) 561 562 It("should set credentials", func() { 563 target := ClusterTargetHeader{ 564 OrgID: "abc", 565 SpaceID: "def", 566 AccountID: "ghi", 567 } 568 err := newCluster(server.URL()).UnsetCredentials(target) 569 Expect(err).To(HaveOccurred()) 570 571 }) 572 }) 573 }) 574 //Bind service 575 Describe("Bind service", func() { 576 Context("When bind service is successful", func() { 577 BeforeEach(func() { 578 server = ghttp.NewServer() 579 server.AppendHandlers( 580 ghttp.CombineHandlers( 581 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/services"), 582 ghttp.RespondWith(http.StatusOK, `{}`), 583 ), 584 ) 585 }) 586 587 It("should bind service to a cluster", func() { 588 target := ClusterTargetHeader{ 589 OrgID: "abc", 590 SpaceID: "def", 591 AccountID: "ghi", 592 } 593 params := ServiceBindRequest{ 594 ClusterNameOrID: "test", ServiceInstanceNameOrID: "cloudantDB", NamespaceID: "default"} 595 serviceResp, err := newCluster(server.URL()).BindService(params, target) 596 Expect(err).NotTo(HaveOccurred()) 597 Expect(serviceResp).ShouldNot(BeNil()) 598 }) 599 }) 600 Context("When bind service is unsuccessful", func() { 601 BeforeEach(func() { 602 server = ghttp.NewServer() 603 server.SetAllowUnhandledRequests(true) 604 server.AppendHandlers( 605 ghttp.CombineHandlers( 606 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/services"), 607 ghttp.RespondWith(http.StatusInternalServerError, `Failed to set credentials`), 608 ), 609 ) 610 }) 611 612 It("should throw error when binding service to a cluster", func() { 613 target := ClusterTargetHeader{ 614 OrgID: "abc", 615 SpaceID: "def", 616 AccountID: "ghi", 617 } 618 params := ServiceBindRequest{ 619 ClusterNameOrID: "test", ServiceInstanceNameOrID: "cloudantDB", NamespaceID: "default"} 620 serviceResp, err := newCluster(server.URL()).BindService(params, target) 621 Expect(err).To(HaveOccurred()) 622 Expect(serviceResp.ServiceInstanceGUID).Should(Equal("")) 623 Expect(serviceResp.SecretName).Should(Equal("")) 624 }) 625 }) 626 }) 627 //Unbind service 628 Describe("UnBind service", func() { 629 Context("When bind service is successful", func() { 630 BeforeEach(func() { 631 server = ghttp.NewServer() 632 server.AppendHandlers( 633 ghttp.CombineHandlers( 634 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test/services/default/cloudantDB"), 635 ghttp.RespondWith(http.StatusOK, `{}`), 636 ), 637 ) 638 }) 639 640 It("should bind service to a cluster", func() { 641 target := ClusterTargetHeader{ 642 OrgID: "abc", 643 SpaceID: "def", 644 AccountID: "ghi", 645 } 646 err := newCluster(server.URL()).UnBindService("test", "default", "cloudantDB", target) 647 Expect(err).NotTo(HaveOccurred()) 648 649 }) 650 }) 651 Context("When unbind service is unsuccessful", func() { 652 BeforeEach(func() { 653 server = ghttp.NewServer() 654 server.SetAllowUnhandledRequests(true) 655 server.AppendHandlers( 656 ghttp.CombineHandlers( 657 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test/services/default/cloudantDB"), 658 ghttp.RespondWith(http.StatusInternalServerError, `Failed to unbind service`), 659 ), 660 ) 661 }) 662 663 It("should set credentials", func() { 664 target := ClusterTargetHeader{ 665 OrgID: "abc", 666 SpaceID: "def", 667 AccountID: "ghi", 668 } 669 err := newCluster(server.URL()).UnBindService("test", "default", "cloudantDB", target) 670 Expect(err).To(HaveOccurred()) 671 672 }) 673 }) 674 }) 675 //List bound services 676 Describe("ListServicesBoundToCluster", func() { 677 Context("When read of cluster services is successful", func() { 678 BeforeEach(func() { 679 server = ghttp.NewServer() 680 server.AppendHandlers( 681 ghttp.CombineHandlers( 682 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test/services/default"), 683 ghttp.RespondWith(http.StatusOK, `[{ 684 "ServiceName": "testService", 685 "ServiceID": "f91adfe2-76c9-4649-939e-b01c37a3704", 686 "ServiceKeyName": "kube-testService", 687 "Namespace": "default" 688 }]`), 689 ), 690 ) 691 }) 692 693 It("should return cluster service list", func() { 694 target := ClusterTargetHeader{ 695 OrgID: "abc", 696 SpaceID: "def", 697 AccountID: "ghi", 698 } 699 boundServices, err := newCluster(server.URL()).ListServicesBoundToCluster("test", "default", target) 700 Expect(boundServices).ShouldNot(BeNil()) 701 for _, service := range boundServices { 702 Expect(err).NotTo(HaveOccurred()) 703 Expect(service.ServiceName).Should(Equal("testService")) 704 Expect(service.ServiceID).Should(Equal("f91adfe2-76c9-4649-939e-b01c37a3704")) 705 } 706 }) 707 }) 708 Context("When read of cluster services is unsuccessful", func() { 709 BeforeEach(func() { 710 server = ghttp.NewServer() 711 server.SetAllowUnhandledRequests(true) 712 server.AppendHandlers( 713 ghttp.CombineHandlers( 714 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test/services/default"), 715 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve clusters`), 716 ), 717 ) 718 }) 719 720 It("should return error when cluster services are retrieved", func() { 721 target := ClusterTargetHeader{ 722 OrgID: "abc", 723 SpaceID: "def", 724 AccountID: "ghi", 725 } 726 service, err := newCluster(server.URL()).ListServicesBoundToCluster("test", "default", target) 727 Expect(err).To(HaveOccurred()) 728 Expect(service).Should(BeNil()) 729 }) 730 }) 731 }) 732 //Find Cluster service 733 Describe("FindServiceBoundToClusters", func() { 734 Context("When read a service bound to cluster is successful", func() { 735 BeforeEach(func() { 736 server = ghttp.NewServer() 737 server.AppendHandlers( 738 ghttp.CombineHandlers( 739 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test/services/default"), 740 ghttp.RespondWith(http.StatusOK, `[{ 741 "ServiceName": "testService", 742 "ServiceID": "f91adfe2-76c9-4649-939e-b01c37a3704", 743 "ServiceKeyName": "kube-testService", 744 "Namespace": "default" 745 }]`), 746 ), 747 ) 748 }) 749 750 It("should return cluster service list", func() { 751 target := ClusterTargetHeader{ 752 OrgID: "abc", 753 SpaceID: "def", 754 AccountID: "ghi", 755 } 756 boundService, err := newCluster(server.URL()).FindServiceBoundToCluster("test", "f91adfe2-76c9-4649-939e-b01c37a3704", "default", target) 757 Expect(boundService).ShouldNot(BeNil()) 758 Expect(err).NotTo(HaveOccurred()) 759 Expect(boundService.ServiceName).Should(Equal("testService")) 760 Expect(boundService.ServiceID).Should(Equal("f91adfe2-76c9-4649-939e-b01c37a3704")) 761 }) 762 }) 763 Context("When read of cluster services is unsuccessful", func() { 764 BeforeEach(func() { 765 server = ghttp.NewServer() 766 server.SetAllowUnhandledRequests(true) 767 server.AppendHandlers( 768 ghttp.CombineHandlers( 769 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/test/services/default"), 770 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve clusters`), 771 ), 772 ) 773 }) 774 775 It("should return error when cluster services are retrieved", func() { 776 target := ClusterTargetHeader{ 777 OrgID: "abc", 778 SpaceID: "def", 779 AccountID: "ghi", 780 } 781 _, err := newCluster(server.URL()).FindServiceBoundToCluster("test", "f91adfe2-76c9-4649-939e-b01c37a3704", "default", target) 782 Expect(err).To(HaveOccurred()) 783 }) 784 }) 785 }) 786 //UpdateClusterWorker 787 Describe("UpdateClusterWorker", func() { 788 Context("When updating cluster workers is successful", func() { 789 BeforeEach(func() { 790 server = ghttp.NewServer() 791 server.AppendHandlers( 792 ghttp.CombineHandlers( 793 ghttp.VerifyRequest(http.MethodPut, "/v1/clusters/test/workers/w1"), 794 ghttp.RespondWith(http.StatusNoContent, `{}`), 795 ), 796 ) 797 }) 798 799 It("should return cluster version updated", func() { 800 target := ClusterTargetHeader{ 801 OrgID: "abc", 802 SpaceID: "def", 803 AccountID: "ghi", 804 } 805 params := UpdateWorkerCommand{ 806 Action: "reload", 807 } 808 err := newCluster(server.URL()).UpdateClusterWorker("test", "w1", params, target) 809 Expect(err).NotTo(HaveOccurred()) 810 }) 811 }) 812 Context("When updating cluster workers is unsuccessful", func() { 813 BeforeEach(func() { 814 server = ghttp.NewServer() 815 server.SetAllowUnhandledRequests(true) 816 server.AppendHandlers( 817 ghttp.CombineHandlers( 818 ghttp.VerifyRequest(http.MethodPut, "/v1/clusters/test/workers/w1"), 819 ghttp.RespondWith(http.StatusInternalServerError, `Failed to update cluster workers`), 820 ), 821 ) 822 }) 823 824 It("should return error during updating cluster version", func() { 825 target := ClusterTargetHeader{ 826 OrgID: "abc", 827 SpaceID: "def", 828 AccountID: "ghi", 829 } 830 params := UpdateWorkerCommand{ 831 Action: "reload", 832 } 833 err := newCluster(server.URL()).UpdateClusterWorker("test", "w1", params, target) 834 Expect(err).To(HaveOccurred()) 835 }) 836 }) 837 }) 838 //Update 839 Describe("Update", func() { 840 Context("When updating cluster version is successful", func() { 841 BeforeEach(func() { 842 server = ghttp.NewServer() 843 server.AppendHandlers( 844 ghttp.CombineHandlers( 845 ghttp.VerifyRequest(http.MethodPut, "/v1/clusters/test"), 846 ghttp.RespondWith(http.StatusNoContent, `{}`), 847 ), 848 ) 849 }) 850 851 It("should return cluster version updated", func() { 852 target := ClusterTargetHeader{ 853 OrgID: "abc", 854 SpaceID: "def", 855 AccountID: "ghi", 856 } 857 params := ClusterUpdateParam{ 858 Action: "update", 859 Force: true, 860 Version: "1.8.6", 861 } 862 err := newCluster(server.URL()).Update("test", params, target) 863 Expect(err).NotTo(HaveOccurred()) 864 }) 865 }) 866 Context("When updating cluster version is unsuccessful", func() { 867 BeforeEach(func() { 868 server = ghttp.NewServer() 869 server.SetAllowUnhandledRequests(true) 870 server.AppendHandlers( 871 ghttp.CombineHandlers( 872 ghttp.VerifyRequest(http.MethodPut, "/v1/clusters/test"), 873 ghttp.RespondWith(http.StatusInternalServerError, `Failed to update cluster version`), 874 ), 875 ) 876 }) 877 878 It("should return error during updating cluster version", func() { 879 target := ClusterTargetHeader{ 880 OrgID: "abc", 881 SpaceID: "def", 882 AccountID: "ghi", 883 } 884 params := ClusterUpdateParam{ 885 Action: "update", 886 Force: true, 887 Version: "1.8.6", 888 } 889 err := newCluster(server.URL()).Update("test", params, target) 890 Expect(err).To(HaveOccurred()) 891 }) 892 }) 893 }) 894 // 895 }) 896 897 func newCluster(url string) Clusters { 898 899 sess, err := session.New() 900 if err != nil { 901 log.Fatal(err) 902 } 903 conf := sess.Config.Copy() 904 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 905 conf.Endpoint = &url 906 907 client := client.Client{ 908 Config: conf, 909 ServiceName: bluemix.MccpService, 910 } 911 return newClusterAPI(&client) 912 }