Skip to content

Commit

Permalink
chore: add modifier validation tests (#852)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Feb 12, 2024
1 parent e1dd4a4 commit a1ea0d3
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
97 changes: 97 additions & 0 deletions pkg/validation/modifier_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
59 changes: 59 additions & 0 deletions pkg/validation/modifiers_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit a1ea0d3

Please sign in to comment.