From 38806e85074c71246549faaf9ac025b6b2bb99c2 Mon Sep 17 00:00:00 2001 From: Tim Rupp Date: Thu, 8 Nov 2018 21:08:27 -0800 Subject: [PATCH] Fixes for bigip_qkview (#48396) Fixes unit tests for ansible 2.8 and adds more error handling for qkview generation --- .../modules/network/f5/bigip_qkview.py | 25 +++++++++++++++---- .../modules/network/f5/test_bigip_qkview.py | 21 ++++++++++------ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/ansible/modules/network/f5/bigip_qkview.py b/lib/ansible/modules/network/f5/bigip_qkview.py index b0392e1194..5d47eb907e 100644 --- a/lib/ansible/modules/network/f5/bigip_qkview.py +++ b/lib/ansible/modules/network/f5/bigip_qkview.py @@ -1,7 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # -# Copyright (c) 2017 F5 Networks Inc. +# Copyright: (c) 2017, F5 Networks Inc. # GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function @@ -82,6 +82,10 @@ EXAMPLES = r''' - audit - secure dest: /tmp/localhost.localdomain.qkview + provider: + password: secret + server: lb.mydomain.com + user: admin delegate_to: localhost ''' @@ -92,6 +96,7 @@ RETURN = r''' import os import re import socket +import ssl import time from ansible.module_utils.basic import AnsibleModule @@ -272,7 +277,11 @@ class BaseManager(object): def present(self): if os.path.exists(self.want.dest) and not self.want.force: raise F5ModuleError( - "The specified 'dest' file already exists" + "The specified 'dest' file already exists." + ) + if not os.path.exists(os.path.dirname(self.want.dest)): + raise F5ModuleError( + "The directory of your 'dest' file does not exist." ) if self.want.exclude: choices = ['all', 'audit', 'secure', 'bash_history'] @@ -455,9 +464,14 @@ class BaseManager(object): while True: try: resp = self.client.api.get(uri, timeout=10) - except socket.timeout: + except (socket.timeout, ssl.SSLError): + continue + try: + response = resp.json() + except ValueError: + # It is possible that the API call can return invalid JSON. + # This invalid JSON appears to be just empty strings. continue - response = resp.json() if response['_taskState'] == 'FAILED': raise F5ModuleError( "qkview creation task failed unexpectedly." @@ -577,8 +591,9 @@ def main(): supports_check_mode=spec.supports_check_mode, ) + client = F5RestClient(**module.params) + try: - client = F5RestClient(**module.params) mm = ModuleManager(module=module, client=client) results = mm.exec_module() cleanup_tokens(client) diff --git a/test/units/modules/network/f5/test_bigip_qkview.py b/test/units/modules/network/f5/test_bigip_qkview.py index 78a0179ea1..28cb05e485 100644 --- a/test/units/modules/network/f5/test_bigip_qkview.py +++ b/test/units/modules/network/f5/test_bigip_qkview.py @@ -14,9 +14,6 @@ from nose.plugins.skip import SkipTest if sys.version_info < (2, 7): raise SkipTest("F5 Ansible modules require Python >= 2.7") -from units.compat import unittest -from units.compat.mock import Mock -from units.compat.mock import patch from ansible.module_utils.basic import AnsibleModule try: @@ -25,9 +22,13 @@ try: from library.modules.bigip_qkview import MadmLocationManager from library.modules.bigip_qkview import BulkLocationManager from library.modules.bigip_qkview import ArgumentSpec - from library.module_utils.network.f5.common import F5ModuleError - from library.module_utils.network.f5.common import iControlUnexpectedHTTPError - from test.unit.modules.utils import set_module_args + + # In Ansible 2.8, Ansible changed import paths. + from test.units.compat import unittest + from test.units.compat.mock import Mock + from test.units.compat.mock import patch + + from test.units.modules.utils import set_module_args except ImportError: try: from ansible.modules.network.f5.bigip_qkview import Parameters @@ -35,8 +36,12 @@ except ImportError: from ansible.modules.network.f5.bigip_qkview import MadmLocationManager from ansible.modules.network.f5.bigip_qkview import BulkLocationManager from ansible.modules.network.f5.bigip_qkview import ArgumentSpec - from ansible.module_utils.network.f5.common import F5ModuleError - from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError + + # Ansible 2.8 imports + from units.compat import unittest + from units.compat.mock import Mock + from units.compat.mock import patch + from units.modules.utils import set_module_args except ImportError: raise SkipTest("F5 Ansible modules require the f5-sdk Python library")