Skip to content

Commit 837ff67

Browse files
committed
Adding more scenarios for reconciliation processor testing
Signed-off-by: zhaque44 <[email protected]>
1 parent 71e6ca6 commit 837ff67

File tree

1 file changed

+116
-4
lines changed

1 file changed

+116
-4
lines changed

internal/reconcile/summarize/processor_test.go

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package summarize
1818

1919
import (
2020
"context"
21+
"errors"
22+
"strings"
2123
"testing"
2224

2325
. "github.com/onsi/gomega"
@@ -65,28 +67,138 @@ func TestRecordReconcileReq(t *testing.T) {
6567
t.Expect(obj).To(HaveStatusLastHandledReconcileAt("now"))
6668
},
6769
},
70+
// Additional test cases for bulletproof coverage:
71+
{
72+
name: "empty reconcile annotation value",
73+
beforeFunc: func(obj client.Object) {
74+
annotations := map[string]string{
75+
meta.ReconcileRequestAnnotation: "",
76+
}
77+
obj.SetAnnotations(annotations)
78+
},
79+
afterFunc: func(t *WithT, obj client.Object) {
80+
t.Expect(obj).To(HaveStatusLastHandledReconcileAt(""))
81+
},
82+
},
83+
{
84+
name: "whitespace-only reconcile annotation value",
85+
beforeFunc: func(obj client.Object) {
86+
annotations := map[string]string{
87+
meta.ReconcileRequestAnnotation: " ",
88+
}
89+
obj.SetAnnotations(annotations)
90+
},
91+
afterFunc: func(t *WithT, obj client.Object) {
92+
t.Expect(obj).To(HaveStatusLastHandledReconcileAt(" "))
93+
},
94+
},
95+
{
96+
name: "reconcile annotation with special characters",
97+
beforeFunc: func(obj client.Object) {
98+
annotations := map[string]string{
99+
meta.ReconcileRequestAnnotation: "2024-01-15T10:30:00Z",
100+
}
101+
obj.SetAnnotations(annotations)
102+
},
103+
afterFunc: func(t *WithT, obj client.Object) {
104+
t.Expect(obj).To(HaveStatusLastHandledReconcileAt("2024-01-15T10:30:00Z"))
105+
},
106+
},
107+
{
108+
name: "reconcile annotation with very long value",
109+
beforeFunc: func(obj client.Object) {
110+
longValue := strings.Repeat("a", 1000)
111+
annotations := map[string]string{
112+
meta.ReconcileRequestAnnotation: longValue,
113+
}
114+
obj.SetAnnotations(annotations)
115+
},
116+
afterFunc: func(t *WithT, obj client.Object) {
117+
longValue := strings.Repeat("a", 1000)
118+
t.Expect(obj).To(HaveStatusLastHandledReconcileAt(longValue))
119+
},
120+
},
121+
{
122+
name: "reconcile annotation mixed with other annotations",
123+
beforeFunc: func(obj client.Object) {
124+
annotations := map[string]string{
125+
"some.other/annotation": "other-value",
126+
meta.ReconcileRequestAnnotation: "mixed-test",
127+
"another/annotation": "another-value",
128+
}
129+
obj.SetAnnotations(annotations)
130+
},
131+
afterFunc: func(t *WithT, obj client.Object) {
132+
t.Expect(obj).To(HaveStatusLastHandledReconcileAt("mixed-test"))
133+
t.Expect(obj.GetAnnotations()).To(HaveKeyWithValue("some.other/annotation", "other-value"))
134+
t.Expect(obj.GetAnnotations()).To(HaveKeyWithValue("another/annotation", "another-value"))
135+
},
136+
},
137+
{
138+
name: "reconcile annotation overwrites existing status value",
139+
beforeFunc: func(obj client.Object) {
140+
// Set initial status
141+
object.SetStatusLastHandledReconcileAt(obj, "old-value")
142+
// Then set annotation
143+
annotations := map[string]string{
144+
meta.ReconcileRequestAnnotation: "new-value",
145+
}
146+
obj.SetAnnotations(annotations)
147+
},
148+
afterFunc: func(t *WithT, obj client.Object) {
149+
t.Expect(obj).To(HaveStatusLastHandledReconcileAt("new-value"))
150+
},
151+
},
152+
{
153+
name: "nil annotations map",
154+
beforeFunc: func(obj client.Object) {
155+
obj.SetAnnotations(nil)
156+
},
157+
afterFunc: func(t *WithT, obj client.Object) {
158+
t.Expect(obj).To(HaveStatusLastHandledReconcileAt(""))
159+
},
160+
},
68161
}
69162

70163
for _, tt := range tests {
71164
t.Run(tt.name, func(t *testing.T) {
72165
g := NewWithT(t)
73-
74166
obj := &sourcev1.GitRepository{
75167
ObjectMeta: metav1.ObjectMeta{
76168
GenerateName: "test-obj",
77169
},
78170
}
79-
80171
if tt.beforeFunc != nil {
81172
tt.beforeFunc(obj)
82173
}
83-
84174
ctx := context.TODO()
85175
RecordReconcileReq(ctx, record.NewFakeRecorder(32), obj, reconcile.ResultEmpty, nil)
86-
87176
if tt.afterFunc != nil {
88177
tt.afterFunc(g, obj)
89178
}
90179
})
91180
}
92181
}
182+
183+
func TestRecordReconcileReq_ParameterHandling(t *testing.T) {
184+
g := NewWithT(t)
185+
obj := &sourcev1.GitRepository{
186+
ObjectMeta: metav1.ObjectMeta{
187+
GenerateName: "test-obj",
188+
Annotations: map[string]string{
189+
meta.ReconcileRequestAnnotation: "param-test",
190+
},
191+
},
192+
}
193+
194+
ctx := context.TODO()
195+
196+
RecordReconcileReq(ctx, nil, obj, reconcile.ResultEmpty, nil)
197+
g.Expect(obj).To(HaveStatusLastHandledReconcileAt("param-test"))
198+
199+
RecordReconcileReq(ctx, record.NewFakeRecorder(32), obj, reconcile.Result{Requeue: true}, nil)
200+
g.Expect(obj).To(HaveStatusLastHandledReconcileAt("param-test"))
201+
202+
RecordReconcileReq(ctx, record.NewFakeRecorder(32), obj, reconcile.ResultEmpty, errors.New("test error"))
203+
g.Expect(obj).To(HaveStatusLastHandledReconcileAt("param-test"))
204+
}

0 commit comments

Comments
 (0)