summaryrefslogtreecommitdiffstats
path: root/test/TestImportIncludeRole.py
blob: 8b581ff23af328a9d716e6240b08a85c1509230b (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
import pytest

from ansiblelint.runner import Runner

ROLE_TASKS_MAIN = '''
- name: shell instead of command
  shell: echo hello world
'''

ROLE_TASKS_WORLD = '''
- command: echo this is a task without a name
'''

PLAY_IMPORT_ROLE = '''
- hosts: all

  tasks:
    - import_role:
        name: test-role
'''

PLAY_IMPORT_ROLE_INCOMPLETE = '''
- hosts: all

  tasks:
    - import_role:
        foo: bar
'''

PLAY_IMPORT_ROLE_INLINE = '''
- hosts: all

  tasks:
    - import_role: name=test-role
'''

PLAY_INCLUDE_ROLE = '''
- hosts: all

  tasks:
    - include_role:
        name: test-role
        tasks_from: world
'''

PLAY_INCLUDE_ROLE_INLINE = '''
- hosts: all

  tasks:
    - include_role: name=test-role tasks_from=world
'''


@pytest.fixture
def playbook_path(request, tmp_path):
    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'],
                 id='IMPORT_ROLE',
                 ),
    pytest.param(PLAY_IMPORT_ROLE_INLINE,
                 ['only when shell functionality is require'],
                 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_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, playbook_path, messages):
    runner = Runner(default_rules_collection, playbook_path, [], [], [])
    results = runner.run()
    for message in messages:
        assert message in str(results)


@pytest.mark.parametrize(('playbook_path', 'messages'), (
    pytest.param(PLAY_IMPORT_ROLE_INCOMPLETE,
                 ["Failed to find required 'name' key in import_role"],
                 id='IMPORT_ROLE_INCOMPLETE',
                 ),
), indirect=('playbook_path', ))
def test_invalid_import_role(default_rules_collection, playbook_path, messages):
    runner = Runner(default_rules_collection, playbook_path, [], [], [])
    results = runner.run()
    for message in messages:
        assert message in str(results)