Skip to content

fix(fault): make an injected fault targetable, bounded and indistinguishable (#480) - #511

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/480-fault-injection
Aug 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/480-fault-injection

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #480

Four findings from #480, taken in the reporter's own priority order. They
compound: together they mean an armed S3 fault often fires on the wrong request,
or fires and cannot be told apart from a real error — and a test that arms one
and sees success has proven nothing.

Finding 1 — an S3 rule could not name a semantic operation

Faults evaluate at server.go's step 4.5 against req.Operation; the S3 plugin
assigns the semantic name at step 5, one step later. So the fault matcher saw the
bare HTTP verb: a rule on PutObject never fired, and a rule on PUT took out
UploadPart too.

The reporter asked for both of their options, and both are right:

  • Option 2 — extractOperation now calls the already-pure
    parseS3Operation when service == "s3", so req.Operation is canonical from
    the start. This also fixes cost and consistency attribution for any request
    that errors before routing, which is wider than faults. The S3 plugin keeps its
    parseS3Operation call (it is how bucket/key are derived) and drops the
    mutation.
  • Option 3 — PathSuffix, QueryKey and HeaderPrefix target the wire
    request within an operation, AND-ed with Service/Operation. QueryKey is
    what separates the multipart sub-operations (uploads vs uploadId vs
    partNumber), which is what makes the reporter's own previously-unwritable
    test — "Complete fails after all parts uploaded, assert no orphaned upload" —
    writable. It is written.

Option 2 had to land with option 3, because QueryKey is only useful once
operations are named correctly.

Finding 2 — no occurrence bound, and no way to know a rule fired

FaultRule.Times bounds a rule to N occurrences. Zero means one, not
unlimited
: fail twice then succeed is the outcome that distinguishes working
retry from no retry, an unbounded rule can only ever produce failure, and reading
a mistyped zero as unlimited consumes the whole retry budget. A negative value
asks for unlimited explicitly.

Per-rule Fired counts surface through the existing GET /v1/fault/rules (a
field, not a new endpoint) and a FaultsFired() accessor. The reporter's point is
the one that matters: a rule matching nothing produces exactly the same passing
test as a consumer's retry working, and finding 1 would have been caught instantly
by any test asserting a non-zero count.

The match, the probability roll and the increment now happen under one lock —
InjectFault previously took RLock to snapshot the config and a separate write
lock for the roll, which is a race for a counter. With Times: 1, N concurrent
requests must produce exactly one failure, and that atomicity is what a
client-side counter cannot provide.

Findings 3 and 4 — an injected error was distinguishable from a real one

Finding 4 causes finding 3, and this is the one property a fault injector must
not have. marshalAWSError's default arm emitted the Query protocol's
<ErrorResponse><Error><Type>Sender</Type>… document, while genuine S3 errors
come from s3ErrorResponseWith as a bare <Error> with an XML declaration and a
<RequestId>. The SDK could not read the code out of the wrong shape, so it fell
back to the HTTP status: a requested SlowDown arrived as ServiceUnavailable.

Fixed at the marshaller. errProtoS3XML builds the document by calling
s3ErrorResponseWith, so that function stays the single source of truth for the
byte shape — error_protocol.go's existing "plugins that need a byte-exact
document build it themselves" pointer is now real rather than aspirational.

cloudfront and route53 are REST-XML too and share the Query arm, but their
real error documents are <ErrorResponse>, so they are correct where they are
and this PR does not sweep them along. The map says so, since the next reader
will wonder.

Also

FaultRule.Probability's PRNG is shared across every rule in a config, so a roll
for any rule advances it for all and a fixed seed reproduces a run only while the
sequence of requests is unchanged. Confirmed, documented, and filed as #510 —
per-rule PRNGs would change existing seeded behaviour, so it is not changed here.

Fixed along the way: NewFaultController and UpdateConfig stored the caller's
rule slice, so incrementing a count reached into the caller's own literal and a
config armed twice started its second arming already spent.

Tests

  • A rule on PutObject fires on PutObject and not on UploadPart; a rule
    on GET no longer matches an S3 request, which is the deliberate compatibility
    break.
  • QueryKey: uploads hits CreateMultipartUpload and not UploadPart; the
    orphaned-upload test the reporter could not write.
  • PathSuffix and HeaderPrefix each select, and every matcher ANDs — asserted
    from both sides, so a matching wire matcher cannot rescue a wrong operation.
  • Times: 2 → two failures then success; Times absent → exactly one,
    asserted deliberately because it is a choice; negative → unlimited.
  • Times: 1 with 10 parallel requests → exactly one failure, under -race.
  • GET /v1/fault/rules reports fired; a rule that matched nothing reports 0.
  • error_code: SlowDown arrives at the client as SlowDown, and the raw body is
    byte-identical to a genuine NoSuchKey document modulo the code and
    message — asserted two ways, since the byte comparison is the only assertion
    that proves indistinguishability.
  • EC2 (Query) and SQS (JSON RPC) injected errors are unchanged, against a
    fault-armed server with no plugins registered at all.

TestServer_PluginError_ReturnsAWSError was asserting the wrapped shape for an
S3 plugin error; it now asserts the bare <Error> and its <RequestId>, which
is the fix's point.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... (0
issues), make test (race), make docs-reference docs-reference-check docs-versions, npm --prefix docs run build, go test -C test/e2e ./... — all
clean.

