-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add modifier validation tests (#852)
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
e1dd4a4
commit a1ea0d3
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
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,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) | ||
}) | ||
} | ||
} |
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,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) | ||
}) | ||
} | ||
} |