예제 #1
0
def _common_tests(svg_path,
                  playbook_path,
                  plays_number=0,
                  tasks_number=0,
                  post_tasks_number=0,
                  pre_tasks_number=0):
    """
    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
    :type svg_path: str
    :type playbook_path: str
    :param plays_number: Number of plays in the playbook
    :type plays_number: int
    :param tasks_number: Number of tasks in the playbook
    :type tasks_number: int
    :param post_tasks_number Number of post tasks in the playbook
    :type post_tasks_number: int
    :return:
    :rtype: dict[str, PyQuery]
    """

    pq = PyQuery(filename=svg_path)
    pq.remove_namespaces()

    # test if the file exist. It will exist only if we write in it
    assert os.path.isfile(svg_path), "The svg file should exist"
    assert pq('#root_node text').text() == playbook_path

    plays = pq("g[id^='play_']")
    tasks = pq("g[id^='task_']")
    post_tasks = pq("g[id^='post_task_']")
    pre_tasks = pq("g[id^='pre_task_']")

    assert plays_number == len(
        plays), "This playbook should contains {} play(s)".format(plays_number)
    assert tasks_number == len(
        tasks), "This playbook should contains {} tasks(s)".format(
            tasks_number)
    assert post_tasks_number == len(
        post_tasks), "This playbook should contains {} post tasks(s)".format(
            post_tasks_number)
    assert pre_tasks_number == len(
        pre_tasks), "This playbook should contains {} pre tasks(s)".format(
            pre_tasks_number)

    return {
        'tasks': tasks,
        'plays': plays,
        'pq': pq,
        'post_tasks': post_tasks,
        'pre_tasks': pre_tasks
    }
예제 #2
0
def _common_tests(svg_path: str, playbook_path: str, plays_number: int = 0, tasks_number: int = 0,
                  post_tasks_number: int = 0, roles_number: int = 0,
                  pre_tasks_number: int = 0) -> Dict[str, List[Element]]:
    """
    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
    :param tasks_number: Number of tasks in the playbook
    :param post_tasks_number: Number of post tasks in the playbook
    :return: A dictionary with the different tasks, roles, pre_tasks as keys and a list of Elements (nodes) as values
    """

    pq = PyQuery(filename=svg_path)
    pq.remove_namespaces()

    # test if the file exist. It will exist only if we write in it.
    assert os.path.isfile(svg_path), "The svg file should exist"
    assert pq('#root_node text').text() == playbook_path

    plays = pq("g[id^='play_']")
    tasks = pq("g[id^='task_']")
    post_tasks = pq("g[id^='post_task_']")
    pre_tasks = pq("g[id^='pre_task_']")
    roles = pq("g[id^='role_']")

    assert plays_number == len(plays), "The playbook '{}' should contains {} play(s) but we found {} play(s)".format(
        playbook_path, plays_number, len(plays))
    assert tasks_number == len(tasks), "The playbook '{}' should contains {} tasks(s) we found {} tasks".format(
        playbook_path, tasks_number, len(tasks))
    assert post_tasks_number == len(post_tasks), "The '{}' playbook should contains {} post tasks(s) we found {} " \
                                                 "post tasks".format(playbook_path, post_tasks_number, len(post_tasks))
    assert pre_tasks_number == len(pre_tasks), "The playbook '{}' should contains {} pre tasks(s) but we found {} " \
                                               "pre tasks".format(playbook_path, pre_tasks_number, len(pre_tasks))

    assert roles_number == len(roles), "The playbook '{}' should contains {} role(s) but we found {} role(s)".format(
        playbook_path, roles_number, len(roles))

    return {'tasks': tasks, 'plays': plays, 'post_tasks': post_tasks, 'pre_tasks': pre_tasks, "roles": roles}
def _common_tests(
    svg_path: str,
    playbook_paths: List[str],
    playbooks_number: int = 1,
    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]]:
    """
    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
    :param pre_tasks_number: Number of pre tasks in the playbook
    :param roles_number: Number of roles in the playbook
    :param tasks_number: Number of tasks in the playbook
    :param post_tasks_number: Number of post tasks in the playbook
    :return: A dictionary with the different tasks, roles, pre_tasks as keys and a list of Elements (nodes) as values
    """

    pq = PyQuery(filename=svg_path)
    pq.remove_namespaces()

    # test if the file exist. It will exist only if we write in it.
    assert os.path.isfile(svg_path), "The svg file should exist"

    playbooks = pq("g[id^='playbook_']")
    plays = pq("g[id^='play_']")
    tasks = pq("g[id^='task_']")
    post_tasks = pq("g[id^='post_task_']")
    pre_tasks = pq("g[id^='pre_task_']")
    blocks = pq("g[id^='block_']")
    roles = pq("g[id^='role_']")

    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)"

    assert (
        len(plays) == plays_number
    ), f"The graph '{svg_path}' should contains {plays_number} play(s) but we found {len(plays)} play(s)"

    assert (
        len(pre_tasks) == pre_tasks_number
    ), f"The graph '{svg_path}' should contains {pre_tasks_number} pre tasks(s) but we found {len(pre_tasks)} pre tasks"

    assert (
        len(roles) == roles_number
    ), f"The graph '{svg_path}' should contains {roles_number} role(s) but we found {len(roles)} role(s)"

    assert (
        len(tasks) == tasks_number
    ), f"The graph '{svg_path}' should contains {tasks_number} tasks(s) but we found {len(tasks)} tasks"

    assert (
        len(post_tasks) == post_tasks_number
    ), f"The graph '{svg_path}' should contains {post_tasks_number} post tasks(s) but we found {len(post_tasks)} post tasks"

    assert (
        len(blocks) == blocks_number
    ), f"The graph '{svg_path}' should contains {blocks_number} blocks(s) but we found {len(blocks)} blocks "

    return {
        "tasks": tasks,
        "plays": plays,
        "post_tasks": post_tasks,
        "pre_tasks": pre_tasks,
        "roles": roles,
    }