def run_grapher(playbook_file, output_filename=None, additional_args=None):
    """
    Utility function to run the grapher
    :param output_filename:
    :type output_filename: str
    :param additional_args:
    :type additional_args: list
    :param playbook_file:
    :type playbook_file: str
    :return:
    """
    additional_args = additional_args or []
    playbook_path = os.path.join(FIXTURES_DIR, playbook_file)
    args = [__prog__]

    if output_filename:  # the default filename is the playbook file name minus .yml
        # put the generated svg in a dedicated folder
        dir_path = os.path.dirname(os.path.realpath(__file__))  # current file directory
        args.extend(['-o', os.path.join(dir_path, "generated_svg", output_filename)])

    args.extend(additional_args)

    args.append(playbook_path)

    cli = get_cli_class()(args)

    cli.parse()

    return cli.run(), playbook_path
Exemple #2
0
def test_cli_verbosity_options(verbosity, verbosity_number):
    """
    Test verbosity options
    """
    args = [__prog__, verbosity, "playbook1.yml"]

    cli = get_cli_class()(args)
    cli.parse()

    assert cli.options.verbosity == verbosity_number
Exemple #3
0
def test_cli_no_playbook():
    """
    Test with no playbook provided
    :return:
    """
    args = [__prog__]

    cli = get_cli_class()(args)

    with pytest.raises((AnsibleOptionsError, SystemExit)) as exception_info:
        cli.parse()
Exemple #4
0
def test_cli_multiple_playbooks():
    """
    Test with multiple playbooks provided
    :return:
    """
    args = [__prog__, "playbook1.yml", "playbook2.yml"]

    cli = get_cli_class()(args)
    cli.parse()

    assert cli.options.playbook_filenames == ["playbook1.yml", "playbook2.yml"]
Exemple #5
0
def test_cli_version(capfd):
    """
    Test version printing
    :return:
    """
    cli = get_cli_class()([__prog__, "--version"])
    with pytest.raises(SystemExit) as exception_info:
        cli.parse()

    out, err = capfd.readouterr()
    assert out == f"{__prog__} {__version__} (with ansible {ansible_version})\n"
def test_cli_multiple_playbooks():
    """
    Test with multiple playbooks provided
    :return:
    """
    args = [__prog__, 'playbook1.yml', 'playbook2.yml']

    cli = get_cli_class()(args)

    with pytest.raises((AnsibleOptionsError, SystemExit)) as exception_info:
        cli.parse()
Exemple #7
0
def test_cli_version(capfd):
    """
    Test version printing
    :return:
    """
    cli = get_cli_class()([__prog__, '--version'])
    with pytest.raises(SystemExit) as exception_info:
        cli.parse()

    out, err = capfd.readouterr()
    assert out == "%s %s (with ansible %s)\n" % (__prog__, __version__, ansible_version)
Exemple #8
0
def test_cli_multiple_playbooks():
    """
    Test with multiple playbooks provided
    :return:
    """
    args = [__prog__, 'playbook1.yml', 'playbook2.yml']

    cli = get_cli_class()(args)

    with pytest.raises(AnsibleOptionsError) as exception_info:
        cli.parse()

    assert "You must specify only one playbook file to graph" in exception_info.value.message
Exemple #9
0
def test_cli_no_playbook():
    """
    Test with no playbook provided
    :return:
    """
    args = [__prog__]

    cli = get_cli_class()(args)

    with pytest.raises(AnsibleOptionsError) as exception_info:
        cli.parse()

    assert "You must specify a playbook file to graph." in exception_info.value.message
Exemple #10
0
def test_cli_output_filename(output_filename_option, expected):
    """
    Test for the output filename option: -o, --ouput-file-name
    :param output_filename_option:
    :param expected:
    :return:
    """
    args = [__prog__] + output_filename_option + ['playbook.yml']

    cli = get_cli_class()(args)

    cli.parse()

    assert cli.options.output_filename == expected
Exemple #11
0
def test_cli_save_dot_file(save_dot_file_option, expected):
    """
    Test for the save dot file option: -s, --save-dot-file
    :param save_dot_file_option:
    :param expected:
    :return:
    """
    args = [__prog__] + save_dot_file_option + ['playbook.yml']

    cli = get_cli_class()(args)

    cli.parse()

    assert cli.options.save_dot_file == expected
