Skip to content

Commit

Permalink
Enable gathering requests and responses for tests with a flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Samathy authored and Samathy Barratt committed Oct 20, 2020
1 parent 9e8a41a commit 75a6ee1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
24 changes: 14 additions & 10 deletions pypsrp/wsman.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def __init__(self, server, max_envelope_size=153600, operation_timeout=20,
connection_timeout=30, encryption='auto', proxy=None,
no_proxy=False, locale='en-US', data_locale=None,
read_timeout=30, reconnection_retries=0,
reconnection_backoff=2.0, **kwargs):
reconnection_backoff=2.0, enable_test_messages=False, **kwargs):
"""
Class that handles WSMan transport over HTTP. This exposes a method per
action that takes in a resource and the header metadata required by
Expand Down Expand Up @@ -186,6 +186,8 @@ def __init__(self, server, max_envelope_size=153600, operation_timeout=20,
:param float reconnection_backoff: Number of seconds to backoff in
between reconnection attempts (first sleeps X, then sleeps 2*X,
4*X, 8*X, ...)
:param enable_test_messages: Enable gathering of request and responses
for constructing tests with.
:param kwargs: Dynamic kwargs based on the auth protocol set
# auth='certificate'
certificate_key_pem: The path to the cert key pem file
Expand Down Expand Up @@ -223,9 +225,10 @@ def __init__(self, server, max_envelope_size=153600, operation_timeout=20,
connection_timeout, encryption, proxy,
no_proxy, read_timeout,
reconnection_retries,
reconnection_backoff, **kwargs)
reconnection_backoff, enable_test_messages, **kwargs)
self.max_envelope_size = max_envelope_size
self.operation_timeout = operation_timeout
self._enable_test_messages = enable_test_messages

# register well known namespace prefixes so ElementTree doesn't
# randomly generate them, saving packet space
Expand Down Expand Up @@ -649,7 +652,7 @@ def __init__(self, server, port=None, username=None, password=None,
cert_validation=True, connection_timeout=30,
encryption='auto', proxy=None, no_proxy=False,
read_timeout=30, reconnection_retries=0,
reconnection_backoff=2.0, **kwargs):
reconnection_backoff=2.0, enable_test_messages=False, **kwargs):
self.server = server
self.port = port if port is not None else (5986 if ssl else 5985)
self.username = username
Expand All @@ -667,6 +670,7 @@ def __init__(self, server, port=None, username=None, password=None,
self.read_timeout = read_timeout
self.reconnection_retries = reconnection_retries
self.reconnection_backoff = reconnection_backoff
self._enable_test_messages = enable_test_messages

# determine the message encryption logic
if encryption not in ["auto", "always", "never"]:
Expand Down Expand Up @@ -709,8 +713,8 @@ def __init__(self, server, port=None, username=None, password=None,
"user: %s" % (self.endpoint, self.username, self.auth))
self.session = None

# used when building tests, keep commented out
# self._test_messages = []
if self._enable_test_messages:
self._test_messages = []

def close(self):
if self.session:
Expand Down Expand Up @@ -738,9 +742,9 @@ def send(self, message):
self.encryption = WinRMEncryption(self.session.auth.contexts[hostname], protocol)

log.debug("Sending message: %s" % message)
# for testing, keep commented out
# self._test_messages.append({"request": message.decode('utf-8'),
# "response": None})
if self._enable_test_messages:
self._test_messages.append({"request": message.decode('utf-8'),
"response": None})

headers = self.session.headers
if self.wrap_required:
Expand Down Expand Up @@ -775,8 +779,8 @@ def _send_request(self, request):
response_text = response.text if response_content else ''

log.debug("Received message: %s" % response_text)
# for testing, keep commented out
# self._test_messages[-1]['response'] = response_text
if self._enable_test_messages:
self._test_messages[-1]['response'] = response_text
try:
response.raise_for_status()
except requests.HTTPError as err:
Expand Down
9 changes: 5 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, test_name, server, port, username, password, ssl, path,
self.endpoint = "%s://%s:%d/%s" \
% ("https" if ssl else "http", server, port, path)
self.session = None
self._enable_test_messages = False

# used in the test only
for key, value in NAMESPACES.items():
Expand Down Expand Up @@ -324,10 +325,10 @@ def mockuuid():
with wsman:
yield wsman

# used as an easy way to be results for a test, requires the _test_messages
# to be uncommented in pypsrp/wsman.py
test_messages = getattr(wsman.transport, '_test_messages', None)
if test_messages is not None:
# used as an easy way to be results for a test, requires `enable_test_messages=True` to be
# passed to wsman constructor
if wsman.transport._enable_test_messages:
test_messages = wsman.transport._test_messages
yaml_text = yaml.dump({"messages": test_messages},
default_flow_style=False,
width=9999)
Expand Down

0 comments on commit 75a6ee1

Please sign in to comment.