| 1 |
# Copyright (c) 2018-2022 NetApp |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
''' unit tests for module_utils netapp.py - solidfire related methods ''' |
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from ansible.module_utils import basic |
| 11 |
from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch |
| 12 |
# pylint: disable=unused-import |
| 13 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import \ |
| 14 |
patch_ansible, create_module, expect_and_capture_ansible_exception |
| 15 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 16 |
|
| 17 |
if not netapp_utils.has_sf_sdk(): |
| 18 |
pytestmark = pytest.mark.skip("skipping as missing required solidfire") |
| 19 |
|
| 20 |
DEFAULT_ARGS = { |
| 21 |
'hostname': 'test', |
| 22 |
'username': 'test_user', |
| 23 |
'password': 'test_pass!', |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
class MockONTAPModule: |
| 28 |
def __init__(self): |
| 29 |
self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec()) |
| 30 |
|
| 31 |
|
| 32 |
def create_ontap_module(default_args=None): |
| 33 |
return create_module(MockONTAPModule, default_args).module |
| 34 |
|
| 35 |
|
| 36 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.HAS_SF_SDK', 'dummy') |
| 37 |
def test_has_sf_sdk(): |
| 38 |
assert netapp_utils.has_sf_sdk() == 'dummy' |
| 39 |
|
| 40 |
|
| 41 |
@patch('solidfire.factory.ElementFactory.create') |
| 42 |
def test_create_sf_connection(mock_sf_create): |
| 43 |
module = create_ontap_module(DEFAULT_ARGS) |
| 44 |
mock_sf_create.return_value = 'dummy' |
| 45 |
assert netapp_utils.create_sf_connection(module) == 'dummy' |
| 46 |
|
| 47 |
|
| 48 |
@patch('solidfire.factory.ElementFactory.create') |
| 49 |
def test_negative_create_sf_connection_exception(mock_sf_create): |
| 50 |
module = create_ontap_module(DEFAULT_ARGS) |
| 51 |
mock_sf_create.side_effect = KeyError('dummy') |
| 52 |
assert str(expect_and_capture_ansible_exception(netapp_utils.create_sf_connection, Exception, module)) == "Unable to create SF connection: 'dummy'" |
| 53 |
|
| 54 |
|
| 55 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.HAS_SF_SDK', False) |
| 56 |
def test_negative_create_sf_connection_no_sdk(): |
| 57 |
module = create_ontap_module(DEFAULT_ARGS) |
| 58 |
assert expect_and_capture_ansible_exception(netapp_utils.create_sf_connection, 'fail', module)['msg'] == 'the python SolidFire SDK module is required' |
| 59 |
|
| 60 |
|
| 61 |
def test_negative_create_sf_connection_no_options(): |
| 62 |
module = create_ontap_module(DEFAULT_ARGS) |
| 63 |
peer_options = {} |
| 64 |
assert expect_and_capture_ansible_exception(netapp_utils.create_sf_connection, 'fail', module, host_options=peer_options)['msg'] ==\ |
| 65 |
'hostname, username, password are required for ElementSW connection.' |
| 66 |
|
| 67 |
|
| 68 |
def test_negative_create_sf_connection_missing_and_extra_options(): |
| 69 |
module = create_ontap_module(DEFAULT_ARGS) |
| 70 |
peer_options = {'hostname': 'host', 'username': 'user'} |
| 71 |
assert expect_and_capture_ansible_exception(netapp_utils.create_sf_connection, 'fail', module, host_options=peer_options)['msg'] ==\ |
| 72 |
'password is required for ElementSW connection.' |
| 73 |
peer_options = {'hostname': 'host', 'username': 'user', 'cert_filepath': 'cert'} |
| 74 |
assert expect_and_capture_ansible_exception(netapp_utils.create_sf_connection, 'fail', module, host_options=peer_options)['msg'] ==\ |
| 75 |
'password is required for ElementSW connection. cert_filepath is not supported for ElementSW connection.' |
| 76 |
|
| 77 |
|
| 78 |
def test_negative_create_sf_connection_extra_options(): |
| 79 |
module = create_ontap_module(DEFAULT_ARGS) |
| 80 |
peer_options = {'hostname': 'host', 'username': 'user'} |
| 81 |
assert expect_and_capture_ansible_exception(netapp_utils.create_sf_connection, 'fail', module, host_options=peer_options)['msg'] ==\ |
| 82 |
'password is required for ElementSW connection.' |
| 83 |
peer_options = {'hostname': 'host', 'username': 'user', 'password': 'pass', 'cert_filepath': 'cert', 'key_filepath': 'key'} |
| 84 |
assert expect_and_capture_ansible_exception(netapp_utils.create_sf_connection, 'fail', module, host_options=peer_options)['msg'] ==\ |
| 85 |
'cert_filepath, key_filepath are not supported for ElementSW connection.' |