Ejemplo n.º 1
0
def test_process_output(cli_script_name, config_file):
    in_stdout = '"The salt master could not be contacted. Is master running?"\n'
    in_stderr = ""
    cmdline = ["--out=json"]
    config = {"conf_file": config_file, "id": "the-id"}
    proc = SaltCli(script_name=cli_script_name, config=config)
    # Call proc.cmdline() so that proc.__json_output__ is properly set
    proc.cmdline()
    stdout, stderr, json_out = proc.process_output(in_stdout, in_stderr, cmdline=cmdline)
    assert stdout == json.loads(in_stdout)
    assert stderr == in_stderr
    assert json_out is None
Ejemplo n.º 2
0
def test_override_output(minion_id, config_dir, config_file, cli_script_name, flag):
    config = {"conf_file": config_file, "id": "the-id"}
    args = []
    if flag.endswith("="):
        flag_overrides_args = [flag + "nested"]
    else:
        flag_overrides_args = [flag, "nested"]

    args.extend(flag_overrides_args)
    args.append("test.ping")
    kwargs = {"minion_tgt": minion_id}
    expected = (
        [
            sys.executable,
            cli_script_name,
            "--config-dir={}".format(config_dir),
            "--log-level=critical",
            minion_id,
        ]
        + flag_overrides_args
        + ["test.ping"]
    )
    proc = SaltCli(script_name=cli_script_name, config=config)
    cmdline = proc.cmdline(*args, **kwargs)
    assert cmdline == expected
Ejemplo n.º 3
0
def test_override_config_dir(minion_id, config_dir, config_file, cli_script_name, flag):
    passed_config_dir = "{}.new".format(config_dir)
    if flag is None:
        flag_overrides_args = ["--config-dir={}".format(config_dir)]
    elif flag.endswith("="):
        flag_overrides_args = [flag + passed_config_dir]
    else:
        flag_overrides_args = [flag, passed_config_dir]

    config = {"conf_file": config_file, "id": "the-id"}
    args = flag_overrides_args + ["test.ping"]
    kwargs = {"minion_tgt": minion_id}
    expected = (
        [
            sys.executable,
            cli_script_name,
            "--out=json",
            "--out-indent=0",
            "--log-level=critical",
            minion_id,
        ]
        + flag_overrides_args
        + ["test.ping"]
    )
    proc = SaltCli(script_name=cli_script_name, config=config)
    cmdline = proc.cmdline(*args, **kwargs)
    assert cmdline == expected
Ejemplo n.º 4
0
def test_override_timeout_bad_value(minion_id, config_dir, config_file,
                                    cli_script_name, flag):
    flag_value = 15
    if flag.endswith("="):
        flag_overrides_args = [flag + str(flag_value) + "i"]
    else:
        flag_overrides_args = [flag, str(flag_value) + "i"]

    timeout = 10
    config = {"conf_file": config_file, "id": "the-id"}
    args = flag_overrides_args + ["test.ping"]
    kwargs = {"minion_tgt": minion_id}
    expected = ([
        sys.executable,
        cli_script_name,
        "--config-dir={}".format(config_dir),
        "--out=json",
        "--out-indent=0",
        "--log-level=critical",
        minion_id,
    ] + flag_overrides_args + ["test.ping"])
    proc = SaltCli(script_name=cli_script_name, config=config, timeout=timeout)
    # We set __cli_timeout_supported__ to True just to test. This would be an attribute set
    # at the class level for Salt CLI's that support the timeout flag, like for example, salt-run
    proc.__cli_timeout_supported__ = True
    # We set the _terminal_timeout attribute just to test. This attribute would be set when calling
    # SaltScriptBase.run() but we don't really want to call it
    proc.impl._terminal_timeout = timeout
    cmdline = proc.cmdline(*args, **kwargs)
    assert cmdline == expected
    # Let's confirm that even though we tried to parse the timeout flag value, it was a bad value and the
    # SaltScriptBase _terminal_timeout attribute was not update
    assert proc.impl._terminal_timeout == timeout
