1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 09:26:54 +00:00
kyverno/cmd/cli/kubectl-kyverno/exception/load_test.go
Mariam Fahmy 76751b96b3
feat: support celexceptions in the CLI apply command (#12182)
* feat: support celexceptions in the CLI

Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>

* feat: add unit tests

Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>

---------

Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>
2025-02-19 08:38:44 +00:00

88 lines
2.8 KiB
Go

package exception
import (
"os"
"testing"
kyvernov2 "github.com/kyverno/kyverno/api/kyverno/v2"
kyvernov2beta1 "github.com/kyverno/kyverno/api/kyverno/v2beta1"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
func Test_load(t *testing.T) {
tests := []struct {
name string
policies string
exceptionsLoaded int
celExceptionsLoaded int
wantErr bool
}{{
name: "not a policy exception",
policies: "../_testdata/resources/namespace.yaml",
wantErr: true,
}, {
name: "policy exception",
policies: "../_testdata/exceptions/exception.yaml",
exceptionsLoaded: 1,
celExceptionsLoaded: 0,
}, {
name: "policy exception and policy",
policies: "../_testdata/exceptions/exception-and-policy.yaml",
wantErr: true,
}, {
name: "cel policy exception",
policies: "../_testdata/exceptions/celexception.yaml",
exceptionsLoaded: 0,
celExceptionsLoaded: 1,
}, {
name: "cel policy exception and policy",
policies: "../_testdata/exceptions/exception-and-celexception.yaml",
exceptionsLoaded: 1,
celExceptionsLoaded: 1,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bytes, err := os.ReadFile(tt.policies)
require.NoError(t, err)
require.NoError(t, err)
if res, err := load(bytes); (err != nil) != tt.wantErr {
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
} else if res != nil {
if len(res.Exceptions) != tt.exceptionsLoaded {
t.Errorf("Load() loaded amount = %v, wantLoaded %v", len(res.Exceptions), tt.exceptionsLoaded)
} else if len(res.CELExceptions) != tt.celExceptionsLoaded {
t.Errorf("Load() loaded amount = %v, wantLoaded %v", len(res.CELExceptions), tt.celExceptionsLoaded)
}
}
})
}
}
func Test_SelectFrom(t *testing.T) {
resources := toUnstructured(t,
&corev1.ConfigMap{TypeMeta: v1.TypeMeta{Kind: "ConfigMap", APIVersion: "v1"}},
&kyvernov2.PolicyException{TypeMeta: v1.TypeMeta{
Kind: exceptionV2.Kind, APIVersion: exceptionV2.GroupVersion().String()},
},
&kyvernov2beta1.PolicyException{TypeMeta: v1.TypeMeta{
Kind: exceptionV2beta1.Kind, APIVersion: exceptionV2beta1.GroupVersion().String()},
},
)
exceptions := SelectFrom(resources)
require.Len(t, exceptions, 2)
}
func toUnstructured(t *testing.T, in ...interface{}) []*unstructured.Unstructured {
var resources []*unstructured.Unstructured
for _, r := range in {
us, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r)
require.NoError(t, err)
resources = append(resources, &unstructured.Unstructured{Object: us})
}
return resources
}