Skip to content

Flat mock files (modules_to_mock) are silently ignored without use_virtualenv marker — real module runs instead #5

@jorge-romero

Description

@jorge-romero

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:

  1. Raise an explicit error or warning when modules_to_mock is specified but use_virtualenv is not, or
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions