1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Chris Archibald 2018-11-19 06:43:32 -08:00 committed by John R Barker
parent c7e2226035
commit 50808ffa8f
4 changed files with 104 additions and 15 deletions

View file

@ -27,10 +27,8 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # 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. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
try: import json
import json import os
except ImportError:
import simplejson as json
from ansible.module_utils.six.moves.urllib.error import HTTPError from ansible.module_utils.six.moves.urllib.error import HTTPError
from ansible.module_utils.urls import open_url from ansible.module_utils.urls import open_url
@ -40,17 +38,13 @@ try:
except ImportError: except ImportError:
ansible_version = 'unknown' ansible_version = 'unknown'
import os
import ssl
HAS_NETAPP_LIB = False
try: try:
from netapp_lib.api.zapi import zapi from netapp_lib.api.zapi import zapi
from netapp_lib.api.zapi import errors as zapi_errors
HAS_NETAPP_LIB = True HAS_NETAPP_LIB = True
except: except ImportError:
HAS_NETAPP_LIB = False HAS_NETAPP_LIB = False
import ssl
HAS_SF_SDK = False HAS_SF_SDK = False
SF_BYTE_MAP = dict( SF_BYTE_MAP = dict(
@ -67,6 +61,20 @@ SF_BYTE_MAP = dict(
yb=1000 ** 8 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: try:
from solidfire.factory import ElementFactory from solidfire.factory import ElementFactory
from solidfire.custom.models import TimeIntervalFrequency 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), password=dict(required=True, type='str', aliases=['pass'], no_log=True),
https=dict(required=False, type='bool', default=False), https=dict(required=False, type='bool', default=False),
validate_certs=dict(required=False, type='bool', default=True), 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'] https = module.params['https']
validate_certs = module.params['validate_certs'] validate_certs = module.params['validate_certs']
port = module.params['http_port'] port = module.params['http_port']
version = module.params['ontapi']
if HAS_NETAPP_LIB: if HAS_NETAPP_LIB:
# set up zapi # set up zapi
@ -136,8 +146,11 @@ def setup_na_ontap_zapi(module, vserver=None):
server.set_password(password) server.set_password(password)
if vserver: if vserver:
server.set_vserver(vserver) server.set_vserver(vserver)
# Todo : Replace hard-coded values with configurable parameters. if version:
server.set_api_version(major=1, minor=110) minor = version
else:
minor = 110
server.set_api_version(major=1, minor=minor)
# default is HTTP # default is HTTP
if https: if https:
if port is None: if port is None:

View file

@ -61,12 +61,17 @@ options:
description: description:
- Override the default port (80 or 443) with this port - Override the default port (80 or 443) with this port
type: int type: int
ontapi:
description:
- The ontap api version to use
type: int
requirements: 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 - 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;' - To enable http on the cluster you must run the following commands 'set -privilege advanced;' 'system services web modify -http-enabled true;'
notes: notes:

View file

@ -18,6 +18,8 @@ setuptools > 0.6 # pytest-xdist installed via requirements does not work with ve
unittest2 ; python_version < '2.7' unittest2 ; python_version < '2.7'
netaddr netaddr
ipaddress ipaddress
netapp-lib
solidfire-sdk-python
# requirements for F5 specific modules # requirements for F5 specific modules
f5-sdk ; python_version >= '2.7' f5-sdk ; python_version >= '2.7'

View file

@ -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