github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/dedicated_host_test.go (about) 1 package containerv2 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 "github.com/onsi/gomega/ghttp" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("dedicatedhosts", func() { 18 var server *ghttp.Server 19 AfterEach(func() { 20 server.Close() 21 }) 22 23 Describe("Create", func() { 24 Context("When creating dedicatedhost is successful", func() { 25 BeforeEach(func() { 26 server = ghttp.NewServer() 27 server.AppendHandlers( 28 ghttp.CombineHandlers( 29 ghttp.VerifyRequest(http.MethodPost, "/v2/createDedicatedHost"), 30 ghttp.VerifyJSON(`{ 31 "flavor": "flavor1", 32 "hostPoolID": "hostpoolid1", 33 "zone": "zone1" 34 }`), 35 ghttp.RespondWith(http.StatusCreated, `{ 36 "id":"dedicatedhostid1" 37 }`), 38 ), 39 ) 40 }) 41 42 It("should create Workerpool in a cluster", func() { 43 target := ClusterTargetHeader{} 44 params := CreateDedicatedHostRequest{ 45 Flavor: "flavor1", 46 HostPoolID: "hostpoolid1", 47 Zone: "zone1", 48 } 49 dh, err := newDedicatedHost(server.URL()).CreateDedicatedHost(params, target) 50 Expect(err).NotTo(HaveOccurred()) 51 Expect(dh.ID).Should(BeEquivalentTo("dedicatedhostid1")) 52 }) 53 }) 54 Context("When creating dedicatedhost is unsuccessful", func() { 55 BeforeEach(func() { 56 server = ghttp.NewServer() 57 server.SetAllowUnhandledRequests(true) 58 server.AppendHandlers( 59 ghttp.CombineHandlers( 60 ghttp.VerifyRequest(http.MethodPost, "/v2/createDedicatedHost"), 61 ghttp.VerifyJSON(`{ 62 "flavor": "flavor1", 63 "hostPoolID": "hostpoolid1", 64 "zone": "zone1" 65 }`), 66 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create dedicatedhost`), 67 ), 68 ) 69 }) 70 71 It("should return error during creating dedicatedhost", func() { 72 params := CreateDedicatedHostRequest{ 73 Flavor: "flavor1", 74 HostPoolID: "hostpoolid1", 75 Zone: "zone1", 76 } 77 target := ClusterTargetHeader{} 78 _, err := newDedicatedHost(server.URL()).CreateDedicatedHost(params, target) 79 Expect(err).To(HaveOccurred()) 80 }) 81 }) 82 }) 83 84 Describe("Get", func() { 85 Context("When Get dedicatedhost is successful", func() { 86 BeforeEach(func() { 87 server = ghttp.NewServer() 88 server.AppendHandlers( 89 ghttp.CombineHandlers( 90 ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHost"), 91 ghttp.RespondWith(http.StatusCreated, `{ 92 "flavor": "flavor1", 93 "id": "dedicatedhostid1", 94 "lifecycle": { 95 "actualState": "actualstate1", 96 "desiredState": "desiredstate1" 97 }, 98 "placementEnabled": true, 99 "resources": { 100 "capacity": { 101 "memoryBytes": 12000, 102 "vcpu": 2 103 }, 104 "consumed": { 105 "memoryBytes": 1, 106 "vcpu": 1 107 } 108 }, 109 "workers": [ 110 { 111 "clusterID": "clusterid1", 112 "flavor": "flavor2", 113 "workerID": "workerid1", 114 "workerPoolID": "workerpoolid1" 115 } 116 ], 117 "zone": "zone1" 118 }`), 119 ), 120 ) 121 }) 122 123 It("should get Workerpool in a cluster", func() { 124 target := ClusterTargetHeader{} 125 dh, err := newDedicatedHost(server.URL()).GetDedicatedHost("dedicatedhostid1", "dedicatedhostpoolid1", target) 126 Expect(err).NotTo(HaveOccurred()) 127 expectedDedicatedHost := GetDedicatedHostResponse{ 128 Flavor: "flavor1", 129 ID: "dedicatedhostid1", 130 Lifecycle: DedicatedHostLifecycle{ 131 ActualState: "actualstate1", 132 DesiredState: "desiredstate1", 133 }, 134 PlacementEnabled: true, 135 Resources: DedicatedHostResources{ 136 Capacity: DedicatedHostResource{ 137 MemoryBytes: 12000, 138 VCPU: 2, 139 }, 140 Consumed: DedicatedHostResource{ 141 MemoryBytes: 1, 142 VCPU: 1, 143 }, 144 }, 145 Workers: []DedicatedHostWorker{ 146 DedicatedHostWorker{ 147 ClusterID: "clusterid1", 148 Flavor: "flavor2", 149 WorkerID: "workerid1", 150 WorkerPoolID: "workerpoolid1", 151 }, 152 }, 153 Zone: "zone1", 154 } 155 Expect(dh).Should(BeEquivalentTo(expectedDedicatedHost)) 156 }) 157 }) 158 Context("When get dedicatedhost is unsuccessful", func() { 159 BeforeEach(func() { 160 server = ghttp.NewServer() 161 server.SetAllowUnhandledRequests(true) 162 server.AppendHandlers( 163 ghttp.CombineHandlers( 164 ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHost"), 165 ghttp.RespondWith(http.StatusInternalServerError, `Failed to get dedicatedhost`), 166 ), 167 ) 168 }) 169 170 It("should return error during get dedicatedhost", func() { 171 target := ClusterTargetHeader{} 172 _, err := newDedicatedHost(server.URL()).GetDedicatedHost("dedicatedhostid1", "dedicatedhostpoolid1", target) 173 Expect(err).To(HaveOccurred()) 174 }) 175 }) 176 }) 177 178 Describe("List", func() { 179 Context("When list dedicatedhost is successful", func() { 180 BeforeEach(func() { 181 server = ghttp.NewServer() 182 server.AppendHandlers( 183 ghttp.CombineHandlers( 184 ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHosts"), 185 ghttp.RespondWith(http.StatusCreated, `[ 186 { 187 "flavor": "flavor1", 188 "id": "dedicatedhostid1", 189 "lifecycle": { 190 "actualState": "actualstate1", 191 "desiredState": "desiredstate1" 192 }, 193 "placementEnabled": true, 194 "resources": { 195 "capacity": { 196 "memoryBytes": 12000, 197 "vcpu": 2 198 }, 199 "consumed": { 200 "memoryBytes": 1, 201 "vcpu": 1 202 } 203 }, 204 "workers": [ 205 { 206 "clusterID": "clusterid1", 207 "flavor": "flavor2", 208 "workerID": "workerid1", 209 "workerPoolID": "workerpoolid1" 210 } 211 ], 212 "zone": "zone1" 213 } 214 ]`), 215 ), 216 ) 217 }) 218 219 It("should list dedicatedhosts in a cluster", func() { 220 target := ClusterTargetHeader{} 221 222 ldh, err := newDedicatedHost(server.URL()).ListDedicatedHosts("dedicatedhostpoolid1", target) 223 Expect(err).NotTo(HaveOccurred()) 224 expectedDedicatedHosts := []GetDedicatedHostResponse{GetDedicatedHostResponse{ 225 Flavor: "flavor1", 226 ID: "dedicatedhostid1", 227 Lifecycle: DedicatedHostLifecycle{ 228 ActualState: "actualstate1", 229 DesiredState: "desiredstate1", 230 }, 231 PlacementEnabled: true, 232 Resources: DedicatedHostResources{ 233 Capacity: DedicatedHostResource{ 234 MemoryBytes: 12000, 235 VCPU: 2, 236 }, 237 Consumed: DedicatedHostResource{ 238 MemoryBytes: 1, 239 VCPU: 1, 240 }, 241 }, 242 Workers: []DedicatedHostWorker{ 243 DedicatedHostWorker{ 244 ClusterID: "clusterid1", 245 Flavor: "flavor2", 246 WorkerID: "workerid1", 247 WorkerPoolID: "workerpoolid1", 248 }, 249 }, 250 Zone: "zone1", 251 }} 252 Expect(ldh).To(BeEquivalentTo(expectedDedicatedHosts)) 253 }) 254 }) 255 Context("When list dedicatedhost is unsuccessful", func() { 256 BeforeEach(func() { 257 server = ghttp.NewServer() 258 server.SetAllowUnhandledRequests(true) 259 server.AppendHandlers( 260 ghttp.CombineHandlers( 261 ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHosts"), 262 ghttp.RespondWith(http.StatusInternalServerError, `Failed to list dedicatedhost`), 263 ), 264 ) 265 }) 266 267 It("should return error during get dedicatedhosts", func() { 268 target := ClusterTargetHeader{} 269 _, err := newDedicatedHost(server.URL()).ListDedicatedHosts("dedicatedhostpoolid1", target) 270 Expect(err).To(HaveOccurred()) 271 }) 272 }) 273 }) 274 275 Describe("Remove", func() { 276 Context("When removing dedicatedhost is successful", func() { 277 BeforeEach(func() { 278 server = ghttp.NewServer() 279 server.AppendHandlers( 280 ghttp.CombineHandlers( 281 ghttp.VerifyRequest(http.MethodPost, "/v2/removeDedicatedHost"), 282 ghttp.VerifyJSON(`{ 283 "host": "host1", 284 "hostPool": "hostpoolid1" 285 }`), 286 ghttp.RespondWith(http.StatusCreated, nil), 287 ), 288 ) 289 }) 290 291 It("should remove dedicatedhost", func() { 292 target := ClusterTargetHeader{} 293 params := RemoveDedicatedHostRequest{ 294 HostID: "host1", 295 HostPoolID: "hostpoolid1", 296 } 297 err := newDedicatedHost(server.URL()).RemoveDedicatedHost(params, target) 298 Expect(err).NotTo(HaveOccurred()) 299 }) 300 }) 301 Context("When removing dedicatedhost is unsuccessful", func() { 302 BeforeEach(func() { 303 server = ghttp.NewServer() 304 server.SetAllowUnhandledRequests(true) 305 server.AppendHandlers( 306 ghttp.CombineHandlers( 307 ghttp.VerifyRequest(http.MethodPost, "/v2/removeDedicatedHost"), 308 ghttp.VerifyJSON(`{ 309 "host": "host1", 310 "hostPool": "hostpoolid1" 311 }`), 312 ghttp.RespondWith(http.StatusInternalServerError, `Failed to remove dedicatedhost`), 313 ), 314 ) 315 }) 316 317 It("should return error during creating dedicatedhost", func() { 318 target := ClusterTargetHeader{} 319 params := RemoveDedicatedHostRequest{ 320 HostID: "host1", 321 HostPoolID: "hostpoolid1", 322 } 323 err := newDedicatedHost(server.URL()).RemoveDedicatedHost(params, target) 324 Expect(err).To(HaveOccurred()) 325 }) 326 }) 327 }) 328 329 Describe("Update", func() { 330 Context("When enabling placement on a dedicatedhost is successful", func() { 331 BeforeEach(func() { 332 server = ghttp.NewServer() 333 server.AppendHandlers( 334 ghttp.CombineHandlers( 335 ghttp.VerifyRequest(http.MethodPost, "/v2/enableDedicatedHostPlacement"), 336 ghttp.VerifyJSON(`{ 337 "hostID": "host1", 338 "hostPoolID": "hostpoolid1" 339 }`), 340 ghttp.RespondWith(http.StatusCreated, nil), 341 ), 342 ) 343 }) 344 345 It("should enable dedicatedhost placement", func() { 346 target := ClusterTargetHeader{} 347 params := UpdateDedicatedHostPlacementRequest{ 348 HostID: "host1", 349 HostPoolID: "hostpoolid1", 350 } 351 err := newDedicatedHost(server.URL()).EnableDedicatedHostPlacement(params, target) 352 Expect(err).NotTo(HaveOccurred()) 353 }) 354 }) 355 Context("When disabling placement on a dedicatedhost is successful", func() { 356 BeforeEach(func() { 357 server = ghttp.NewServer() 358 server.AppendHandlers( 359 ghttp.CombineHandlers( 360 ghttp.VerifyRequest(http.MethodPost, "/v2/disableDedicatedHostPlacement"), 361 ghttp.VerifyJSON(`{ 362 "hostID": "host1", 363 "hostPoolID": "hostpoolid1" 364 }`), 365 ghttp.RespondWith(http.StatusCreated, nil), 366 ), 367 ) 368 }) 369 370 It("should disable dedicatedhost placement", func() { 371 target := ClusterTargetHeader{} 372 params := UpdateDedicatedHostPlacementRequest{ 373 HostID: "host1", 374 HostPoolID: "hostpoolid1", 375 } 376 err := newDedicatedHost(server.URL()).DisableDedicatedHostPlacement(params, target) 377 Expect(err).NotTo(HaveOccurred()) 378 }) 379 }) 380 Context("When enabling placement on a dedicatedhost is unsuccessful", func() { 381 BeforeEach(func() { 382 server = ghttp.NewServer() 383 server.AppendHandlers( 384 ghttp.CombineHandlers( 385 ghttp.VerifyRequest(http.MethodPost, "/v2/enableDedicatedHostPlacement"), 386 ghttp.VerifyJSON(`{ 387 "hostID": "host1", 388 "hostPoolID": "hostpoolid1" 389 }`), 390 ghttp.RespondWith(http.StatusInternalServerError, `Bang`), 391 ), 392 ) 393 }) 394 395 It("should enable dedicatedhost placement", func() { 396 target := ClusterTargetHeader{} 397 params := UpdateDedicatedHostPlacementRequest{ 398 HostID: "host1", 399 HostPoolID: "hostpoolid1", 400 } 401 err := newDedicatedHost(server.URL()).EnableDedicatedHostPlacement(params, target) 402 Expect(err).To(HaveOccurred()) 403 }) 404 }) 405 Context("When disabling placement on a dedicatedhost is unsuccessful", func() { 406 BeforeEach(func() { 407 server = ghttp.NewServer() 408 server.AppendHandlers( 409 ghttp.CombineHandlers( 410 ghttp.VerifyRequest(http.MethodPost, "/v2/disableDedicatedHostPlacement"), 411 ghttp.VerifyJSON(`{ 412 "hostID": "host1", 413 "hostPoolID": "hostpoolid1" 414 }`), 415 ghttp.RespondWith(http.StatusInternalServerError, `Bang`), 416 ), 417 ) 418 }) 419 420 It("should disable dedicatedhost placement", func() { 421 target := ClusterTargetHeader{} 422 params := UpdateDedicatedHostPlacementRequest{ 423 HostID: "host1", 424 HostPoolID: "hostpoolid1", 425 } 426 err := newDedicatedHost(server.URL()).DisableDedicatedHostPlacement(params, target) 427 Expect(err).To(HaveOccurred()) 428 }) 429 }) 430 }) 431 }) 432 433 func newDedicatedHost(url string) DedicatedHost { 434 435 sess, err := session.New() 436 if err != nil { 437 log.Fatal(err) 438 } 439 conf := sess.Config.Copy() 440 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 441 conf.Endpoint = &url 442 443 client := client.Client{ 444 Config: conf, 445 ServiceName: bluemix.VpcContainerService, 446 } 447 return newDedicatedHostAPI(&client) 448 }