github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv1/worker_pool_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/ginkgo" 13 . "github.com/onsi/gomega" 14 "github.com/onsi/gomega/ghttp" 15 ) 16 17 var _ = Describe("WorkerPool", func() { 18 var server *ghttp.Server 19 Describe("CreateWorkerPool", func() { 20 Context("When creating a worker pool is successful", func() { 21 BeforeEach(func() { 22 server = ghttp.NewServer() 23 server.AppendHandlers( 24 ghttp.CombineHandlers( 25 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/workerpools"), 26 ghttp.RespondWith(http.StatusCreated, `{"Name":"testpool","Size":5,"MachineType": "u2c.2x4","Isolation": "public","ID":"rtr4tg5", "Region":"us-south", "State":"normal", "ReasonForDelete":"","IsBalanced":true,"Entitlement":""}`), 27 ), 28 ) 29 }) 30 31 It("should return worker pools added to cluster", func() { 32 workerPoolProperties := WorkerPoolRequest{ 33 WorkerPoolConfig: WorkerPoolConfig{ 34 Name: "test-pool", 35 Size: 5, 36 MachineType: "u2c.2x4", 37 Isolation: "public", 38 Entitlement: "", 39 }, 40 DiskEncryption: true, 41 } 42 target := ClusterTargetHeader{ 43 OrgID: "abc", 44 SpaceID: "def", 45 AccountID: "ghi", 46 Region: "eu-de", 47 } 48 49 _, err := newWorkerPool(server.URL()).CreateWorkerPool("test", workerPoolProperties, target) 50 Expect(err).NotTo(HaveOccurred()) 51 }) 52 }) 53 Context("When creating a worker pool with OS is successful", func() { 54 BeforeEach(func() { 55 server = ghttp.NewServer() 56 server.AppendHandlers( 57 ghttp.CombineHandlers( 58 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/workerpools"), 59 ghttp.RespondWith(http.StatusCreated, `{"Name":"testpool","Size":5,"MachineType": "u2c.2x4","Isolation": "public","ID":"rtr4tg5", "Region":"us-south", "State":"normal", "ReasonForDelete":"","IsBalanced":true,"Entitlement":"", "operatingSystem":"REDHAT_7_64"}`), 60 ), 61 ) 62 }) 63 64 It("should return worker pools added to cluster", func() { 65 workerPoolProperties := WorkerPoolRequest{ 66 WorkerPoolConfig: WorkerPoolConfig{ 67 Name: "test-pool", 68 Size: 5, 69 MachineType: "u2c.2x4", 70 Isolation: "public", 71 Entitlement: "", 72 OperatingSystem: "REDHAT_7_64", 73 }, 74 DiskEncryption: true, 75 } 76 target := ClusterTargetHeader{ 77 OrgID: "abc", 78 SpaceID: "def", 79 AccountID: "ghi", 80 Region: "eu-de", 81 } 82 83 wp, err := newWorkerPool(server.URL()).CreateWorkerPool("test", workerPoolProperties, target) 84 Expect(err).NotTo(HaveOccurred()) 85 Expect(wp.OperatingSystem).Should(Equal("REDHAT_7_64")) 86 }) 87 }) 88 Context("When creating worker pool is unsuccessful", func() { 89 BeforeEach(func() { 90 91 server = ghttp.NewServer() 92 server.SetAllowUnhandledRequests(true) 93 server.AppendHandlers( 94 ghttp.CombineHandlers( 95 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/workerpools"), 96 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create worker pools to cluster`), 97 ), 98 ) 99 }) 100 101 It("should return worker pools added to cluster", func() { 102 workerPoolProperties := WorkerPoolRequest{ 103 WorkerPoolConfig: WorkerPoolConfig{ 104 Name: "test-pool", 105 Size: 5, 106 MachineType: "u2c.2x4", 107 Isolation: "public", 108 Entitlement: "", 109 }, 110 DiskEncryption: true, 111 } 112 target := ClusterTargetHeader{ 113 OrgID: "abc", 114 SpaceID: "def", 115 AccountID: "ghi", 116 Region: "eu-de", 117 } 118 _, err := newWorkerPool(server.URL()).CreateWorkerPool("test", workerPoolProperties, target) 119 Expect(err).To(HaveOccurred()) 120 }) 121 }) 122 }) 123 //List 124 Describe("List", func() { 125 Context("When retrieving available worker pools of a cluster is successful", func() { 126 BeforeEach(func() { 127 server = ghttp.NewServer() 128 server.AppendHandlers( 129 ghttp.CombineHandlers( 130 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workerpools"), 131 ghttp.RespondWith(http.StatusOK, `[{"Name":"testpool","Size":5,"MachineType": "u2c.2x4","Isolation": "public","ID":"rtr4tg5", "Region":"us-south", "State":"normal","ReasonForDelete":"","IsBalanced":true,"Entitlement":""}]`), 132 ), 133 ) 134 }) 135 136 It("should return available worker pools ", func() { 137 target := ClusterTargetHeader{ 138 OrgID: "abc", 139 SpaceID: "def", 140 AccountID: "ghi", 141 Region: "eu-de", 142 } 143 144 worker, err := newWorkerPool(server.URL()).ListWorkerPools("myCluster", target) 145 Expect(err).NotTo(HaveOccurred()) 146 Expect(worker).ShouldNot(BeNil()) 147 for _, wObj := range worker { 148 Expect(wObj).ShouldNot(BeNil()) 149 Expect(wObj.State).Should(Equal("normal")) 150 } 151 }) 152 }) 153 Context("When retrieving available worker pools is unsuccessful", func() { 154 BeforeEach(func() { 155 server = ghttp.NewServer() 156 server.SetAllowUnhandledRequests(true) 157 server.AppendHandlers( 158 ghttp.CombineHandlers( 159 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workerpools"), 160 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve workerpools`), 161 ), 162 ) 163 }) 164 165 It("should return error during retrieveing worker pools", func() { 166 target := ClusterTargetHeader{ 167 OrgID: "abc", 168 SpaceID: "def", 169 AccountID: "ghi", 170 Region: "eu-de", 171 } 172 173 _, err := newWorkerPool(server.URL()).ListWorkerPools("myCluster", target) 174 Expect(err).To(HaveOccurred()) 175 }) 176 }) 177 }) 178 //Get 179 Describe("Get", func() { 180 Context("When retrieving worker pool of a cluster is successful", func() { 181 BeforeEach(func() { 182 server = ghttp.NewServer() 183 server.AppendHandlers( 184 ghttp.CombineHandlers( 185 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workerpools/abc-123-def"), 186 ghttp.RespondWith(http.StatusOK, `{"Name":"testpool","Size":5,"MachineType": "u2c.2x4","Isolation": "public","ID":"rtr4tg5", "Region":"us-south", "State":"normal", "ReasonForDelete":"","IsBalanced":true,"Entitlement":"", "operatingSystem":"REDHAT_7_64"}`), 187 ), 188 ) 189 }) 190 191 It("should return worker pool", func() { 192 target := ClusterTargetHeader{ 193 OrgID: "abc", 194 SpaceID: "def", 195 AccountID: "ghi", 196 Region: "eu-de", 197 } 198 199 wp, err := newWorkerPool(server.URL()).GetWorkerPool("myCluster", "abc-123-def", target) 200 Expect(err).NotTo(HaveOccurred()) 201 Expect(wp.OperatingSystem).Should(Equal("REDHAT_7_64")) 202 Expect(wp.AutoscaleEnabled).Should(BeFalse()) 203 }) 204 }) 205 Context("When retrieving a worker pool with AutoscaleEnabled is successful", func() { 206 BeforeEach(func() { 207 server = ghttp.NewServer() 208 server.AppendHandlers( 209 ghttp.CombineHandlers( 210 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workerpools/abc-123-def"), 211 ghttp.RespondWith(http.StatusOK, `{ 212 "Name":"testpool", 213 "Size":5, 214 "MachineType": "u2c.2x4", 215 "Isolation": "public", 216 "ID":"rtr4tg5", 217 "Region":"us-south", 218 "State":"normal", 219 "ReasonForDelete":"", 220 "IsBalanced":true, 221 "Entitlement":"", 222 "operatingSystem":"REDHAT_7_64", 223 "autoscaleEnabled" : true 224 }`), 225 ), 226 ) 227 }) 228 229 It("should return worker pool", func() { 230 target := ClusterTargetHeader{ 231 OrgID: "abc", 232 SpaceID: "def", 233 AccountID: "ghi", 234 Region: "eu-de", 235 } 236 237 wp, err := newWorkerPool(server.URL()).GetWorkerPool("myCluster", "abc-123-def", target) 238 Expect(err).NotTo(HaveOccurred()) 239 Expect(wp.AutoscaleEnabled).Should(BeTrue()) 240 }) 241 }) 242 Context("When retrieving worker pool is unsuccessful", func() { 243 BeforeEach(func() { 244 server = ghttp.NewServer() 245 server.SetAllowUnhandledRequests(true) 246 server.AppendHandlers( 247 ghttp.CombineHandlers( 248 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workerpools/abc-123-def"), 249 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve workerpool`), 250 ), 251 ) 252 }) 253 254 It("should return error during retrieveing worker pool", func() { 255 target := ClusterTargetHeader{ 256 OrgID: "abc", 257 SpaceID: "def", 258 AccountID: "ghi", 259 Region: "eu-de", 260 } 261 262 _, err := newWorkerPool(server.URL()).GetWorkerPool("myCluster", "abc-123-def", target) 263 Expect(err).To(HaveOccurred()) 264 }) 265 }) 266 }) 267 //Delete 268 Describe("Delete", func() { 269 Context("When delete of worker pool is successful", func() { 270 BeforeEach(func() { 271 server = ghttp.NewServer() 272 server.AppendHandlers( 273 ghttp.CombineHandlers( 274 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test/workerpools/abc-123-def-ghi"), 275 ghttp.RespondWith(http.StatusOK, `{ 276 }`), 277 ), 278 ) 279 }) 280 281 It("should delete worker pool", func() { 282 target := ClusterTargetHeader{ 283 OrgID: "abc", 284 SpaceID: "def", 285 AccountID: "ghi", 286 Region: "eu-de", 287 } 288 289 err := newWorkerPool(server.URL()).DeleteWorkerPool("test", "abc-123-def-ghi", target) 290 Expect(err).NotTo(HaveOccurred()) 291 }) 292 }) 293 Context("When worker pool delete is failed", func() { 294 BeforeEach(func() { 295 server = ghttp.NewServer() 296 server.SetAllowUnhandledRequests(true) 297 server.AppendHandlers( 298 ghttp.CombineHandlers( 299 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test/workerpools/abc-123-def-ghi"), 300 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete worker pool`), 301 ), 302 ) 303 }) 304 305 It("should return error deleting worker pool", func() { 306 target := ClusterTargetHeader{ 307 OrgID: "abc", 308 SpaceID: "def", 309 AccountID: "ghi", 310 Region: "eu-de", 311 } 312 313 err := newWorkerPool(server.URL()).DeleteWorkerPool("test", "abc-123-def-ghi", target) 314 Expect(err).To(HaveOccurred()) 315 }) 316 }) 317 }) 318 //Patch worker pool 319 Describe("Patch", func() { 320 Context("When resize of worker pool is successful", func() { 321 BeforeEach(func() { 322 server = ghttp.NewServer() 323 server.AppendHandlers( 324 ghttp.CombineHandlers( 325 ghttp.VerifyRequest(http.MethodPatch, "/v1/clusters/test/workerpools/abc-123-def-ghi"), 326 ghttp.RespondWith(http.StatusOK, `{ 327 }`), 328 ), 329 ) 330 }) 331 332 It("should increase the size of worker pool", func() { 333 target := ClusterTargetHeader{ 334 OrgID: "abc", 335 SpaceID: "def", 336 AccountID: "ghi", 337 Region: "eu-de", 338 } 339 340 err := newWorkerPool(server.URL()).ResizeWorkerPool("test", "abc-123-def-ghi", 6, target) 341 Expect(err).NotTo(HaveOccurred()) 342 }) 343 }) 344 Context("When resize of worker pool is failed", func() { 345 BeforeEach(func() { 346 server = ghttp.NewServer() 347 server.SetAllowUnhandledRequests(true) 348 server.AppendHandlers( 349 ghttp.CombineHandlers( 350 ghttp.VerifyRequest(http.MethodPatch, "/v1/clusters/test/workerpools/abc-123-def-ghi"), 351 ghttp.RespondWith(http.StatusInternalServerError, `Failed to resize worker pool`), 352 ), 353 ) 354 }) 355 356 It("should return error resizing worker pool", func() { 357 target := ClusterTargetHeader{ 358 OrgID: "abc", 359 SpaceID: "def", 360 AccountID: "ghi", 361 Region: "eu-de", 362 } 363 364 err := newWorkerPool(server.URL()).ResizeWorkerPool("test", "abc-123-def-ghi", 6, target) 365 Expect(err).To(HaveOccurred()) 366 }) 367 }) 368 }) 369 //Add zone 370 Describe("Post", func() { 371 Context("When adding a zone is successful", func() { 372 BeforeEach(func() { 373 server = ghttp.NewServer() 374 server.AppendHandlers( 375 ghttp.CombineHandlers( 376 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/workerpools/abc-123-def-ghi/zones"), 377 ghttp.RespondWith(http.StatusOK, `{ 378 }`), 379 ), 380 ) 381 }) 382 383 It("should add zone to the specified worker pool", func() { 384 workerPoolZone := WorkerPoolZone{ 385 ID: "abc-123-def-ghi", 386 WorkerPoolZoneNetwork: WorkerPoolZoneNetwork{ 387 PrivateVLAN: "12345", 388 PublicVLAN: "43215", 389 }, 390 } 391 target := ClusterTargetHeader{ 392 OrgID: "abc", 393 SpaceID: "def", 394 AccountID: "ghi", 395 Region: "eu-de", 396 } 397 398 err := newWorkerPool(server.URL()).AddZone("test", "abc-123-def-ghi", workerPoolZone, target) 399 Expect(err).NotTo(HaveOccurred()) 400 }) 401 }) 402 Context("When adding zone to worker pool is failed", func() { 403 BeforeEach(func() { 404 server = ghttp.NewServer() 405 server.SetAllowUnhandledRequests(true) 406 server.AppendHandlers( 407 ghttp.CombineHandlers( 408 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/workerpools/abc-123-def-ghi/zones"), 409 ghttp.RespondWith(http.StatusInternalServerError, `Failed to add zone to worker pool`), 410 ), 411 ) 412 }) 413 414 It("should return error adding zone to worker pool", func() { 415 416 workerPoolZone := WorkerPoolZone{ 417 ID: "abc-123-def-ghi", 418 WorkerPoolZoneNetwork: WorkerPoolZoneNetwork{ 419 PrivateVLAN: "12345", 420 PublicVLAN: "43215", 421 }, 422 } 423 target := ClusterTargetHeader{ 424 OrgID: "abc", 425 SpaceID: "def", 426 AccountID: "ghi", 427 Region: "eu-de", 428 } 429 430 err := newWorkerPool(server.URL()).AddZone("test", "abc-123-def-ghi", workerPoolZone, target) 431 Expect(err).To(HaveOccurred()) 432 }) 433 }) 434 }) 435 //Remove zone 436 Describe("Delete", func() { 437 Context("When delete of zone of a worker pool is successful", func() { 438 BeforeEach(func() { 439 server = ghttp.NewServer() 440 server.AppendHandlers( 441 ghttp.CombineHandlers( 442 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test/workerpools/abc-123-def-ghi/zones/dal10"), 443 ghttp.RespondWith(http.StatusOK, `{ 444 }`), 445 ), 446 ) 447 }) 448 449 It("should delete worker pool in that zone", func() { 450 target := ClusterTargetHeader{ 451 OrgID: "abc", 452 SpaceID: "def", 453 AccountID: "ghi", 454 Region: "eu-de", 455 } 456 457 err := newWorkerPool(server.URL()).RemoveZone("test", "dal10", "abc-123-def-ghi", target) 458 Expect(err).NotTo(HaveOccurred()) 459 }) 460 }) 461 Context("When delete of zone of a worker pool delete is failed", func() { 462 BeforeEach(func() { 463 server = ghttp.NewServer() 464 server.SetAllowUnhandledRequests(true) 465 server.AppendHandlers( 466 ghttp.CombineHandlers( 467 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test/workerpools/abc-123-def-ghi/zones/dal10"), 468 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete zone of a worker pool`), 469 ), 470 ) 471 }) 472 473 It("should return error deleting worker pool in the specific zone", func() { 474 target := ClusterTargetHeader{ 475 OrgID: "abc", 476 SpaceID: "def", 477 AccountID: "ghi", 478 Region: "eu-de", 479 } 480 481 err := newWorkerPool(server.URL()).RemoveZone("test", "dal10", "abc-123-def-ghi", target) 482 Expect(err).To(HaveOccurred()) 483 }) 484 }) 485 }) 486 //UpdateZoneNetwork 487 Describe("Patch", func() { 488 Context("When update of worker pool zone is successful", func() { 489 BeforeEach(func() { 490 server = ghttp.NewServer() 491 server.AppendHandlers( 492 ghttp.CombineHandlers( 493 ghttp.VerifyRequest(http.MethodPatch, "/v1/clusters/test/workerpools/abc-123-def-ghi/zones/dal10"), 494 ghttp.RespondWith(http.StatusOK, `{ 495 }`), 496 ), 497 ) 498 }) 499 500 It("should update worker pool zone", func() { 501 target := ClusterTargetHeader{ 502 OrgID: "abc", 503 SpaceID: "def", 504 AccountID: "ghi", 505 Region: "eu-de", 506 } 507 508 err := newWorkerPool(server.URL()).UpdateZoneNetwork("test", "dal10", "abc-123-def-ghi", "12345", "43215", target) 509 Expect(err).NotTo(HaveOccurred()) 510 }) 511 }) 512 Context("When update of worker pool zone is failed", func() { 513 BeforeEach(func() { 514 server = ghttp.NewServer() 515 server.SetAllowUnhandledRequests(true) 516 server.AppendHandlers( 517 ghttp.CombineHandlers( 518 ghttp.VerifyRequest(http.MethodPatch, "/v1/clusters/test/workerpools/abc-123-def-ghi/zones/dal10"), 519 ghttp.RespondWith(http.StatusInternalServerError, `Failed to update worker pool zone`), 520 ), 521 ) 522 }) 523 524 It("should return updating worker pool zone", func() { 525 target := ClusterTargetHeader{ 526 OrgID: "abc", 527 SpaceID: "def", 528 AccountID: "ghi", 529 Region: "eu-de", 530 } 531 532 err := newWorkerPool(server.URL()).UpdateZoneNetwork("test", "dal10", "abc-123-def-ghi", "12345", "43215", target) 533 Expect(err).To(HaveOccurred()) 534 }) 535 }) 536 }) 537 }) 538 539 func newWorkerPool(url string) WorkerPool { 540 541 sess, err := session.New() 542 if err != nil { 543 log.Fatal(err) 544 } 545 conf := sess.Config.Copy() 546 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 547 conf.Endpoint = &url 548 549 client := client.Client{ 550 Config: conf, 551 ServiceName: bluemix.MccpService, 552 } 553 return newWorkerPoolAPI(&client) 554 }