Exemple #12
0
def test_cli_open_protocol_custom_formats_not_provided():
    """
    The custom formats must be provided when the protocol handler is set to custom
    :return:
    """
    args = [__prog__, "--open-protocol-handler", "custom", "playbook1.yml"]

    cli = get_cli_class()(args)
    with pytest.raises(AnsibleOptionsError) as exception_info:
        cli.parse()

    assert (
        "you must provide the formats to use with --open-protocol-custom-formats"
        in exception_info.value.message)
Exemple #13
0
def test_cli_include_role_tasks(include_role_tasks_option, expected):
    """
    Test for the include role tasks option: --include-role-tasks
    :param include_role_tasks_option:
    :param expected:
    :return:
    """

    args = [__prog__] + include_role_tasks_option + ['playboook.yml']

    cli = get_cli_class()(args)

    cli.parse()

    assert cli.options.include_role_tasks == expected
Exemple #14
0
def test_skip_tags(skip_tags_option, expected):
    """

    :param tags_option:
    :param expected:
    :return:
    """
    args = [__prog__] + skip_tags_option + ['playbook.yml']

    cli = get_cli_class()(args)

    cli.parse()

    # Ansible uses a set to construct the tags list. It may happen that the order of tags changes between two runs. As
    # the order of tags doesn't matter, I sorted them to avoid the test to fail
    assert sorted(cli.options.skip_tags) == sorted(expected)
Exemple #15
0
def test_cli_help(help_option, capfd):
    """
    Test for the help option : -h, --help
    :param help_option:
    :param capfd:
    :return:
    """
    args = [__prog__, help_option]

    cli = get_cli_class()(args)

    with pytest.raises(SystemExit) as exception_info:
        cli.parse()

    out, err = capfd.readouterr()

    assert "Make graph from your Playbook." in out
def run_grapher(
    playbook_files: List[str],
    output_filename: str = None,
    additional_args: List[str] = None,
) -> Tuple[str, List[str]]:
    """
    Utility function to run the grapher
    :param output_filename:
    :param additional_args:
    :param playbook_files:
    :return: SVG path and playbook absolute path
    """
    additional_args = additional_args or []
    # Explicitly add verbosity to the tests
    additional_args.insert(0, "-vv")

    if os.environ.get("TEST_VIEW_GENERATED_FILE") == "1":
        additional_args.insert(0, "--view")

    if "--open-protocol-handler" not in additional_args:
        additional_args.insert(0, "--open-protocol-handler")
        additional_args.insert(1, "vscode")

    playbook_paths = [
        os.path.join(FIXTURES_DIR, p_file) for p_file in playbook_files
    ]
    args = [__prog__]

    if output_filename:  # the default filename is the playbook file name minus .yml
        # put the generated svg in a dedicated folder
        output_filename = output_filename.replace("[", "-").replace("]", "")
        args.extend(
            ["-o",
             os.path.join(DIR_PATH, "generated_svg", output_filename)])

    args.extend(additional_args)

    args.extend(playbook_paths)

    cli = get_cli_class()(args)

    return cli.run(), playbook_paths
Exemple #17
0
def test_cli_open_protocol_custom_formats_invalid_inputs(
        formats, expected_message, capsys):
    """
    The custom formats must be a valid json data
    :return:
    """
    args = [
        __prog__,
        "--open-protocol-handler",
        "custom",
        "--open-protocol-custom-formats",
        formats,
        "playbook1.yml",
    ]

    cli = get_cli_class()(args)
    with pytest.raises(SystemExit) as exception_info:
        cli.parse()

    error_msg = capsys.readouterr().err
    assert expected_message in error_msg
Exemple #18
0
def test_cli_open_protocol_custom_formats():
    """
    The provided format should be converted to a dict
    :return:
    """
    formats_str = '{"file": "{path}", "folder": "{path}"}'
    args = [
        __prog__,
        "--open-protocol-handler",
        "custom",
        "--open-protocol-custom-formats",
        formats_str,
        "playbook1.yml",
    ]

    cli = get_cli_class()(args)
    cli.parse()
    assert cli.options.open_protocol_custom_formats == {
        "file": "{path}",
        "folder": "{path}",
    }, "The formats should be converted to json"