Summary
setup_mocks() is only called from inside setup_virtualenv(). If a test uses @pytest.mark.modules_to_mock(...) without also using @pytest.mark.use_virtualenv, the mock files are registered but never substituted for the real Ansible modules. The real module executes silently, causing network calls, file writes, or other side effects — with no warning that the mock was skipped.
Affected file
ansible_playtest/core/playbook_runner.py
def setup_virtualenv(self):
if not self.use_virtualenv:
return # ← early exit; setup_mocks() is never called
...
self.module_mocker.setup_mocks(self.virtualenv.path)
Reproducer
@pytest.mark.modules_to_mock({"ansible.builtin.uri": "tests/mocks/ansible.builtin.uri.py"})
# Missing: @pytest.mark.use_virtualenv
def test_my_playbook(playbook_runner):
...
The ansible.builtin.uri mock file exists and is correctly referenced, but the real uri module runs and makes a live HTTP request.
Expected behaviour
Either:
- Raise an explicit error or warning when
modules_to_mock is specified but use_virtualenv is not, or
- Activate flat mock files independently of the virtualenv setup path.
Impact
This is particularly dangerous because the test does not fail — it appears to pass while actually hitting real infrastructure.
Workaround
Always pair @pytest.mark.modules_to_mock(...) with @pytest.mark.use_virtualenv. Note that @pytest.mark.mock_collections_dir(...) does not have this constraint.
Summary
setup_mocks()is only called from insidesetup_virtualenv(). If a test uses@pytest.mark.modules_to_mock(...)without also using@pytest.mark.use_virtualenv, the mock files are registered but never substituted for the real Ansible modules. The real module executes silently, causing network calls, file writes, or other side effects — with no warning that the mock was skipped.Affected file
ansible_playtest/core/playbook_runner.pyReproducer
The
ansible.builtin.urimock file exists and is correctly referenced, but the realurimodule runs and makes a live HTTP request.Expected behaviour
Either:
modules_to_mockis specified butuse_virtualenvis not, orImpact
This is particularly dangerous because the test does not fail — it appears to pass while actually hitting real infrastructure.
Workaround
Always pair
@pytest.mark.modules_to_mock(...)with@pytest.mark.use_virtualenv. Note that@pytest.mark.mock_collections_dir(...)does not have this constraint.