Пример #1
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
Пример #2
0
def test_cli_timeout_greater_than_timeout_kw(minion_id, config_dir,
                                             config_file, cli_script_name):
    # Both --timeout and _timeout are passed.
    # Since --timeout is greater than _timeout, the value of _timeout is updated to the value of --timeout plus 5
    timeout = 10
    explicit_timeout = 20
    cli_timeout = 60
    config = {"conf_file": config_file, "id": "the-id"}
    args = ["--timeout", str(cli_timeout), "test.ping"]
    kwargs = {"minion_tgt": minion_id, "_timeout": explicit_timeout}
    expected = [
        sys.executable,
        cli_script_name,
        "--config-dir={}".format(config_dir),
        "--out=json",
        "--out-indent=0",
        "--log-level=critical",
        minion_id,
        "--timeout",
        "60",
        "test.ping",
    ]

    popen_mock = mock.MagicMock()
    popen_mock.pid = os.getpid()
    popen_mock.poll = mock.MagicMock(
        side_effect=[None, None, None, None, True])
    terminate_mock = mock.MagicMock(return_value=ProcessResult(
        returncode=0, stdout="", stderr="", cmdline=()))
    popen_mock.terminate = terminate_mock

    proc = SaltCli(script_name=cli_script_name, config=config, timeout=timeout)
    with mock.patch.object(proc.impl, "init_terminal",
                           popen_mock), mock.patch.object(
                               proc, "terminate", terminate_mock):
        proc.impl._terminal = popen_mock
        # 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
        proc.run(*args, **kwargs)
        assert proc.impl._terminal_timeout == cli_timeout + SALT_TIMEOUT_FLAG_INCREASE
        assert popen_mock.call_args[0][0] == expected  # pylint: disable=unsubscriptable-object
Пример #3
0
def test_cli_timeout_updates_to_timeout_kw_plus_10(
    minion_id, config_dir, config_file, cli_script_name
):
    # _timeout is passed, the value of --timeout is _timeout, internal timeout is added 10 seconds
    timeout = 10
    explicit_timeout = 60
    config = {"conf_file": config_file, "id": "the-id"}
    args = ["test.ping"]
    kwargs = {"minion_tgt": minion_id, "_timeout": explicit_timeout}
    expected = [
        sys.executable,
        cli_script_name,
        "--config-dir={}".format(config_dir),
        "--timeout={}".format(explicit_timeout),
        "--out=json",
        "--out-indent=0",
        "--log-level=critical",
        minion_id,
        "test.ping",
    ]

    popen_mock = mock.MagicMock()
    popen_mock.pid = os.getpid()
    popen_mock.poll = mock.MagicMock(side_effect=[None, None, None, None, True])
    popen_mock.terminate = mock.MagicMock(return_value=ProcessResult(0, "", "", cmdline=()))
    terminate_mock = mock.MagicMock(return_value=ProcessResult(0, "", ""))

    proc = SaltCli(script_name=cli_script_name, config=config, timeout=timeout)
    with mock.patch.object(proc.impl, "init_terminal", popen_mock), mock.patch.object(
        proc, "terminate", terminate_mock
    ):
        proc.impl._terminal = popen_mock
        # 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
        ret = proc.run(*args, **kwargs)
        assert proc.impl._terminal_timeout == explicit_timeout + 10
        assert popen_mock.call_args[0][0] == expected  # pylint: disable=unsubscriptable-object