diff --git a/pkg/validation/modifier_test.go b/pkg/validation/modifier_test.go new file mode 100644 index 000000000..432b0464a --- /dev/null +++ b/pkg/validation/modifier_test.go @@ -0,0 +1,97 @@ +package validation + +import ( + "testing" + + "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" + "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func TestValidateModifier(t *testing.T) { + multiple := v1alpha1.Modifier{ + Label: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + Merge: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + } + tests := []struct { + name string + path *field.Path + modifier v1alpha1.Modifier + want field.ErrorList + }{{ + name: "invalid match", + path: field.NewPath("foo"), + modifier: v1alpha1.Modifier{ + Match: &v1alpha1.Match{}, + Label: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }, + want: field.ErrorList{ + field.Invalid(field.NewPath("foo").Child("match"), &v1alpha1.Check{}, "a value must be specified"), + }, + }, { + name: "no statement", + path: field.NewPath("foo"), + modifier: v1alpha1.Modifier{}, + want: field.ErrorList{ + field.Invalid(field.NewPath("foo"), v1alpha1.Modifier{}, "no statement found in modifier"), + }, + }, { + name: "annotate", + path: field.NewPath("foo"), + modifier: v1alpha1.Modifier{ + Annotate: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }, + want: nil, + }, { + name: "label", + path: field.NewPath("foo"), + modifier: v1alpha1.Modifier{ + Label: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }, + want: nil, + }, { + name: "merge", + path: field.NewPath("foo"), + modifier: v1alpha1.Modifier{ + Merge: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }, + want: nil, + }, { + name: "multiple statements", + path: field.NewPath("foo"), + modifier: multiple, + want: field.ErrorList{ + field.Invalid(field.NewPath("foo"), multiple, "only one statement is allowed per modifier (found 2)"), + }, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ValidateModifier(tt.path, tt.modifier) + assert.Equal(t, tt.want, got) + }) + } +} diff --git a/pkg/validation/modifiers_test.go b/pkg/validation/modifiers_test.go new file mode 100644 index 000000000..993512524 --- /dev/null +++ b/pkg/validation/modifiers_test.go @@ -0,0 +1,59 @@ +package validation + +import ( + "testing" + + "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" + "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func TestValidateModifiers(t *testing.T) { + tests := []struct { + name string + path *field.Path + modifiers []v1alpha1.Modifier + want field.ErrorList + }{{ + name: "nil", + path: nil, + modifiers: nil, + want: nil, + }, { + name: "empty", + path: field.NewPath("foo"), + modifiers: []v1alpha1.Modifier{}, + want: nil, + }, { + name: "invalid match", + path: field.NewPath("foo"), + modifiers: []v1alpha1.Modifier{{ + Match: &v1alpha1.Match{}, + Label: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }}, + want: field.ErrorList{ + field.Invalid(field.NewPath("foo").Index(0).Child("match"), &v1alpha1.Check{}, "a value must be specified"), + }, + }, { + name: "valid", + path: field.NewPath("foo"), + modifiers: []v1alpha1.Modifier{{ + Annotate: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }}, + want: nil, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ValidateModifiers(tt.path, tt.modifiers...) + assert.Equal(t, tt.want, got) + }) + } +}