Backend audit and soak gate

You add a backend audit target and a 10-minute soak script that scan dependencies, lint production code, run race tests, and check goroutine recovery.

Ticket

Build a repeatable backend audit and a soak script. The audit target in backend/Makefile must run vulnerability scanning, production-code linting and race tests. The script at backend/scripts/soak.sh must run the existing browsing workload while recording goroutine and heap measurements from pprof.

The product needs these checks because a successful short test run does not prove that the service can handle sustained traffic without leaking resources. The next exercise adds per-minute order statistics for restaurant staff. Keep the same quality gates in place as the backend grows.

No test calls Go code directly in this exercise. The tests run make -n audit, read .github/workflows/backend.yml and backend/scripts/soak.sh, run bash -n on the script, and run an end-to-end burst of 150 clients against the running API while reading the goroutine count from the private pprof listener.

Acceptance criteria

  • The make -n audit output contains govulncheck ./..., a golangci-lint run command and a go test -race command.
  • The .github/workflows/backend.yml workflow mentions govulncheck and runs tests with -race.
  • The backend/scripts/soak.sh script references both debug/pprof/goroutine and debug/pprof/heap, invokes k6 run, and passes bash -n.
  • After a burst of 150 concurrent clients, the goroutine count returns to within 15 of its baseline within 15 seconds, using 500 ms polling intervals. The API and pprof server must be running for this end-to-end check.

Constraints

  • The audit target must run govulncheck ./..., golangci-lint run and go test -race in that order. Stop at the first failure.
  • The .github/workflows/backend.yml workflow must include a govulncheck step and run tests with -race.
  • The backend/scripts/soak.sh script must invoke k6 run, sample both debug/pprof/goroutine and debug/pprof/heap, and pass bash -n.
  • After 150 concurrent clients finish their requests, the goroutine count must return to within 15 of baseline within 15 seconds.
  • Do not modify the provided tests.

Design direction

Keep the audit target in the Makefile as a short sequence of independent commands. Let Make stop when a command fails rather than hiding errors inside a wrapper.

Keep the CI vulnerability check separate from lint so the workflow reports which gate failed. Production-only linting lets you check the implementation without treating later exercise test files as production code.

Keep the soak script responsible for orchestration and measurement. Let k6 generate traffic, curl collect pprof profiles, and Bash manage the child process and CSV output.

Think about the distinction between a static check and a runtime observation. A clean audit cannot prove that a goroutine returns after a request finishes. A stable goroutine count cannot prove that your code has no data races. Both checks have a separate purpose. Any layout that meets the contract passes.

How to check your work

Run these commands in sequence from the repository root:

bash
go install golang.org/x/vuln/cmd/govulncheck@latest
cd backend
make audit
make test W=6B6
TEST_API_URL=http://localhost:8080 make test W=6B6
./scripts/soak.sh
curl -s "http://127.0.0.1:6060/debug/pprof/goroutine?debug=1" | head -1

Keep the API and worker running throughout the soak. The default duration is ten minutes. The end-to-end test needs the API running at http://localhost:8080 and the private pprof listener at 127.0.0.1:6060.

Run the final curl once before the soak and once after it to compare goroutine counts.

Inspect loadtest/soak.csv after the run. Compare the goroutines column against its baseline and check the heap_inuse_bytes column for sustained growth. The script exits non-zero if the final goroutine count exceeds baseline by more than 20.

When a test fails, read its first failing assertion before changing code. It identifies the specific contract that failed, such as a missing audit command, a missing CI flag, an invalid soak script or a goroutine count that remains elevated.

Finished Exercise 6.13?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. make audit runs vulnerability scanning, production lint and race tests.
  2. CI includes a separate vulnerability-check step and race-enabled tests.
  3. Production-code lint findings are fixed without changing behaviour.
  4. soak.sh records goroutine and heap samples in loadtest/soak.csv.
  5. The soak finishes with no more than 20 goroutines above baseline.
  6. The end-to-end burst test returns within 15 goroutines of baseline.

Commands

terminal
cd backend && make audit
terminal
cd backend && ./scripts/soak.sh   # 10 minutes against the running API
terminal
cd backend && TEST_API_URL=http://localhost:8080 make test W=6B6

Tests

  • TestW6B6_GoroutinesReturnToBaselineAfterABurst in backend/e2e/w6_test.go
  • TestW6B6_MakeAuditRunsVulncheckLintAndRaceTests in backend/internal/audit/audit_test.go
  • TestW6B6_CIWorkflowRunsGovulncheck in backend/internal/audit/audit_test.go
  • TestW6B6_SoakScriptSamplesGoroutinesAndHeap in backend/internal/audit/audit_test.go