Skip to content

Add chaos behavior for probabilistic fault injection - #919

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

tugkanboz wants to merge 6 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.

@mattherman

Copy link
Copy Markdown
Contributor

Hoping to have time to look at this soon - apologies for letting it sit. It seems like a reasonable idea.

@tugkanboz

Copy link
Copy Markdown
Author

Thanks @mattherman, no rush. Looking forward to your feedback.

@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

Hi @mattherman , just checking in. Have you had a chance to review this PR?

Please let me know if there’s anything I should update or change. Thanks!

Comment thread test/models/behaviors/chaosTest.js Outdated
Comment thread test/models/behaviors/chaosTest.js
Comment thread test/models/behaviors/chaosTest.js
Comment thread src/models/behaviors.js
Comment thread src/views/docs/api/behaviors/chaos.ejs
Comment thread src/views/docs/api/behaviors/chaos.ejs Outdated
Comment thread src/models/behaviors.js
Comment thread src/views/docs/api/behaviors/chaos.ejs Outdated
Comment thread test/models/behaviors/chaosTest.js
@tugkanboz

Copy link
Copy Markdown
Author

Thanks for the thorough review, @mattherman! Pushed changes addressing all the feedback:

  • Reworked the errorRate: 0 / maxLatencyMs: 0 tests to use a non-zero random draw, so they actually validate the config rather than a low random draw
  • Added the missing latencyRate validation test
  • maxLatencyMs now defaults to 1000ms instead of being a silent no-op when only latencyRate is set
  • Docs now clarify chaos error injection is HTTP/HTTPS-only, with a link to the fault response for TCP
  • Dropped "faults" wording in the docs to avoid confusion with the fault response
  • Turned the docs example into an actual documentation test (deterministic, errorRate: 1)
  • Added API tests in mbTest/api/http/httpBehaviorsTest.js

Left the status code range validation as a follow-up per your note. Let me know if anything else needs adjusting.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

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

2 participants