Skip to content

Commit a99249c

Browse files
committed
test(operator): pin overwrite-rollback contract, minor polish
Address the fourth-pass self-review's non-blocking NITs: * Add TestBumpArtifactGeneratorRequeue_PatchFailure_RollbackRestoresPriorBump to cover the subtle case the two existing rollback tests miss: when the tracking annotations were already set from a prior bump, a failed Patch must restore the PRIOR values, not delete the keys. A refactor that naively swapped rollback for `delete(ag.Annotations, key)` would pass the nil-rollback tests but silently regress this contract. * Rename the cloneAnnotations subtest from "deep copy" to "independent copy" — the code documents shallow copy semantics and the misnomer was the only thing pointing readers at the wrong mental model. * Hoist `now.UTC().Format(time.RFC3339Nano)` to a local so the two timestamp annotations use one formatted value instead of two identical Format calls. Signed-off-by: Ivan Okhotnikov <[email protected]>
1 parent 0958b81 commit a99249c

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎internal/operator/packagesource_reconciler.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,10 @@ func (r *PackageSourceReconciler) bumpArtifactGeneratorRequeue(ctx context.Conte
684684
if ag.Annotations == nil {
685685
ag.Annotations = map[string]string{}
686686
}
687-
ag.Annotations[annotationFluxRequestedAt] = now.UTC().Format(time.RFC3339Nano)
687+
nowStr := now.UTC().Format(time.RFC3339Nano)
688+
ag.Annotations[annotationFluxRequestedAt] = nowStr
688689
ag.Annotations[annotationRequeueAttempts] = strconv.Itoa(nextAttempt)
689-
ag.Annotations[annotationLastRequeueAt] = now.UTC().Format(time.RFC3339Nano)
690+
ag.Annotations[annotationLastRequeueAt] = nowStr
690691
if err := r.Patch(ctx, ag, client.MergeFrom(patchBase)); err != nil {
691692
ag.Annotations = priorAnnotations
692693
return err

‎internal/operator/packagesource_reconciler_test.go‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func TestCloneAnnotations(t *testing.T) {
392392
t.Errorf("cloneAnnotations(empty) len = %d, want 0", len(got))
393393
}
394394
})
395-
t.Run("populated in, deep copy out", func(t *testing.T) {
395+
t.Run("populated in, independent copy out", func(t *testing.T) {
396396
src := map[string]string{"a": "1", "b": "2"}
397397
got := cloneAnnotations(src)
398398
if len(got) != 2 || got["a"] != "1" || got["b"] != "2" {
@@ -519,6 +519,41 @@ func TestBumpArtifactGeneratorRequeue_PatchFailure_RollbackPreservesUnrelated(t
519519
}
520520
}
521521

522+
// TestBumpArtifactGeneratorRequeue_PatchFailure_RollbackRestoresPriorBump
523+
// covers the rollback contract in its most subtle form: our tracking keys are
524+
// already present from an EARLIER bump, and the new bump's Patch fails. The
525+
// rollback must restore the PRIOR bump's values, not delete the keys — a
526+
// refactor that naively used `delete(ag.Annotations, key)` on failure would
527+
// pass the two nil-rollback tests but silently regress this one.
528+
func TestBumpArtifactGeneratorRequeue_PatchFailure_RollbackRestoresPriorBump(t *testing.T) {
529+
priorTimestamp := referenceTime.Add(-time.Minute).UTC().Format(time.RFC3339Nano)
530+
ag := newAG(map[string]string{
531+
annotationFluxRequestedAt: priorTimestamp,
532+
annotationRequeueAttempts: "1",
533+
annotationLastRequeueAt: priorTimestamp,
534+
})
535+
baseClient := fake.NewClientBuilder().WithScheme(testScheme(t)).WithObjects(ag).Build()
536+
failing := interceptor.NewClient(baseClient, interceptor.Funcs{
537+
Patch: func(_ context.Context, _ client.WithWatch, _ client.Object, _ client.Patch, _ ...client.PatchOption) error {
538+
return errors.New("simulated Patch failure")
539+
},
540+
})
541+
r := &PackageSourceReconciler{Client: failing, Scheme: testScheme(t)}
542+
543+
if err := r.bumpArtifactGeneratorRequeue(context.Background(), ag, referenceTime, 2); err == nil {
544+
t.Fatal("bump succeeded, want error")
545+
}
546+
if got := ag.Annotations[annotationRequeueAttempts]; got != "1" {
547+
t.Errorf("annotationRequeueAttempts = %q after rollback, want prior value 1", got)
548+
}
549+
if got := ag.Annotations[annotationFluxRequestedAt]; got != priorTimestamp {
550+
t.Errorf("annotationFluxRequestedAt = %q after rollback, want prior %q", got, priorTimestamp)
551+
}
552+
if got := ag.Annotations[annotationLastRequeueAt]; got != priorTimestamp {
553+
t.Errorf("annotationLastRequeueAt = %q after rollback, want prior %q", got, priorTimestamp)
554+
}
555+
}
556+
522557
// TestClearRequeueTracking_Noop asserts that if the AG has no tracking
523558
// annotations, we don't issue a Patch at all — otherwise every healthy
524559
// reconcile would generate a no-op write.

0 commit comments

Comments
 (0)