Skip to content

Commit

Permalink
Add test and update the scaleworkload tasks README
Browse files Browse the repository at this point in the history
  • Loading branch information
viveksinghggits committed Dec 21, 2023
1 parent 11c7417 commit e593a62
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
42 changes: 34 additions & 8 deletions pkg/function/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *ScaleSuite) TearDownTest(c *C) {
}
}

func newScaleBlueprint(kind string) *crv1alpha1.Blueprint {
func newScaleBlueprint(kind string, scaleUpCount string) *crv1alpha1.Blueprint {
return &crv1alpha1.Blueprint{
Actions: map[string]*crv1alpha1.BlueprintAction{
"echoHello": {
Expand Down Expand Up @@ -122,7 +122,7 @@ func newScaleBlueprint(kind string) *crv1alpha1.Blueprint {
Name: "testScale",
Func: ScaleWorkloadFuncName,
Args: map[string]interface{}{
ScaleWorkloadReplicas: "2",
ScaleWorkloadReplicas: scaleUpCount,
},
},
},
Expand All @@ -133,7 +133,8 @@ func newScaleBlueprint(kind string) *crv1alpha1.Blueprint {

func (s *ScaleSuite) TestScaleDeployment(c *C) {
ctx := context.Background()
d := testutil.NewTestDeployment(1)
var originalReplicaCount int32 = 1
d := testutil.NewTestDeployment(originalReplicaCount)
d.Spec.Template.Spec.Containers[0].Lifecycle = &v1.Lifecycle{
PreStop: &v1.LifecycleHandler{
Exec: &v1.ExecAction{
Expand All @@ -160,15 +161,27 @@ func (s *ScaleSuite) TestScaleDeployment(c *C) {
Namespace: s.namespace,
},
}
var scaleUpToReplicas int32 = 2
for _, action := range []string{"scaleUp", "echoHello", "scaleDown"} {
tp, err := param.New(ctx, s.cli, fake.NewSimpleDynamicClient(k8sscheme.Scheme, d), s.crCli, s.osCli, as)
c.Assert(err, IsNil)
bp := newScaleBlueprint(kind)
bp := newScaleBlueprint(kind, fmt.Sprintf("%d", scaleUpToReplicas))
phases, err := kanister.GetPhases(*bp, action, kanister.DefaultVersion, *tp)
c.Assert(err, IsNil)
for _, p := range phases {
_, err = p.Exec(context.Background(), *bp, action, *tp)
out, err := p.Exec(context.Background(), *bp, action, *tp)
c.Assert(err, IsNil)
// at the start workload has `originalReplicaCount` replicas, the first phase that is going to get executed is
// `scaleUp` which would change that count to 2, but the function would return the count that workload originally had
// i.e., `originalReplicaCount`
if action == "scaleUp" {
c.Assert(out[outputArtifactOriginalReplicaCount], Equals, originalReplicaCount)
}
// `scaleDown` is going to change the replica count to 0 from 2. Because the workload already had 2 replicas
// (previous phase), so ouptut artifact from the function this time would be what the workload already had i.e., 2
if action == "scaleDown" {
c.Assert(out[outputArtifactOriginalReplicaCount], Equals, scaleUpToReplicas)
}
}
ok, _, err := kube.DeploymentReady(ctx, s.cli, d.GetNamespace(), d.GetName())
c.Assert(err, IsNil)
Expand All @@ -182,7 +195,8 @@ func (s *ScaleSuite) TestScaleDeployment(c *C) {

func (s *ScaleSuite) TestScaleStatefulSet(c *C) {
ctx := context.Background()
ss := testutil.NewTestStatefulSet(1)
var originalReplicaCount int32 = 1
ss := testutil.NewTestStatefulSet(originalReplicaCount)
ss.Spec.Template.Spec.Containers[0].Lifecycle = &v1.Lifecycle{
PreStop: &v1.LifecycleHandler{
Exec: &v1.ExecAction{
Expand All @@ -209,15 +223,27 @@ func (s *ScaleSuite) TestScaleStatefulSet(c *C) {
},
}

var scaleUpToReplicas int32 = 2
for _, action := range []string{"scaleUp", "echoHello", "scaleDown"} {
tp, err := param.New(ctx, s.cli, fake.NewSimpleDynamicClient(k8sscheme.Scheme, ss), s.crCli, s.osCli, as)
c.Assert(err, IsNil)
bp := newScaleBlueprint(kind)
bp := newScaleBlueprint(kind, fmt.Sprintf("%d", scaleUpToReplicas))
phases, err := kanister.GetPhases(*bp, action, kanister.DefaultVersion, *tp)
c.Assert(err, IsNil)
for _, p := range phases {
_, err = p.Exec(context.Background(), *bp, action, *tp)
out, err := p.Exec(context.Background(), *bp, action, *tp)
c.Assert(err, IsNil)
// at the start workload has `originalReplicaCount` replicas, the first phase that is going to get executed is
// `scaleUp` which would change that count to 2, but the function would return the count that workload originally had
// i.e., `originalReplicaCount`
if action == "scaleUp" {
c.Assert(out[outputArtifactOriginalReplicaCount], Equals, originalReplicaCount)
}
// `scaleDown` is going to change the replica count to 0 from 2. Because the workload already had 2 replicas
// (previous phase), so ouptut artifact from the function this time would be what the workload already had i.e., 2
if action == "scaleDown" {
c.Assert(out[outputArtifactOriginalReplicaCount], Equals, scaleUpToReplicas)
}
}
ok, _, err := kube.StatefulSetReady(ctx, s.cli, ss.GetNamespace(), ss.GetName())
c.Assert(err, IsNil)
Expand Down
12 changes: 6 additions & 6 deletions pkg/function/scale_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,33 @@ func (s *scaleWorkloadFunc) Exec(ctx context.Context, tp param.TemplateParams, a
}
switch strings.ToLower(s.kind) {
case param.StatefulSetKind:
count, err := kube.StatefulSetReplicas(ctx, cli, namespace, name)
count, err := kube.StatefulSetReplicas(ctx, cli, s.namespace, s.name)
if err != nil {
return nil, err
}
return map[string]interface{}{
outputArtifactOriginalReplicaCount: count,
}, kube.ScaleStatefulSet(ctx, cli, namespace, name, replicas, waitForReady)
}, kube.ScaleStatefulSet(ctx, cli, s.namespace, s.name, s.replicas, s.waitForReady)
case param.DeploymentKind:
count, err := kube.DeploymentReplicas(ctx, cli, namespace, name)
count, err := kube.DeploymentReplicas(ctx, cli, s.namespace, s.name)
if err != nil {
return nil, err
}
return map[string]interface{}{
outputArtifactOriginalReplicaCount: count,
}, kube.ScaleDeployment(ctx, cli, namespace, name, replicas, waitForReady)
}, kube.ScaleDeployment(ctx, cli, s.namespace, s.name, s.replicas, s.waitForReady)
case param.DeploymentConfigKind:
osCli, err := osversioned.NewForConfig(cfg)
if err != nil {
return nil, errors.Wrapf(err, "Failed to create OpenShift client")
}
count, err := kube.DeploymentConfigReplicas(ctx, osCli, namespace, name)
count, err := kube.DeploymentConfigReplicas(ctx, osCli, s.namespace, s.name)
if err != nil {
return nil, err
}
return map[string]interface{}{
outputArtifactOriginalReplicaCount: count,
}, kube.ScaleDeploymentConfig(ctx, cli, osCli, namespace, name, replicas, waitForReady)
}, kube.ScaleDeploymentConfig(ctx, cli, osCli, s.namespace, s.name, s.replicas, s.waitForReady)
}
return nil, errors.New("Workload type not supported " + s.kind)
}
Expand Down

0 comments on commit e593a62

Please sign in to comment.