github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/icd/icdv4/scaling_test.go (about)

     1  package icdv4
     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("Scaling", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  	Describe("Update", func() {
    24  		Context("When update group is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodPatch, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/groups/member"),
    30  						ghttp.RespondWith(http.StatusOK, `
    31                             {
    32                                "task": {
    33                                  "id": "5abb6a7d11a1a5001479a0ab",
    34                                  "description": "Scaling database deployment",
    35                                  "status": "running",
    36                                  "deployment_id": "59b14b19874a1c0018009482",
    37                                  "progress_percent": 5,
    38                                  "created_at": "2018-03-28T10:20:30Z"
    39                                }
    40                              }
    41                          `),
    42  					),
    43  				)
    44  			})
    45  			It("should return group updated", func() {
    46  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    47  				target2 := "member"
    48  				memoryReq := MemoryReq{AllocationMb: 2018}
    49  				cpuReq := CpuReq{AllocationCount: 2}
    50  				diskReq := DiskReq{AllocationMb: 2018}
    51  				groupBdy := GroupBdy{
    52  					Memory: &memoryReq,
    53  					Disk:   &diskReq,
    54  					Cpu:    &cpuReq,
    55  				}
    56  				params := GroupReq{
    57  					GroupBdy: groupBdy,
    58  				}
    59  				myTask, err := newGroup(server.URL()).UpdateGroup(target1, target2, params)
    60  				Expect(err).NotTo(HaveOccurred())
    61  				Expect(myTask).ShouldNot(BeNil())
    62  				Expect(myTask.Id).Should(Equal("5abb6a7d11a1a5001479a0ab"))
    63  				Expect(myTask.Description).Should(Equal("Scaling database deployment"))
    64  				Expect(myTask.Status).Should(Equal("running"))
    65  				Expect(myTask.DeploymentId).Should(Equal("59b14b19874a1c0018009482"))
    66  				Expect(myTask.ProgressPercent).Should(Equal(5))
    67  				Expect(myTask.CreatedAt).Should(Equal("2018-03-28T10:20:30Z"))
    68  			})
    69  		})
    70  		Context("When update is unsuccessful", func() {
    71  			BeforeEach(func() {
    72  				server = ghttp.NewServer()
    73  				server.SetAllowUnhandledRequests(true)
    74  				server.AppendHandlers(
    75  					ghttp.CombineHandlers(
    76  						ghttp.VerifyRequest(http.MethodPatch, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/groups/member"),
    77  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to update group`),
    78  					),
    79  				)
    80  			})
    81  
    82  			It("should return error during group update", func() {
    83  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    84  				target2 := "member"
    85  				memoryReq := MemoryReq{AllocationMb: 2018}
    86  				cpuReq := CpuReq{AllocationCount: 2}
    87  				diskReq := DiskReq{AllocationMb: 2018}
    88  				groupBdy := GroupBdy{
    89  					Memory: &memoryReq,
    90  					Disk:   &diskReq,
    91  					Cpu:    &cpuReq,
    92  				}
    93  				params := GroupReq{
    94  					GroupBdy: groupBdy,
    95  				}
    96  				myTask, err := newGroup(server.URL()).UpdateGroup(target1, target2, params)
    97  				Expect(err).To(HaveOccurred())
    98  				Expect(myTask.Id).Should(Equal(""))
    99  			})
   100  		})
   101  	})
   102  	Describe("GetDefault", func() {
   103  		Context("When get default is successful", func() {
   104  			BeforeEach(func() {
   105  				server = ghttp.NewServer()
   106  				server.AppendHandlers(
   107  					ghttp.CombineHandlers(
   108  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployables/etcd/groups"),
   109  						ghttp.RespondWith(http.StatusOK, `
   110                                  {
   111                                    "groups": [
   112                                      {
   113                                        "id": "member",
   114                                        "count": 2,
   115                                        "memory": {
   116                                          "units": "mb",
   117                                          "allocation_mb": 2048,
   118                                          "minimum_mb": 2048,
   119                                          "step_size_mb": 256,
   120                                          "is_adjustable": true
   121                                        },
   122                                        "cpu": {
   123                                          "units": "2",
   124                                          "allocation_count": 2,
   125                                          "minimum_count": 2,
   126                                          "step_size_count": 2,
   127                                          "is_adjustable": false
   128                                        },
   129                                        "disk": {
   130                                          "units": "mb",
   131                                          "allocation_mb": 5120,
   132                                          "minimum_mb": 5120,
   133                                          "step_size_mb": 2048,
   134                                          "is_adjustable": false
   135                                        }
   136                                      }
   137                                    ]
   138                                  }
   139                          `),
   140  					),
   141  				)
   142  			})
   143  
   144  			It("should return default groups", func() {
   145  				target1 := "etcd"
   146  				groupList, err := newGroup(server.URL()).GetDefaultGroups(target1)
   147  				Expect(err).NotTo(HaveOccurred())
   148  				Expect(groupList).ShouldNot(BeNil())
   149  				Expect(groupList.Groups[0].Id).Should(Equal("member"))
   150  				Expect(groupList.Groups[0].Count).Should(Equal(2))
   151  				Expect(groupList.Groups[0].Memory.Units).Should(Equal("mb"))
   152  				Expect(groupList.Groups[0].Memory.AllocationMb).Should(Equal(2048))
   153  				Expect(groupList.Groups[0].Memory.MinimumMb).Should(Equal(2048))
   154  				Expect(groupList.Groups[0].Memory.StepSizeMb).Should(Equal(256))
   155  				Expect(groupList.Groups[0].Memory.IsAdjustable).Should(Equal(true))
   156  				Expect(groupList.Groups[0].Cpu.Units).Should(Equal("2"))
   157  				Expect(groupList.Groups[0].Cpu.AllocationCount).Should(Equal(2))
   158  				Expect(groupList.Groups[0].Cpu.MinimumCount).Should(Equal(2))
   159  				Expect(groupList.Groups[0].Cpu.StepSizeCount).Should(Equal(2))
   160  				Expect(groupList.Groups[0].Cpu.IsAdjustable).Should(Equal(false))
   161  				Expect(groupList.Groups[0].Disk.Units).Should(Equal("mb"))
   162  				Expect(groupList.Groups[0].Disk.AllocationMb).Should(Equal(5120))
   163  				Expect(groupList.Groups[0].Disk.MinimumMb).Should(Equal(5120))
   164  				Expect(groupList.Groups[0].Disk.StepSizeMb).Should(Equal(2048))
   165  				Expect(groupList.Groups[0].Disk.IsAdjustable).Should(Equal(false))
   166  			})
   167  		})
   168  		Context("When get default 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, "/v4/ibm/deployables/etcd/groups"),
   175  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get groups`),
   176  					),
   177  				)
   178  			})
   179  
   180  			It("should return error during group get", func() {
   181  				target1 := "etcd"
   182  				_, err := newGroup(server.URL()).GetDefaultGroups(target1)
   183  				Expect(err).To(HaveOccurred())
   184  			})
   185  		})
   186  	})
   187  	Describe("Get", func() {
   188  		Context("When get is successful", func() {
   189  			BeforeEach(func() {
   190  				server = ghttp.NewServer()
   191  				server.AppendHandlers(
   192  					ghttp.CombineHandlers(
   193  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/groups"),
   194  						ghttp.RespondWith(http.StatusOK, `
   195                                  {
   196                                    "groups": [
   197                                      {
   198                                        "id": "member",
   199                                        "count": 2,
   200                                        "memory": {
   201                                          "units": "mb",
   202                                          "allocation_mb": 2048,
   203                                          "minimum_mb": 2048,
   204                                          "step_size_mb": 256,
   205                                          "is_adjustable": true
   206                                        },
   207                                        "cpu": {
   208                                          "units": "2",
   209                                          "allocation_count": 2,
   210                                          "minimum_count": 2,
   211                                          "step_size_count": 2,
   212                                          "is_adjustable": false
   213                                        },
   214                                        "disk": {
   215                                          "units": "mb",
   216                                          "allocation_mb": 5120,
   217                                          "minimum_mb": 5120,
   218                                          "step_size_mb": 2048,
   219                                          "is_adjustable": false
   220                                        }
   221                                      }
   222                                    ]
   223                                  }
   224                          `),
   225  					),
   226  				)
   227  			})
   228  
   229  			It("should return groups", func() {
   230  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   231  				groupList, err := newGroup(server.URL()).GetGroups(target1)
   232  				Expect(err).NotTo(HaveOccurred())
   233  				Expect(groupList).ShouldNot(BeNil())
   234  				Expect(groupList.Groups[0].Id).Should(Equal("member"))
   235  				Expect(groupList.Groups[0].Count).Should(Equal(2))
   236  				Expect(groupList.Groups[0].Memory.Units).Should(Equal("mb"))
   237  				Expect(groupList.Groups[0].Memory.AllocationMb).Should(Equal(2048))
   238  				Expect(groupList.Groups[0].Memory.MinimumMb).Should(Equal(2048))
   239  				Expect(groupList.Groups[0].Memory.StepSizeMb).Should(Equal(256))
   240  				Expect(groupList.Groups[0].Memory.IsAdjustable).Should(Equal(true))
   241  				Expect(groupList.Groups[0].Cpu.Units).Should(Equal("2"))
   242  				Expect(groupList.Groups[0].Cpu.AllocationCount).Should(Equal(2))
   243  				Expect(groupList.Groups[0].Cpu.MinimumCount).Should(Equal(2))
   244  				Expect(groupList.Groups[0].Cpu.StepSizeCount).Should(Equal(2))
   245  				Expect(groupList.Groups[0].Cpu.IsAdjustable).Should(Equal(false))
   246  				Expect(groupList.Groups[0].Disk.Units).Should(Equal("mb"))
   247  				Expect(groupList.Groups[0].Disk.AllocationMb).Should(Equal(5120))
   248  				Expect(groupList.Groups[0].Disk.MinimumMb).Should(Equal(5120))
   249  				Expect(groupList.Groups[0].Disk.StepSizeMb).Should(Equal(2048))
   250  				Expect(groupList.Groups[0].Disk.IsAdjustable).Should(Equal(false))
   251  			})
   252  		})
   253  		Context("When get default is unsuccessful", func() {
   254  			BeforeEach(func() {
   255  				server = ghttp.NewServer()
   256  				server.SetAllowUnhandledRequests(true)
   257  				server.AppendHandlers(
   258  					ghttp.CombineHandlers(
   259  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/groups"),
   260  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get groups`),
   261  					),
   262  				)
   263  			})
   264  
   265  			It("should return error during group get", func() {
   266  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   267  				_, err := newGroup(server.URL()).GetGroups(target1)
   268  				Expect(err).To(HaveOccurred())
   269  			})
   270  		})
   271  	})
   272  })
   273  
   274  func newGroup(url string) Groups {
   275  
   276  	sess, err := session.New()
   277  	if err != nil {
   278  		log.Fatal(err)
   279  	}
   280  	conf := sess.Config.Copy()
   281  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   282  	conf.Endpoint = &url
   283  
   284  	client := client.Client{
   285  		Config:      conf,
   286  		ServiceName: bluemix.ICDService,
   287  	}
   288  	return newGroupAPI(&client)
   289  }