Ejemplo n.º 5
0
def test_default_cli_flags(minion_id, config_dir, config_file, cli_script_name):
    config = {"conf_file": config_file, "id": "the-id"}
    args = ["test.ping"]
    kwargs = {"minion_tgt": minion_id}
    expected = [
        sys.executable,
        cli_script_name,
        "--config-dir={}".format(config_dir),
        "--out=json",
        "--out-indent=0",
        "--log-level=critical",
        minion_id,
        "test.ping",
    ]
    proc = SaltCli(script_name=cli_script_name, config=config)
    cmdline = proc.cmdline(*args, **kwargs)
    assert cmdline == expected
Ejemplo n.º 6
0
def test_jsonify_kwargs(minion_id, config_dir, config_file, cli_script_name):
    config = {"conf_file": config_file, "id": "the-id"}
    args = ["test.ping"]
    # Strings
    extra_kwargs = OrderedDict((("look", "Ma"), ("no", "Hands!")))
    kwargs = OrderedDict((("minion_tgt", minion_id),))
    kwargs.update(extra_kwargs)
    expected = [
        sys.executable,
        cli_script_name,
        "--config-dir={}".format(config_dir),
        "--out=json",
        "--out-indent=0",
        "--log-level=critical",
        minion_id,
        "test.ping",
    ]
    for key, value in extra_kwargs.items():
        expected.append("{}={}".format(key, value))
    proc = SaltCli(script_name=cli_script_name, config=config)
    cmdline = proc.cmdline(*args, **kwargs)
    # Function **kwargs are not ordered dictionaries on some python versions
    # let's just use sorted to make sure everything is in the output
    assert sorted(cmdline) == sorted(expected)

    # Numbers
    extra_kwargs = OrderedDict((("width", 1.27), ("height", 3)))
    kwargs = OrderedDict((("minion_tgt", minion_id),))
    kwargs.update(extra_kwargs)
    expected = [
        sys.executable,
        cli_script_name,
        "--config-dir={}".format(config_dir),
        "--out=json",
        "--out-indent=0",
        "--log-level=critical",
        minion_id,
        "test.ping",
    ]
    for key, value in extra_kwargs.items():
        value = json.dumps(value)
        expected.append("{}={}".format(key, value))
    proc = SaltCli(script_name=cli_script_name, config=config)
    cmdline = proc.cmdline(*args, **kwargs)
    # Function **kwargs are not ordered dictionaries on some python versions
    # let's just use sorted to make sure everything is in the output
    assert sorted(cmdline) == sorted(expected)

    # Booleans
    extra_kwargs = OrderedDict((("short", False), ("tall", True)))
    kwargs = OrderedDict((("minion_tgt", minion_id),))
    kwargs.update(extra_kwargs)
    expected = [
        sys.executable,
        cli_script_name,
        "--config-dir={}".format(config_dir),
        "--out=json",
        "--out-indent=0",
        "--log-level=critical",
        minion_id,
        "test.ping",
    ]
    for key, value in extra_kwargs.items():
        value = json.dumps(value)
        expected.append("{}={}".format(key, value))
    proc = SaltCli(script_name=cli_script_name, config=config)
    cmdline = proc.cmdline(*args, **kwargs)
    # Function **kwargs are not ordered dictionaries on some python versions
    # let's just use sorted to make sure everything is in the output
    assert sorted(cmdline) == sorted(expected)

    # JSon structure
    extra_kwargs = {"look": "Ma", "no": "Hands!"}
    kwargs = {"minion_tgt": minion_id, "extra": extra_kwargs}
    expected = [
        sys.executable,
        cli_script_name,
        "--config-dir={}".format(config_dir),
        "--out=json",
        "--out-indent=0",
        "--log-level=critical",
        minion_id,
        "test.ping",
        "extra={}".format(json.dumps(extra_kwargs)),
    ]
    proc = SaltCli(script_name=cli_script_name, config=config)
    cmdline = proc.cmdline(*args, **kwargs)
    # Function **kwargs are not ordered dictionaries on some python versions
    # let's just use sorted to make sure everything is in the output
    assert sorted(cmdline) == sorted(expected)