Skip to content

Commit

Permalink
Added handlers to parse timeout values from string
Browse files Browse the repository at this point in the history
  • Loading branch information
jborean93 committed Nov 28, 2017
1 parent f2fae36 commit edce459
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions winrm/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def __init__(
@param bool message_encryption_enabled: Will encrypt the WinRM messages if set to True and the transport auth supports message encryption (Default True).
"""

try:
read_timeout_sec = int(read_timeout_sec)
except ValueError as ve:
raise ValueError("failed to parse read_timeout_sec as int: %s" % str(ve))

try:
operation_timeout_sec = int(operation_timeout_sec)
except ValueError as ve:
raise ValueError("failed to parse operation_timeout_sec as int: %s" % str(ve))

if operation_timeout_sec >= read_timeout_sec or operation_timeout_sec < 1:
raise WinRMError("read_timeout_sec must exceed operation_timeout_sec, and both must be non-zero")

Expand Down
37 changes: 37 additions & 0 deletions winrm/tests/test_protocol.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import pytest

from winrm.protocol import Protocol


def test_open_shell_and_close_shell(protocol_fake):
shell_id = protocol_fake.open_shell()
assert shell_id == '11111111-1111-1111-1111-111111111113'
Expand Down Expand Up @@ -34,3 +39,35 @@ def test_get_command_output(protocol_fake):

protocol_fake.cleanup_command(shell_id, command_id)
protocol_fake.close_shell(shell_id)


def test_set_timeout_as_sec():
protocol = Protocol('endpoint',
username='username',
password='password',
read_timeout_sec='30',
operation_timeout_sec='29')
assert protocol.read_timeout_sec == 30
assert protocol.operation_timeout_sec == 29


def test_fail_set_read_timeout_as_sec():
with pytest.raises(ValueError) as exc:
protocol = Protocol('endpoint',
username='username',
password='password',
read_timeout_sec='30a',
operation_timeout_sec='29')
assert str(exc.value) == "failed to parse read_timeout_sec as int: " \
"invalid literal for int() with base 10: '30a'"


def test_fail_set_operation_timeout_as_sec():
with pytest.raises(ValueError) as exc:
protocol = Protocol('endpoint',
username='username',
password='password',
read_timeout_sec=30,
operation_timeout_sec='29a')
assert str(exc.value) == "failed to parse operation_timeout_sec as int: " \
"invalid literal for int() with base 10: '29a'"

0 comments on commit edce459

Please sign in to comment.