Level 8 / Project 08 - Fault Injection Harness¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
- Configurable fault injection with decorator and context manager APIs
- Fault types: exception, delay, timeout, data corruption
- Probability-based triggering with seeded RNG for reproducibility
- Scoped fault injection with automatic cleanup
- Statistics tracking: trigger rates, events, interception counts
Why this project exists¶
Netflix's Chaos Monkey proved that injecting failures proactively builds more resilient systems. Error-handling code paths are the least tested in most codebases — they only run during real outages when stakes are highest. This project creates a configurable fault injection framework that can introduce exceptions, delays, and data corruption into any function call, teaching how to test the code paths that rarely execute in normal operation. This is the foundation of chaos engineering.
Run (copy/paste)¶
Expected terminal output¶
{
"stats": {"calls_intercepted": 40, "faults_triggered": 10, ...},
"results_sample": [...],
"corruption_demo": {"original": {...}, "corrupted": {...}}
}
7 passed
Expected artifacts¶
- Console JSON output showing fault injection stats and corruption demo
- Passing tests
- Updated
notes.md
Alter it (required)¶
- Add a
FaultType.DATA_CORRUPTIONtype that callscorrupt_dataautomatically on function return values. - Add a
max_triggersfield toFaultConfigthat limits how many times a rule can fire. - Add a
--reportflag that outputs allFaultEvententries as a JSON summary.
Break it (required)¶
- Set
probabilityto a value > 1.0 — doesFaultConfigvalidation catch it? - Use the
scope()context manager and raise inside it — are temporary rules still cleaned up? - Apply the
@injectdecorator to a function that takes**kwargs— doesfunc.__name__survive?
Fix it (required)¶
- Ensure
scope()uses atry/finallyso rules are removed even on exceptions. - Use
functools.wrapsin theinjectdecorator to preserve function metadata. - Add a test that verifies
max_triggersstops injection after the limit.
Explain it (teach-back)¶
- What is fault injection and why do teams use it in production (chaos engineering)?
- How does the
@contextmanagerdecorator turn a generator into a context manager? - Why does
corrupt_datacheckisinstance(value, bool)beforeisinstance(value, (int, float))? - How would you extend this to inject network-level faults (e.g. packet loss)?
Mastery check¶
You can move on when you can:
- explain why bool must be checked before int in Python's type hierarchy,
- add a new fault type end-to-end (config + injection + test),
- describe the difference between chaos engineering and traditional testing,
- explain how probability-based injection helps find intermittent bugs.
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|