Skip to content

Commit

Permalink
Fix incorrect conversion between int types (#3205)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
viveksinghggits and mergify[bot] authored Oct 28, 2024
1 parent 227a815 commit 8058758
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/function/scale_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions pkg/function/scale_workload_test.go
Original file line number Diff line number Diff line change
@@ -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 := &param.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)
}
}

0 comments on commit 8058758

Please sign in to comment.