github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/dedicated_host_flavors_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("dedicatedhostflavor", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  
    23  	Describe("List", func() {
    24  		Context("When list dedicatedhostflavors is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHostFlavors"),
    30  						ghttp.RespondWith(http.StatusCreated, `[
    31  							{
    32  								"deprecated":false,
    33  								"flavorClass":"flavorclass1",
    34  								"id":"flavorid1",
    35  								"instanceStorage":[
    36  									{
    37  										"count": 0,
    38  										"size":	0
    39  									}
    40  								],
    41  								"maxMemory": 52,
    42  								"maxVCPUs":	12,
    43  								"region": "region1",
    44  								"zone": "zone1"
    45  							  }
    46  						  ]`),
    47  					),
    48  				)
    49  			})
    50  
    51  			It("should list dedicatedhostflavors in a zone", func() {
    52  				target := ClusterTargetHeader{}
    53  
    54  				ldhf, err := newDedicatedHostFlavor(server.URL()).ListDedicatedHostFlavors("zone1", target)
    55  				Expect(err).NotTo(HaveOccurred())
    56  				expectedDedicatedHostFlavors := GetDedicatedHostFlavors{
    57  					GetDedicatedHostFlavor{
    58  						ID:          "flavorid1",
    59  						FlavorClass: "flavorclass1",
    60  						Region:      "region1",
    61  						Zone:        "zone1",
    62  						Deprecated:  false,
    63  						MaxVCPUs:    12,
    64  						MaxMemory:   52,
    65  						InstanceStorage: []InstanceStorage{
    66  							InstanceStorage{
    67  								Count: 0,
    68  								Size:  0,
    69  							},
    70  						},
    71  					},
    72  				}
    73  				Expect(ldhf).To(BeEquivalentTo(expectedDedicatedHostFlavors))
    74  			})
    75  		})
    76  		Context("When list dedicatedhostflavors is unsuccessful", func() {
    77  			BeforeEach(func() {
    78  				server = ghttp.NewServer()
    79  				server.SetAllowUnhandledRequests(true)
    80  				server.AppendHandlers(
    81  					ghttp.CombineHandlers(
    82  						ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHostFlavors"),
    83  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to list dedicatedhostflavors`),
    84  					),
    85  				)
    86  			})
    87  
    88  			It("should return error during get dedicatedhosts", func() {
    89  				target := ClusterTargetHeader{}
    90  				_, err := newDedicatedHostFlavor(server.URL()).ListDedicatedHostFlavors("zone1", target)
    91  				Expect(err).To(HaveOccurred())
    92  			})
    93  		})
    94  	})
    95  
    96  })
    97  
    98  func newDedicatedHostFlavor(url string) DedicatedHostFlavor {
    99  
   100  	sess, err := session.New()
   101  	if err != nil {
   102  		log.Fatal(err)
   103  	}
   104  	conf := sess.Config.Copy()
   105  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   106  	conf.Endpoint = &url
   107  
   108  	client := client.Client{
   109  		Config:      conf,
   110  		ServiceName: bluemix.VpcContainerService,
   111  	}
   112  	return newDedicatedHostFlavorAPI(&client)
   113  }