summaryrefslogtreecommitdiff
path: root/test/units/modules/storage/netapp/test_na_ontap_vscan.py
blob: bdc9eb1c59ee424def4220c19764812fda90d2f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# (c) 2018, NetApp, Inc
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

''' unit tests for Ansible module: na_ontap_vscan'''

from __future__ import print_function
import json
import pytest

from units.compat import unittest
from units.compat.mock import patch
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
import ansible.module_utils.netapp as netapp_utils

from ansible.modules.storage.netapp.na_ontap_vscan \
    import NetAppOntapVscan as vscan_module  # module under test

if not netapp_utils.has_netapp_lib():
    pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
HAS_NETAPP_ZAPI_MSG = "pip install netapp_lib is required"


# REST API canned responses when mocking send_request
SRR = {
    # common responses
    'is_rest': (200, None),
    'is_zapi': (400, "Unreachable"),
    'empty_good': ({}, None),
    'end_of_sequence': (None, "Ooops, the UT needs one more SRR response"),
    'generic_error': (None, "Expected error"),
    # module specific responses
    'enabled': ({'records': [{'enabled': True, 'svm': {'uuid': 'testuuid'}}]}, None),
    'disabled': ({'records': [{'enabled': False, 'svm': {'uuid': 'testuuid'}}]}, None),
}


def set_module_args(args):
    """prepare arguments so that they will be picked up during module creation"""
    args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
    basic._ANSIBLE_ARGS = to_bytes(args)  # pylint: disable=protected-access


class AnsibleExitJson(Exception):
    """Exception class to be raised by module.exit_json and caught by the test case"""
    pass


class AnsibleFailJson(Exception):
    """Exception class to be raised by module.fail_json and caught by the test case"""
    pass


def exit_json(*args, **kwargs):  # pylint: disable=unused-argument
    """function to patch over exit_json; package return data into an exception"""
    if 'changed' not in kwargs:
        kwargs['changed'] = False
    raise AnsibleExitJson(kwargs)


def fail_json(*args, **kwargs):  # pylint: disable=unused-argument
    """function to patch over fail_json; package return data into an exception"""
    kwargs['failed'] = True
    raise AnsibleFailJson(kwargs)


class MockONTAPConnection(object):
    ''' mock server connection to ONTAP host '''

    def __init__(self, kind=None, data=None):
        ''' save arguments '''
        self.kind = kind
        self.params = data
        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.kind == 'enable':
            xml = self.build_vscan_status_info(self.params)
        self.xml_out = xml
        return xml

    @staticmethod
    def build_vscan_status_info(status):
        xml = netapp_utils.zapi.NaElement('xml')
        attributes = {'num-records': 1,
                      'attributes-list': {'vscan-status-info': {'is-vscan-enabled': status}}}
        xml.translate_struct(attributes)
        return xml


