Skip to content

fix(cli): always build the fault controller, and seed it from config - #585

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/fault-wiring
Aug 6, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/fault-wiring

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Two gaps that made the fault tier unreachable from a config file

A default server answered 501. substrate server built a FaultController
only when the config file had fault.enabled or at least one rule
(main.go:153-156). Without one, all three /v1/fault/rules endpoints returned
501 fault injection not enabled on this server, so a harness could not arm a rule
on a default server at all — which is precisely the remedy docs/testing-guide.md
points a reader at. This is not hypothetical: it silently swallowed the first probe
of the #540 investigation.

Its seed was the wall clock. When the controller was built, it came up with
time.Now().UnixNano(), and FaultCfg had no seed field, so a config file could
not pin it. A probabilistic rule from a config file therefore produced a different
run every time — the one property this emulator exists to avoid.
StartTestServer already did the right thing in-process (NewFaultController(..., 1)); only the CLI path was affected.

The fix

The controller is always constructed. A disabled one makes InjectFault a
no-op, so behavior is unchanged for every run that never arms a rule; only a
Server constructed in-process with no controller still answers 501, which
TestFaultControl_NilController_Returns501 asserts and which still passes.

fault.seed (int64, mapstructure:"seed") supplies the seed, defaulting to
0 — deterministic, which is what Substrate is for. Documented in
configs/substrate-local.yaml and the testing guide.

Construction moved into a newFaultController function so a test can assert both
halves without starting a server. That seam exists because it was needed: the first
run of the mutation pass had the mutant restoring conditional construction
SURVIVE, since every existing test built its server in-process and none
exercised the CLI's construction path at all.

Also: the testing guide's fault example did not compile

It called substrate.WithFaultController(fc) — a symbol that exists nowhere in the
repo. The option is substrate.ServerOptions{Fault: fc}. Found by grep while
scoping this.

The <!-- TODO(#178): document server.WithFaultController option once stabilised --> beneath it is replaced with the documentation it was deferring: #178 is closed,
and the symbol it names never existed. In its place, a short section on arming rules
over the wire and on fault.seed.

Tests

  • cmd/substrate/main_test.go — TestNewFaultController_AlwaysBuiltAndSeeded: a
    default config yields a non-nil controller (with preconditions asserting the
    default config really does not enable faults, so the test cannot pass vacuously);
    it injects nothing until armed; arming makes a rule fire; and the configured seed
    reaches the controller — the same seed traces identically over 24 requests, a
    different seed does not, and the trace contains both outcomes so "reproducible"
    is not trivially true.
  • emulator/fault_wiring_test.go — fault.seed round-trips through
    ToFaultConfig with the default asserted as 0, and a disabled controller
    accepts rules over the wire and then fires them with fired incremented.

Mutation tested, 2 mutants, both CAUGHT after the seam was added:
restoring the conditional construction, and restoring time.Now().UnixNano().

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 vet -C test/e2e ./..., go test -C test/e2e ./... — all clean.

End to end against a default substrate server with no fault: block:

$ curl -s -o /dev/null -w '%{http_code}\n' -X POST localhost:4623/v1/fault/rules -d '{...}'
200                                     # was 501
$ AWS_MAX_ATTEMPTS=1 aws s3api put-object --bucket faultwire --key k --body /tmp/v
An error occurred (InternalError) when calling the PutObject operation: injected fault
$ AWS_MAX_ATTEMPTS=1 aws s3api put-object --bucket faultwire --key k --body /tmp/v
{ ... }                                 # times: 1, so the retry succeeds
$ curl -s localhost:4623/v1/fault/rules | jq '.rules[0].fired'
1

substrate server built a FaultController only when the config file had
fault.enabled or at least one rule. On a default server all three
/v1/fault/rules endpoints therefore answered

  501 fault injection not enabled on this server

so a harness could not arm a rule at all — which is the remedy the docs point
at. The controller is now always constructed; a disabled one makes InjectFault a
no-op, so behavior is unchanged for a run that never arms a rule. A Server
constructed in-process with no controller still answers 501, which
TestFaultControl_NilController_Returns501 asserts.

The seed came from time.Now().UnixNano(), so a probabilistic rule loaded from a
config file produced a different run every time. fault.seed now supplies it,
defaulting to 0 — deterministic, which is what this emulator is for; the
wall-clock seed was the anomaly.

Construction moved into newFaultController so a test can assert both halves
without starting a server. Both mutants — restoring the conditional construction,
and restoring the wall-clock seed — are caught.

Also fixes the testing guide's fault example, which called
substrate.WithFaultController(fc): that symbol exists nowhere in the repo, so the
example never compiled. The option is substrate.ServerOptions{Fault: fc}. The
guide gains a section on arming rules over the wire and on fault.seed, replacing
a TODO that pointed at a closed issue and at that same non-existent symbol.
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.

1 participant