Skip to content

Add chaos behavior for probabilistic fault injection#919

Open
tugkanboz wants to merge 2 commits into
mountebank-testing:masterfrom
tugkanboz:feat/chaos-behavior
Open

Add chaos behavior for probabilistic fault injection#919
tugkanboz wants to merge 2 commits into
mountebank-testing:masterfrom
tugkanboz:feat/chaos-behavior

Conversation

@tugkanboz
Copy link
Copy Markdown

Closes #918.

Adds a chaos behavior to the existing _behaviors framework. It probabilistically injects errors (replacing the response with an error status code) and/or random latency. Both are independent and all fields default to 0, so the behavior is a no-op unless explicitly configured. No new dependencies, just Math.random.

Example:

{
  "responses": [{ "is": { "statusCode": 200, "body": "ok" } }],
  "_behaviors": {
    "chaos": {
      "errorRate": 0.1,
      "errorStatusCode": 503,
      "latencyRate": 0.05,
      "maxLatencyMs": 1000
    }
  }
}

Changes:

  • behavior + validation schema in src/models/behaviors.js
  • 12 unit tests in test/models/behaviors/chaosTest.js
  • doc page src/views/docs/api/behaviors/chaos.ejs plus entry in behaviors.ejs

All 750 existing unit tests still pass and lint is clean.

Naming question from #918 is still open: chaos, fault, or faults? Happy to rename before merge.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
3 Security Hotspots

See analysis details on SonarQube Cloud

@tugkanboz
Copy link
Copy Markdown
Author

On the 3 SonarCloud security hotspots: they all flag Math.random() in the new chaos behavior. These are intentional. Chaos fault injection only needs uniform probability, not cryptographic randomness, and switching to crypto.randomInt would add cost without behavioral value. The same Math.random pattern already appears in the documented example for the wait behavior. Happy to take a different approach if you'd prefer.

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.

Add a chaos behavior for probabilistic fault injection

1 participant