コード例 #1
0
def run_grapher(playbook_path, 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_path:
    :type playbook_path: str
    :return:
    :rtype:
    """
    additional_args = additional_args or []
    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 = PlaybookGrapherCLI(args)

    cli.parse()

    return cli.run()
コード例 #2
0
def test_cli_version(capfd):
    """
    Test version printing
    :return:
    """
    cli = PlaybookGrapherCLI([__prog__, '--version'])
    with pytest.raises(SystemExit) as exception_info:
        cli.parse()

    out, err = capfd.readouterr()
    assert out == "%s %s\n" % (__prog__, __version__)
コード例 #3
0
def test_cli_multiple_playbooks():
    """
    Test with multiple playbooks provided
    :return:
    """
    args = [__prog__, 'playbook1.yml', 'playbook2.yml']

    cli = PlaybookGrapherCLI(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
コード例 #4
0
def test_cli_no_playbook():
    """
    Test with no playbook provided
    :return:
    """
    args = [__prog__]

    cli = PlaybookGrapherCLI(args)

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

    assert "You must specify a playbook file to graph." in exception_info.value.message
コード例 #5
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 = PlaybookGrapherCLI(args)

    cli.parse()

    assert cli.options.output_filename == expected
コード例 #6
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 = PlaybookGrapherCLI(args)

    cli.parse()

    assert cli.options.save_dot_file == expected
コード例 #7
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 = PlaybookGrapherCLI(args)

    cli.parse()

    assert cli.options.include_role_tasks == expected
コード例 #8
0
def grapher_cli(request) -> PlaybookGrapherCLI:
    """
    Because Ansible is not designed to be used as a library, we need the CLI everywhere. The CLI is the main entrypoint
    of Ansible and it sets some global variables that are needed by some classes and methods.
    See this commit: https://github.com/ansible/ansible/commit/afdbb0d9d5bebb91f632f0d4a1364de5393ba17a
    As such, this fixture is just used to init this global context
    :return:
    """
    # The request param should be the path to the playbook
    args_params = request.param.copy()
    # The last item of the args should be the name of the playbook file in the fixtures.
    args_params[-1] = os.path.join(FIXTURES_DIR, args_params[-1])
    cli = PlaybookGrapherCLI([__prog__] + args_params)
    cli.parse()
    return cli
コード例 #9
0
def test_skip_tags(skip_tags_option, expected):
    """

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

    cli = PlaybookGrapherCLI(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)
コード例 #10
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 = PlaybookGrapherCLI(args)

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

    out, err = capfd.readouterr()

    assert "Make graph from your Playbook." in out
コード例 #11
0
def run_grapher(args):
    cli = PlaybookGrapherCLI(args)

    cli.parse()

    return cli.run()