@@ -17,192 +17,151 @@ limitations under the License.
1717package watch
1818
1919import (
20- "testing"
21-
2220 admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
2321 appsv1 "k8s.io/api/apps/v1"
2422 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2523 "sigs.k8s.io/controller-runtime/pkg/event"
24+
25+ . "github.com/onsi/ginkgo/v2"
26+ . "github.com/onsi/gomega"
2627)
2728
28- func TestMutatingWebhookConfigurationEventHandler_OnCreateFunc (t * testing.T ) {
29- var webhookName = "test"
30- var fakeWebhookName = "fakeTest"
31-
32- // 1. the Object is not mutatingWebhookConfiguration
33- createEvent := event.CreateEvent {
34- Object : & appsv1.DaemonSet {},
35- }
36- mutatingWebhookConfigurationEventHandler := & mutatingWebhookConfigurationEventHandler {}
37- f := mutatingWebhookConfigurationEventHandler .onCreateFunc (webhookName )
38- predicate := f (createEvent )
39-
40- if predicate {
41- t .Errorf ("The event %v should not be reconciled, but skip." , createEvent )
42- }
43-
44- // 2. the Object is mutatingWebhookConfiguration
45- createEvent = event.CreateEvent {
46- Object : & admissionregistrationv1.MutatingWebhookConfiguration {
47- ObjectMeta : metav1.ObjectMeta {
48- Name : webhookName ,
49- },
50- },
51- }
52-
53- f = mutatingWebhookConfigurationEventHandler .onCreateFunc (webhookName )
54- predicate = f (createEvent )
55-
56- if ! predicate {
57- t .Errorf ("The event %v should be reconciled, but skip." , createEvent )
58- }
59-
60- // 3. the Object is mutatingWebhookConfiguration
61- createEvent = event.CreateEvent {
62- Object : & admissionregistrationv1.MutatingWebhookConfiguration {
63- ObjectMeta : metav1.ObjectMeta {
64- Name : fakeWebhookName ,
65- },
66- },
67- }
68-
69- f = mutatingWebhookConfigurationEventHandler .onCreateFunc (webhookName )
70- predicate = f (createEvent )
71-
72- if predicate {
73- t .Errorf ("The event %v should not be reconciled, but skip." , createEvent )
74- }
75-
76- }
77-
78- func TestMutatingWebhookConfigurationEventHandler_OnUpdateFunc (t * testing.T ) {
79- var webhookName = "test"
80- var fakeWebhookName = "fakeTest"
81-
82- // 1. the Object is not mutatingWebhookConfiguration
83- updateEvent := event.UpdateEvent {
84- ObjectOld : & admissionregistrationv1.MutatingWebhookConfiguration {
85- ObjectMeta : metav1.ObjectMeta {
86- Name : webhookName ,
87- },
88- Webhooks : []admissionregistrationv1.MutatingWebhook {
89- {
90- Name : "old" ,
29+ var _ = Describe ("mutatingWebhookConfigurationEventHandler" , func () {
30+ var (
31+ webhookName = "test"
32+ fakeWebhookName = "fakeTest"
33+ handler * mutatingWebhookConfigurationEventHandler
34+ )
35+
36+ BeforeEach (func () {
37+ handler = & mutatingWebhookConfigurationEventHandler {}
38+ })
39+
40+ Describe ("onCreateFunc" , func () {
41+ It ("should not reconcile if the object is not a MutatingWebhookConfiguration" , func () {
42+ createEvent := event.CreateEvent {
43+ Object : & appsv1.DaemonSet {},
44+ }
45+ f := handler .onCreateFunc (webhookName )
46+ Expect (f (createEvent )).To (BeFalse ())
47+ })
48+
49+ It ("should reconcile if the object is a MutatingWebhookConfiguration with the correct name" , func () {
50+ createEvent := event.CreateEvent {
51+ Object : & admissionregistrationv1.MutatingWebhookConfiguration {
52+ ObjectMeta : metav1.ObjectMeta {
53+ Name : webhookName ,
54+ },
9155 },
92- },
93- },
94- ObjectNew : & appsv1.DaemonSet {},
95- }
96- mutatingWebhookConfigurationEventHandler := & mutatingWebhookConfigurationEventHandler {}
97- f := mutatingWebhookConfigurationEventHandler .onUpdateFunc (webhookName )
98- predicate := f (updateEvent )
99-
100- if predicate {
101- t .Errorf ("The event %v should not be reconciled, but skip." , updateEvent )
102- }
103-
104- updateEvent = event.UpdateEvent {
105- ObjectOld : & appsv1.DaemonSet {},
106- ObjectNew : & admissionregistrationv1.MutatingWebhookConfiguration {
107- ObjectMeta : metav1.ObjectMeta {
108- Name : webhookName ,
109- },
110- Webhooks : []admissionregistrationv1.MutatingWebhook {
111- {
112- Name : "new" ,
56+ }
57+ f := handler .onCreateFunc (webhookName )
58+ Expect (f (createEvent )).To (BeTrue ())
59+ })
60+
61+ It ("should not reconcile if the object is a MutatingWebhookConfiguration with a different name" , func () {
62+ createEvent := event.CreateEvent {
63+ Object : & admissionregistrationv1.MutatingWebhookConfiguration {
64+ ObjectMeta : metav1.ObjectMeta {
65+ Name : fakeWebhookName ,
66+ },
11367 },
114- },
115- },
116- }
117- f = mutatingWebhookConfigurationEventHandler .onUpdateFunc (webhookName )
118- predicate = f (updateEvent )
119-
120- if predicate {
121- t .Errorf ("The event %v should not be reconciled, but skip." , updateEvent )
122- }
123-
124- // 2. the Object is mutatingWebhookConfiguration and name is respect
125- updateEvent = event.UpdateEvent {
126- ObjectOld : & admissionregistrationv1.MutatingWebhookConfiguration {
127- ObjectMeta : metav1.ObjectMeta {
128- Name : webhookName ,
129- },
130- Webhooks : []admissionregistrationv1.MutatingWebhook {
131- {
132- Name : "old" ,
68+ }
69+ f := handler .onCreateFunc (webhookName )
70+ Expect (f (createEvent )).To (BeFalse ())
71+ })
72+ })
73+
74+ Describe ("onUpdateFunc" , func () {
75+ It ("should not reconcile if the new object is not a MutatingWebhookConfiguration" , func () {
76+ updateEvent := event.UpdateEvent {
77+ ObjectOld : & admissionregistrationv1.MutatingWebhookConfiguration {
78+ ObjectMeta : metav1.ObjectMeta {
79+ Name : webhookName ,
80+ },
81+ Webhooks : []admissionregistrationv1.MutatingWebhook {
82+ {Name : "old" },
83+ },
13384 },
134- },
135- },
136- ObjectNew : & admissionregistrationv1.MutatingWebhookConfiguration {
137- ObjectMeta : metav1.ObjectMeta {
138- Name : webhookName ,
139- },
140- Webhooks : []admissionregistrationv1.MutatingWebhook {
141- {
142- Name : "new" ,
85+ ObjectNew : & appsv1.DaemonSet {},
86+ }
87+ f := handler .onUpdateFunc (webhookName )
88+ Expect (f (updateEvent )).To (BeFalse ())
89+ })
90+
91+ It ("should not reconcile if the old object is not a MutatingWebhookConfiguration" , func () {
92+ updateEvent := event.UpdateEvent {
93+ ObjectOld : & appsv1.DaemonSet {},
94+ ObjectNew : & admissionregistrationv1.MutatingWebhookConfiguration {
95+ ObjectMeta : metav1.ObjectMeta {
96+ Name : webhookName ,
97+ },
98+ Webhooks : []admissionregistrationv1.MutatingWebhook {
99+ {Name : "new" },
100+ },
143101 },
144- },
145- },
146- }
147-
148- f = mutatingWebhookConfigurationEventHandler .onUpdateFunc (webhookName )
149- predicate = f (updateEvent )
150-
151- if ! predicate {
152- t .Errorf ("The event %v should be reconciled, but skip." , updateEvent )
153- }
154-
155- // 3. the Object is mutatingWebhookConfiguration and name is not respecr
156- updateEvent = event.UpdateEvent {
157- ObjectOld : & admissionregistrationv1.MutatingWebhookConfiguration {
158- ObjectMeta : metav1.ObjectMeta {
159- Name : fakeWebhookName ,
160- },
161- Webhooks : []admissionregistrationv1.MutatingWebhook {
162- {
163- Name : "old" ,
102+ }
103+ f := handler .onUpdateFunc (webhookName )
104+ Expect (f (updateEvent )).To (BeFalse ())
105+ })
106+
107+ It ("should reconcile if both objects are MutatingWebhookConfiguration with the correct name" , func () {
108+ updateEvent := event.UpdateEvent {
109+ ObjectOld : & admissionregistrationv1.MutatingWebhookConfiguration {
110+ ObjectMeta : metav1.ObjectMeta {
111+ Name : webhookName ,
112+ },
113+ Webhooks : []admissionregistrationv1.MutatingWebhook {
114+ {Name : "old" },
115+ },
164116 },
165- },
166- },
167- ObjectNew : & admissionregistrationv1.MutatingWebhookConfiguration {
168- ObjectMeta : metav1.ObjectMeta {
169- Name : fakeWebhookName ,
170- },
171- Webhooks : []admissionregistrationv1.MutatingWebhook {
172- {
173- Name : "new" ,
117+ ObjectNew : & admissionregistrationv1.MutatingWebhookConfiguration {
118+ ObjectMeta : metav1.ObjectMeta {
119+ Name : webhookName ,
120+ },
121+ Webhooks : []admissionregistrationv1.MutatingWebhook {
122+ {Name : "new" },
123+ },
174124 },
175- },
176- },
177- }
178-
179- f = mutatingWebhookConfigurationEventHandler .onUpdateFunc (webhookName )
180- predicate = f (updateEvent )
181-
182- if predicate {
183- t .Errorf ("The event %v should not be reconciled, but skip." , updateEvent )
184- }
185-
186- }
187-
188- func TestMutatingWebhookConfigurationEventHandler_OnDeleteFunc (t * testing.T ) {
189- var webhookName = "test"
190-
191- mutatingWebhookConfigurationEventHandler := & mutatingWebhookConfigurationEventHandler {}
192- f := mutatingWebhookConfigurationEventHandler .onDeleteFunc (webhookName )
193-
194- deleteEvent := event.DeleteEvent {
195- Object : & admissionregistrationv1.MutatingWebhookConfiguration {
196- ObjectMeta : metav1.ObjectMeta {
197- Name : webhookName ,
198- },
199- },
200- }
201-
202- predicate := f (deleteEvent )
203-
204- if predicate {
205- t .Errorf ("The event %v should not be skip, but not." , deleteEvent )
206- }
207-
208- }
125+ }
126+ f := handler .onUpdateFunc (webhookName )
127+ Expect (f (updateEvent )).To (BeTrue ())
128+ })
129+
130+ It ("should not reconcile if both objects are MutatingWebhookConfiguration with a different name" , func () {
131+ updateEvent := event.UpdateEvent {
132+ ObjectOld : & admissionregistrationv1.MutatingWebhookConfiguration {
133+ ObjectMeta : metav1.ObjectMeta {
134+ Name : fakeWebhookName ,
135+ },
136+ Webhooks : []admissionregistrationv1.MutatingWebhook {
137+ {Name : "old" },
138+ },
139+ },
140+ ObjectNew : & admissionregistrationv1.MutatingWebhookConfiguration {
141+ ObjectMeta : metav1.ObjectMeta {
142+ Name : fakeWebhookName ,
143+ },
144+ Webhooks : []admissionregistrationv1.MutatingWebhook {
145+ {Name : "new" },
146+ },
147+ },
148+ }
149+ f := handler .onUpdateFunc (webhookName )
150+ Expect (f (updateEvent )).To (BeFalse ())
151+ })
152+ })
153+
154+ Describe ("onDeleteFunc" , func () {
155+ It ("should not reconcile on delete" , func () {
156+ deleteEvent := event.DeleteEvent {
157+ Object : & admissionregistrationv1.MutatingWebhookConfiguration {
158+ ObjectMeta : metav1.ObjectMeta {
159+ Name : webhookName ,
160+ },
161+ },
162+ }
163+ f := handler .onDeleteFunc (webhookName )
164+ Expect (f (deleteEvent )).To (BeFalse ())
165+ })
166+ })
167+ })
0 commit comments