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

     1  package icdv4
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	"github.com/onsi/gomega/ghttp"
    10  
    11  	bluemix "github.com/IBM-Cloud/bluemix-go"
    12  	"github.com/IBM-Cloud/bluemix-go/client"
    13  	bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
    14  	"github.com/IBM-Cloud/bluemix-go/session"
    15  )
    16  
    17  var _ = Describe("Auto Scaling", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  	Describe("Set", func() {
    23  		Context("When Set Auto Scaling group is successful", func() {
    24  			BeforeEach(func() {
    25  				server = ghttp.NewServer()
    26  				server.AppendHandlers(
    27  					ghttp.CombineHandlers(
    28  						ghttp.VerifyRequest(http.MethodPatch, "/v4/ibm/deployments/crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::/groups/member/autoscaling"),
    29  						ghttp.RespondWith(http.StatusOK, `
    30                             {
    31  							  "task": 
    32  							  	{
    33  									"id":"crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f:task:e3f0d867-f6a1-4242-b753-16ba59659766",
    34  									"description":"Updating autoscale settings.",
    35  									"status":"running",
    36  									"deployment_id":"crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::",
    37  									"progress_percent":0,
    38  									"created_at":"2020-08-12T11:15:45.000Z"}
    39  								}
    40                              }
    41                          `),
    42  					),
    43  				)
    44  			})
    45  			It("should return group updated", func() {
    46  				target1 := "crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::"
    47  				target2 := "member"
    48  				memoryReq := ASGBody{
    49  					Scalers: ScalersBody{
    50  						IO: &IOBody{
    51  							Enabled:      true,
    52  							AbovePercent: 35,
    53  							OverPeriod:   "15m",
    54  						},
    55  					},
    56  					Rate: RateBody{
    57  						IncreasePercent:  10,
    58  						LimitMBPerMember: 5000,
    59  						PeriodSeconds:    900,
    60  						Units:            "mb",
    61  					},
    62  				}
    63  
    64  				groupBdy := AutoscalingGroup{
    65  					Memory: &memoryReq,
    66  				}
    67  				params := AutoscalingSetGroup{
    68  					Autoscaling: groupBdy,
    69  				}
    70  				myTask, err := newScalingGroup(server.URL()).SetAutoScaling(target1, target2, params)
    71  				Expect(err).NotTo(HaveOccurred())
    72  				Expect(myTask).ShouldNot(BeNil())
    73  			})
    74  		})
    75  		Context("When update is unsuccessful", func() {
    76  			BeforeEach(func() {
    77  				server = ghttp.NewServer()
    78  				server.SetAllowUnhandledRequests(true)
    79  				server.AppendHandlers(
    80  					ghttp.CombineHandlers(
    81  						ghttp.VerifyRequest(http.MethodPatch, "/v4/ibm/deployments/crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::/groups/member/autoscaling"),
    82  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to update group`),
    83  					),
    84  				)
    85  			})
    86  
    87  			It("should return error during Auto Scaling group Set", func() {
    88  				target1 := "crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::"
    89  				target2 := "member"
    90  				memoryReq := ASGBody{
    91  					Scalers: ScalersBody{
    92  						IO: &IOBody{
    93  							Enabled:      true,
    94  							AbovePercent: 35,
    95  							OverPeriod:   "15m",
    96  						},
    97  					},
    98  					Rate: RateBody{
    99  						IncreasePercent:  10,
   100  						LimitMBPerMember: 5000,
   101  						PeriodSeconds:    900,
   102  						Units:            "mb",
   103  					},
   104  				}
   105  				groupBdy := AutoscalingGroup{
   106  					Memory: &memoryReq,
   107  					// Disk:   &diskReq,
   108  					// CPU:    &cpuReq,
   109  				}
   110  				params := AutoscalingSetGroup{
   111  					Autoscaling: groupBdy,
   112  				}
   113  				myTask, err := newScalingGroup(server.URL()).SetAutoScaling(target1, target2, params)
   114  				Expect(err).To(HaveOccurred())
   115  				Expect(myTask.Id).Should(Equal(""))
   116  			})
   117  		})
   118  	})
   119  	Describe("Get", func() {
   120  		Context("When get is successful", func() {
   121  			BeforeEach(func() {
   122  				server = ghttp.NewServer()
   123  				server.AppendHandlers(
   124  					ghttp.CombineHandlers(
   125  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployments/crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::/groups/member/autoscaling"),
   126  						ghttp.RespondWith(http.StatusOK, `
   127  						{
   128  							"autoscaling":
   129  							{
   130  								"disk":
   131  								{
   132  									"scalers":
   133  									{
   134  										"capacity":
   135  										{
   136  											"enabled":false,
   137  											"free_space_less_than_percent":10
   138  										},
   139  										"io_utilization":
   140  										{
   141  											"enabled":false,
   142  											"over_period":"15m",
   143  											"above_percent":90
   144  										}
   145  									},
   146  									"rate":
   147  									{
   148  										"increase_percent":10.0,
   149  										"period_seconds":900,
   150  										"limit_mb_per_member":3670016,
   151  										"units":"mb"
   152  									}
   153  								},
   154  								"memory":
   155  								{
   156  									"scalers":
   157  									{
   158  										"io_utilization":
   159  										{
   160  											"enabled":true,
   161  											"over_period":"15m",
   162  											"above_percent":35
   163  										}
   164  									},
   165  									"rate":
   166  									{
   167  										"increase_percent":10.0,
   168  										"period_seconds":900,
   169  										"limit_mb_per_member":5000,
   170  										"units":"mb"
   171  									}
   172  								},
   173  								"cpu":
   174  								{
   175  									"scalers":{},
   176  									"rate":
   177  									{
   178  										"increase_percent":10.0,
   179  										"period_seconds":900,
   180  										"units":"count",
   181  										"limit_count_per_member":30}
   182  									}
   183  								}
   184  							}
   185                          `),
   186  					),
   187  				)
   188  			})
   189  
   190  			It("should return groups", func() {
   191  				target1 := "crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::"
   192  				_, err := newScalingGroup(server.URL()).GetAutoScaling(target1, "member")
   193  				Expect(err).NotTo(HaveOccurred())
   194  			})
   195  		})
   196  		Context("When get is unsuccessful", func() {
   197  			BeforeEach(func() {
   198  				server = ghttp.NewServer()
   199  				server.SetAllowUnhandledRequests(true)
   200  				server.AppendHandlers(
   201  					ghttp.CombineHandlers(
   202  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployments/crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::/groups/member/autoscaling"),
   203  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get groups`),
   204  					),
   205  				)
   206  			})
   207  
   208  			It("should return error during AUto Scaling Group get", func() {
   209  				target1 := "crn:v1:bluemix:public:databases-for-redis:us-south:a/4448261269a14562b839e0a3019ed980:58a93bd0-de14-410d-a775-8495a295e47f::"
   210  				_, err := newScalingGroup(server.URL()).GetAutoScaling(target1, "member")
   211  				Expect(err).To(HaveOccurred())
   212  			})
   213  		})
   214  	})
   215  })
   216  
   217  func newScalingGroup(url string) AutoScaling {
   218  
   219  	sess, err := session.New()
   220  	if err != nil {
   221  		log.Fatal(err)
   222  	}
   223  	conf := sess.Config.Copy()
   224  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   225  	conf.Endpoint = &url
   226  
   227  	client := client.Client{
   228  		Config:      conf,
   229  		ServiceName: bluemix.ICDService,
   230  	}
   231  	return newAutoScalingAPI(&client)
   232  }