The testing for developers section of the Cloud Custodian documentation walks through recording and playing back API responses for use in testing. Some cases can be tricky to reproduce in isolation though, because they occur due to concurrency, timing or caching issues that are difficult to replicate.
If you know that a particular error is possible but it's complex to reproduce inside a test, one alternative is forcing the error outside the Custodian test suite. You can record the error response in a way that simulates Custodian's own placebo-based flight recording, and then use it as an input to your tests.
This (extremely niche and low budget) example generates a couple error cases referenced in cloud-custodian/cloud-custodian#7227. Given an existing Directory Services directory with an ID of d-12345abcde, activate a Cloud Custodian development environment and run:
python /path/to/workspaces_deregister_errorgenerator.py d-12345abcdeThis should generate recorded JSON files in the current directory, similar to the ones in this gist.