Skip to content

Make unit tests hermetic: no real AWS/DNS calls from make check (#968) - #1003

Merged
scttfrdmn merged 3 commits into
mainfrom
fix/968
Oct 15, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
fix/968

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #968

The bug

TestNewAgent_StillActivates called the real NewAgent with DNSName/JobArrayID/PublicIP. With ambient credentials, every make check sent a real Route53 UPSERT through the spore.host DNS API, an EC2 CreateTags, and a DynamoDB PutItem. In CI, with no credentials, those calls failed. The test passed in both cases because NewAgent only logs the failures.

Fix

  • pkg/agent seams: package vars newDNSClient, newPeerRegistry (same name and shape as Register only hybrid job-array members with the hybrid registry (#904) #920's), and newTagPutter. The activation test now builds a real dns.Client and PeerRegistry aimed at httptest fakes. It asserts a leased UPSERT demo -> 203.0.113.10, a spawn:dns-status=registered tag, a registry PutItem, and peer discovery. A new Agent.fsxMounter seam keeps the FSx gating test from polling DescribeFileSystems.
  • Other offenders. I found these by running every module, root and nested, -short and full, under the new gate. Static greps found nothing more.
    where what it reached
    pkg/agent TestMaybeMountPendingFSx_GatingAndIdempotency FSx DescribeFileSystems (a background poll, up to 25 min)
    pkg/provider local-provider tests api.ipify.org / ifconfig.me / icanhazip.com (now a lookupPublicIP seam, stubbed in TestMain)
    pkg/config TestLoadLocalConfig_PublicIPResolution (non-short) the same IP services (now faked, and runs in short mode too)
    pkg/taskpool exec/worker/substrate tests S3 PutObject of tasks/nf-*/completion.json and .exitcode through the real aws CLI (a stub aws now goes first on PATH in TestMain)
    lambda/ttl-reaper (every test) STS GetCallerIdentity from package init (moved into main as configure())
    lambda/dashboard-api TestHandlerMissingTeamID DynamoDB GetItem on spawn-user-accounts (new loadAWSConfig seam)
    lambda/dns-updater two handler alias tests DynamoDB Scan of the live DNS account registry (aliasTestZone now stubs the registry)

Gate

internal/hermetic plus test/hermetic, a go test -exec wrapper. For each test binary it:

  • strips every AWS_* variable and sets fake static credentials, AWS_CONFIG_FILE/AWS_SHARED_CREDENTIALS_FILE=/dev/null, and AWS_EC2_METADATA_DISABLED=true;
  • points AWS_ENDPOINT_URL and HTTP(S)_PROXY at a loopback recorder, with NO_PROXY set to loopback only. The proxy is what catches the DNS client: it is a plain net/http POST to a Lambda Function URL, so AWS_ENDPOINT_URL does not touch it. httptest and Substrate stay reachable because Go never proxies loopback;
  • fails the package and names each request, e.g. HTTPS CONNECT …lambda-url.us-east-1.on.aws:443, service=ec2 action=CreateTags, target=DynamoDB_20120810.PutItem.

It is wired into make check (new test-hermetic target), test-race, CI's Test, Race and API-surface steps, and scripts/nested-modules.sh. pkg/agent also installs it through its own TestMain, so a plain go test ./pkg/agent is hermetic too. TestGateIsWired fails if any non-tagged go test step in ci.yml, or make check, or the nested script stops using the wrapper. TestRun_* checks that the wrapper fails a binary making a DNS-API-shaped request and one making an SDK request, and passes a quiet one.

Evidence

  • Original test file against the fixed code: --- PASS: TestNewAgent_StillActivates, then HERMETIC GATE FAILED … 3 network request(s) (DNS Function URL CONNECT, ec2 CreateTags, DynamoDB PutItem), so FAIL pkg/agent.
  • Compiling revert of the three seam call sites: the new test fails (DNS API got 0 request(s)) and the gate fails with the same three requests.
  • Compiling revert of the gate (HTTPS_PROXY dropped): TestRun_…/dns-api fails. Dropping -exec from the CI race step: TestGateIsWired fails.
  • With the fix, run hermetically (no credentials in the environment): make check passes except the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker. make check-nested passes (9 modules), as do make check-tidy and make test-race. A full non-short go test ./... under the wrapper, root and every nested module, reports no hermetic failures.

Left for you to decide

  • The production record demo.1kpqzg2c.spore.host -> 203.0.113.10 (TEST-NET-3), created by this test, is still in place. It is leased, so the reaper's lease expiry should remove it if enabled; deleting it by hand would be another production write. Repeated make check runs with credentials may have kept renewing it, and other developers' runs may have left similar demo.<their-account> records.
  • Conflict with Register only hybrid job-array members with the hybrid registry (#904) #920: if Register only hybrid job-array members with the hybrid registry (#904) #920 merges first, the newPeerRegistry declarations collide (resolve by keeping one). Its joinsHybridRegistry also means a plain EC2 job-array member no longer registers, so this test's PutItem assertion would need Burst: true in queryProvider().

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 15.20000% with 106 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/hermetic/hermetic.go 0.00% 92 Missing ⚠️
test/hermetic/main.go 41.66% 12 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

TestNewAgent_StillActivates called the real NewAgent, which built a real DNS
client, DynamoDB registry and EC2 tag writer from ambient credentials: every
`make check` with a profile set did a real Route53 UPSERT via the DNS API, an
EC2 CreateTags and a DynamoDB PutItem, and passed either way.

- pkg/agent: newDNSClient/newPeerRegistry/newTagPutter seams (newPeerRegistry
  named as in #920); the activation test now asserts the leased UPSERT, the
  dns-status tag and the registry PutItem through httptest fakes. An
  fsxMounter seam keeps the FSx gating test off DescribeFileSystems.
- Other offenders found by running every module under the new gate: provider
  and config public-IP lookups (ipify/ifconfig.me/icanhazip), taskpool job
  scripts' `aws s3 cp` (stub aws on PATH), ttl-reaper's init-time STS
  GetCallerIdentity (moved to main), a dashboard-api handler test's DynamoDB
  GetItem (loadAWSConfig seam), dns-updater handler tests' registry Scan.
- Gate: internal/hermetic + test/hermetic, a `go test -exec` wrapper that
  strips AWS_* and sets fake creds, points AWS_ENDPOINT_URL and HTTP(S)_PROXY
  at a loopback recorder, and fails the package naming every request. The
  proxy is what catches the DNS client, which is not an SDK client. Wired into
  make check, CI (test, race, API surface) and scripts/nested-modules.sh;
  pkg/agent also installs it via TestMain. TestGateIsWired checks the wiring.

(#968)
@scttfrdmn
scttfrdmn merged commit b9e8b33 into main Oct 15, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/968 branch October 15, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pkg/agent unit test registers a real spore.host DNS record with ambient credentials

1 participant