class TestMyModule(unittest.TestCase):
    ''' Unit tests for na_ontap_job_schedule '''

    def setUp(self):
        self.mock_module_helper = patch.multiple(basic.AnsibleModule,
                                                 exit_json=exit_json,
                                                 fail_json=fail_json)
        self.mock_module_helper.start()
        self.addCleanup(self.mock_module_helper.stop)

    def mock_args(self):
        return {
            'enable': False,
            'vserver': 'vserver',
            'hostname': 'test',
            'username': 'test_user',
            'password': 'test_pass!'
        }

    def get_vscan_mock_object(self, type='zapi', kind=None, status=None):
        vscan_obj = vscan_module()
        if type == 'zapi':
            if kind is None:
                vscan_obj.server = MockONTAPConnection()
            else:
                vscan_obj.server = MockONTAPConnection(kind=kind, data=status)
        # For rest, mocking is achieved through side_effect
        return vscan_obj

    def test_module_fail_when_required_args_missing(self):
        ''' required arguments are reported as errors '''
        with pytest.raises(AnsibleFailJson) as exc:
            set_module_args({})
            vscan_module()
        print('Info: %s' % exc.value.args[0]['msg'])

    def test_successfully_enable(self):
        data = self.mock_args()
        data['enable'] = True
        set_module_args(data)
        with pytest.raises(AnsibleExitJson) as exc:
            self.get_vscan_mock_object('zapi', 'enable', 'false').apply()
        assert exc.value.args[0]['changed']

    def test_idempotently_enable(self):
        data = self.mock_args()
        data['enable'] = True
        set_module_args(data)
        with pytest.raises(AnsibleExitJson) as exc:
            self.get_vscan_mock_object('zapi', 'enable', 'true').apply()
        assert not exc.value.args[0]['changed']

    def test_successfully_disable(self):
        data = self.mock_args()
        data['enable'] = False
        set_module_args(data)
        with pytest.raises(AnsibleExitJson) as exc:
            self.get_vscan_mock_object('zapi', 'enable', 'true').apply()
        assert exc.value.args[0]['changed']

    def test_idempotently_disable(self):
        data = self.mock_args()
        data['enable'] = False
        set_module_args(data)
        with pytest.raises(AnsibleExitJson) as exc:
            self.get_vscan_mock_object('zapi', 'enable', 'false').apply()
        assert not exc.value.args[0]['changed']

    @patch('ansible.module_utils.netapp.OntapRestAPI.send_request')
    def test_rest_error(self, mock_request):
        data = self.mock_args()
        set_module_args(data)
        mock_request.side_effect = [
            SRR['is_rest'],
            SRR['generic_error'],
            SRR['end_of_sequence']
        ]
        with pytest.raises(AnsibleFailJson) as exc:
            self.get_vscan_mock_object(type='rest').apply()
        assert exc.value.args[0]['msg'] == SRR['generic_error'][1]

    @patch('ansible.module_utils.netapp.OntapRestAPI.send_request')
    def test_rest_successly_enable(self, mock_request):
        data = self.mock_args()
        data['enable'] = True
        set_module_args(data)
        mock_request.side_effect = [
            SRR['is_rest'],
            SRR['disabled'],
            SRR['empty_good'],
            SRR['end_of_sequence']
        ]
        with pytest.raises(AnsibleExitJson) as exc:
            self.get_vscan_mock_object(type='rest').apply()
        assert exc.value.args[0]['changed']

    @patch('ansible.module_utils.netapp.OntapRestAPI.send_request')
    def test_rest_idempotently_enable(self, mock_request):
        data = self.mock_args()
        data['enable'] = True
        set_module_args(data)
        mock_request.side_effect = [
            SRR['is_rest'],
            SRR['enabled'],
            SRR['end_of_sequence']
        ]
        with pytest.raises(AnsibleExitJson) as exc:
            self.get_vscan_mock_object(type='rest').apply()
        assert not exc.value.args[0]['changed']

    @patch('ansible.module_utils.netapp.OntapRestAPI.send_request')
    def test_rest_successly_disable(self, mock_request):
        data = self.mock_args()
        data['enable'] = False
        set_module_args(data)
        mock_request.side_effect = [
            SRR['is_rest'],
            SRR['enabled'],
            SRR['empty_good'],
            SRR['end_of_sequence']
        ]
        with pytest.raises(AnsibleExitJson) as exc:
            self.get_vscan_mock_object(type='rest').apply()
        assert exc.value.args[0]['changed']

    @patch('ansible.module_utils.netapp.OntapRestAPI.send_request')
    def test_rest_idempotently_disable(self, mock_request):
        data = self.mock_args()
        data['enable'] = False
        set_module_args(data)
        mock_request.side_effect = [
            SRR['is_rest'],
            SRR['disabled'],
            SRR['end_of_sequence']
        ]
        with pytest.raises(AnsibleExitJson) as exc:
            self.get_vscan_mock_object(type='rest').apply()
        assert not exc.value.args[0]['changed']