Skip to content

Commit

Permalink
Add tests for function reading helm.sdk.operatorframework.io/rollback…
Browse files Browse the repository at this point in the history
…-force annotation

Signed-off-by: Edmund Ochieng <ochienged@gmail.com>
  • Loading branch information
OchiengEd committed Aug 17, 2023
1 parent 3fd7105 commit de5931b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 15 deletions.
33 changes: 18 additions & 15 deletions internal/helm/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,21 +316,10 @@ func (r HelmOperatorReconciler) Reconcile(ctx context.Context, request reconcile
release.ForceUpgrade(force),
}

// this function allows the user to set the value for the annotation,
// "helm.sdk.operatorframework.io/rollback-force" to false. However,
// it does keep the default set to true
forceRollback := func() bool {
val, ok := o.GetAnnotations()[helmRollbackForceAnnotation]
if !ok {
return true
}
r, err := strconv.ParseBool(val)
if err != nil {
return true
}

return r
}()
// the forceRollback variable takes the value of the annotation,
// "helm.sdk.operatorframework.io/rollback-force".
// The default value for the annotation is true
forceRollback := readBoolAnnotationWithDefault(o, helmRollbackForceAnnotation, true)
upgradeOptions = append(upgradeOptions, release.ForceRollback(forceRollback))

previousRelease, upgradedRelease, err := manager.UpgradeRelease(ctx, upgradeOptions...)
Expand Down Expand Up @@ -468,6 +457,20 @@ func hasAnnotation(anno string, o *unstructured.Unstructured) bool {
return value
}

func readBoolAnnotationWithDefault(obj *unstructured.Unstructured, annotation string, fallback bool) bool {
val, ok := obj.GetAnnotations()[annotation]
if !ok {
return fallback
}
r, err := strconv.ParseBool(val)
if err != nil {
log.Error(err, "error passing annotation", "annotation", annotation)
return fallback
}

return r
}

func (r HelmOperatorReconciler) updateResource(ctx context.Context, o client.Object) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return r.Client.Update(ctx, o)
Expand Down
61 changes: 61 additions & 0 deletions internal/helm/controller/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,64 @@ func annotations(m map[string]interface{}) *unstructured.Unstructured {
},
}
}

func Test_readBoolAnnotationWithDefault(t *testing.T) {
objBuilder := func(anno map[string]string) *unstructured.Unstructured {
object := &unstructured.Unstructured{}
object.SetAnnotations(anno)
return object
}

type args struct {
obj *unstructured.Unstructured
annotation string
fallback bool
}

tests := []struct {
name string
args args
want bool
}{
{
name: "Should return value of annotation read",
args: args{
obj: objBuilder(map[string]string{
"helm.sdk.operatorframework.io/rollback-force": "false",
}),
annotation: "helm.sdk.operatorframework.io/rollback-force",
fallback: true,
},
want: false,
},
{
name: "Should return fallback when annotation is not present",
args: args{
obj: objBuilder(map[string]string{
"helm.sdk.operatorframework.io/upgrade-force": "true",
}),
annotation: "helm.sdk.operatorframework.io/rollback-force",
fallback: false,
},
want: false,
},
{
name: "Should return fallback when errors while parsing bool value",
args: args{
obj: objBuilder(map[string]string{
"helm.sdk.operatorframework.io/rollback-force": "force",
}),
annotation: "helm.sdk.operatorframework.io/rollback-force",
fallback: true,
},
want: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := readBoolAnnotationWithDefault(tc.args.obj, tc.args.annotation, tc.args.fallback); got != tc.want {
t.Errorf("readBoolAnnotationWithDefault() = %v, want %v", got, tc.want)
}
})
}
}

0 comments on commit de5931b

Please sign in to comment.