Mutation-tested at 32 mutants: 31 caught, 1 proven equivalent, 0 survived.
-race is part of that harness's gate rather than an extra, because the
atomicity mutants are only detectable as a reported race. Four mutants survived
the first run and produced four new tests (ErrorDefaults,
UpdateConfigDoesNotMutateTheCallersRules, and two WireMatchers rows for an
explicitly-empty query value and for the AND asserted from the operation side).
The one equivalent mutant carries a written proof: parseS3Operation returns
"" only when req.Operation is already "", so the guard it removes can never
suppress a write that would change anything.

End to end through the AWS CLI against substrate server:

POST /v1/fault/rules {"service":"s3","operation":"PutObject",
                      "error_code":"SlowDown","http_status":503,"times":2}
put-object ×3        → SlowDown, SlowDown, 200
GET /v1/fault/rules  → "fired": 2

The injected document on the wire, beside a genuine one:

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SlowDown</Code><Message>injected fault</Message><RequestId>SUBSTRATE</RequestId></Error>
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>NoSuchBucket</Code><Message>The specified bucket does not exist.</Message><RequestId>SUBSTRATE</RequestId></Error>

And the semantic-operation fix, both requests being PUT:

rule {"service":"s3","operation":"UploadPart","times":-1}
put-object   → 200
upload-part  → InternalError

…ishable (#480)

Four findings that compound into a fault fixture being silently wrong rather
than merely limited.

An S3 rule could not name a semantic operation. Faults evaluate at pipeline
step 4.5, one step before the S3 plugin resolves the operation name, so
req.Operation was still the bare HTTP verb: a rule on PutObject never fired,
and a rule on PUT took out UploadPart with it. The parser now calls the
already-pure parseS3Operation for s3 requests, so the operation is canonical
from the start — which also fixes cost and consistency attribution for any
request that errors before routing.

A rule could not be bounded, so recovery was unobservable. FaultRule.Times
bounds a rule to N occurrences, and zero means one rather than unlimited: fail
twice then succeed is the outcome that distinguishes working retry from no
retry, and reading a mistyped zero as unlimited consumes the whole retry
budget. A negative value asks for unlimited explicitly.

A rule that matched nothing looked exactly like a consumer's retry working.
Per-rule Fired counts surface through GET /v1/fault/rules and FaultsFired();
the match, the probability roll and the increment now happen under one lock, so
Times: 1 with N concurrent requests produces exactly one failure.

An injected S3 error was distinguishable from a real one — the one property a
fault injector must not have. marshalAWSError emitted the Query protocol's
<ErrorResponse> wrapper where genuine S3 errors are a bare <Error> with an XML
declaration and a <RequestId>, so an SDK could not read the code out and fell
back to the HTTP status: a requested SlowDown arrived as ServiceUnavailable.
errProtoS3XML delegates to s3ErrorResponseWith, keeping that function the
single source of the byte shape. cloudfront and route53 are REST-XML too but
their real documents are wrapped, so they stay on the Query arm; EC2 and SQS
are unchanged.

PathSuffix, QueryKey and HeaderPrefix target the wire request within an
operation — QueryKey is what separates the multipart sub-operations, which makes
#480's own "assert no orphaned upload" test writable.

Closes #480
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.64865% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/fault.go 97.95% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 45e3610 into main Aug 2, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/480-fault-injection branch August 2, 2026 21:58
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.

Fault injection: S3 rules can't name an operation, can't be bounded to N fires, and drop ErrorCode on the wire

1 participant