@@ -23,14 +23,27 @@ import (
2323
2424type whateverStatus struct {
2525 ReconcileRequestStatus `json:",inline"`
26+ ForceRequestStatus `json:",inline"`
2627}
2728
2829type whatever struct {
2930 Annotations map [string ]string
30- Status whateverStatus `json:"status,omitempty "`
31+ Status whateverStatus `json:"status"`
3132}
3233
33- func TestGetAnnotationValue (t * testing.T ) {
34+ func (w * whatever ) GetAnnotations () map [string ]string {
35+ return w .Annotations
36+ }
37+
38+ func (w * whatever ) GetLastHandledReconcileRequest () string {
39+ return w .Status .GetLastHandledReconcileRequest ()
40+ }
41+
42+ func (w * whatever ) GetLastHandledForceRequestStatus () * string {
43+ return & w .Status .LastHandledForceAt
44+ }
45+
46+ func TestGetReconcileAnnotationValue (t * testing.T ) {
3447 obj := whatever {
3548 Annotations : map [string ]string {},
3649 }
@@ -65,3 +78,112 @@ func TestGetAnnotationValue(t *testing.T) {
6578 t .Error ("expected to detect change in annotation value" )
6679 }
6780}
81+
82+ func TestShouldHandleForceRequest (t * testing.T ) {
83+ obj := & whatever {
84+ Annotations : map [string ]string {
85+ ReconcileRequestAnnotation : "b" ,
86+ ForceRequestAnnotation : "b" ,
87+ },
88+ Status : whateverStatus {
89+ ReconcileRequestStatus : ReconcileRequestStatus {
90+ LastHandledReconcileAt : "a" ,
91+ },
92+ ForceRequestStatus : ForceRequestStatus {
93+ LastHandledForceAt : "a" ,
94+ },
95+ },
96+ }
97+
98+ if ! ShouldHandleForceRequest (obj ) {
99+ t .Error ("ShouldHandleForceRequest() = false" )
100+ }
101+
102+ if obj .Status .LastHandledForceAt != "b" {
103+ t .Error ("ShouldHandleForceRequest did not update LastHandledForceAt" )
104+ }
105+ }
106+
107+ func TestHandleAnnotationRequest (t * testing.T ) {
108+ const requestAnnotation = "requestAnnotation"
109+
110+ tests := []struct {
111+ name string
112+ annotations map [string ]string
113+ lastHandledReconcile string
114+ lastHandledRequest string
115+ want bool
116+ expectLastHandledRequest string
117+ }{
118+ {
119+ name : "valid request and reconcile annotations" ,
120+ annotations : map [string ]string {
121+ ReconcileRequestAnnotation : "b" ,
122+ requestAnnotation : "b" ,
123+ },
124+ want : true ,
125+ expectLastHandledRequest : "b" ,
126+ },
127+ {
128+ name : "mismatched annotations" ,
129+ annotations : map [string ]string {
130+ ReconcileRequestAnnotation : "b" ,
131+ requestAnnotation : "c" ,
132+ },
133+ want : false ,
134+ expectLastHandledRequest : "c" ,
135+ },
136+ {
137+ name : "reconcile matches previous request" ,
138+ annotations : map [string ]string {
139+ ReconcileRequestAnnotation : "b" ,
140+ requestAnnotation : "b" ,
141+ },
142+ lastHandledReconcile : "a" ,
143+ lastHandledRequest : "b" ,
144+ want : false ,
145+ expectLastHandledRequest : "b" ,
146+ },
147+ {
148+ name : "request matches previous reconcile" ,
149+ annotations : map [string ]string {
150+ ReconcileRequestAnnotation : "b" ,
151+ requestAnnotation : "b" ,
152+ },
153+ lastHandledReconcile : "b" ,
154+ lastHandledRequest : "a" ,
155+ want : false ,
156+ expectLastHandledRequest : "b" ,
157+ },
158+ {
159+ name : "missing annotations" ,
160+ annotations : map [string ]string {},
161+ lastHandledRequest : "a" ,
162+ want : false ,
163+ expectLastHandledRequest : "a" ,
164+ },
165+ }
166+
167+ for _ , tt := range tests {
168+ t .Run (tt .name , func (t * testing.T ) {
169+ obj := & whatever {
170+ Annotations : tt .annotations ,
171+ Status : whateverStatus {
172+ ReconcileRequestStatus : ReconcileRequestStatus {
173+ LastHandledReconcileAt : tt .lastHandledReconcile ,
174+ },
175+ },
176+ }
177+
178+ lastHandled := tt .lastHandledRequest
179+ result := HandleAnnotationRequest (obj , requestAnnotation , & lastHandled )
180+
181+ if result != tt .want {
182+ t .Errorf ("HandleAnnotationRequest() = %v, want %v" , result , tt .want )
183+ }
184+ if lastHandled != tt .expectLastHandledRequest {
185+ t .Errorf ("lastHandledRequest = %v, want %v" , lastHandled , tt .expectLastHandledRequest )
186+ }
187+ })
188+ }
189+ }
0 commit comments