2022-01-29 13:47:09 +00:00
|
|
|
import os
|
2022-01-06 22:49:52 +00:00
|
|
|
from typing import List
|
|
|
|
|
2021-10-03 14:04:08 +00:00
|
|
|
import pytest
|
2021-10-03 19:08:28 +00:00
|
|
|
from ansible.utils.display import Display
|
2021-10-03 14:04:08 +00:00
|
|
|
|
|
|
|
from ansibleplaybookgrapher import PlaybookParser
|
|
|
|
from ansibleplaybookgrapher.cli import PlaybookGrapherCLI
|
2023-05-13 12:41:07 +00:00
|
|
|
from ansibleplaybookgrapher.graph_model import (
|
2022-02-07 22:41:13 +00:00
|
|
|
TaskNode,
|
|
|
|
BlockNode,
|
|
|
|
RoleNode,
|
2022-06-17 22:11:16 +00:00
|
|
|
Node,
|
2022-02-07 22:41:13 +00:00
|
|
|
CompositeNode,
|
|
|
|
)
|
2022-01-29 13:47:09 +00:00
|
|
|
from tests import FIXTURES_DIR
|
|
|
|
|
|
|
|
# This file directory abspath
|
|
|
|
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
# Fixtures abspath
|
|
|
|
FIXTURES_PATH = os.path.join(DIR_PATH, FIXTURES_DIR)
|
2022-01-06 22:49:52 +00:00
|
|
|
|
|
|
|
|
2022-06-17 22:11:16 +00:00
|
|
|
def get_all_tasks(nodes: List[Node]) -> List[TaskNode]:
|
2022-01-06 22:49:52 +00:00
|
|
|
"""
|
2022-06-17 22:11:16 +00:00
|
|
|
Recursively Get all tasks from a list of nodes
|
|
|
|
:param nodes:
|
2022-01-06 22:49:52 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
tasks = []
|
|
|
|
|
2022-06-17 22:11:16 +00:00
|
|
|
for n in nodes:
|
|
|
|
if isinstance(n, CompositeNode):
|
|
|
|
tasks.extend(n.get_all_tasks())
|
|
|
|
else:
|
|
|
|
tasks.append(n)
|
2022-01-06 22:49:52 +00:00
|
|
|
|
|
|
|
return tasks
|
2021-10-03 14:04:08 +00:00
|
|
|
|
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
@pytest.mark.parametrize("grapher_cli", [["example.yml"]], indirect=True)
|
2022-01-29 13:47:09 +00:00
|
|
|
def test_example_parsing(grapher_cli: PlaybookGrapherCLI, display: Display):
|
|
|
|
"""
|
|
|
|
Test the parsing of example.yml
|
|
|
|
:param grapher_cli:
|
|
|
|
:param display:
|
|
|
|
:return:
|
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
parser = PlaybookParser(grapher_cli.options.playbook_filenames[0])
|
2022-01-29 13:47:09 +00:00
|
|
|
playbook_node = parser.parse()
|
|
|
|
assert len(playbook_node.plays) == 1
|
|
|
|
assert playbook_node.path == os.path.join(FIXTURES_PATH, "example.yml")
|
|
|
|
assert playbook_node.line == 1
|
|
|
|
assert playbook_node.column == 1
|
2023-05-10 16:45:57 +00:00
|
|
|
assert (
|
|
|
|
playbook_node.index is None
|
|
|
|
), "The index of the playbook should be None (it has no parent)"
|
2022-01-29 13:47:09 +00:00
|
|
|
|
2022-06-17 22:11:16 +00:00
|
|
|
play_node = playbook_node.plays[0]
|
2022-01-29 13:47:09 +00:00
|
|
|
assert play_node.path == os.path.join(FIXTURES_PATH, "example.yml")
|
|
|
|
assert play_node.line == 2
|
2023-05-10 16:45:57 +00:00
|
|
|
assert play_node.index == 1
|
2022-01-29 13:47:09 +00:00
|
|
|
|
|
|
|
pre_tasks = play_node.pre_tasks
|
|
|
|
assert len(pre_tasks) == 2
|
2023-05-10 16:45:57 +00:00
|
|
|
assert pre_tasks[0].index == 1, "The index of the first pre_task should be 1"
|
|
|
|
assert pre_tasks[1].index == 2, "The index of the second pre_task should be 2"
|
|
|
|
|
|
|
|
tasks = play_node.tasks
|
2022-01-29 13:47:09 +00:00
|
|
|
assert len(tasks) == 4
|
2023-05-10 16:45:57 +00:00
|
|
|
for task_counter, task in enumerate(tasks):
|
|
|
|
assert (
|
|
|
|
task.index == task_counter + len(pre_tasks) + 1
|
|
|
|
), "The index of the task should start after the pre_tasks"
|
|
|
|
|
|
|
|
post_tasks = play_node.post_tasks
|
2022-01-29 13:47:09 +00:00
|
|
|
assert len(post_tasks) == 2
|
2023-05-10 16:45:57 +00:00
|
|
|
for post_task_counter, task in enumerate(post_tasks):
|
|
|
|
assert (
|
|
|
|
task.index == post_task_counter + len(pre_tasks) + len(tasks) + 1
|
|
|
|
), "The index of the post task should start after the pre_tasks and tasks"
|
2022-01-29 13:47:09 +00:00
|
|
|
|
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
@pytest.mark.parametrize("grapher_cli", [["with_roles.yml"]], indirect=True)
|
2022-01-29 13:47:09 +00:00
|
|
|
def test_with_roles_parsing(grapher_cli: PlaybookGrapherCLI):
|
|
|
|
"""
|
|
|
|
Test the parsing of with_roles.yml
|
|
|
|
:param grapher_cli:
|
|
|
|
:return:
|
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
parser = PlaybookParser(grapher_cli.options.playbook_filenames[0])
|
2022-01-29 13:47:09 +00:00
|
|
|
playbook_node = parser.parse()
|
|
|
|
assert len(playbook_node.plays) == 1
|
2022-06-17 22:11:16 +00:00
|
|
|
play_node = playbook_node.plays[0]
|
2023-05-10 16:45:57 +00:00
|
|
|
assert play_node.index == 1
|
|
|
|
|
2022-01-29 13:47:09 +00:00
|
|
|
assert len(play_node.roles) == 2
|
|
|
|
|
2022-06-17 22:11:16 +00:00
|
|
|
fake_role = play_node.roles[0]
|
2022-01-29 13:47:09 +00:00
|
|
|
assert isinstance(fake_role, RoleNode)
|
|
|
|
assert not fake_role.include_role
|
|
|
|
assert fake_role.path == os.path.join(FIXTURES_PATH, "roles", "fake_role")
|
|
|
|
assert fake_role.line is None
|
|
|
|
assert fake_role.column is None
|
2023-05-10 16:45:57 +00:00
|
|
|
assert fake_role.index == 3
|
|
|
|
|
|
|
|
for task_counter, task in enumerate(fake_role.tasks):
|
|
|
|
assert (
|
|
|
|
task.index == task_counter + 1
|
|
|
|
), "The index of the task in the role should start at 1"
|
|
|
|
|
|
|
|
display_some_facts = play_node.roles[1]
|
|
|
|
for task_counter, task in enumerate(display_some_facts.tasks):
|
|
|
|
assert (
|
|
|
|
task.index == task_counter + 1
|
|
|
|
), "The index of the task in the role the should start at 1"
|
2022-01-29 13:47:09 +00:00
|
|
|
|
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
@pytest.mark.parametrize("grapher_cli", [["include_role.yml"]], indirect=True)
|
2022-01-29 13:47:09 +00:00
|
|
|
def test_include_role_parsing(grapher_cli: PlaybookGrapherCLI, capsys):
|
2021-10-03 14:04:08 +00:00
|
|
|
"""
|
|
|
|
Test parsing of include_role
|
|
|
|
:param grapher_cli:
|
|
|
|
:return:
|
|
|
|
"""
|
2022-02-07 22:41:13 +00:00
|
|
|
parser = PlaybookParser(
|
2022-08-14 20:59:46 +00:00
|
|
|
grapher_cli.options.playbook_filenames[0], include_role_tasks=True
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2021-10-03 14:04:08 +00:00
|
|
|
playbook_node = parser.parse()
|
|
|
|
assert len(playbook_node.plays) == 1
|
2022-06-17 22:11:16 +00:00
|
|
|
play_node = playbook_node.plays[0]
|
2021-10-03 14:04:08 +00:00
|
|
|
tasks = play_node.tasks
|
2022-01-11 21:32:10 +00:00
|
|
|
assert len(tasks) == 6
|
2021-10-03 14:04:08 +00:00
|
|
|
|
2022-01-11 21:42:03 +00:00
|
|
|
# Since we use some loops inside the playbook, a warning should be displayed
|
2022-02-07 22:41:13 +00:00
|
|
|
assert (
|
|
|
|
"Looping on tasks or roles are not supported for the moment"
|
|
|
|
in capsys.readouterr().err
|
|
|
|
), "A warning should be displayed regarding loop being not supported"
|
2022-01-11 21:42:03 +00:00
|
|
|
|
2022-08-17 20:20:55 +00:00
|
|
|
# first include_role using a block
|
|
|
|
block_include_role = tasks[0]
|
|
|
|
assert isinstance(block_include_role, BlockNode)
|
|
|
|
include_role_1 = block_include_role.tasks[0]
|
2021-10-03 14:04:08 +00:00
|
|
|
assert isinstance(include_role_1, RoleNode)
|
2022-01-29 13:47:09 +00:00
|
|
|
assert include_role_1.include_role
|
|
|
|
assert include_role_1.path == os.path.join(FIXTURES_PATH, "include_role.yml")
|
2022-08-17 20:20:55 +00:00
|
|
|
assert include_role_1.line == 10, "The first include role should be at line 9"
|
2022-02-07 22:41:13 +00:00
|
|
|
assert (
|
|
|
|
len(include_role_1.tasks) == 0
|
|
|
|
), "We don't support adding tasks from include_role with loop"
|
2023-05-13 16:21:39 +00:00
|
|
|
assert include_role_1.has_loop(), "The first include role has a loop"
|
2021-10-03 14:04:08 +00:00
|
|
|
|
2022-01-11 21:32:10 +00:00
|
|
|
# first task
|
2022-06-17 22:11:16 +00:00
|
|
|
assert tasks[1].name == "(1) Debug"
|
|
|
|
assert tasks[1].when == '[when: ansible_os == "ubuntu"]'
|
2021-10-03 14:04:08 +00:00
|
|
|
|
|
|
|
# second include_role
|
2022-06-17 22:11:16 +00:00
|
|
|
include_role_2 = tasks[2]
|
2021-10-03 14:04:08 +00:00
|
|
|
assert isinstance(include_role_2, RoleNode)
|
2022-01-29 13:47:09 +00:00
|
|
|
assert include_role_2.include_role
|
2021-10-03 14:04:08 +00:00
|
|
|
assert len(include_role_2.tasks) == 3
|
2023-05-13 16:21:39 +00:00
|
|
|
assert not include_role_2.has_loop(), "The second include role doesn't have a loop"
|
2021-10-03 14:04:08 +00:00
|
|
|
|
2022-01-11 21:32:10 +00:00
|
|
|
# second task
|
2022-06-17 22:11:16 +00:00
|
|
|
assert tasks[3].name == "(3) Debug 2"
|
2022-01-11 21:32:10 +00:00
|
|
|
|
2022-01-09 19:24:52 +00:00
|
|
|
# third include_role
|
2022-06-17 22:11:16 +00:00
|
|
|
include_role_3 = tasks[4]
|
|
|
|
assert tasks[4].when == "[when: x is not defined]"
|
2022-01-09 19:24:52 +00:00
|
|
|
assert isinstance(include_role_3, RoleNode)
|
2022-01-29 13:47:09 +00:00
|
|
|
assert include_role_3.include_role
|
2022-01-11 21:32:10 +00:00
|
|
|
assert len(include_role_3.tasks) == 3
|
2023-05-13 16:21:39 +00:00
|
|
|
assert not include_role_3.has_loop(), "The second include role doesn't have a loop"
|
2022-01-11 21:32:10 +00:00
|
|
|
|
|
|
|
# fourth include_role
|
2022-06-17 22:11:16 +00:00
|
|
|
include_role_4 = tasks[5]
|
2022-01-11 21:32:10 +00:00
|
|
|
assert isinstance(include_role_4, RoleNode)
|
2022-01-29 13:47:09 +00:00
|
|
|
assert include_role_4.include_role
|
2022-02-07 22:41:13 +00:00
|
|
|
assert (
|
|
|
|
len(include_role_4.tasks) == 0
|
|
|
|
), "We don't support adding tasks from include_role with loop"
|
2023-05-13 16:21:39 +00:00
|
|
|
assert include_role_4.has_loop(), "The third include role has a loop"
|
2022-01-09 19:24:52 +00:00
|
|
|
|
2021-10-03 14:04:08 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
@pytest.mark.parametrize("grapher_cli", [["with_block.yml"]], indirect=True)
|
2022-01-29 13:47:09 +00:00
|
|
|
def test_block_parsing(grapher_cli: PlaybookGrapherCLI):
|
2021-10-03 14:04:08 +00:00
|
|
|
"""
|
|
|
|
The parsing of a playbook with blocks
|
|
|
|
:param grapher_cli:
|
|
|
|
:return:
|
|
|
|
"""
|
2022-02-07 22:41:13 +00:00
|
|
|
parser = PlaybookParser(
|
2022-08-14 20:59:46 +00:00
|
|
|
grapher_cli.options.playbook_filenames[0], include_role_tasks=True
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2021-10-03 14:04:08 +00:00
|
|
|
playbook_node = parser.parse()
|
|
|
|
assert len(playbook_node.plays) == 1
|
|
|
|
|
2022-06-17 22:11:16 +00:00
|
|
|
play_node = playbook_node.plays[0]
|
2022-01-06 22:49:52 +00:00
|
|
|
pre_tasks = play_node.pre_tasks
|
2021-10-03 14:04:08 +00:00
|
|
|
tasks = play_node.tasks
|
|
|
|
post_tasks = play_node.post_tasks
|
2022-06-17 22:11:16 +00:00
|
|
|
|
2022-01-06 22:49:52 +00:00
|
|
|
total_pre_tasks = get_all_tasks(pre_tasks)
|
|
|
|
total_tasks = get_all_tasks(tasks)
|
|
|
|
total_post_tasks = get_all_tasks(post_tasks)
|
2022-06-17 22:11:16 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
assert (
|
|
|
|
len(total_pre_tasks) == 4
|
|
|
|
), f"The play should contain 4 pre tasks but we found {len(total_pre_tasks)} pre task(s)"
|
|
|
|
assert (
|
|
|
|
len(total_tasks) == 7
|
|
|
|
), f"The play should contain 3 tasks but we found {len(total_tasks)} task(s)"
|
|
|
|
assert (
|
|
|
|
len(total_post_tasks) == 2
|
|
|
|
), f"The play should contain 2 post tasks but we found {len(total_post_tasks)} post task(s)"
|
2022-01-06 22:49:52 +00:00
|
|
|
|
|
|
|
# Check pre tasks
|
2022-02-07 22:41:13 +00:00
|
|
|
assert isinstance(
|
2022-06-17 22:11:16 +00:00
|
|
|
pre_tasks[0], RoleNode
|
2022-02-07 22:41:13 +00:00
|
|
|
), "The first edge should have a RoleNode as destination"
|
2022-06-17 22:11:16 +00:00
|
|
|
pre_task_block = pre_tasks[1]
|
2022-02-07 22:41:13 +00:00
|
|
|
assert isinstance(
|
|
|
|
pre_task_block, BlockNode
|
|
|
|
), "The second edge should have a BlockNode as destination"
|
2022-01-29 13:47:09 +00:00
|
|
|
assert pre_task_block.path == os.path.join(FIXTURES_PATH, "with_block.yml")
|
|
|
|
assert pre_task_block.line == 7
|
2021-10-03 14:04:08 +00:00
|
|
|
|
|
|
|
# Check tasks
|
2022-06-17 22:11:16 +00:00
|
|
|
task_1 = tasks[0]
|
2021-10-03 14:04:08 +00:00
|
|
|
assert isinstance(task_1, TaskNode)
|
|
|
|
assert task_1.name == "Install tree"
|
|
|
|
|
|
|
|
# Check the second task: the first block
|
2022-06-17 22:11:16 +00:00
|
|
|
first_block = tasks[1]
|
2021-10-03 14:04:08 +00:00
|
|
|
assert isinstance(first_block, BlockNode)
|
|
|
|
assert first_block.name == "Install Apache"
|
|
|
|
assert len(first_block.tasks) == 4
|
2023-05-10 16:45:57 +00:00
|
|
|
assert first_block.index == 4
|
|
|
|
for task_counter, task in enumerate(first_block.tasks):
|
|
|
|
assert (
|
|
|
|
task.index == task_counter + 1
|
|
|
|
), "The index of the task in the block should start at 1"
|
2022-01-01 08:47:25 +00:00
|
|
|
|
2023-05-13 16:21:39 +00:00
|
|
|
assert first_block.tasks[0].name == "Install some packages"
|
|
|
|
assert first_block.tasks[0].has_loop(), "The task has a 'with_items'"
|
|
|
|
|
2021-10-03 14:04:08 +00:00
|
|
|
# Check the second block (nested block)
|
2022-06-17 22:11:16 +00:00
|
|
|
nested_block = first_block.tasks[2]
|
2022-01-29 13:47:09 +00:00
|
|
|
assert isinstance(nested_block, BlockNode)
|
2021-10-03 14:04:08 +00:00
|
|
|
assert len(nested_block.tasks) == 2
|
2022-06-17 22:11:16 +00:00
|
|
|
assert nested_block.tasks[0].name == "get_url"
|
|
|
|
assert nested_block.tasks[1].name == "command"
|
2023-05-10 16:45:57 +00:00
|
|
|
assert nested_block.index == 3
|
|
|
|
|
|
|
|
for task_counter, task in enumerate(nested_block.tasks):
|
|
|
|
assert (
|
|
|
|
task.index == task_counter + 1
|
|
|
|
), "The index of the task in the block should start at 1"
|
2021-10-03 14:04:08 +00:00
|
|
|
|
|
|
|
# Check the post task
|
2022-06-17 22:11:16 +00:00
|
|
|
assert post_tasks[0].name == "Debug"
|
2023-05-10 16:45:57 +00:00
|
|
|
assert post_tasks[0].index == 6
|
2022-07-02 09:40:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("grapher_cli", [["multi-plays.yml"]], indirect=True)
|
2022-08-20 12:35:27 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
[
|
|
|
|
"group_roles_by_name",
|
|
|
|
"roles_number",
|
2023-05-10 16:45:57 +00:00
|
|
|
"nb_fake_role",
|
|
|
|
"nb_display_some_facts",
|
|
|
|
"nb_nested_include_role",
|
2022-08-20 12:35:27 +00:00
|
|
|
],
|
2023-05-10 16:45:57 +00:00
|
|
|
[(False, 8, 1, 1, 1), (True, 3, 3, 3, 1)],
|
2022-08-20 12:35:27 +00:00
|
|
|
ids=["no_group", "group"],
|
|
|
|
)
|
2023-05-10 16:45:57 +00:00
|
|
|
def test_roles_usage_multi_plays(
|
2022-08-20 12:35:27 +00:00
|
|
|
grapher_cli: PlaybookGrapherCLI,
|
2023-05-10 16:45:57 +00:00
|
|
|
roles_number: int,
|
2022-08-20 12:35:27 +00:00
|
|
|
group_roles_by_name: bool,
|
2023-05-10 16:45:57 +00:00
|
|
|
nb_fake_role: int,
|
|
|
|
nb_display_some_facts: int,
|
|
|
|
nb_nested_include_role: int,
|
2022-08-20 12:35:27 +00:00
|
|
|
):
|
2022-07-02 09:40:00 +00:00
|
|
|
"""
|
2023-05-10 16:45:57 +00:00
|
|
|
Test the role_usages method for multiple plays referencing the same roles
|
2022-08-20 12:35:27 +00:00
|
|
|
:param grapher_cli:
|
|
|
|
:param roles_number: The number of uniq roles in the graph
|
|
|
|
:param group_roles_by_name: flag to enable grouping roles or not
|
2023-05-10 16:45:57 +00:00
|
|
|
:param nb_fake_role: number of usages for the role fake_role
|
|
|
|
:param nb_display_some_facts: number of usages for the role display_some_facts
|
|
|
|
:param nb_nested_include_role: number of usages for the role nested_include_role
|
2022-07-02 09:40:00 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
parser = PlaybookParser(
|
2022-08-20 12:35:27 +00:00
|
|
|
grapher_cli.options.playbook_filenames[0],
|
|
|
|
include_role_tasks=True,
|
|
|
|
group_roles_by_name=group_roles_by_name,
|
2022-07-02 09:40:00 +00:00
|
|
|
)
|
|
|
|
playbook_node = parser.parse()
|
|
|
|
roles_usage = playbook_node.roles_usage()
|
|
|
|
|
2022-08-20 12:35:27 +00:00
|
|
|
expectation = {
|
2023-05-10 16:45:57 +00:00
|
|
|
"fake_role": nb_fake_role,
|
|
|
|
"display_some_facts": nb_display_some_facts,
|
|
|
|
"nested_include_role": nb_nested_include_role,
|
2022-08-20 12:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert roles_number == len(
|
|
|
|
roles_usage
|
|
|
|
), "The number of unique roles should be equal to the number of usages"
|
|
|
|
|
2022-07-02 09:40:00 +00:00
|
|
|
for role, plays in roles_usage.items():
|
|
|
|
assert all(
|
2022-08-17 20:20:55 +00:00
|
|
|
map(lambda node: node.id.startswith("play_"), plays)
|
2022-07-02 09:40:00 +00:00
|
|
|
), "All nodes IDs should be play"
|
2022-08-17 20:20:55 +00:00
|
|
|
|
|
|
|
nb_plays_for_the_role = len(plays)
|
2022-07-02 09:40:00 +00:00
|
|
|
|
2022-08-20 12:35:27 +00:00
|
|
|
assert (
|
|
|
|
expectation.get(role.name) == nb_plays_for_the_role
|
2023-05-10 16:45:57 +00:00
|
|
|
), f"The role '{role.name}' is used {nb_plays_for_the_role} times but we expect {expectation.get(role.name)}"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("grapher_cli", [["group-roles-by-name.yml"]], indirect=True)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
[
|
|
|
|
"group_roles_by_name",
|
|
|
|
],
|
|
|
|
[(False,), (True,)],
|
|
|
|
ids=["no_group", "group"],
|
|
|
|
)
|
|
|
|
def test_roles_usage_single_play(
|
|
|
|
grapher_cli: PlaybookGrapherCLI, group_roles_by_name: bool
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Test the role_usages method for a single play using the same roles multiple times.
|
|
|
|
The role usage should always be one regardless of the number of usages
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
parser = PlaybookParser(
|
|
|
|
grapher_cli.options.playbook_filenames[0],
|
|
|
|
include_role_tasks=True,
|
|
|
|
group_roles_by_name=group_roles_by_name,
|
|
|
|
)
|
|
|
|
playbook_node = parser.parse()
|
|
|
|
roles_usage = playbook_node.roles_usage()
|
|
|
|
for role, plays in roles_usage.items():
|
|
|
|
assert len(plays) == 1, "The number of plays should be equal to 1"
|
2022-10-10 22:32:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("grapher_cli", [["roles_dependencies.yml"]], indirect=True)
|
|
|
|
def test_roles_dependencies(grapher_cli: PlaybookGrapherCLI):
|
|
|
|
"""
|
|
|
|
Test if the role dependencies in meta/main.yml are included in the graph
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
parser = PlaybookParser(
|
|
|
|
grapher_cli.options.playbook_filenames[0], include_role_tasks=True
|
|
|
|
)
|
|
|
|
playbook_node = parser.parse()
|
|
|
|
roles = playbook_node.plays[0].roles
|
|
|
|
assert len(roles) == 1, "Only one explicit role is called inside the playbook"
|
|
|
|
role_with_dependencies = roles[0]
|
|
|
|
tasks = role_with_dependencies.tasks
|
|
|
|
|
|
|
|
expected_tasks = 5
|
|
|
|
dependant_role_name = "fake_role"
|
|
|
|
assert (
|
|
|
|
len(tasks) == expected_tasks
|
|
|
|
), f"There should be {expected_tasks} tasks in the graph"
|
|
|
|
# The first 3 tasks are coming from the dependency
|
|
|
|
for task_from_dependency in tasks[:3]:
|
|
|
|
assert (
|
|
|
|
dependant_role_name in task_from_dependency.name
|
|
|
|
), f"The task name should include the dependant role name '{dependant_role_name}'"
|