← Back to Pull requests

docs: proposal for unit tests that check state #5071

Open
kostis-codefresh opened this pull request with 150 lines diff across 1 files
View on GitHub
kostis-codefresh kostis-codefresh commented on Sep 30, 2026 10:41 UTC
Author

This is a proposal for the third bullet point mentioned here https://github.com/argoproj/argo-rollouts/issues/107

Checklist:

[ ] Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this is a chore.
[x] The title of the PR is (a) conventional with a list of types and scopes found here, (b) states what changed, and (c) suffixes the related issues number. E.g. "fix(controller): Updates such and such. Fixes #1234".
[x] I've signed my commits with DCO
[ ] My builds are green. Try syncing with master if they are not.
[ ] I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
[ ] I have run all tests locally (including the flaky ones) and they pass on my workstation
[ ] I have used LLM/AI/Agent tools for this PR but I am responsible for all code of this PR
[ ] I understand what the code does and WHY/HOW it works in several scenarios
[ ] I know if my code is just adding new functionality or changing old functionality for existing users
[x] My organization is added to USERS.md.

Diff: 150 lines (+150 / -0) across 1 files Reviewer: agaudreault Review duration: 41892m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.33) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.33 · 150 lines diff across 1 files
0.33
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.92 · 4 substantive comment(s)
0.92
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.300
Real PR Conversation Timeline
5 items
agaudreault agaudreault commented on code on Oct 19, 2026 17:47 UTC
Code Review
docs/proposals/unit-tests-verify-end-state.md:1
@@ -0,0 +1,150 @@
+---
+title: Unit tests that verify end state
+authors:
+  - '@kostis-codefresh'
+creation-date: 2026-09-30
+---
+
+# Unit tests that verify end state
+
+The unit tests of the Argo Rollouts controllers verify the exact sequence of Kubernetes API calls instead of the resulting state of the cluster.
+
+This document proposes a gradual migration to unit tests that verify the end state of the Kubernetes objects after a reconciliation.
+
+## Summary
+
+Most controller unit tests today describe *how* the controller talks to the API server ("first create a ReplicaSet, then update the Rollout, then patch its status, then update the ReplicaSet"),
+and not *what* the cluster should look like after the reconciliation ("the new ReplicaSet has 1 replica and the Rollout has the Progressing condition").
+
+As a result, any internal refactoring of the controller breaks a large number of tests, even when the behavior of the controller has not changed at all.
+
+## Motivation
+
+The main test fixture of the Rollout controller (see [rollout/controller_test.go](https://github.com/argoproj/argo-rollouts/blob/master/rollout/controller_test.go)) keeps
+a list of expected API actions. After every sync the actual actions recorded by the fake clientset are compared one by one against this list. Tests then fetch the
+objects that were sent to the API server by their position in the list.
+
+This pattern is used everywhere:
+
+- There are around 500 `f.expect*()` calls in the unit tests, most of them in the `rollout` package.
+- There are around 240 lookups that fetch a created/patched/updated object by its index in the action list.
+- The same action filtering helper (`filterInformerActions`) has been copied in the `rollout`, `experiments` and `analysis` packages.
+
+This approach has several drawbacks:
+
+- **Refactorings break tests.** Merging an update into a patch, reordering two independent calls or adding an extra GET fails many tests even though the result is identical.
+- **Tests are recorded, not reasoned about.** When a test breaks, contributors often "fix" it by re-recording the new call order instead of checking if the behavior is still correct.
+- **Tests can pass with the wrong result.** A test that matches the expected call sequence can still pass if the final object is wrong and the test does not check that particular field.
+- **Tests are hard to read.** The intent of the test is hidden behind a list of API calls and index bookkeeping.
+- **Cleanups are discouraged.** The cost of updating dozens of tests stops contributors from simplifying the controller code.
+
+This is one of the items tracked in [#107 Test refactoring, cleanup, and standardization](https://github.com/argoproj/argo-rollouts/issues/107) and it is also a natural first step
+towards [#252 Utilize envtest.Environment instead of fake clients](https://github.com/argoproj/argo-rollouts/issues/252).
+
+## Goals
+
+The goals of this proposal are:
+
+- Unit tests assert on the resulting state of Kubernetes objects (Rollouts, ReplicaSets, Services, AnalysisRuns, Experiments etc.) after a reconciliation
+- Internal refactorings that keep the same behavior do not require test changes
+- Tests clearly express the expected behavior of the controller
+- The migration can happen incrementally, one test file at a time
+- The new assertion style can be reused later with a real API server (envtest)
+
+## Non-Goals
+
+- Migrating the unit tests to envtest. This is covered by [#252](https://github.com/argoproj/argo-rollouts/issues/252).
+- Changing any behavior of the controllers.
+- Rewriting all tests in a single pull request.
+- Removing *all* assertions on API calls. Some tests are specifically about the calls themselves (see below).
+
+## Proposal
+
+The fake clientsets used in unit tests already keep an in-memory object tracker that stores every object the controller creates, updates or patches.
+Some tests (for example in the `experiments` package) already use this tracker directly.
+
+Instead of recording the expected API calls, tests will run the reconciliation and then read the objects back from the tracker and check their state.
+
+### Example
+
+Here is a simplified version of a canary test as it is written today:
+
+```go
+createdRSIndex := f.expectCreateReplicaSetAction(rs2)
+updatedRolloutIndex := f.expectUpdateRolloutAction(r2)
+updatedConditionsIndex := f.expectUpdateRolloutStatusAction(r2)
+f.expectGetRolloutAction(r2)
+f.expectPatchRolloutAction(r2)
+updatedRSIndex := f.expectUpdateReplicaSetAction(rs2)
+f.runWithSyncs(getKey(r2, t), 2)
+
+updatedRS := f.getUpdatedReplicaSet(updatedRSIndex)
+assert.Equal(t, int32(1), *updatedRS.Spec.Replicas)
+
+updatedRollout := f.getUpdatedRollout(updatedConditionsIndex)
+progressing := conditions.GetRolloutCondition(updatedRollout.Status, v1alpha1.RolloutProgressing)
+assert.Equal(t, conditions.NewReplicaSetReason, progressing.Reason)
+```
+
+And here is the same test written with end-state assertions:
+
+```go
+f.runWithSyncs(getKey(r2, t), 2)
+
+newRS := f.getReplicaSet(rs2.Name)
+assert.Equal(t, int32(1), *newRS.Spec.Replicas)
+assert.Equal(t, "2", newRS.Annotations[annotations.RevisionAnnotation])
+
+rollout := f.getRollout(r2.Name)
+progressing := conditions.GetRolloutCondition(rollout.Status, v1alpha1.RolloutProgressing)
+assert.Equal(t, conditions.NewReplicaSetReason, progressing.Reason)
+```
+
+The second version does not care whether the ReplicaSet was scaled with an update or a patch, or in which order the Rollout and the ReplicaSet were written.
+
+### Test fixture changes
+
+The test fixtures will get a small set of helpers:
+
+- Getters that read the current version of an object from the fake clientset (e.g. `getRollout`, `getReplicaSet`, `getService`, `getAnalysisRun`)
+- A switch that disables the strict call-order verification for tests that have been migrated
+- A few narrow helpers for the cases where the API calls *are* the behavior under test, for example "no write happened during this sync" or "the ReplicaSet was created exactly once"
+
+The existing helpers stay in place until all tests in a package are migrated.
+
+## Migration strategy
+
+1. Finish the smaller cleanups of [#107](https://github.com/argoproj/argo-rollouts/issues/107) first (a single set of ReplicaSet builders and no hard-coded pod hashes). This makes the migrated tests shorter and easier to review.
+2. Add the new helpers next to the existing ones.
+3. Migrate the tests file by file, starting with the `rollout` package (`canary_test.go`, `bluegreen_test.go`, `sync_test.go`, etc.), and then `experiments` and `analysis`.

I think the problem with migrating existing test is that "how" we get to the final state will not be tested anymore. While it is important to have a test to validate the final result, we have to test the "unit". In this context, a "unit" is an individual reconciliation, or parts of that individual reconciliation. The current tests validate multiple things. It is a code-smell, but we cannot simply remove the action-list machinery, we have to replace it with individual unit test, so we keep validating each transition individually.

kostis-codefresh kostis-codefresh commented on code on Oct 20, 2026 13:02 UTC
Code Review
docs/proposals/unit-tests-verify-end-state.md:1
@@ -0,0 +1,150 @@
+---
+title: Unit tests that verify end state
+authors:
+  - '@kostis-codefresh'
+creation-date: 2026-09-30
+---
+
+# Unit tests that verify end state
+
+The unit tests of the Argo Rollouts controllers verify the exact sequence of Kubernetes API calls instead of the resulting state of the cluster.
+
+This document proposes a gradual migration to unit tests that verify the end state of the Kubernetes objects after a reconciliation.
+
+## Summary
+
+Most controller unit tests today describe *how* the controller talks to the API server ("first create a ReplicaSet, then update the Rollout, then patch its status, then update the ReplicaSet"),
+and not *what* the cluster should look like after the reconciliation ("the new ReplicaSet has 1 replica and the Rollout has the Progressing condition").
+
+As a result, any internal refactoring of the controller breaks a large number of tests, even when the behavior of the controller has not changed at all.
+
+## Motivation
+
+The main test fixture of the Rollout controller (see [rollout/controller_test.go](https://github.com/argoproj/argo-rollouts/blob/master/rollout/controller_test.go)) keeps
+a list of expected API actions. After every sync the actual actions recorded by the fake clientset are compared one by one against this list. Tests then fetch the
+objects that were sent to the API server by their position in the list.
+
+This pattern is used everywhere:
+
+- There are around 500 `f.expect*()` calls in the unit tests, most of them in the `rollout` package.
+- There are around 240 lookups that fetch a created/patched/updated object by its index in the action list.
+- The same action filtering helper (`filterInformerActions`) has been copied in the `rollout`, `experiments` and `analysis` packages.
+
+This approach has several drawbacks:
+
+- **Refactorings break tests.** Merging an update into a patch, reordering two independent calls or adding an extra GET fails many tests even though the result is identical.
+- **Tests are recorded, not reasoned about.** When a test breaks, contributors often "fix" it by re-recording the new call order instead of checking if the behavior is still correct.
+- **Tests can pass with the wrong result.** A test that matches the expected call sequence can still pass if the final object is wrong and the test does not check that particular field.
+- **Tests are hard to read.** The intent of the test is hidden behind a list of API calls and index bookkeeping.
+- **Cleanups are discouraged.** The cost of updating dozens of tests stops contributors from simplifying the controller code.
+
+This is one of the items tracked in [#107 Test refactoring, cleanup, and standardization](https://github.com/argoproj/argo-rollouts/issues/107) and it is also a natural first step
+towards [#252 Utilize envtest.Environment instead of fake clients](https://github.com/argoproj/argo-rollouts/issues/252).
+
+## Goals
+
+The goals of this proposal are:
+
+- Unit tests assert on the resulting state of Kubernetes objects (Rollouts, ReplicaSets, Services, AnalysisRuns, Experiments etc.) after a reconciliation
+- Internal refactorings that keep the same behavior do not require test changes
+- Tests clearly express the expected behavior of the controller
+- The migration can happen incrementally, one test file at a time
+- The new assertion style can be reused later with a real API server (envtest)
+
+## Non-Goals
+
+- Migrating the unit tests to envtest. This is covered by [#252](https://github.com/argoproj/argo-rollouts/issues/252).
+- Changing any behavior of the controllers.
+- Rewriting all tests in a single pull request.
+- Removing *all* assertions on API calls. Some tests are specifically about the calls themselves (see below).
+
+## Proposal
+
+The fake clientsets used in unit tests already keep an in-memory object tracker that stores every object the controller creates, updates or patches.
+Some tests (for example in the `experiments` package) already use this tracker directly.
+
+Instead of recording the expected API calls, tests will run the reconciliation and then read the objects back from the tracker and check their state.
+
+### Example
+
+Here is a simplified version of a canary test as it is written today:
+
+```go
+createdRSIndex := f.expectCreateReplicaSetAction(rs2)
+updatedRolloutIndex := f.expectUpdateRolloutAction(r2)
+updatedConditionsIndex := f.expectUpdateRolloutStatusAction(r2)
+f.expectGetRolloutAction(r2)
+f.expectPatchRolloutAction(r2)
+updatedRSIndex := f.expectUpdateReplicaSetAction(rs2)
+f.runWithSyncs(getKey(r2, t), 2)
+
+updatedRS := f.getUpdatedReplicaSet(updatedRSIndex)
+assert.Equal(t, int32(1), *updatedRS.Spec.Replicas)
+
+updatedRollout := f.getUpdatedRollout(updatedConditionsIndex)
+progressing := conditions.GetRolloutCondition(updatedRollout.Status, v1alpha1.RolloutProgressing)
+assert.Equal(t, conditions.NewReplicaSetReason, progressing.Reason)
+```
+
+And here is the same test written with end-state assertions:
+
+```go
+f.runWithSyncs(getKey(r2, t), 2)
+
+newRS := f.getReplicaSet(rs2.Name)
+assert.Equal(t, int32(1), *newRS.Spec.Replicas)
+assert.Equal(t, "2", newRS.Annotations[annotations.RevisionAnnotation])
+
+rollout := f.getRollout(r2.Name)
+progressing := conditions.GetRolloutCondition(rollout.Status, v1alpha1.RolloutProgressing)
+assert.Equal(t, conditions.NewReplicaSetReason, progressing.Reason)
+```
+
+The second version does not care whether the ReplicaSet was scaled with an update or a patch, or in which order the Rollout and the ReplicaSet were written.
+
+### Test fixture changes
+
+The test fixtures will get a small set of helpers:
+
+- Getters that read the current version of an object from the fake clientset (e.g. `getRollout`, `getReplicaSet`, `getService`, `getAnalysisRun`)
+- A switch that disables the strict call-order verification for tests that have been migrated
+- A few narrow helpers for the cases where the API calls *are* the behavior under test, for example "no write happened during this sync" or "the ReplicaSet was created exactly once"
+
+The existing helpers stay in place until all tests in a package are migrated.
+
+## Migration strategy
+
+1. Finish the smaller cleanups of [#107](https://github.com/argoproj/argo-rollouts/issues/107) first (a single set of ReplicaSet builders and no hard-coded pod hashes). This makes the migrated tests shorter and easier to review.
+2. Add the new helpers next to the existing ones.
+3. Migrate the tests file by file, starting with the `rollout` package (`canary_test.go`, `bluegreen_test.go`, `sync_test.go`, etc.), and then `experiments` and `analysis`.

Yes agreed. This will not happen for all tests. Only for those where we actually care about end state. For some tests we do indeed care about the how. It is the first bullet point under Risks

Reviewers
Assignees
kostis-codefresh kostis-codefresh
Labels
risk: low confidence: 91.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 33% (LOW) · Higher = larger blast radius
33%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 91.7% · Lower = rushed or shallow review
91.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 2.7% · Safe to merge
2.7%