summaryrefslogtreecommitdiffstats
path: root/test/test_import_include_role.py
blob: bc3fdbe33200fb142bc5e65693bd65efa9d0e2ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Tests related to role imports."""
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from ansiblelint.runner import Runner

if TYPE_CHECKING:
    from pathlib import Path

    from _pytest.fixtures import SubRequest

    from ansiblelint.rules import RulesCollection

ROLE_TASKS_MAIN = """\
---
- name: Shell instead of command
  shell: echo hello world # noqa: fqcn no-free-form
  changed_when: false
"""

ROLE_TASKS_WORLD = """\
---
- ansible.builtin.debug:
    msg: "this is a task without a name"
"""

PLAY_IMPORT_ROLE = """\
---
- name: Test fixture
  hosts: all

  tasks:
    - name: Some import # noqa: fqcn
      import_role:
        name: test-role
"""

PLAY_IMPORT_ROLE_FQCN = """\
---
- name: Test fixture
  hosts: all

  tasks:
    - name: Some import
      ansible.builtin.import_role:
        name: test-role
"""

PLAY_IMPORT_ROLE_INLINE = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Some import
      import_role: name=test-role  # noqa: no-free-form fqcn
"""

PLAY_INCLUDE_ROLE = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Some import
      include_role:
        name: test-role
        tasks_from: world
"""

PLAY_INCLUDE_ROLE_FQCN = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Some import
      ansible.builtin.include_role:
        name: test-role
        tasks_from: world
"""

PLAY_INCLUDE_ROLE_INLINE = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Some import
      include_role: name=test-role tasks_from=world  # noqa: no-free-form
"""


@pytest.fixture(name="playbook_path")
def fixture_playbook_path(request: SubRequest, tmp_path: Path) -> str:
    """Create a reusable per-test role skeleton."""
    playbook_text = request.param
    role_tasks_dir = tmp_path / "test-role" / "tasks"
    role_tasks_dir.mkdir(parents=True)
    (role_tasks_dir / "main.yml").write_text(ROLE_TASKS_MAIN)
    (role_tasks_dir / "world.yml").write_text(ROLE_TASKS_WORLD)
    play_path = tmp_path / "playbook.yml"
    play_path.write_text(playbook_text)
    return str(play_path)


@pytest.mark.parametrize(
    ("playbook_path", "messages"),
    (
        pytest.param(
            PLAY_IMPORT_ROLE,
            ["only when shell functionality is required", "All tasks should be named"],
            id="IMPORT_ROLE",
        ),
        pytest.param(
            PLAY_IMPORT_ROLE_FQCN,
            ["only when shell functionality is required", "All tasks should be named"],
            id="IMPORT_ROLE_FQCN",
        ),
        pytest.param(
            PLAY_IMPORT_ROLE_INLINE,
            ["only when shell functionality is require", "All tasks should be named"],
            id="IMPORT_ROLE_INLINE",
        ),
        pytest.param(
            PLAY_INCLUDE_ROLE,
            ["only when shell functionality is require", "All tasks should be named"],
            id="INCLUDE_ROLE",
        ),
        pytest.param(
            PLAY_INCLUDE_ROLE_FQCN,
            ["only when shell functionality is require", "All tasks should be named"],
            id="INCLUDE_ROLE_FQCN",
        ),
        pytest.param(
            PLAY_INCLUDE_ROLE_INLINE,
            ["only when shell functionality is require", "All tasks should be named"],
            id="INCLUDE_ROLE_INLINE",
        ),
    ),
    indirect=("playbook_path",),
)
def test_import_role2(
    default_rules_collection: RulesCollection,
    playbook_path: str,
    messages: list[str],
) -> None:
    """Test that include_role digs deeper than import_role."""
    runner = Runner(
        playbook_path,
        rules=default_rules_collection,
        skip_list=["fqcn[action-core]"],
    )
    results = runner.run()
    for message in messages:
        assert message in str(results)
    # Ensure no other unexpected messages are present
    assert len(messages) == len(results), results