1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-07 00:17:13 +00:00
kyverno/pkg/cel/exception/compiler_test.go
Mariam Fahmy 202ab74ff5
feat: compile CEL exceptions (#12066)
Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>
2025-02-03 17:17:41 +02:00

51 lines
1.3 KiB
Go

package exception
import (
"testing"
kyvernov2alpha1 "github.com/kyverno/kyverno/api/kyverno/v2alpha1"
"github.com/stretchr/testify/assert"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Test_compiler_Compile(t *testing.T) {
tests := []struct {
name string
exception *kyvernov2alpha1.CELPolicyException
wantErr bool
}{
{
name: "simple",
exception: &kyvernov2alpha1.CELPolicyException{
TypeMeta: metav1.TypeMeta{
APIVersion: kyvernov2alpha1.GroupVersion.String(),
Kind: "CELPolicyException",
},
ObjectMeta: metav1.ObjectMeta{
Name: "exception",
},
Spec: kyvernov2alpha1.CELPolicyExceptionSpec{
MatchConditions: []admissionregistrationv1.MatchCondition{
{
Name: "check env label",
Expression: "has(object.metadata.labels) && 'env' in object.metadata.labels && object.metadata.labels['env'] == 'prod'",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewCompiler()
compiled, errs := c.Compile(tt.exception)
if tt.wantErr {
assert.Error(t, errs.ToAggregate())
} else {
assert.NoError(t, errs.ToAggregate())
assert.NotNil(t, compiled)
}
})
}
}