|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// resolvePhaseBudget honors STEPSEC_PHASE_BUDGET_<NAME> > map entry > default, |
| 10 | +// and treats an env "0"/"off" or an explicit map 0 as "no deadline". Every |
| 11 | +// subtest sets the env var explicitly (to "" when it shouldn't apply) so an |
| 12 | +// inherited host/CI value can't make the result nondeterministic. |
| 13 | +func TestResolvePhaseBudget(t *testing.T) { |
| 14 | + cases := []struct { |
| 15 | + name string |
| 16 | + phase string |
| 17 | + env string // value for STEPSEC_PHASE_BUDGET_<PHASE> |
| 18 | + wantBudget time.Duration |
| 19 | + wantEnabled bool |
| 20 | + }{ |
| 21 | + {"env override wins over map", "node_scan", "20m", 20 * time.Minute, true}, |
| 22 | + {"env off disables", "node_scan", "off", 0, false}, |
| 23 | + {"env 0 disables", "node_scan", "0", 0, false}, |
| 24 | + {"env junk ignored, falls to map", "node_scan", "junk", 15 * time.Minute, true}, |
| 25 | + {"env negative ignored, falls to map", "node_scan", "-5m", 15 * time.Minute, true}, |
| 26 | + {"map entry used when env empty", "ide_scan", "", 2 * time.Minute, true}, |
| 27 | + {"unlisted phase falls to default", "totally_made_up_phase", "", defaultPhaseBudget, true}, |
| 28 | + } |
| 29 | + for _, tc := range cases { |
| 30 | + t.Run(tc.name, func(t *testing.T) { |
| 31 | + // Hermetic: set the exact env key this phase reads (to "" when the |
| 32 | + // case shouldn't have an override). |
| 33 | + t.Setenv("STEPSEC_PHASE_BUDGET_"+upper(tc.phase), tc.env) |
| 34 | + gotBudget, gotEnabled := resolvePhaseBudget(tc.phase) |
| 35 | + if gotEnabled != tc.wantEnabled { |
| 36 | + t.Fatalf("enabled = %v, want %v", gotEnabled, tc.wantEnabled) |
| 37 | + } |
| 38 | + if tc.wantEnabled && gotBudget != tc.wantBudget { |
| 39 | + t.Errorf("budget = %v, want %v", gotBudget, tc.wantBudget) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// An explicit 0 in the phaseBudgets map disables the deadline — distinct from a |
| 46 | +// missing entry, which falls back to the default. (Copilot finding: the old |
| 47 | +// `if budget == 0 { budget = default }` conflated the two.) |
| 48 | +func TestResolvePhaseBudget_ExplicitZeroInMapDisables(t *testing.T) { |
| 49 | + const phase = "zzz_explicit_zero_phase" |
| 50 | + phaseBudgets[phase] = 0 |
| 51 | + t.Cleanup(func() { delete(phaseBudgets, phase) }) |
| 52 | + t.Setenv("STEPSEC_PHASE_BUDGET_"+upper(phase), "") // no env override |
| 53 | + |
| 54 | + d, enabled := resolvePhaseBudget(phase) |
| 55 | + if enabled || d != 0 { |
| 56 | + t.Errorf("explicit map 0 must disable the deadline: got (%v, enabled=%v)", d, enabled) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// startPhase must produce a context with NO deadline when the budget is |
| 61 | +// disabled (only the parent scan deadline applies), and a deadline bounded by |
| 62 | +// the budget otherwise. |
| 63 | +func TestStartPhase_DeadlineWiring(t *testing.T) { |
| 64 | + tracker := NewPhaseTracker() |
| 65 | + |
| 66 | + t.Run("disabled budget → no phase deadline", func(t *testing.T) { |
| 67 | + t.Setenv("STEPSEC_PHASE_BUDGET_NODE_SCAN", "off") |
| 68 | + ctx, cancel := startPhase(context.Background(), tracker, "node_scan") |
| 69 | + defer cancel() |
| 70 | + if _, ok := ctx.Deadline(); ok { |
| 71 | + t.Error("disabled phase budget must not set a context deadline") |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("enabled budget → bounded deadline", func(t *testing.T) { |
| 76 | + t.Setenv("STEPSEC_PHASE_BUDGET_NODE_SCAN", "20m") |
| 77 | + ctx, cancel := startPhase(context.Background(), tracker, "node_scan") |
| 78 | + defer cancel() |
| 79 | + dl, ok := ctx.Deadline() |
| 80 | + if !ok { |
| 81 | + t.Fatal("enabled phase budget must set a context deadline") |
| 82 | + } |
| 83 | + if remaining := time.Until(dl); remaining <= 0 || remaining > 20*time.Minute { |
| 84 | + t.Errorf("deadline %v out of expected ~20m window", remaining) |
| 85 | + } |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// upper uppercases an ASCII phase name for the env-var key, mirroring |
| 90 | +// strings.ToUpper without importing it into the test for one call. |
| 91 | +func upper(s string) string { |
| 92 | + b := []byte(s) |
| 93 | + for i := range b { |
| 94 | + if b[i] >= 'a' && b[i] <= 'z' { |
| 95 | + b[i] -= 'a' - 'A' |
| 96 | + } |
| 97 | + } |
| 98 | + return string(b) |
| 99 | +} |
0 commit comments