summaryrefslogtreecommitdiffstats
path: root/test/test_skip_playbook_items.py
blob: 2861c6aff3dea0b175be0a57710a5ed03a6f0e62 (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
"""Tests related to use of noqa inside playbooks."""
import pytest

from ansiblelint.testing import RunFromText

PLAYBOOK_PRE_TASKS = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Bad git 1  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 2
      action: ansible.builtin.git a=b c=d
  pre_tasks:
    - name: Bad git 3  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 4
      action: ansible.builtin.git a=b c=d
"""

PLAYBOOK_POST_TASKS = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Bad git 1  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 2
      action: ansible.builtin.git a=b c=d
  post_tasks:
    - name: Bad git 3  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 4
      action: ansible.builtin.git a=b c=d
"""

PLAYBOOK_HANDLERS = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Bad git 1  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 2
      action: ansible.builtin.git a=b c=d
  handlers:
    - name: Bad git 3  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 4
      action: ansible.builtin.git a=b c=d
"""

PLAYBOOK_TWO_PLAYS = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Bad git 1  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 2
      action: ansible.builtin.git a=b c=d

- name: Fixture 2
  hosts: all
  tasks:
    - name: Bad git 3  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 4
      action: ansible.builtin.git a=b c=d
"""

PLAYBOOK_WITH_BLOCK = """\
---
- name: Fixture
  hosts: all
  tasks:
    - name: Bad git 1  # noqa: latest[git]
      action: ansible.builtin.git a=b c=d
    - name: Bad git 2
      action: ansible.builtin.git a=b c=d
    - name: Block with rescue and always section
      block:
        - name: Bad git 3  # noqa: latest[git]
          action: ansible.builtin.git a=b c=d
        - name: Bad git 4
          action: ansible.builtin.git a=b c=d
      rescue:
        - name: Bad git 5  # noqa: latest[git]
          action: ansible.builtin.git a=b c=d
        - name: Bad git 6
          action: ansible.builtin.git a=b c=d
      always:
        - name: Bad git 7  # noqa: latest[git]
          action: ansible.builtin.git a=b c=d
        - name: Bad git 8
          action: ansible.builtin.git a=b c=d
"""


@pytest.mark.parametrize(
    ("playbook", "length"),
    (
        pytest.param(PLAYBOOK_PRE_TASKS, 6, id="PRE_TASKS"),
        pytest.param(PLAYBOOK_POST_TASKS, 6, id="POST_TASKS"),
        pytest.param(PLAYBOOK_HANDLERS, 6, id="HANDLERS"),
        pytest.param(PLAYBOOK_TWO_PLAYS, 6, id="TWO_PLAYS"),
        pytest.param(PLAYBOOK_WITH_BLOCK, 12, id="WITH_BLOCK"),
    ),
)
def test_pre_tasks(
    default_text_runner: RunFromText,
    playbook: str,
    length: int,
) -> None:
    """Check that skipping is possible in different playbook parts."""
    # When
    results = default_text_runner.run_playbook(playbook)

    # Then
    assert len(results) == length