Repository navigation
fix: run cleanup and destroy when a verifier fails - #4678
Merged
tanwigeetika1618 merged 1 commit intoAug 6, 2026
Merged
Conversation
When a verifier reported a non-zero return code, molecule exited immediately after the verify action and skipped cleanup and destroy, even though --destroy=always is the default. Instances were left running. The testinfra verifier signaled failure by calling util.sysexit_with_message(), which raises SystemExit. Every other action in a scenario sequence raises ScenarioFailureError on failure, and the --destroy=always teardown in _run_scenarios() only catches ScenarioFailureError. SystemExit passed straight through it, so cleanup and destroy never ran. testinfra was the only action in the verify/sequence path using sysexit instead of raising. Raise ScenarioFailureError with the verifier's return code instead, so a verify failure flows through the same teardown path as a failed playbook action. Cleanup and destroy now run, and the failure is still surfaced with the same exit code. Also record the failure on the scenario results, matching the ansible verifier, so the molecule test recap reports the failed verify as failed rather than defaulting an unrecorded action to successful. Fixes: ansible#4677 Assisted-by: Claude (Anthropic) Signed-off-by: Jeff Pullen <9343691+jeffcpullen@users.noreply.github.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughTestinfra verifier failures now record a failed completion and raise ChangesTestinfra failure handling
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
tanwigeetika1618
approved these changes
Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4677.
Problem
When a verifier fails during
molecule test, molecule exits immediately after theverifyaction and never runscleanupordestroy— even though--destroy=alwaysis the default. Instances are left running.This contradicts the documented
molecule testbehavior, which is to stop on the first failure but still tear down unless--destroy=never(docs/workflow.md). A verify failure was the one path that stopped and skipped teardown.Root cause
The testinfra verifier reports a non-zero return code by calling
util.sysexit_with_message(), which raisesSystemExit:Every other action in a scenario sequence signals failure by raising
ScenarioFailureError(e.g. a failed playbook inprovisioner/ansible_playbook.py). The--destroy=alwaysteardown incommand/base.py::_run_scenariosonly catchesScenarioFailureError:SystemExitis not aScenarioFailureError(it isn't even anException), so it passes straight through the handler and the process exits before cleanup/destroy run. testinfra was the only action in the whole verify/sequence path usingsysexitinstead of raising.Fix
Raise
ScenarioFailureErrorwith the verifier's return code instead of callingsysexit, and record the failure on the scenario results. A verify failure now flows through the same teardown path as a failed playbook action, so cleanup and destroy run under--destroy=always, and the failure is still surfaced with the same exit code.The
add_completion(...)call mirrors what the ansible verifier already does (provisioner/ansible_playbook.py). Without it, amolecule testrun's recap renders a failedverifyas "successful", because an action with no recorded state defaults to successful — so this keeps the recap honest.Two things worth noting for review:
ScenarioFailureErroralso means a verify failure is now caught by the parallel worker'sexcept Exceptionhandler (worker.py) instead of escaping it as aSystemExit(aBaseException) — so--workersruns report the failed scenario cleanly rather than losing the worker.ScenarioFailureErrorlogs its message atCRITICALon construction, the "Verifier tests failed" line now logs atCRITICAL(and, like every other scenario failure, is echoed once more atERRORas the run exits). This matches how failed playbook actions already report; it is a small, deliberate consistency change from the previousERROR-only line.Reproduction
I used the minimal scenario from #4677 (default driver,
create → verify → destroy, an intentionally-failing Testinfra test, and marker files written by the create/destroy playbooks), and ranmolecule testwith molecule installed from source — once with this change reverted, once with it applied.Before the fix —
molecule teststops atverify:After the fix — the failure still exits non-zero, but cleanup and destroy run:
Tests
tests/unit/verifier/test_testinfra.pyhad a test namedtest_testinfra_executes_catches_and_exits_return_codethat stubbedrun_commandto raiseSystemExit, so it never actually exercised the non-zero-return-code branch. I replaced it with a test that drives a real non-zero return code and asserts the verifier raisesScenarioFailureErrorcarrying that code — covering the changed branch directly.I also added
tests/unit/test_click_command_ex.py::test_click_command_ex_scenario_failure_exits_with_code, which asserts aScenarioFailureErrorcarrying a non-1code exits the process with that same code — so a failed verifier's return code is preserved end to end, not flattened to a generic1.The existing
--destroy=alwayshandling intests/unit/command/test_base.pyalready coversScenarioFailureErrortriggering teardown.Full unit suite green:
866 passed, 6 skipped(tox -e py -- tests/unit/).Summary by CodeRabbit