From 124b0a3abda35a58b01d8a58c7eaefd81fe2a9bd Mon Sep 17 00:00:00 2001 From: Devaansh Bhandari <107868772+brf153@users.noreply.github.com> Date: Tue, 18 Jun 2024 12:17:18 +0530 Subject: [PATCH] add test for HasAutoGenAnnotation (#10487) Signed-off-by: brf153 <153hsb@gmail.com> --- api/kyverno/v1/clusterpolicy_test.go | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/api/kyverno/v1/clusterpolicy_test.go b/api/kyverno/v1/clusterpolicy_test.go index c9e4722c38..1d49232112 100644 --- a/api/kyverno/v1/clusterpolicy_test.go +++ b/api/kyverno/v1/clusterpolicy_test.go @@ -53,3 +53,37 @@ func Test_ClusterPolicy_Autogen_All(t *testing.T) { assert.Equal(t, len(errs), 1) assert.Equal(t, errs[0].Error(), "metadata.annotations: Forbidden: Autogen annotation does not support 'all' anymore, remove the annotation or set it to a valid value") } + +func Test_ClusterPolicy_HasAutoGenAnnotation(t *testing.T) { + tests := []struct { + name string + annotations map[string]string + expected bool + }{ + { + name: "Policy with AutoGen annotation (true)", + annotations: map[string]string{kyverno.AnnotationAutogenControllers: "pod-policies.kyverno.io/autogen-controllers"}, + expected: true, + }, + { + name: "Policy with AutoGen annotation (false)", + annotations: map[string]string{kyverno.AnnotationAutogenControllers: "none"}, + expected: false, + }, + { + name: "Policy without AutoGen annotation", + annotations: map[string]string{}, + expected: false, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + policy := &ClusterPolicy{ObjectMeta: metav1.ObjectMeta{Annotations: tc.annotations}} + result := policy.HasAutoGenAnnotation() + if result != tc.expected { + t.Errorf("Expected HasAutoGenAnnotation for policy %s to be %t, but got %t", tc.name, tc.expected, result) + } + }) + } +}