From 782f8df6e41f29dce2db4970a3ad84aaeb7d8c5f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 14 May 2024 22:04:50 +0200 Subject: Adding upstream version 4.3.7. Signed-off-by: Daniel Baumann --- test/TestImportIncludeRole.py | 103 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 test/TestImportIncludeRole.py (limited to 'test/TestImportIncludeRole.py') diff --git a/test/TestImportIncludeRole.py b/test/TestImportIncludeRole.py new file mode 100644 index 0000000..8b581ff --- /dev/null +++ b/test/TestImportIncludeRole.py @@ -0,0 +1,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) -- cgit v1.2.3