summaryrefslogtreecommitdiff
path: root/test/units/modules/storage/netapp/test_netapp_e_iscsi_target.py
blob: 8f44fb06c22628b823df2e665c6e6f506f377898 (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
# coding=utf-8
# (c) 2018, NetApp Inc.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from mock import MagicMock

from ansible.modules.storage.netapp.netapp_e_iscsi_target import IscsiTarget
from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args

__metaclass__ = type

import mock

from units.compat.mock import PropertyMock


class IscsiTargetTest(ModuleTestCase):
    REQUIRED_PARAMS = {
        'api_username': 'rw',
        'api_password': 'password',
        'api_url': 'http://localhost',
        'ssid': '1',
        'name': 'abc',
    }

    CHAP_SAMPLE = 'a' * 14

    REQ_FUNC = 'ansible.modules.storage.netapp.netapp_e_iscsi_target.request'

    def _set_args(self, args=None):
        module_args = self.REQUIRED_PARAMS.copy()
        if args is not None:
            module_args.update(args)
        set_module_args(module_args)

    def test_validate_params(self):
        """Ensure we can pass valid parameters to the module"""
        for i in range(12, 16):
            secret = 'a' * i
            self._set_args(dict(chap=secret))
            tgt = IscsiTarget()

    def test_invalid_chap_secret(self):
        for secret in [11 * 'a', 17 * 'a', u'©' * 12]:
            with self.assertRaisesRegexp(AnsibleFailJson, r'.*?CHAP secret is not valid.*') as result:
                self._set_args(dict(chap=secret))
                tgt = IscsiTarget()

    def test_apply_iscsi_settings(self):
        """Ensure that the presence of CHAP always triggers an update."""
        self._set_args(dict(chap=self.CHAP_SAMPLE))
        tgt = IscsiTarget()

        # CHAP is enabled
        fake = dict(alias=self.REQUIRED_PARAMS.get('name'), chap=True)

        # We don't care about the return here
        with mock.patch(self.REQ_FUNC, return_value=(200, "")) as request:
            with mock.patch.object(IscsiTarget, 'target', new_callable=PropertyMock) as call:
                call.return_value = fake
                self.assertTrue(tgt.apply_iscsi_settings())
                self.assertTrue(request.called, msg="An update was expected!")

                # Retest with check_mode enabled
                tgt.check_mode = True
                request.reset_mock()
                self.assertTrue(tgt.apply_iscsi_settings())
                self.assertFalse(request.called, msg="No update was expected in check_mode!")

    def test_apply_iscsi_settings_no_change(self):
        """Ensure that we don't make unnecessary requests or updates"""
        name = 'abc'
        self._set_args(dict(alias=name))
        fake = dict(alias=name, chap=False)
        with mock.patch(self.REQ_FUNC, return_value=(200, "")) as request:
            with mock.patch.object(IscsiTarget, 'target', new_callable=PropertyMock) as call:
                call.return_value = fake
                tgt = IscsiTarget()
                self.assertFalse(tgt.apply_iscsi_settings())
                self.assertFalse(request.called, msg="No update was expected!")

    def test_apply_iscsi_settings_fail(self):
        """Ensure we handle request failures cleanly"""
        self._set_args()
        fake = dict(alias='', chap=True)
        with self.assertRaisesRegexp(AnsibleFailJson, r".*?update.*"):
            with mock.patch(self.REQ_FUNC, side_effect=Exception) as request:
                with mock.patch.object(IscsiTarget, 'target', new_callable=PropertyMock) as call:
                    call.return_value = fake
                    tgt = IscsiTarget()
                    tgt.apply_iscsi_settings()

    def test_apply_target_changes(self):
        """Ensure that changes trigger an update."""
        self._set_args(dict(ping=True, unnamed_discovery=True))
        tgt = IscsiTarget()

        # CHAP is enabled
        fake = dict(ping=False, unnamed_discovery=False)

        # We don't care about the return here
        with mock.patch(self.REQ_FUNC, return_value=(200, "")) as request:
            with mock.patch.object(IscsiTarget, 'target', new_callable=PropertyMock) as call:
                call.return_value = fake
                self.assertTrue(tgt.apply_target_changes())
                self.assertTrue(request.called, msg="An update was expected!")

                # Retest with check_mode enabled
                tgt.check_mode = True
                request.reset_mock()
                self.assertTrue(tgt.apply_target_changes())
                self.assertFalse(request.called, msg="No update was expected in check_mode!")

    def test_apply_target_changes_no_change(self):
        """Ensure that we don't make unnecessary requests or updates"""
        self._set_args(dict(ping=True, unnamed_discovery=True))
        fake = dict(ping=True, unnamed_discovery=True)
        with mock.patch(self.REQ_FUNC, return_value=(200, "")) as request:
            with mock.patch.object(IscsiTarget, 'target', new_callable=PropertyMock) as call:
                call.return_value = fake
                tgt = IscsiTarget()
                self.assertFalse(tgt.apply_target_changes())
                self.assertFalse(request.called, msg="No update was expected!")

    def test_apply_target_changes_fail(self):
        """Ensure we handle request failures cleanly"""
        self._set_args()
        fake = dict(ping=False, unnamed_discovery=False)
        with self.assertRaisesRegexp(AnsibleFailJson, r".*?update.*"):
            with mock.patch(self.REQ_FUNC, side_effect=Exception) as request:
                with mock.patch.object(IscsiTarget, 'target', new_callable=PropertyMock) as call:
                    call.return_value = fake
                    tgt = IscsiTarget()
                    tgt.apply_target_changes()