-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add BuildDefault validation rule result util code for all p…
…lugins to use (#419) ## Description Moves the code in `pkg/validators/common.go` of https://github.com/validator-labs/validator-plugin-aws to this repo so it can be used in all plugins, as function `BuildDefault`. Sometimes the default result doesn't use the `<prefix>-<sanitized_rule_name>` format, so there is a param that can be used to override this and use a particular value. --------- Signed-off-by: Matt Welke <matt.welke@spectrocloud.com>
- Loading branch information
Showing
6 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Package validationruleresult includes code to help work with ValidationRuleResults. | ||
package validationruleresult | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/validator-labs/validator/api/v1alpha1" | ||
"github.com/validator-labs/validator/pkg/constants" | ||
"github.com/validator-labs/validator/pkg/types" | ||
"github.com/validator-labs/validator/pkg/util" | ||
) | ||
|
||
// 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"). | ||
// | ||
// One of the validation rule 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 | ||
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, | ||
State: &state, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
}) | ||
} | ||
} |