|
| 1 | +"""Testing file path evaluation when using import_tasks / include_tasks.""" |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +import textwrap |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from ansiblelint.runner import Runner |
| 10 | + |
| 11 | +LAYOUT_IMPORTS = { |
| 12 | + 'directories': ['tasks', 'tasks/subtasks'], |
| 13 | + 'files': { |
| 14 | + 'main.yml': textwrap.dedent( |
| 15 | + """\ |
| 16 | + --- |
| 17 | + - hosts: target |
| 18 | + gather_facts: false |
| 19 | + tasks: |
| 20 | + - name: from main import task 1 |
| 21 | + import_tasks: tasks/task_1.yml |
| 22 | + """ |
| 23 | + ), |
| 24 | + 'tasks/task_1.yml': textwrap.dedent( |
| 25 | + """\ |
| 26 | + --- |
| 27 | + - name: from task 1 import task 2 |
| 28 | + import_tasks: tasks/task_2.yml |
| 29 | + """ |
| 30 | + ), |
| 31 | + 'tasks/task_2.yml': textwrap.dedent( |
| 32 | + """\ |
| 33 | + --- |
| 34 | + - name: from task 2 import subtask 1 |
| 35 | + import_tasks: tasks/subtasks/subtask_1.yml |
| 36 | + """ |
| 37 | + ), |
| 38 | + 'tasks/subtasks/subtask_1.yml': textwrap.dedent( |
| 39 | + """\ |
| 40 | + --- |
| 41 | + - name: from subtask 1 import subtask 2 |
| 42 | + import_tasks: tasks/subtasks/subtask_2.yml |
| 43 | + """ |
| 44 | + ), |
| 45 | + 'tasks/subtasks/subtask_2.yml': textwrap.dedent( |
| 46 | + """\ |
| 47 | + --- |
| 48 | + - name: from subtask 2 do something |
| 49 | + debug: |
| 50 | + msg: | |
| 51 | + Something... |
| 52 | + """ |
| 53 | + ), |
| 54 | + }, |
| 55 | +} |
| 56 | + |
| 57 | +LAYOUT_INCLUDES = { |
| 58 | + 'directories': ['tasks', 'tasks/subtasks'], |
| 59 | + 'files': { |
| 60 | + 'main.yml': textwrap.dedent( |
| 61 | + """\ |
| 62 | + --- |
| 63 | + - hosts: target |
| 64 | + gather_facts: false |
| 65 | + tasks: |
| 66 | + - name: from main import task 1 |
| 67 | + include_tasks: tasks/task_1.yml |
| 68 | + """ |
| 69 | + ), |
| 70 | + 'tasks/task_1.yml': textwrap.dedent( |
| 71 | + """\ |
| 72 | + --- |
| 73 | + - name: from task 1 import task 2 |
| 74 | + include_tasks: tasks/task_2.yml |
| 75 | + """ |
| 76 | + ), |
| 77 | + 'tasks/task_2.yml': textwrap.dedent( |
| 78 | + """\ |
| 79 | + --- |
| 80 | + - name: from task 2 import subtask 1 |
| 81 | + include_tasks: tasks/subtasks/subtask_1.yml |
| 82 | + """ |
| 83 | + ), |
| 84 | + 'tasks/subtasks/subtask_1.yml': textwrap.dedent( |
| 85 | + """\ |
| 86 | + --- |
| 87 | + - name: from subtask 1 import subtask 2 |
| 88 | + include_tasks: tasks/subtasks/subtask_2.yml |
| 89 | + """ |
| 90 | + ), |
| 91 | + 'tasks/subtasks/subtask_2.yml': textwrap.dedent( |
| 92 | + """\ |
| 93 | + --- |
| 94 | + - name: from subtask 2 do something |
| 95 | + debug: |
| 96 | + msg: | |
| 97 | + Something... |
| 98 | + """ |
| 99 | + ), |
| 100 | + }, |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.parametrize( |
| 105 | + 'ansible_project_layout', |
| 106 | + ( |
| 107 | + pytest.param(LAYOUT_IMPORTS, id='using only import_tasks'), |
| 108 | + pytest.param(LAYOUT_INCLUDES, id='using only include_tasks'), |
| 109 | + ), |
| 110 | +) |
| 111 | +@pytest.mark.xfail( |
| 112 | + reason='https://github.com/ansible-community/ansible-lint/issues/1446' |
| 113 | +) |
| 114 | +def test_file_path_evaluation(default_rules_collection, ansible_project_layout): |
| 115 | + """Test if the usage of import_tasks / include_tasks will end up with false positive load-failure due to incorrect file path evaluation.""" |
| 116 | + root_dir_path = tempfile.mkdtemp() |
| 117 | + |
| 118 | + for directory in ansible_project_layout['directories']: |
| 119 | + os.makedirs(os.path.join(root_dir_path, directory), exist_ok=True) |
| 120 | + |
| 121 | + for file_path, file_content in ansible_project_layout['files'].items(): |
| 122 | + open(os.path.join(root_dir_path, file_path), 'w').write(file_content) |
| 123 | + |
| 124 | + result = Runner(root_dir_path, rules=default_rules_collection).run() |
| 125 | + |
| 126 | + shutil.rmtree(root_dir_path) |
| 127 | + |
| 128 | + assert '[Errno 2] No such file or directory:' in result[0].message |
0 commit comments