@@ -10,6 +10,7 @@ import (
1010
1111 "github.com/stretchr/testify/assert"
1212 admv1 "k8s.io/api/admissionregistration/v1"
13+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314)
1415
1516func TestGetValidatingAdmissionPolicy (t * testing.T ) {
@@ -50,9 +51,121 @@ func TestGetValidatingAdmissionPolicy(t *testing.T) {
5051 })
5152}
5253
54+ func TestMutateValidatingAdmissionPolicy (t * testing.T ) {
55+ t .Parallel ()
56+
57+ tests := []struct {
58+ name string
59+ isHub bool
60+ resourceVersion string
61+ initialLabels map [string ]string
62+ }{
63+ {
64+ name : "preserves ResourceVersion and updates labels for member cluster" ,
65+ isHub : false ,
66+ resourceVersion : "12345" ,
67+ initialLabels : map [string ]string {"existing" : "label" },
68+ },
69+ {
70+ name : "preserves ResourceVersion and updates labels for hub cluster" ,
71+ isHub : true ,
72+ resourceVersion : "67890" ,
73+ initialLabels : map [string ]string {"old" : "value" },
74+ },
75+ {
76+ name : "preserves empty ResourceVersion and sets labels" ,
77+ isHub : false ,
78+ resourceVersion : "" ,
79+ initialLabels : nil ,
80+ },
81+ {
82+ name : "overwrites existing managed label while preserving ResourceVersion" ,
83+ isHub : false ,
84+ resourceVersion : "54321" ,
85+ initialLabels : map [string ]string {"fleet.azure.com/managed-by" : "old-value" , "other" : "label" },
86+ },
87+ }
88+
89+ for _ , tt := range tests {
90+ t .Run (tt .name , func (t * testing.T ) {
91+ t .Parallel ()
92+
93+ vap := & admv1.ValidatingAdmissionPolicy {
94+ ObjectMeta : metav1.ObjectMeta {
95+ Name : "test-policy" ,
96+ ResourceVersion : tt .resourceVersion ,
97+ Labels : tt .initialLabels ,
98+ },
99+ }
100+
101+ mutateValidatingAdmissionPolicy (vap , tt .isHub )
102+
103+ assert .Equal (t , tt .resourceVersion , vap .ResourceVersion , "ResourceVersion should be preserved" )
104+ assert .Equal (t , "arm" , vap .Labels ["fleet.azure.com/managed-by" ], "managed-by label should be set to 'arm'" )
105+
106+ // Verify that only the managed-by label exists (other labels are not preserved)
107+ expectedLabels := map [string ]string {
108+ "fleet.azure.com/managed-by" : "arm" ,
109+ }
110+ assert .Equal (t , expectedLabels , vap .Labels , "Only the managed-by label should exist" )
111+ })
112+ }
113+ }
114+
53115func TestGetValidatingAdmissionPolicyBinding (t * testing.T ) {
54116 t .Parallel ()
55117
56118 vap := getValidatingAdmissionPolicyBinding ()
57119 assert .NotNil (t , vap )
58120}
121+
122+ func TestMutateValidatingAdmissionPolicyBinding (t * testing.T ) {
123+ t .Parallel ()
124+
125+ tests := []struct {
126+ name string
127+ resourceVersion string
128+ initialLabels map [string ]string
129+ }{
130+ {
131+ name : "preserves ResourceVersion and updates labels" ,
132+ resourceVersion : "12345" ,
133+ initialLabels : map [string ]string {"existing" : "label" },
134+ },
135+ {
136+ name : "preserves empty ResourceVersion and sets labels" ,
137+ resourceVersion : "" ,
138+ initialLabels : nil ,
139+ },
140+ {
141+ name : "overwrites existing managed label while preserving ResourceVersion" ,
142+ resourceVersion : "67890" ,
143+ initialLabels : map [string ]string {"fleet.azure.com/managed-by" : "old-value" , "other" : "label" },
144+ },
145+ }
146+
147+ for _ , tt := range tests {
148+ t .Run (tt .name , func (t * testing.T ) {
149+ t .Parallel ()
150+
151+ vapb := & admv1.ValidatingAdmissionPolicyBinding {
152+ ObjectMeta : metav1.ObjectMeta {
153+ Name : "test-binding" ,
154+ ResourceVersion : tt .resourceVersion ,
155+ Labels : tt .initialLabels ,
156+ },
157+ }
158+
159+ mutateValidatingAdmissionPolicyBinding (vapb )
160+
161+ assert .Equal (t , tt .resourceVersion , vapb .ResourceVersion , "ResourceVersion should be preserved" )
162+ assert .Equal (t , "arm" , vapb .Labels ["fleet.azure.com/managed-by" ], "managed-by label should be set to 'arm'" )
163+
164+ // Verify that only the managed-by label exists (other labels are not preserved)
165+ expectedLabels := map [string ]string {
166+ "fleet.azure.com/managed-by" : "arm" ,
167+ }
168+ assert .Equal (t , expectedLabels , vapb .Labels , "Only the managed-by label should exist" )
169+ })
170+ }
171+ }
0 commit comments