def test_default_cli_flags_with_timeout(minion_id, config_dir, config_file, cli_script_name):
    default_timeout = 10
    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.strpath),
        "--timeout={}".format(default_timeout - 5),
        "--out=json",
        "--log-level=quiet",
        minion_id,
        "test.ping",
    ]
    proc = SaltCliFactory(
        cli_script_name=cli_script_name, config=config, default_timeout=default_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._terminal_timeout = proc.default_timeout
    cmdline = proc.build_cmdline(*args, **kwargs)
    assert cmdline == expected
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"]

    default_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.strpath),
            "--out=json",
            "--log-level=quiet",
            minion_id,
        ]
        + flag_overrides_args
        + ["test.ping",]
    )
    proc = SaltCliFactory(
        cli_script_name=cli_script_name, config=config, default_timeout=default_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._terminal_timeout = flag_value
    cmdline = proc.build_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._terminal_timeout == flag_value