Fix compatibility to fetch_url change in ansible-core devel (#339)

* Fix compatibility to fetch_url change in ansible-core devel.

* Adjust tests.
pull/341/head
Felix Fontein 2021-11-17 21:26:49 +01:00 committed by GitHub
parent cf0d2679aa
commit 5de50b9f91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 5 deletions

View File

@ -0,0 +1,2 @@
minor_changes:
- "acme_* modules - fix usage of ``fetch_url`` with changes in latest ansible-core ``devel`` branch (https://github.com/ansible-collections/community.crypto/pull/339)."

View File

@ -15,8 +15,9 @@ import locale
import traceback import traceback
from ansible.module_utils.basic import missing_required_lib from ansible.module_utils.basic import missing_required_lib
from ansible.module_utils.urls import fetch_url
from ansible.module_utils.common.text.converters import to_bytes from ansible.module_utils.common.text.converters import to_bytes
from ansible.module_utils.urls import fetch_url
from ansible.module_utils.six import PY3
from ansible_collections.community.crypto.plugins.module_utils.acme.backend_openssl_cli import ( from ansible_collections.community.crypto.plugins.module_utils.acme.backend_openssl_cli import (
OpenSSLCLIBackend, OpenSSLCLIBackend,
@ -237,9 +238,14 @@ class ACMEClient(object):
resp, info = fetch_url(self.module, url, data=data, headers=headers, method='POST') resp, info = fetch_url(self.module, url, data=data, headers=headers, method='POST')
_assert_fetch_url_success(self.module, resp, info) _assert_fetch_url_success(self.module, resp, info)
result = {} result = {}
try: try:
# In Python 2, reading from a closed response yields a TypeError.
# In Python 3, read() simply returns ''
if PY3 and resp.closed:
raise TypeError
content = resp.read() content = resp.read()
except AttributeError: except (AttributeError, TypeError):
content = info.pop('body', None) content = info.pop('body', None)
if content or not parse_json_result: if content or not parse_json_result:
@ -293,8 +299,12 @@ class ACMEClient(object):
_assert_fetch_url_success(self.module, resp, info) _assert_fetch_url_success(self.module, resp, info)
try: try:
# In Python 2, reading from a closed response yields a TypeError.
# In Python 3, read() simply returns ''
if PY3 and resp.closed:
raise TypeError
content = resp.read() content = resp.read()
except AttributeError: except (AttributeError, TypeError):
content = info.pop('body', None) content = info.pop('body', None)
# Process result # Process result

View File

@ -7,8 +7,8 @@
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
from ansible.module_utils.six import binary_type
from ansible.module_utils.common.text.converters import to_text from ansible.module_utils.common.text.converters import to_text
from ansible.module_utils.six import binary_type, PY3
def format_error_problem(problem, subproblem_prefix=''): def format_error_problem(problem, subproblem_prefix=''):
@ -52,8 +52,12 @@ class ACMEProtocolException(ModuleFailException):
# Try to get hold of content, if response is given and content is not provided # Try to get hold of content, if response is given and content is not provided
if content is None and content_json is None and response is not None: if content is None and content_json is None and response is not None:
try: try:
# In Python 2, reading from a closed response yields a TypeError.
# In Python 3, read() simply returns ''
if PY3 and response.closed:
raise TypeError
content = response.read() content = response.read()
except AttributeError: except (AttributeError, TypeError):
content = info.pop('body', None) content = info.pop('body', None)
# Make sure that content_json is None or a dictionary # Make sure that content_json is None or a dictionary

View File

@ -115,12 +115,14 @@ def test_format_error_problem(problem, subproblem_prefix, result):
def create_regular_response(response_text): def create_regular_response(response_text):
response = MagicMock() response = MagicMock()
response.read = MagicMock(return_value=response_text.encode('utf-8')) response.read = MagicMock(return_value=response_text.encode('utf-8'))
response.closed = False
return response return response
def create_error_response(): def create_error_response():
response = MagicMock() response = MagicMock()
response.read = MagicMock(side_effect=AttributeError('read')) response.read = MagicMock(side_effect=AttributeError('read'))
response.closed = True
return response return response