Skip to content

Commit

Permalink
Add validation rule override to function. Add tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Welke <matt.welke@spectrocloud.com>
  • Loading branch information
mattwelke committed Sep 11, 2024
1 parent 748d493 commit 0a5cd27
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pkg/validationruleresult/validation_rule_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ import (
"github.com/validator-labs/validator/pkg/util"
)

// BuildDefault builds a default ValidationResult for a given validation type. The rule name is
// sanitized and then used as part of the one-word validation rule description in the latest
// condition. The latest condition message should be one that conveys that validation succeeded. The
// validation type should be unique for each combination of plugin and rule (e.g.
// BuildDefault builds a default ValidationResult for a given validation type.
//
// The latest condition message param should be one that conveys that validation succeeded. The
// validation type param should be unique for each combination of plugin and rule (e.g.
// "aws-iam-role-policy").
func BuildDefault(ruleName, latestConditionMsg, validationType string) *types.ValidationRuleResult {
//
// One of the validation rule param or rule name params must be provided. If validation rule is
// provided, it is used as the validation rule description of the condition. If it isn't, a
// description is generated based on the rule name and used instead.
func BuildDefault(latestConditionMsg, validationType, validationRule, ruleName string) *types.ValidationRuleResult {
state := v1alpha1.ValidationSucceeded
latestCondition := v1alpha1.DefaultValidationCondition()
latestCondition.Message = latestConditionMsg
latestCondition.ValidationRule = fmt.Sprintf(
"%s-%s",
constants.ValidationRulePrefix, util.Sanitize(ruleName),
)
if validationRule != "" {
latestCondition.ValidationRule = validationRule
} else {
latestCondition.ValidationRule = fmt.Sprintf(
"%s-%s",
constants.ValidationRulePrefix, util.Sanitize(ruleName),
)
}
latestCondition.ValidationType = validationType
return &types.ValidationRuleResult{
Condition: &latestCondition,
Expand Down
81 changes: 81 additions & 0 deletions pkg/validationruleresult/validation_rule_result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Package validationruleresult includes code to help work with ValidationRuleResults.
package validationruleresult

import (
"encoding/json"
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/validator-labs/validator/api/v1alpha1"
"github.com/validator-labs/validator/pkg/types"
"github.com/validator-labs/validator/pkg/util"
)

func TestBuildDefault(t *testing.T) {
type args struct {
latestConditionMsg string
validationType string
validationRule string
ruleName string
}
tests := []struct {
name string
args args
want *types.ValidationRuleResult
}{
{
name: "uses validation rule description if provided",
args: args{
latestConditionMsg: "a",
validationType: "b",
validationRule: "c",
ruleName: "",
},
want: &types.ValidationRuleResult{
Condition: &v1alpha1.ValidationCondition{
ValidationType: "b",
ValidationRule: "c",
Message: "a",
Status: corev1.ConditionTrue,
},
State: util.Ptr(v1alpha1.ValidationSucceeded),
},
},
{
name: "correctly generates a validation rule description based on rule name if validation rule not provided",
args: args{
latestConditionMsg: "a",
validationType: "b",
validationRule: "",
ruleName: "d",
},
want: &types.ValidationRuleResult{
Condition: &v1alpha1.ValidationCondition{
ValidationType: "b",
ValidationRule: "validation-d",
Message: "a",
Status: corev1.ConditionTrue,
},
State: util.Ptr(v1alpha1.ValidationSucceeded),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := BuildDefault(tt.args.latestConditionMsg, tt.args.validationType, tt.args.validationRule, tt.args.ruleName)

// Remove the time. It will be different each run.
got.Condition.LastValidationTime = metav1.Time{}

gotJSON, _ := json.MarshalIndent(got, "", " ")
wantJSON, _ := json.MarshalIndent(tt.want, "", " ")

if !reflect.DeepEqual(gotJSON, wantJSON) {
t.Errorf("BuildDefault() = %v, want %v", string(gotJSON), string(wantJSON))
}
})
}
}

0 comments on commit 0a5cd27

Please sign in to comment.