-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new tests for 'import_tasks' and 'include_tasks'
Regression tests for issue number 1446
- Loading branch information
Showing
1 changed file
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"""Testing file path evaluation when using import_tasks / include_tasks.""" | ||
import os | ||
import shutil | ||
import tempfile | ||
import textwrap | ||
|
||
import pytest | ||
|
||
from ansiblelint.runner import Runner | ||
|
||
LAYOUT_IMPORTS = { | ||
'directories': ['tasks', 'tasks/subtasks'], | ||
'files': { | ||
'main.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- hosts: target | ||
gather_facts: false | ||
tasks: | ||
- name: from main import task 1 | ||
import_tasks: tasks/task_1.yml | ||
""" | ||
), | ||
'tasks/task_1.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- name: from task 1 import task 2 | ||
import_tasks: tasks/task_2.yml | ||
""" | ||
), | ||
'tasks/task_2.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- name: from task 2 import subtask 1 | ||
import_tasks: tasks/subtasks/subtask_1.yml | ||
""" | ||
), | ||
'tasks/subtasks/subtask_1.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- name: from subtask 1 import subtask 2 | ||
import_tasks: tasks/subtasks/subtask_2.yml | ||
""" | ||
), | ||
'tasks/subtasks/subtask_2.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- name: from subtask 2 do something | ||
debug: | ||
msg: | | ||
Something... | ||
""" | ||
), | ||
}, | ||
} | ||
|
||
LAYOUT_INCLUDES = { | ||
'directories': ['tasks', 'tasks/subtasks'], | ||
'files': { | ||
'main.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- hosts: target | ||
gather_facts: false | ||
tasks: | ||
- name: from main import task 1 | ||
include_tasks: tasks/task_1.yml | ||
""" | ||
), | ||
'tasks/task_1.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- name: from task 1 import task 2 | ||
include_tasks: tasks/task_2.yml | ||
""" | ||
), | ||
'tasks/task_2.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- name: from task 2 import subtask 1 | ||
include_tasks: tasks/subtasks/subtask_1.yml | ||
""" | ||
), | ||
'tasks/subtasks/subtask_1.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- name: from subtask 1 import subtask 2 | ||
include_tasks: tasks/subtasks/subtask_2.yml | ||
""" | ||
), | ||
'tasks/subtasks/subtask_2.yml': textwrap.dedent( | ||
"""\ | ||
--- | ||
- name: from subtask 2 do something | ||
debug: | ||
msg: | | ||
Something... | ||
""" | ||
), | ||
}, | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'ansible_project_layout', | ||
( | ||
pytest.param(LAYOUT_IMPORTS, id='using only import_tasks'), | ||
pytest.param(LAYOUT_INCLUDES, id='using only include_tasks'), | ||
), | ||
) | ||
@pytest.mark.xfail( | ||
reason='https://github.com/ansible-community/ansible-lint/issues/1446' | ||
) | ||
def test_file_path_evaluation(default_rules_collection, ansible_project_layout): | ||
"""Test if the usage of import_tasks / include_tasks will end up with false positive load-failure due to incorrect file path evaluation.""" | ||
root_dir_path = tempfile.mkdtemp() | ||
|
||
for directory in ansible_project_layout['directories']: | ||
os.makedirs(os.path.join(root_dir_path, directory), exist_ok=True) | ||
|
||
for file_path, file_content in ansible_project_layout['files'].items(): | ||
open(os.path.join(root_dir_path, file_path), 'w').write(file_content) | ||
|
||
result = Runner(root_dir_path, rules=default_rules_collection).run() | ||
|
||
shutil.rmtree(root_dir_path) | ||
|
||
assert '[Errno 2] No such file or directory:' in result[0].message |