mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-10 09:56:55 +00:00
* feat: support conditions in PolicyException Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * fix matchesException func Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * add codegen-all files Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * fix after review Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * remove variable validation from PolicyException Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * fix after review Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * add kuttl tests Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * remove ValidateVariables() from tests Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * fix errors Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * remove check-variables kuttl test Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * fix after review Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> * add sleep step to kuttl Signed-off-by: Rakshit Gondwal <98955085+rakshitgondwal@users.noreply.github.com> * miinor fix Signed-off-by: Rakshit Gondwal <98955085+rakshitgondwal@users.noreply.github.com> * add readme for kuttl test Signed-off-by: Rakshit Gondwal <98955085+rakshitgondwal@users.noreply.github.com> --------- Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com> Signed-off-by: Rakshit Gondwal <98955085+rakshitgondwal@users.noreply.github.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com>
76 lines
2.5 KiB
Go
76 lines
2.5 KiB
Go
package exception
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
|
admissionutils "github.com/kyverno/kyverno/pkg/utils/admission"
|
|
"gotest.tools/assert"
|
|
)
|
|
|
|
func Test_Validate(t *testing.T) {
|
|
type args struct {
|
|
opts ValidationOptions
|
|
resource []byte
|
|
}
|
|
tc := []struct {
|
|
name string
|
|
args args
|
|
want int
|
|
}{
|
|
{
|
|
name: "PolicyExceptions disabled.",
|
|
args: args{
|
|
opts: ValidationOptions{
|
|
Enabled: false,
|
|
Namespace: "kyverno",
|
|
},
|
|
resource: []byte(`{"apiVersion":"kyverno.io/v2beta1","kind":"PolicyException","metadata":{"name":"enforce-label-exception","namespace":"delta"},"spec":{"exceptions":[{"policyName":"enforce-label","ruleNames":["enforce-label"]}],"match":{"any":[{"resources":{"kinds":["Pod"]}}]}}}`),
|
|
},
|
|
want: 1,
|
|
},
|
|
{
|
|
name: "PolicyExceptions enabled. Defined namespace doesn't match namespace passed.",
|
|
args: args{
|
|
opts: ValidationOptions{
|
|
Enabled: true,
|
|
Namespace: "kyverno",
|
|
},
|
|
resource: []byte(`{"apiVersion":"kyverno.io/v2beta1","kind":"PolicyException","metadata":{"name":"enforce-label-exception","namespace":"delta"},"spec":{"exceptions":[{"policyName":"enforce-label","ruleNames":["enforce-label"]}],"match":{"any":[{"resources":{"kinds":["Pod"]}}]}}}`),
|
|
},
|
|
want: 1,
|
|
},
|
|
{
|
|
name: "PolicyExceptions enabled. Defined namespace matches namespace passed",
|
|
args: args{
|
|
opts: ValidationOptions{
|
|
Enabled: true,
|
|
Namespace: "kyverno",
|
|
},
|
|
resource: []byte(`{"apiVersion":"kyverno.io/v2beta1","kind":"PolicyException","metadata":{"name":"enforce-label-exception","namespace":"kyverno"},"spec":{"exceptions":[{"policyName":"enforce-label","ruleNames":["enforce-label"]}],"match":{"any":[{"resources":{"kinds":["Pod"]}}]}}}`),
|
|
},
|
|
want: 0,
|
|
},
|
|
{
|
|
name: "PolicyExceptions enabled. No namespace defined",
|
|
args: args{
|
|
opts: ValidationOptions{
|
|
Enabled: true,
|
|
Namespace: "",
|
|
},
|
|
resource: []byte(`{"apiVersion":"kyverno.io/v2beta1","kind":"PolicyException","metadata":{"name":"enforce-label-exception","namespace":"kyverno"},"spec":{"exceptions":[{"policyName":"enforce-label","ruleNames":["enforce-label"]}],"match":{"any":[{"resources":{"kinds":["Pod"]}}]}}}`),
|
|
},
|
|
want: 0,
|
|
},
|
|
}
|
|
for _, c := range tc {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
polex, err := admissionutils.UnmarshalPolicyException(c.args.resource)
|
|
assert.NilError(t, err)
|
|
warnings, err := Validate(context.Background(), logging.GlobalLogger(), polex, c.args.opts)
|
|
assert.NilError(t, err)
|
|
assert.Assert(t, len(warnings) == c.want)
|
|
})
|
|
}
|
|
}
|