2022-10-17 13:46:07 +00:00
|
|
|
import json
|
2017-11-21 11:17:33 +00:00
|
|
|
import os
|
2021-02-14 01:32:03 +00:00
|
|
|
from _elementtree import Element
|
2021-02-14 01:41:33 +00:00
|
|
|
from typing import Dict, List, Tuple
|
2018-12-30 20:58:06 +00:00
|
|
|
|
2019-03-08 13:47:28 +00:00
|
|
|
import pytest
|
2019-02-18 01:52:54 +00:00
|
|
|
from pyquery import PyQuery
|
2017-11-21 11:17:33 +00:00
|
|
|
|
2019-02-18 01:52:54 +00:00
|
|
|
from ansibleplaybookgrapher import __prog__
|
2022-10-09 12:12:57 +00:00
|
|
|
from ansibleplaybookgrapher.cli import PlaybookGrapherCLI
|
2017-11-21 11:17:33 +00:00
|
|
|
from tests import FIXTURES_DIR
|
|
|
|
|
2022-01-29 13:47:09 +00:00
|
|
|
# This file directory abspath
|
|
|
|
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
2017-11-21 11:17:33 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
def run_grapher(
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_files: List[str],
|
2023-05-13 12:41:07 +00:00
|
|
|
output_filename: str,
|
2022-08-14 20:59:46 +00:00
|
|
|
additional_args: List[str] = None,
|
|
|
|
) -> Tuple[str, List[str]]:
|
2019-03-08 14:49:13 +00:00
|
|
|
"""
|
|
|
|
Utility function to run the grapher
|
|
|
|
:param output_filename:
|
|
|
|
:param additional_args:
|
2022-08-14 20:59:46 +00:00
|
|
|
:param playbook_files:
|
2023-05-13 12:41:07 +00:00
|
|
|
:return: SVG path and playbooks absolute paths
|
2019-03-08 14:49:13 +00:00
|
|
|
"""
|
|
|
|
additional_args = additional_args or []
|
2021-08-29 18:10:34 +00:00
|
|
|
# Explicitly add verbosity to the tests
|
|
|
|
additional_args.insert(0, "-vv")
|
2021-09-24 22:09:20 +00:00
|
|
|
|
2021-10-03 17:59:30 +00:00
|
|
|
if os.environ.get("TEST_VIEW_GENERATED_FILE") == "1":
|
2022-01-29 13:47:09 +00:00
|
|
|
additional_args.insert(0, "--view")
|
|
|
|
|
2022-10-17 13:46:07 +00:00
|
|
|
if os.environ.get("GITHUB_ACTIONS") == "true":
|
|
|
|
# Setting a custom protocol handler for browsing on github
|
|
|
|
additional_args.insert(0, "--open-protocol-handler")
|
|
|
|
additional_args.insert(1, "custom")
|
|
|
|
|
|
|
|
repo = os.environ["GITHUB_REPOSITORY"]
|
|
|
|
commit_sha = os.environ["COMMIT_SHA"]
|
|
|
|
formats = {
|
|
|
|
"file": f"https://github.com/{repo}/blob/{commit_sha}" + "/{path}#L{line}",
|
|
|
|
"folder": f"https://github.com/{repo}/tree/{commit_sha}" + "/{path}",
|
|
|
|
"remove_from_path": os.environ["GITHUB_WORKSPACE"],
|
|
|
|
}
|
|
|
|
|
|
|
|
additional_args.insert(2, "--open-protocol-custom-formats")
|
|
|
|
additional_args.insert(3, json.dumps(formats))
|
|
|
|
|
2022-01-29 13:47:09 +00:00
|
|
|
if "--open-protocol-handler" not in additional_args:
|
|
|
|
additional_args.insert(0, "--open-protocol-handler")
|
|
|
|
additional_args.insert(1, "vscode")
|
2021-09-24 22:09:20 +00:00
|
|
|
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths = [os.path.join(FIXTURES_DIR, p_file) for p_file in playbook_files]
|
2019-03-08 14:49:13 +00:00
|
|
|
args = [__prog__]
|
|
|
|
|
2023-05-13 12:41:07 +00:00
|
|
|
# Clean the name a little bit
|
|
|
|
output_filename = output_filename.replace("[", "-").replace("]", "")
|
|
|
|
# put the generated file in a dedicated folder
|
|
|
|
args.extend(["-o", os.path.join(DIR_PATH, "generated-svgs", output_filename)])
|
2019-03-08 14:49:13 +00:00
|
|
|
|
|
|
|
args.extend(additional_args)
|
|
|
|
|
2022-08-14 20:59:46 +00:00
|
|
|
args.extend(playbook_paths)
|
2019-03-08 14:49:13 +00:00
|
|
|
|
2022-10-09 12:12:57 +00:00
|
|
|
cli = PlaybookGrapherCLI(args)
|
2019-02-18 23:28:51 +00:00
|
|
|
|
2022-08-14 20:59:46 +00:00
|
|
|
return cli.run(), playbook_paths
|
2019-02-18 23:28:51 +00:00
|
|
|
|
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
def _common_tests(
|
|
|
|
svg_path: str,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths: List[str],
|
|
|
|
playbooks_number: int = 1,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number: int = 0,
|
|
|
|
tasks_number: int = 0,
|
|
|
|
post_tasks_number: int = 0,
|
|
|
|
roles_number: int = 0,
|
|
|
|
pre_tasks_number: int = 0,
|
|
|
|
blocks_number: int = 0,
|
|
|
|
) -> Dict[str, List[Element]]:
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
|
|
|
Perform some common tests on the generated svg file:
|
|
|
|
- Existence of svg file
|
|
|
|
- Check number of plays, tasks, pre_tasks, role_tasks, post_tasks
|
|
|
|
- Root node text that must be the playbook path
|
|
|
|
:param plays_number: Number of plays in the playbook
|
2021-09-24 21:54:41 +00:00
|
|
|
:param pre_tasks_number: Number of pre tasks in the playbook
|
|
|
|
:param roles_number: Number of roles in the playbook
|
2019-02-18 01:52:54 +00:00
|
|
|
:param tasks_number: Number of tasks in the playbook
|
2020-06-18 12:44:29 +00:00
|
|
|
:param post_tasks_number: Number of post tasks in the playbook
|
2021-02-14 01:41:33 +00:00
|
|
|
:return: A dictionary with the different tasks, roles, pre_tasks as keys and a list of Elements (nodes) as values
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
2018-12-30 20:58:06 +00:00
|
|
|
|
2020-06-18 12:44:29 +00:00
|
|
|
# test if the file exist. It will exist only if we write in it.
|
2019-02-18 01:52:54 +00:00
|
|
|
assert os.path.isfile(svg_path), "The svg file should exist"
|
2017-11-21 11:17:33 +00:00
|
|
|
|
2023-05-13 12:41:07 +00:00
|
|
|
pq = PyQuery(filename=svg_path)
|
|
|
|
pq.remove_namespaces()
|
|
|
|
|
2022-08-14 20:59:46 +00:00
|
|
|
playbooks = pq("g[id^='playbook_']")
|
2019-02-18 01:52:54 +00:00
|
|
|
plays = pq("g[id^='play_']")
|
|
|
|
tasks = pq("g[id^='task_']")
|
|
|
|
post_tasks = pq("g[id^='post_task_']")
|
|
|
|
pre_tasks = pq("g[id^='pre_task_']")
|
2021-09-26 16:18:09 +00:00
|
|
|
blocks = pq("g[id^='block_']")
|
2019-03-07 23:35:44 +00:00
|
|
|
roles = pq("g[id^='role_']")
|
2017-11-21 11:17:33 +00:00
|
|
|
|
2022-08-14 20:59:46 +00:00
|
|
|
playbooks_file_names = [e.text for e in playbooks.find("text")]
|
|
|
|
assert (
|
|
|
|
playbooks_file_names == playbook_paths
|
|
|
|
), "The playbook file names should be in the svg file"
|
|
|
|
|
|
|
|
assert (
|
|
|
|
len(playbooks) == playbooks_number
|
|
|
|
), f"The graph '{svg_path}' should contains {playbooks_number} play(s) but we found {len(playbooks)} play(s)"
|
|
|
|
|
2022-06-26 16:02:49 +00:00
|
|
|
assert (
|
|
|
|
len(plays) == plays_number
|
2022-08-14 20:59:46 +00:00
|
|
|
), f"The graph '{svg_path}' should contains {plays_number} play(s) but we found {len(plays)} play(s)"
|
2022-01-02 12:39:13 +00:00
|
|
|
|
2022-06-26 16:02:49 +00:00
|
|
|
assert (
|
|
|
|
len(pre_tasks) == pre_tasks_number
|
2022-08-14 20:59:46 +00:00
|
|
|
), f"The graph '{svg_path}' should contains {pre_tasks_number} pre tasks(s) but we found {len(pre_tasks)} pre tasks"
|
2022-01-02 12:39:13 +00:00
|
|
|
|
2022-06-26 16:02:49 +00:00
|
|
|
assert (
|
|
|
|
len(roles) == roles_number
|
2022-08-20 12:35:27 +00:00
|
|
|
), f"The graph '{svg_path}' should contains {roles_number} role(s) but we found {len(roles)} role(s)"
|
2022-01-02 12:39:13 +00:00
|
|
|
|
2022-06-26 16:02:49 +00:00
|
|
|
assert (
|
|
|
|
len(tasks) == tasks_number
|
2022-08-14 20:59:46 +00:00
|
|
|
), f"The graph '{svg_path}' should contains {tasks_number} tasks(s) but we found {len(tasks)} tasks"
|
2022-01-02 12:39:13 +00:00
|
|
|
|
2022-06-26 16:02:49 +00:00
|
|
|
assert (
|
|
|
|
len(post_tasks) == post_tasks_number
|
2022-08-14 20:59:46 +00:00
|
|
|
), f"The graph '{svg_path}' should contains {post_tasks_number} post tasks(s) but we found {len(post_tasks)} post tasks"
|
2022-01-02 12:39:13 +00:00
|
|
|
|
2022-06-26 16:02:49 +00:00
|
|
|
assert (
|
|
|
|
len(blocks) == blocks_number
|
2022-08-14 20:59:46 +00:00
|
|
|
), f"The graph '{svg_path}' should contains {blocks_number} blocks(s) but we found {len(blocks)} blocks "
|
2017-11-21 11:17:33 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
return {
|
|
|
|
"tasks": tasks,
|
|
|
|
"plays": plays,
|
|
|
|
"post_tasks": post_tasks,
|
|
|
|
"pre_tasks": pre_tasks,
|
|
|
|
"roles": roles,
|
|
|
|
}
|
2017-11-21 11:17:33 +00:00
|
|
|
|
|
|
|
|
2019-03-27 21:28:25 +00:00
|
|
|
def test_simple_playbook(request):
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
|
|
|
Test simple_playbook.yml
|
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["simple_playbook.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=["-i", os.path.join(FIXTURES_DIR, "inventory")],
|
|
|
|
)
|
2019-02-18 01:52:54 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
post_tasks_number=2,
|
|
|
|
)
|
2019-02-18 01:52:54 +00:00
|
|
|
|
|
|
|
|
2019-03-27 21:28:25 +00:00
|
|
|
def test_example(request):
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
|
|
|
Test example.yml
|
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["example.yml"], output_filename=request.node.name
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-02-18 01:52:54 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
tasks_number=4,
|
|
|
|
post_tasks_number=2,
|
|
|
|
pre_tasks_number=2,
|
|
|
|
)
|
2019-02-18 01:52:54 +00:00
|
|
|
|
|
|
|
|
2019-03-27 21:28:25 +00:00
|
|
|
def test_include_tasks(request):
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
2019-03-21 23:35:58 +00:00
|
|
|
Test include_tasks.yml, an example with some included tasks
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["include_tasks.yml"], output_filename=request.node.name
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-02-18 01:52:54 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
_common_tests(
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path=svg_path, playbook_paths=playbook_paths, plays_number=1, tasks_number=7
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-02-18 01:52:54 +00:00
|
|
|
|
|
|
|
|
2019-03-27 21:28:25 +00:00
|
|
|
def test_import_tasks(request):
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
2022-06-17 22:11:16 +00:00
|
|
|
Test import_tasks.yml, an example with some imported tasks
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["import_tasks.yml"], output_filename=request.node.name
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-02-18 01:52:54 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
_common_tests(
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path=svg_path, playbook_paths=playbook_paths, plays_number=1, tasks_number=5
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-02-18 01:52:54 +00:00
|
|
|
|
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["include_role_tasks_option", "expected_tasks_number"],
|
|
|
|
[("--", 2), ("--include-role-tasks", 8)],
|
|
|
|
ids=["no_include_role_tasks_option", "include_role_tasks_option"],
|
|
|
|
)
|
2019-03-21 23:35:58 +00:00
|
|
|
def test_with_roles(request, include_role_tasks_option, expected_tasks_number):
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
2019-03-21 23:35:58 +00:00
|
|
|
Test with_roles.yml, an example with roles
|
2019-02-18 01:52:54 +00:00
|
|
|
"""
|
2020-06-18 14:58:57 +00:00
|
|
|
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["with_roles.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=[include_role_tasks_option],
|
|
|
|
)
|
|
|
|
|
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
tasks_number=expected_tasks_number,
|
|
|
|
post_tasks_number=2,
|
|
|
|
roles_number=2,
|
2022-08-14 20:59:46 +00:00
|
|
|
pre_tasks_number=2,
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["include_role_tasks_option", "expected_tasks_number"],
|
2022-08-20 12:35:27 +00:00
|
|
|
[("--", 2), ("--include-role-tasks", 14)],
|
2022-02-07 22:41:13 +00:00
|
|
|
ids=["no_include_role_tasks_option", "include_role_tasks_option"],
|
|
|
|
)
|
2019-03-21 23:35:58 +00:00
|
|
|
def test_include_role(request, include_role_tasks_option, expected_tasks_number):
|
2019-02-26 23:16:35 +00:00
|
|
|
"""
|
2019-03-21 23:35:58 +00:00
|
|
|
Test include_role.yml, an example with include_role
|
2019-02-26 23:16:35 +00:00
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["include_role.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=[include_role_tasks_option],
|
|
|
|
)
|
2019-02-26 23:16:35 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
2022-08-17 20:20:55 +00:00
|
|
|
blocks_number=1,
|
2022-02-07 22:41:13 +00:00
|
|
|
tasks_number=expected_tasks_number,
|
2022-08-20 12:35:27 +00:00
|
|
|
roles_number=6,
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-02-26 23:16:35 +00:00
|
|
|
|
|
|
|
|
2019-03-21 23:35:58 +00:00
|
|
|
def test_with_block(request):
|
2019-02-18 18:40:54 +00:00
|
|
|
"""
|
2022-01-29 13:47:09 +00:00
|
|
|
Test with_block.yml, an example with roles
|
2019-02-18 18:40:54 +00:00
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["with_block.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
2022-02-15 18:47:32 +00:00
|
|
|
additional_args=["--include-role-tasks", "--save-dot-file"],
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-02-24 15:21:02 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
tasks_number=7,
|
|
|
|
post_tasks_number=2,
|
2022-08-14 20:59:46 +00:00
|
|
|
roles_number=1,
|
2022-02-07 22:41:13 +00:00
|
|
|
pre_tasks_number=4,
|
|
|
|
blocks_number=4,
|
|
|
|
)
|
2019-02-24 15:21:02 +00:00
|
|
|
|
|
|
|
|
2019-03-21 23:35:58 +00:00
|
|
|
def test_nested_include_tasks(request):
|
2019-02-24 15:21:02 +00:00
|
|
|
"""
|
2022-01-06 22:49:52 +00:00
|
|
|
Test nested_include.yml, an example with an include_tasks that include another tasks
|
2019-02-24 15:21:02 +00:00
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["nested_include_tasks.yml"], output_filename=request.node.name
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-02-18 18:40:54 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
_common_tests(
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path=svg_path, playbook_paths=playbook_paths, plays_number=1, tasks_number=3
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2019-03-12 19:31:41 +00:00
|
|
|
|
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["include_role_tasks_option", "expected_tasks_number"],
|
|
|
|
[("--", 1), ("--include-role-tasks", 7)],
|
|
|
|
ids=["no_include_role_tasks_option", "include_role_tasks_option"],
|
|
|
|
)
|
2019-03-21 23:35:58 +00:00
|
|
|
def test_import_role(request, include_role_tasks_option, expected_tasks_number):
|
2019-03-12 19:31:41 +00:00
|
|
|
"""
|
2019-03-21 23:35:58 +00:00
|
|
|
Test import_role.yml, an example with import role.
|
2020-06-18 12:44:29 +00:00
|
|
|
Import role is special because the tasks imported from role are treated as "normal tasks" when the playbook is parsed.
|
2019-03-12 19:31:41 +00:00
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["import_role.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=[include_role_tasks_option],
|
|
|
|
)
|
2019-03-12 19:31:41 +00:00
|
|
|
|
2022-02-07 22:41:13 +00:00
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
tasks_number=expected_tasks_number,
|
|
|
|
roles_number=1,
|
|
|
|
)
|
2019-03-25 22:36:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_import_playbook(request):
|
|
|
|
"""
|
|
|
|
Test import_playbook
|
|
|
|
"""
|
2020-06-18 14:58:57 +00:00
|
|
|
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["import_playbook.yml"], output_filename=request.node.name
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
tasks_number=4,
|
|
|
|
post_tasks_number=2,
|
2022-08-14 20:59:46 +00:00
|
|
|
pre_tasks_number=2,
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["include_role_tasks_option", "expected_tasks_number"],
|
|
|
|
[("--", 4), ("--include-role-tasks", 7)],
|
|
|
|
ids=["no_include_role_tasks_option", "include_role_tasks_option"],
|
|
|
|
)
|
|
|
|
def test_nested_import_playbook(
|
|
|
|
request, include_role_tasks_option, expected_tasks_number
|
|
|
|
):
|
2019-03-25 23:23:34 +00:00
|
|
|
"""
|
2019-03-30 00:43:41 +00:00
|
|
|
Test nested import playbook with an import_role and include_tasks
|
2019-03-25 23:23:34 +00:00
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["nested_import_playbook.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=[include_role_tasks_option],
|
|
|
|
)
|
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=2,
|
|
|
|
tasks_number=expected_tasks_number,
|
|
|
|
)
|
2020-05-16 20:54:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_relative_var_files(request):
|
|
|
|
"""
|
|
|
|
Test a playbook with a relative var file
|
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["relative_var_files.yml"], output_filename=request.node.name
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
|
|
|
res = _common_tests(
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path=svg_path, playbook_paths=playbook_paths, plays_number=1, tasks_number=2
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2020-05-16 20:54:50 +00:00
|
|
|
|
|
|
|
# check if the plays title contains the interpolated variables
|
2022-02-07 22:41:13 +00:00
|
|
|
assert (
|
|
|
|
"Cristiano Ronaldo" in res["tasks"][0].find("g/a/text").text
|
|
|
|
), "The title should contain player name"
|
|
|
|
assert (
|
|
|
|
"Lionel Messi" in res["tasks"][1].find("g/a/text").text
|
|
|
|
), "The title should contain player name"
|
2020-06-18 12:44:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_tags(request):
|
|
|
|
"""
|
|
|
|
Test a playbook by only graphing a specific tasks based on the given tags
|
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["tags.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=["-t", "pre_task_tag_1"],
|
|
|
|
)
|
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
pre_tasks_number=1,
|
|
|
|
)
|
2020-06-18 12:44:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_skip_tags(request):
|
|
|
|
"""
|
|
|
|
Test a playbook by only graphing a specific tasks based on the given tags
|
|
|
|
"""
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["tags.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=["--skip-tags", "pre_task_tag_1", "--include-role-tasks"],
|
|
|
|
)
|
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
tasks_number=3,
|
2022-08-14 20:59:46 +00:00
|
|
|
roles_number=1,
|
|
|
|
pre_tasks_number=1,
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2022-01-29 13:47:09 +00:00
|
|
|
|
|
|
|
|
2022-07-02 09:40:00 +00:00
|
|
|
def test_multi_plays(request):
|
|
|
|
"""
|
|
|
|
Test with multiple plays, include_role and roles
|
|
|
|
"""
|
|
|
|
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["multi-plays.yml"],
|
2022-07-02 09:40:00 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=["--include-role-tasks"],
|
|
|
|
)
|
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-07-02 09:40:00 +00:00
|
|
|
plays_number=3,
|
2022-08-20 12:35:27 +00:00
|
|
|
tasks_number=25,
|
2022-07-02 09:40:00 +00:00
|
|
|
post_tasks_number=2,
|
2022-08-20 12:35:27 +00:00
|
|
|
roles_number=8,
|
2022-08-14 20:59:46 +00:00
|
|
|
pre_tasks_number=2,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_multiple_playbooks(request):
|
|
|
|
"""
|
|
|
|
Test with multiple playbooks
|
|
|
|
"""
|
|
|
|
|
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["multi-plays.yml", "relative_var_files.yml", "with_roles.yml"],
|
|
|
|
output_filename=request.node.name,
|
2022-08-15 00:37:03 +00:00
|
|
|
additional_args=["--include-role-tasks", "--save-dot-file"],
|
2022-08-14 20:59:46 +00:00
|
|
|
)
|
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
|
|
|
playbook_paths=playbook_paths,
|
|
|
|
playbooks_number=3,
|
2022-08-15 00:37:03 +00:00
|
|
|
plays_number=5,
|
|
|
|
pre_tasks_number=4,
|
2022-08-20 12:35:27 +00:00
|
|
|
roles_number=10,
|
|
|
|
tasks_number=35,
|
2022-08-15 00:37:03 +00:00
|
|
|
post_tasks_number=4,
|
2022-07-02 09:40:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-29 13:47:09 +00:00
|
|
|
def test_with_roles_with_custom_protocol_handlers(request):
|
|
|
|
"""
|
|
|
|
Test with_roles.yml with a custom protocol handlers
|
|
|
|
"""
|
|
|
|
formats_str = '{"file": "vscode://file/{path}:{line}", "folder": "{path}"}'
|
2022-08-14 20:59:46 +00:00
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["with_roles.yml"],
|
2022-02-07 22:41:13 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=[
|
|
|
|
"--open-protocol-handler",
|
|
|
|
"custom",
|
|
|
|
"--open-protocol-custom-formats",
|
|
|
|
formats_str,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
res = _common_tests(
|
|
|
|
svg_path=svg_path,
|
2022-08-14 20:59:46 +00:00
|
|
|
playbook_paths=playbook_paths,
|
2022-02-07 22:41:13 +00:00
|
|
|
plays_number=1,
|
|
|
|
tasks_number=2,
|
|
|
|
post_tasks_number=2,
|
|
|
|
roles_number=2,
|
2022-08-14 20:59:46 +00:00
|
|
|
pre_tasks_number=2,
|
2022-02-07 22:41:13 +00:00
|
|
|
)
|
2022-01-29 13:47:09 +00:00
|
|
|
|
|
|
|
xlink_ref_selector = "{http://www.w3.org/1999/xlink}href"
|
|
|
|
for t in res["tasks"]:
|
2022-02-07 22:41:13 +00:00
|
|
|
assert (
|
|
|
|
t.find("g/a")
|
|
|
|
.get(xlink_ref_selector)
|
|
|
|
.startswith(f"vscode://file/{DIR_PATH}")
|
|
|
|
), "Tasks should be open with vscode"
|
2022-01-29 13:47:09 +00:00
|
|
|
|
|
|
|
for r in res["roles"]:
|
|
|
|
assert r.find("g/a").get(xlink_ref_selector).startswith(DIR_PATH)
|
2022-07-30 18:54:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_community_download_roles_and_collection(request):
|
|
|
|
"""
|
|
|
|
Test if the grapher is able to find some downloaded roles and collections when graphing the playbook
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
run_grapher(
|
2022-08-14 20:59:46 +00:00
|
|
|
["docker-mysql-galaxy.yml"],
|
2022-07-30 18:54:55 +00:00
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=["--include-role-tasks"],
|
|
|
|
)
|
2022-08-20 12:35:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["flag", "roles_number", "tasks_number", "post_tasks_number"],
|
|
|
|
[("--", 6, 9, 8), ("--group-roles-by-name", 3, 6, 2)],
|
|
|
|
ids=["no_group", "group"],
|
|
|
|
)
|
|
|
|
def test_group_roles_by_name(
|
|
|
|
request, flag, roles_number, tasks_number, post_tasks_number
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Test group roles by name
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
svg_path, playbook_paths = run_grapher(
|
|
|
|
["group-roles-by-name.yml"],
|
|
|
|
output_filename=request.node.name,
|
|
|
|
additional_args=["--include-role-tasks", flag],
|
|
|
|
)
|
|
|
|
_common_tests(
|
|
|
|
svg_path=svg_path,
|
|
|
|
playbook_paths=playbook_paths,
|
|
|
|
plays_number=1,
|
|
|
|
roles_number=roles_number,
|
|
|
|
tasks_number=tasks_number,
|
|
|
|
post_tasks_number=post_tasks_number,
|
|
|
|
blocks_number=1,
|
|
|
|
)
|