Skip to content

Commit a5c516e

Browse files
harcheclaude
andcommitted
pkg/readiness: Make AllChecks injectable so TestRunAllMixedResults tests real RunAll
AllChecks was a func, so the mixed-results test had to reimplement the RunAll orchestration loop manually — missing goroutine scheduling, timeouts, and status aggregation. Make AllChecks a var so tests can swap in fake checks and exercise the real RunAll path. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2c5bfa4 commit a5c516e

2 files changed

Lines changed: 24 additions & 48 deletions

File tree

pkg/readiness/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const (
6363
// Checks are split into two categories:
6464
// - cluster_conditions: reads CVO's already-computed state (no re-querying)
6565
// - everything else: gathers NEW data that CVO doesn't already track
66-
func AllChecks() []Check {
66+
var AllChecks = func() []Check {
6767
return []Check{
6868
&ClusterConditionsCheck{}, // reads existing CVO conditions — no duplication
6969
&OperatorHealthCheck{}, // per-CO detail + MCPs (CVO only aggregates)

pkg/readiness/check_test.go

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -148,71 +148,47 @@ func TestSectionError(t *testing.T) {
148148
}
149149

150150
func TestRunAllMixedResults(t *testing.T) {
151-
okCheck := &fakeCheck{name: "passing", data: map[string]any{"healthy": true}}
152-
failCheck := &fakeCheck{name: "failing", err: errors.New("something broke")}
153-
partialCheck := &fakeCheck{name: "partial", data: map[string]any{"partial": true}, err: errors.New("partial failure")}
154-
155-
origAllChecks := AllChecks
156-
// We can't override AllChecks directly since it's a function, so test RunAll
157-
// indirectly through the real checks. Instead, test the orchestration logic
158-
// by running checks manually and verifying the output structure.
159-
160-
_ = origAllChecks // satisfy usage
161-
162-
// Simulate RunAll behavior manually with our fake checks
163-
checks := []Check{okCheck, failCheck, partialCheck}
164-
results := make(map[string]CheckResult, len(checks))
165-
ok := 0
166-
errored := 0
167-
168-
for _, ch := range checks {
169-
data, err := ch.Run(context.Background(), nil, "4.21.5", "4.21.8")
170-
result := CheckResult{Elapsed: 0.1}
171-
if err != nil {
172-
result.Status = "error"
173-
result.Error = err.Error()
174-
if data != nil {
175-
result.Data = data
176-
} else {
177-
result.Data = map[string]any{}
178-
}
179-
errored++
180-
} else {
181-
result.Status = "ok"
182-
result.Data = data
183-
ok++
151+
orig := AllChecks
152+
defer func() { AllChecks = orig }()
153+
154+
AllChecks = func() []Check {
155+
return []Check{
156+
&fakeCheck{name: "passing", data: map[string]any{"healthy": true}},
157+
&fakeCheck{name: "failing", err: errors.New("something broke")},
158+
&fakeCheck{name: "partial", data: map[string]any{"partial": true}, err: errors.New("partial failure")},
184159
}
185-
results[ch.Name()] = result
186160
}
187161

188-
if ok != 1 {
189-
t.Errorf("ok count = %d, want 1", ok)
162+
output := RunAll(context.Background(), nil, "4.21.5", "4.21.8")
163+
164+
if output.Meta.TotalChecks != 3 {
165+
t.Errorf("TotalChecks = %d, want 3", output.Meta.TotalChecks)
166+
}
167+
if output.Meta.ChecksOK != 1 {
168+
t.Errorf("ChecksOK = %d, want 1", output.Meta.ChecksOK)
190169
}
191-
if errored != 2 {
192-
t.Errorf("errored count = %d, want 2", errored)
170+
if output.Meta.ChecksErrored != 2 {
171+
t.Errorf("ChecksErrored = %d, want 2", output.Meta.ChecksErrored)
193172
}
194173

195-
// Verify passing check
196-
passing := results["passing"]
197-
if passing.Status != "ok" {
174+
passing := output.Checks["passing"]
175+
if passing.Status != StatusOK {
198176
t.Errorf("passing.Status = %q, want ok", passing.Status)
199177
}
200178
if passing.Data["healthy"] != true {
201179
t.Errorf("passing.Data[healthy] = %v", passing.Data["healthy"])
202180
}
203181

204-
// Verify failing check
205-
failing := results["failing"]
206-
if failing.Status != "error" {
182+
failing := output.Checks["failing"]
183+
if failing.Status != StatusError {
207184
t.Errorf("failing.Status = %q, want error", failing.Status)
208185
}
209186
if failing.Error != "something broke" {
210187
t.Errorf("failing.Error = %q", failing.Error)
211188
}
212189

213-
// Verify partial failure preserves data
214-
partial := results["partial"]
215-
if partial.Status != "error" {
190+
partial := output.Checks["partial"]
191+
if partial.Status != StatusError {
216192
t.Errorf("partial.Status = %q, want error", partial.Status)
217193
}
218194
if partial.Data["partial"] != true {

0 commit comments

Comments
 (0)