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()
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__)
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
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
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
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
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
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
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)
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
def run_grapher(args): cli = PlaybookGrapherCLI(args) cli.parse() return cli.run()