From 80587588cc24fadba2d9f713bfc077e27452a890 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Mon, 28 Oct 2024 12:04:28 +0100 Subject: [PATCH] Fix incorrect conversion between int types (#3205) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- pkg/function/scale_workload.go | 4 +-- pkg/function/scale_workload_test.go | 46 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 pkg/function/scale_workload_test.go diff --git a/pkg/function/scale_workload.go b/pkg/function/scale_workload.go index 419c056786..5acae60a01 100644 --- a/pkg/function/scale_workload.go +++ b/pkg/function/scale_workload.go @@ -167,8 +167,8 @@ func (s *scaleWorkloadFunc) setArgs(tp param.TemplateParams, args map[string]int case int64: replicas = int32(val) case string: - var v int - if v, err = strconv.Atoi(val); err != nil { + v, err := strconv.ParseInt(val, 10, 32) + if err != nil { return errkit.Wrap(err, fmt.Sprintf("Cannot convert %s to int", val)) } replicas = int32(v) diff --git a/pkg/function/scale_workload_test.go b/pkg/function/scale_workload_test.go new file mode 100644 index 0000000000..2b74d0b64c --- /dev/null +++ b/pkg/function/scale_workload_test.go @@ -0,0 +1,46 @@ +package function + +import ( + "gopkg.in/check.v1" + + "github.com/kanisterio/kanister/pkg/param" +) + +type ScaleWorkloadSuite struct{} + +var _ = check.Suite(&ScaleWorkloadSuite{}) + +func (s *ScaleWorkloadSuite) TestSetArgs(c *check.C) { + stsParams := ¶m.StatefulSetParams{} + for _, tc := range []struct { + replicas interface{} + expectedReplicas int32 + }{ + { + replicas: 4, + expectedReplicas: 4, + }, + { + replicas: 234324, + expectedReplicas: 234324, + }, + { + replicas: 234324, + expectedReplicas: 234324, + }, + { + replicas: 2147483647, + expectedReplicas: 2147483647, // 2147483647 is the maximum value int32 can hold + }, + } { + s := scaleWorkloadFunc{} + err := s.setArgs(param.TemplateParams{ + StatefulSet: stsParams, + }, map[string]interface{}{ + "replicas": tc.replicas, + }) + + c.Assert(err, check.IsNil) + c.Assert(s.replicas, check.Equals, tc.expectedReplicas) + } +}