github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/dedicated_host_pool_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("dedicatedhostpools", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  
    23  	Describe("Create", func() {
    24  		Context("When creating dedicatedhostpool is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodPost, "/v2/createDedicatedHostPool"),
    30  						ghttp.VerifyJSON(`{
    31  							"flavorClass": "flavorclass1",
    32  							"metro": "metro1",
    33  							"name": "name1"
    34  						  }`),
    35  						ghttp.RespondWith(http.StatusCreated, `{
    36  							"id":"dedicatedhostpoolid1"
    37  						}`),
    38  					),
    39  				)
    40  			})
    41  
    42  			It("should create Workerpool in a cluster", func() {
    43  				target := ClusterTargetHeader{}
    44  				params := CreateDedicatedHostPoolRequest{
    45  					FlavorClass: "flavorclass1",
    46  					Metro:       "metro1",
    47  					Name:        "name1",
    48  				}
    49  				dh, err := newDedicatedHostPool(server.URL()).CreateDedicatedHostPool(params, target)
    50  				Expect(err).NotTo(HaveOccurred())
    51  				Expect(dh.ID).Should(BeEquivalentTo("dedicatedhostpoolid1"))
    52  			})
    53  		})
    54  		Context("When creating dedicatedhostpool 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/createDedicatedHostPool"),
    61  						ghttp.VerifyJSON(`{
    62  							"flavorClass": "flavorclass1",
    63  							"metro": "metro1",
    64  							"name": "name1"
    65  						  }`),
    66  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create dedicatedhostpool`),
    67  					),
    68  				)
    69  			})
    70  
    71  			It("should return error during creating dedicatedhostpool", func() {
    72  				params := CreateDedicatedHostPoolRequest{
    73  					FlavorClass: "flavorclass1",
    74  					Metro:       "metro1",
    75  					Name:        "name1",
    76  				}
    77  				target := ClusterTargetHeader{}
    78  				_, err := newDedicatedHostPool(server.URL()).CreateDedicatedHostPool(params, target)
    79  				Expect(err).To(HaveOccurred())
    80  			})
    81  		})
    82  	})
    83  
    84  	Describe("Get", func() {
    85  		Context("When Get dedicatedhostpool is successful", func() {
    86  			BeforeEach(func() {
    87  				server = ghttp.NewServer()
    88  				server.AppendHandlers(
    89  					ghttp.CombineHandlers(
    90  						ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHostPool"),
    91  						ghttp.RespondWith(http.StatusCreated, `{
    92  							"flavorClass": "flavorclass1",
    93  							"hostCount": 3,
    94  							"id": "dedicatedhostpool1",
    95  							"metro": "metro1",
    96  							"name": "name1",
    97  							"state": "state1",
    98  							"workerPools": [
    99  							  {
   100  								"clusterID": "cluster1",
   101  								"workerPoolID": "workerpool1"
   102  							  }
   103  							],
   104  							"zones": [
   105  							  {
   106  								"capacity": {
   107  								  "memoryBytes": 12,
   108  								  "vcpu": 1
   109  								},
   110  								"hostCount": 3,
   111  								"zone": "zone1"
   112  							  }
   113  							]
   114  						  }`),
   115  					),
   116  				)
   117  			})
   118  
   119  			It("should get Workerpool in a cluster", func() {
   120  				target := ClusterTargetHeader{}
   121  				dh, err := newDedicatedHostPool(server.URL()).GetDedicatedHostPool("dedicatedhostpoolid1", target)
   122  				Expect(err).NotTo(HaveOccurred())
   123  				expectedDedicatedHostPool := GetDedicatedHostPoolResponse{
   124  					FlavorClass: "flavorclass1",
   125  					HostCount:   3,
   126  					ID:          "dedicatedhostpool1",
   127  					Metro:       "metro1",
   128  					Name:        "name1",
   129  					State:       "state1",
   130  					WorkerPools: []DedicatedHostPoolWorkerPool{
   131  						DedicatedHostPoolWorkerPool{
   132  							ClusterID:    "cluster1",
   133  							WorkerPoolID: "workerpool1",
   134  						},
   135  					},
   136  					Zones: []DedicatedHostZoneResources{
   137  						DedicatedHostZoneResources{
   138  							Capacity: DedicatedHostResource{
   139  								MemoryBytes: 12,
   140  								VCPU:        1,
   141  							},
   142  							HostCount: 3,
   143  							Zone:      "zone1",
   144  						},
   145  					},
   146  				}
   147  				Expect(dh).Should(BeEquivalentTo(expectedDedicatedHostPool))
   148  			})
   149  		})
   150  		Context("When get dedicatedhostpool is unsuccessful", func() {
   151  			BeforeEach(func() {
   152  				server = ghttp.NewServer()
   153  				server.SetAllowUnhandledRequests(true)
   154  				server.AppendHandlers(
   155  					ghttp.CombineHandlers(
   156  						ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHostPool"),
   157  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get dedicatedhostpool`),
   158  					),
   159  				)
   160  			})
   161  
   162  			It("should return error during get dedicatedhostpool", func() {
   163  				target := ClusterTargetHeader{}
   164  				_, err := newDedicatedHostPool(server.URL()).GetDedicatedHostPool("dedicatedhostpoolid1", target)
   165  				Expect(err).To(HaveOccurred())
   166  			})
   167  		})
   168  	})
   169  
   170  	Describe("List", func() {
   171  		Context("When list dedicatedhostpool is successful", func() {
   172  			BeforeEach(func() {
   173  				server = ghttp.NewServer()
   174  				server.AppendHandlers(
   175  					ghttp.CombineHandlers(
   176  						ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHostPools"),
   177  						ghttp.RespondWith(http.StatusCreated, `[
   178  							{
   179  								"flavorClass": "flavorclass1",
   180  								"hostCount": 3,
   181  								"id": "dedicatedhostpool1",
   182  								"metro": "metro1",
   183  								"name": "name1",
   184  								"state": "state1",
   185  								"workerPools": [
   186  								  {
   187  									"clusterID": "cluster1",
   188  									"workerPoolID": "workerpool1"
   189  								  }
   190  								],
   191  								"zones": [
   192  								  {
   193  									"capacity": {
   194  									  "memoryBytes": 12,
   195  									  "vcpu": 1
   196  									},
   197  									"hostCount": 3,
   198  									"zone": "zone1"
   199  								  }
   200  								]
   201  							  }
   202  						  ]`),
   203  					),
   204  				)
   205  			})
   206  
   207  			It("should list dedicatedhostpools in a cluster", func() {
   208  				target := ClusterTargetHeader{}
   209  
   210  				ldh, err := newDedicatedHostPool(server.URL()).ListDedicatedHostPools(target)
   211  				Expect(err).NotTo(HaveOccurred())
   212  				expectedDedicatedHostPools := []GetDedicatedHostPoolResponse{GetDedicatedHostPoolResponse{
   213  					FlavorClass: "flavorclass1",
   214  					HostCount:   3,
   215  					ID:          "dedicatedhostpool1",
   216  					Metro:       "metro1",
   217  					Name:        "name1",
   218  					State:       "state1",
   219  					WorkerPools: []DedicatedHostPoolWorkerPool{
   220  						DedicatedHostPoolWorkerPool{
   221  							ClusterID:    "cluster1",
   222  							WorkerPoolID: "workerpool1",
   223  						},
   224  					},
   225  					Zones: []DedicatedHostZoneResources{
   226  						DedicatedHostZoneResources{
   227  							Capacity: DedicatedHostResource{
   228  								MemoryBytes: 12,
   229  								VCPU:        1,
   230  							},
   231  							HostCount: 3,
   232  							Zone:      "zone1",
   233  						},
   234  					},
   235  				}}
   236  				Expect(ldh).To(BeEquivalentTo(expectedDedicatedHostPools))
   237  			})
   238  		})
   239  		Context("When list dedicatedhostpool is unsuccessful", func() {
   240  			BeforeEach(func() {
   241  				server = ghttp.NewServer()
   242  				server.SetAllowUnhandledRequests(true)
   243  				server.AppendHandlers(
   244  					ghttp.CombineHandlers(
   245  						ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHostPools"),
   246  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to list dedicatedhostpool`),
   247  					),
   248  				)
   249  			})
   250  
   251  			It("should return error during get dedicatedhostpools", func() {
   252  				target := ClusterTargetHeader{}
   253  				_, err := newDedicatedHostPool(server.URL()).ListDedicatedHostPools(target)
   254  				Expect(err).To(HaveOccurred())
   255  			})
   256  		})
   257  	})
   258  
   259  	Describe("Remove", func() {
   260  		Context("When removing dedicatedhostpool is successful", func() {
   261  			BeforeEach(func() {
   262  				server = ghttp.NewServer()
   263  				server.AppendHandlers(
   264  					ghttp.CombineHandlers(
   265  						ghttp.VerifyRequest(http.MethodPost, "/v2/removeDedicatedHostPool"),
   266  						ghttp.VerifyJSON(`{
   267  							"hostPool": "hostpoolid1"
   268  						  }`),
   269  						ghttp.RespondWith(http.StatusCreated, nil),
   270  					),
   271  				)
   272  			})
   273  
   274  			It("should remove dedicatedhostpool", func() {
   275  				target := ClusterTargetHeader{}
   276  				params := RemoveDedicatedHostPoolRequest{
   277  					HostPoolID: "hostpoolid1",
   278  				}
   279  				err := newDedicatedHostPool(server.URL()).RemoveDedicatedHostPool(params, target)
   280  				Expect(err).NotTo(HaveOccurred())
   281  			})
   282  		})
   283  		Context("When removing dedicatedhostpool is unsuccessful", func() {
   284  			BeforeEach(func() {
   285  				server = ghttp.NewServer()
   286  				server.SetAllowUnhandledRequests(true)
   287  				server.AppendHandlers(
   288  					ghttp.CombineHandlers(
   289  						ghttp.VerifyRequest(http.MethodPost, "/v2/removeDedicatedHostPool"),
   290  						ghttp.VerifyJSON(`{
   291  							"hostPool": "hostpoolid1"
   292  						  }`),
   293  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to remove dedicatedhostpool`),
   294  					),
   295  				)
   296  			})
   297  
   298  			It("should return error during creating dedicatedhostpool", func() {
   299  				target := ClusterTargetHeader{}
   300  				params := RemoveDedicatedHostPoolRequest{
   301  					HostPoolID: "hostpoolid1",
   302  				}
   303  				err := newDedicatedHostPool(server.URL()).RemoveDedicatedHostPool(params, target)
   304  				Expect(err).To(HaveOccurred())
   305  			})
   306  		})
   307  	})
   308  
   309  })
   310  
   311  func newDedicatedHostPool(url string) DedicatedHostPool {
   312  
   313  	sess, err := session.New()
   314  	if err != nil {
   315  		log.Fatal(err)
   316  	}
   317  	conf := sess.Config.Copy()
   318  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   319  	conf.Endpoint = &url
   320  
   321  	client := client.Client{
   322  		Config:      conf,
   323  		ServiceName: bluemix.VpcContainerService,
   324  	}
   325  	return newDedicatedHostPoolAPI(&client)
   326  }