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


from ansible.modules.storage.netapp.netapp_e_global import GlobalSettings
from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args

__metaclass__ = type
from ansible.compat.tests import mock


class GlobalSettingsTest(ModuleTestCase):
    REQUIRED_PARAMS = {
        'api_username': 'rw',
        'api_password': 'password',
        'api_url': 'http://localhost',
        'ssid': '1',
    }
    REQ_FUNC = 'ansible.modules.storage.netapp.netapp_e_global.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_set_name(self):
        """Ensure we can successfully set the name"""
        self._set_args(dict(name="x"))

        expected = dict(name='y', status='online')
        namer = GlobalSettings()
        # Expecting an update
        with mock.patch(self.REQ_FUNC, return_value=(200, expected)) as req:
            with mock.patch.object(namer, 'get_name', return_value='y'):
                update = namer.update_name()
                self.assertTrue(update)
        # Expecting no update
        with mock.patch(self.REQ_FUNC, return_value=(200, expected)) as req:
            with mock.patch.object(namer, 'get_name', return_value='x'):
                update = namer.update_name()
                self.assertFalse(update)

        # Expecting an update, but no actual calls, since we're using check_mode=True
        namer.check_mode = True
        with mock.patch(self.REQ_FUNC, return_value=(200, expected)) as req:
            with mock.patch.object(namer, 'get_name', return_value='y'):
                update = namer.update_name()
                self.assertEquals(0, req.called)
                self.assertTrue(update)

    def test_get_name(self):
        """Ensure we can successfully set the name"""
        self._set_args()

        expected = dict(name='y', status='online')
        namer = GlobalSettings()

        with mock.patch(self.REQ_FUNC, return_value=(200, expected)) as req:
            name = namer.get_name()
            self.assertEquals(name, expected['name'])

    def test_get_name_fail(self):
        """Ensure we can successfully set the name"""
        self._set_args()

        expected = dict(name='y', status='offline')
        namer = GlobalSettings()

        with self.assertRaises(AnsibleFailJson):
            with mock.patch(self.REQ_FUNC, side_effect=Exception()) as req:
                name = namer.get_name()

        with self.assertRaises(AnsibleFailJson):
            with mock.patch(self.REQ_FUNC, return_value=(200, expected)) as req:
                update = namer.update_name()