Commit 1ebc033
chore(cmd/proxy): decompose main() for testability + close delta-coverage gap (#121)
## Summary
Decomposes `cmd/proxy/main.go` `func main()` into separately-testable
startup
helpers and adds a helper-process `TestMain` that exercises `main()`
end-to-end.
Closes the structural delta-coverage gap surfaced by #118 — every
function in
`cmd/proxy/` now meets the 95% per-function gate, so future PRs touching
this
file (including #120's `--generate-ca` flag addition) land without false
coverage failures.
## Motivation
- #118 closed a silent-skip bug in `.github/scripts/delta-coverage.sh`.
Before
that fix, PRs touching `cmd/proxy/main.go` quietly bypassed the
per-function
gate.
- #120 is the first PR to actually hit the now-loud gate. `main()` has
always
been 0% covered (network setup + signal handling), so any change to the
file
fails 95%.
- Rather than per-PR workarounds, this PR fixes the root cause on
`main`:
refactor `main()` into testable helpers + helper-process tests that
exercise
the real binary.
## What changed
- New `cmd/proxy/startup.go` — `proxyHTTPServer`, `startManagementAPI`,
`runManagementAPI`, `runHTTPServer`, `closeProxyServer` (extracted from
`main()`). `closeProxyServer` takes `io.Closer` so the error-log branch
is
exercisable with a fake.
- New `cmd/proxy/shutdown.go` — `installShutdownHandler` (extracted from
`main()`).
- New unit tests in `cmd/proxy/startup_test.go` and
`cmd/proxy/shutdown_test.go`
covering each helper at 100%. `TestCloseProxyServer_*` capture the
default
logger's output and assert the error message is/isn't emitted, not just
that
both branches execute.
- `cmd/proxy/main_test.go` — `TestMain` dispatcher + four helper-process
tests
(lifecycle, zero-packs guard, proxy-port conflict, mgmt-port conflict)
using
the canonical Go helper-process pattern (`GO_WANT_HELPER_PROCESS=1` +
re-exec
of `os.Args[0]`). `TestPrintBanner_ZeroValueConfig_DoesNotPanic`
preserves
the zero-value-config guarantee previously owned by `TestMain_Smoke`.
- `cmd/proxy/main.go` `func main()` reduced to orchestration; no
behaviour
change.
## Per-function coverage (`go tool cover -func`)
```
cmd/proxy/main.go:39: main 100.0%
cmd/proxy/main.go:66: printBanner 100.0%
cmd/proxy/shutdown.go:13: installShutdownHandler 100.0%
cmd/proxy/startup.go:17: proxyHTTPServer 100.0%
cmd/proxy/startup.go:28: startManagementAPI 100.0%
cmd/proxy/startup.go:37: runManagementAPI 100.0%
cmd/proxy/startup.go:45: runHTTPServer 100.0%
cmd/proxy/startup.go:54: closeProxyServer 100.0%
total: 100.0%
```
Local `delta-coverage.sh coverage.out 95.0 origin/main` exits 0:
```
=== Delta Coverage Check (threshold: 95.0%) ===
Changed source files:
cmd/proxy/main.go
cmd/proxy/shutdown.go
cmd/proxy/startup.go
Checked 8 functions in 3 changed files.
SUCCESS: All functions in changed files meet 95.0% coverage threshold.
```
## Quality gates
- [x] `go test -race ./...` green (all packages) — see
`TestTokenFormatNonRetriggering` evidence below
- [x] `bash .github/scripts/delta-coverage.sh coverage.out 95.0
origin/main`
exits 0 with `SUCCESS`
- [x] Coverage minimums hold: `internal/anonymizer` 95.6%,
`internal/config`
100%, `internal/anonymizer/packs` 97.6%; overall well above 85%
- [x] Lifecycle helper-process test stable across 5 consecutive local
runs
- [x] No `t.Skip()`, no `// coverage-ignore`, no hardcoded
test-satisfying
values. Four `//nolint:gosec` and two `// #nosec` directives carry
substantive reason comments per the existing repo convention.
- [ ] `make lint` / `make check` — `golangci-lint` in the local sandbox
is
built with go1.25 and refuses go1.26.3 modules (same failure on a clean
`main` checkout). CI runs all four sub-gates against a fresher
toolchain.
## Gate #2 — `TestTokenFormatNonRetriggering` for every PII type
Both variants live in `internal/anonymizer/anonymizer_test.go` and ran
green at
head `c69911f`:
```
=== RUN TestTokenFormatNonRetriggering
[ANONYMIZER] loaded 41 patterns from 7 enabled packs: [SECRETS GLOBAL DE FR NL FINANCE_EU HEALTHCARE]
--- PASS: TestTokenFormatNonRetriggering (0.04s)
=== RUN TestTokenFormatNonRetriggeringAllPacks
[ANONYMIZER] loaded 47 patterns from 8 enabled packs: [SECRETS GLOBAL DE FR US NL FINANCE_EU HEALTHCARE]
--- PASS: TestTokenFormatNonRetriggeringAllPacks (0.04s)
```
Each variant iterates over a static list of `PIIType` values and, for
each,
asserts the generated `[PII_*_<16hex>]` token does not match any loaded
pack
pattern. The PII types covered:
| Pack | PII types asserted by the test |
|---|---|
| Generic / built-in | `PIIEmail`, `PIIPhone`, `PIISSN`,
`PIICreditCard`, `PIIIPAddress`, `PIIAPIKey`, `PIIName`, `PIIAddress`,
`PIIMedical`, `PIISalary`, `PIICompany`, `PIIJobTitle` |
| DE | `PIISteuerID`, `PIISVNR`, `PIIKFZ` |
| SECRETS | `PIISSHKey`, `PIIJWT`, `PIIBearer`, `PIIDBConn`,
`PIIAWSKey`, `PIIGHToken` |
| FR | `PIINIR`, `PIISIRET`, `PIISIREN` |
| NL | `PIIBSN`, `PIIKVK` |
| FINANCE_EU | `PIIIBAN`, `PIISWIFTBIC`, `PIIVATID` |
| HEALTHCARE | `PIIMRN`, `PIIICD10`, `PIIInsuranceID` |
That's 32 PII types in the default-packs variant.
`TestTokenFormatNonRetriggeringAllPacks`
adds the US pack and drops the four AI-only types
(`PIIMedical/Salary/Company/JobTitle`),
covering 28 types under the full pattern set (`PIIKFZ`/`PIIDBConn` are
noted in
the test as known low-confidence retriggers against the broad US phone
pattern
and routed through the AI gate — documented in the test, not silenced).
The two variants together cross every PII type declared in
`internal/anonymizer/anonymizer.go`. None of the changed files in this
PR touch
the anonymizer, the pack files, or the PII type list, so no per-type
evidence
shifted between baseline and head — but the gate is exercised end-to-end
at
head as shown above.
## §6 Test Inventory — baseline vs head
Keyed by **pack** per `docs/test-plans/ai-proxy-test-method.md` §6.
Captured by running `go test -count=1 -json ./internal/anonymizer
./internal/anonymizer/packs`
on `origin/main` (`98511d7`) and on PR head (`c69911f`), then
attributing
each top-level Test event to its pack via the file list in §6.
| Pack | Files (per §6) | Baseline PASS | Baseline FAIL | Head PASS |
Head FAIL | Delta |
|---|---|---:|---:|---:|---:|---:|
| GLOBAL | `packs/global_test.go` | 6 | 0 | 6 | 0 | 0 |
| DE | `packs/de_test.go` | 5 | 0 | 5 | 0 | 0 |
| US | `packs/us_test.go` | 10 | 0 | 10 | 0 | 0 |
| FR | `packs/fr_test.go` | 9 | 0 | 9 | 0 | 0 |
| SECRETS | `packs/secrets_test.go` + `secrets_report_test.go` +
`secrets_priority_report_test.go` | 31 | 0 | 31 | 0 | 0 |
| NL | `packs/nl_test.go` + `nl_report_test.go` | 6 | 0 | 6 | 0 | 0 |
| FINANCE_EU | `packs/finance_eu_test.go` + `finance_eu_report_test.go`
| 7 | 0 | 7 | 0 | 0 |
| HEALTHCARE | `packs/healthcare_test.go` + `healthcare_report_test.go`
| 6 | 0 | 6 | 0 | 0 |
| Cross-pack | all `*_report_test.go` | 9 | 0 | 9 | 0 | 0 |
Counts are top-level `func Test*` events from the JSON stream, filtered
by the
test-name list extracted from each pack's `_test.go` file(s). The
`secrets_priority_report_test.go` file lives alongside
`secrets_report_test.go`,
was added after §6 was written, and is included under SECRETS (its
prefix
matches the SECRETS pack convention). The Cross-pack row overlaps with
the
pack rows by design (it sums all `*_report_test.go` content, which the
relevant per-pack rows already include).
**Supplementary Go-package view** — `go test -count=1 -json ./...` gives
861 → 871 PASS / 0 → 0 FAIL across all 10 Go packages; the `cmd/proxy`
row
went 4 → 14, every other row unchanged. The per-pack table above is the
gate-required view.
## §6 changed-files attribution
None of the changed files in this PR (`cmd/proxy/main.go`,
`cmd/proxy/startup.go`,
`cmd/proxy/shutdown.go`, plus the corresponding `_test.go` files) live
under
any pack's file list, so the identical per-pack counts are the expected
measured outcome, not a substitution for measurement.
## Test plan
- [x] `go test -race ./cmd/proxy/...` — all green including new
helper-process tests
- [x] `go test -race -run '^TestTokenFormatNonRetriggering'
./internal/anonymizer/...` — both variants PASS at head
- [x] `go tool cover -func` — every function in `cmd/proxy/` at 100%
- [x] Local `delta-coverage.sh` simulation against `origin/main` — exits
0
- [x] Lifecycle test run 5x consecutively — 5/5 pass
- [x] CI green (Lint, Test, Security Scan, Benchmark, CodeQL × 2, Build)
at
`c69911f`
## Out of scope
- Any changes under `packaging/linux/` — Fedora `ca-certificates` and
openSUSE
trust-store path belong to #120 follow-up commits after rebase.
- Any changes to `internal/*`, `.github/workflows/`, or
`.github/scripts/delta-coverage.sh`.
- Adding a second binary or the `--generate-ca` flag (#120's territory).
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 98511d7 commit 1ebc033
6 files changed
Lines changed: 516 additions & 56 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | 26 | | |
28 | 27 | | |
29 | | - | |
30 | 28 | | |
31 | 29 | | |
32 | 30 | | |
| |||
41 | 39 | | |
42 | 40 | | |
43 | 41 | | |
44 | | - | |
45 | 42 | | |
46 | 43 | | |
47 | 44 | | |
48 | 45 | | |
49 | 46 | | |
50 | 47 | | |
51 | | - | |
52 | | - | |
53 | 48 | | |
54 | | - | |
55 | | - | |
56 | 49 | | |
57 | 50 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 51 | + | |
66 | 52 | | |
67 | | - | |
68 | 53 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
| 54 | + | |
77 | 55 | | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 56 | + | |
| 57 | + | |
83 | 58 | | |
84 | | - | |
85 | 59 | | |
86 | 60 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
| 61 | + | |
96 | 62 | | |
97 | | - | |
98 | | - | |
99 | | - | |
| 63 | + | |
100 | 64 | | |
101 | 65 | | |
102 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
| 7 | + | |
6 | 8 | | |
| 9 | + | |
| 10 | + | |
7 | 11 | | |
| 12 | + | |
| 13 | + | |
8 | 14 | | |
| 15 | + | |
9 | 16 | | |
10 | 17 | | |
11 | 18 | | |
12 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
13 | 31 | | |
14 | 32 | | |
15 | 33 | | |
| |||
76 | 94 | | |
77 | 95 | | |
78 | 96 | | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
90 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
91 | 143 | | |
92 | | - | |
93 | | - | |
94 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
95 | 274 | | |
| 275 | + | |
96 | 276 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
0 commit comments