From 50808ffa8fa8a2c6561035cd9c755309ffe4ea0e Mon Sep 17 00:00:00 2001 From: Chris Archibald Date: Mon, 19 Nov 2018 06:43:32 -0800 Subject: [PATCH] Add common files for ONTAP and SOLIDFIRE unit tests (#48739) * And unit test files * Changes to netapp.py * Matt's changed, and pep8 change for test_netapp.py --- lib/ansible/module_utils/netapp.py | 39 +++++++---- .../utils/module_docs_fragments/netapp.py | 9 ++- test/runner/requirements/units.txt | 2 + .../modules/storage/netapp/test_netapp.py | 69 +++++++++++++++++++ 4 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 test/units/modules/storage/netapp/test_netapp.py diff --git a/lib/ansible/module_utils/netapp.py b/lib/ansible/module_utils/netapp.py index 4484bbb2e3..3fead51efe 100644 --- a/lib/ansible/module_utils/netapp.py +++ b/lib/ansible/module_utils/netapp.py @@ -27,10 +27,8 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -try: - import json -except ImportError: - import simplejson as json +import json +import os from ansible.module_utils.six.moves.urllib.error import HTTPError from ansible.module_utils.urls import open_url @@ -40,17 +38,13 @@ try: except ImportError: ansible_version = 'unknown' -import os -import ssl - -HAS_NETAPP_LIB = False try: from netapp_lib.api.zapi import zapi - from netapp_lib.api.zapi import errors as zapi_errors HAS_NETAPP_LIB = True -except: +except ImportError: HAS_NETAPP_LIB = False +import ssl HAS_SF_SDK = False SF_BYTE_MAP = dict( @@ -67,6 +61,20 @@ SF_BYTE_MAP = dict( yb=1000 ** 8 ) +POW2_BYTE_MAP = dict( + # Here, 1 kb = 1024 + bytes=1, + b=1, + kb=1024, + mb=1024 ** 2, + gb=1024 ** 3, + tb=1024 ** 4, + pb=1024 ** 5, + eb=1024 ** 6, + zb=1024 ** 7, + yb=1024 ** 8 +) + try: from solidfire.factory import ElementFactory from solidfire.custom.models import TimeIntervalFrequency @@ -93,7 +101,8 @@ def na_ontap_host_argument_spec(): password=dict(required=True, type='str', aliases=['pass'], no_log=True), https=dict(required=False, type='bool', default=False), validate_certs=dict(required=False, type='bool', default=True), - http_port=dict(required=False, type='int') + http_port=dict(required=False, type='int'), + ontapi=dict(required=False, type='int') ) @@ -128,6 +137,7 @@ def setup_na_ontap_zapi(module, vserver=None): https = module.params['https'] validate_certs = module.params['validate_certs'] port = module.params['http_port'] + version = module.params['ontapi'] if HAS_NETAPP_LIB: # set up zapi @@ -136,8 +146,11 @@ def setup_na_ontap_zapi(module, vserver=None): server.set_password(password) if vserver: server.set_vserver(vserver) - # Todo : Replace hard-coded values with configurable parameters. - server.set_api_version(major=1, minor=110) + if version: + minor = version + else: + minor = 110 + server.set_api_version(major=1, minor=minor) # default is HTTP if https: if port is None: diff --git a/lib/ansible/utils/module_docs_fragments/netapp.py b/lib/ansible/utils/module_docs_fragments/netapp.py index 51c76724df..ee65e81cb6 100644 --- a/lib/ansible/utils/module_docs_fragments/netapp.py +++ b/lib/ansible/utils/module_docs_fragments/netapp.py @@ -61,12 +61,17 @@ options: description: - Override the default port (80 or 443) with this port type: int + ontapi: + description: + - The ontap api version to use + type: int requirements: - - A physical or virtual clustered Data ONTAP system. The modules were developed with Clustered Data ONTAP 9.3 + - A physical or virtual clustered Data ONTAP system. The modules support Data ONTAP 9.1 and onward - Ansible 2.6 - - netapp-lib (2017.10.30). Install using 'pip install netapp-lib' + - Python2 netapp-lib (2017.10.30) or later. Install using 'pip install netapp-lib' + - Python3 netapp-lib (2018.11.13) or later. Install using 'pip install netapp-lib' - To enable http on the cluster you must run the following commands 'set -privilege advanced;' 'system services web modify -http-enabled true;' notes: diff --git a/test/runner/requirements/units.txt b/test/runner/requirements/units.txt index 83d5cad5b0..1e0c6b4673 100644 --- a/test/runner/requirements/units.txt +++ b/test/runner/requirements/units.txt @@ -18,6 +18,8 @@ setuptools > 0.6 # pytest-xdist installed via requirements does not work with ve unittest2 ; python_version < '2.7' netaddr ipaddress +netapp-lib +solidfire-sdk-python # requirements for F5 specific modules f5-sdk ; python_version >= '2.7' diff --git a/test/units/modules/storage/netapp/test_netapp.py b/test/units/modules/storage/netapp/test_netapp.py new file mode 100644 index 0000000000..5adabc6ac3 --- /dev/null +++ b/test/units/modules/storage/netapp/test_netapp.py @@ -0,0 +1,69 @@ +# Copyright (c) 2018 NetApp +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +''' unit tests for module_utils netapp.py ''' +from __future__ import absolute_import, division, print_function + +from ansible.module_utils.ansible_release import __version__ as ansible_version + +import pytest +import ansible.module_utils.netapp as netapp_utils + +HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() +HAS_NETAPP_LIB_MSG = "pip install netapp_lib is required" + + +class MockONTAPConnection(object): + ''' mock a server connection to ONTAP host ''' + + def __init__(self, kind=None, parm1=None): + ''' save arguments ''' + self.type = kind + self.parm1 = parm1 + self.xml_in = None + self.xml_out = None + + def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument + ''' mock invoke_successfully returning xml data ''' + self.xml_in = xml + if self.type == 'vserver': + xml = self.build_vserver_info(self.parm1) + self.xml_out = xml + return xml + + @staticmethod + def build_vserver_info(vserver): + ''' build xml data for vserser-info ''' + xml = netapp_utils.zapi.NaElement('xml') + attributes = netapp_utils.zapi.NaElement('attributes-list') + attributes.add_node_with_children('vserver-info', + **{'vserver-name': vserver}) + xml.add_child_elem(attributes) + return xml + + +@pytest.fixture +def has_zapi(): + assert HAS_NETAPP_LIB, HAS_NETAPP_LIB_MSG + + +pytestmark = pytest.mark.usefixtures("has_zapi") + + +def test_ems_log_event_version(): + ''' validate Ansible version is correctly read ''' + source = 'unittest' + server = MockONTAPConnection() + netapp_utils.ems_log_event(source, server) + xml = server.xml_in + version = xml.get_child_content('app-version') + assert version == ansible_version + print("Ansible version: %s" % ansible_version) + + +def test_get_cserver(): + ''' validate cluster vserser name is correctly retrieved ''' + svm_name = 'svm1' + server = MockONTAPConnection('vserver', svm_name) + cserver = netapp_utils.get_cserver(server) + assert cserver == svm_name