1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 17:37:12 +00:00
kyverno/pkg/validation/exception/globalcontext/validate_test.go

82 lines
2.6 KiB
Go
Raw Normal View History

feat: register webhook configurations for validatingpolicies (#11892) * feat: add spec.webhookConfiguration Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: refactor build webhook for kyverno policies Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update yamls Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: add listers Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: update api Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: remove matchPolicy Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update crd yaml Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: add short name Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update deepcopy Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: upadte spec Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: fix description Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: add missing files Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: register webhook for validatingpolicies Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: fix import Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: add unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update docs Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update manifests Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update unit tests Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update manifests Signed-off-by: ShutingZhao <shuting@nirmata.com> --------- Signed-off-by: ShutingZhao <shuting@nirmata.com>
2025-01-17 17:33:47 +08:00
package globalcontext
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
wantErr bool
}{
{
name: "GlobalContextEntry disabled.",
args: args{
opts: ValidationOptions{
Enabled: false,
},
resource: []byte(`{"apiVersion":"kyverno.io/v2alpha1","kind":"GlobalContextEntry","metadata":{"name":"ingress"},"spec":{"apiCall":{"service":{"url":"https://svc.kyverno/example","caBundle":"-----BEGIN CERTIFICATE-----\n-----REDACTED-----\n-----END CERTIFICATE-----"},"refreshInterval":"10ns"}}}`),
},
want: 1,
wantErr: false,
},
{
name: "GlobalContextEntry enabled, both KubernetesResource and APICall present",
args: args{
opts: ValidationOptions{
Enabled: true,
},
resource: []byte(`{"apiVersion":"kyverno.io/v2alpha1","kind":"GlobalContextEntry","metadata":{"name":"ingress"},"spec":{"apiCall":{"service":{"url":"https://svc.kyverno/example","caBundle":"-----BEGIN CERTIFICATE-----\n-----REDACTED-----\n-----END CERTIFICATE-----"},"refreshInterval":"10ns"},"kubernetesResource":{"group":"apis/networking.k8s.io","version":"v1","resource":"ingresses","namespace":"apps"}}}`),
},
want: 0,
wantErr: true,
},
{
name: "GlobalContextEntry enabled, neither KubernetesResource nor APICall present",
args: args{
opts: ValidationOptions{
Enabled: true,
},
resource: []byte(`{"apiVersion":"kyverno.io/v2alpha1","kind":"GlobalContextEntry","metadata":{"name":"ingress"},"spec":{}}`),
},
want: 0,
wantErr: true,
},
{
name: "GlobalContextEntry enabled.",
args: args{
opts: ValidationOptions{
Enabled: true,
},
resource: []byte(`{"apiVersion":"kyverno.io/v2alpha1","kind":"GlobalContextEntry","metadata":{"name":"ingress"},"spec":{"apiCall":{"service":{"url":"https://svc.kyverno/example","caBundle":"-----BEGIN CERTIFICATE-----\n-----REDACTED-----\n-----END CERTIFICATE-----"},"refreshInterval":"10ns"}}}`),
},
want: 0,
wantErr: false,
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
gctx, err := admissionutils.UnmarshalGlobalContextEntry(c.args.resource)
assert.NilError(t, err)
warnings, err := Validate(context.Background(), logging.GlobalLogger(), gctx, c.args.opts)
if c.wantErr {
assert.Assert(t, err != nil)
} else {
assert.NilError(t, err)
}
assert.Assert(t, len(warnings) == c.want)
})
}
}