summaryrefslogtreecommitdiff
path: root/test/units/modules/network/nxos/storage/test_nxos_vsan.py
blob: 5e94601d72197e9486baa130506e99b8dc10ed01 (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
232
233
234
235
236
237
#!/usr/bin/env python
# Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

import pytest

from units.compat.mock import patch
from units.modules.utils import AnsibleFailJson
from ansible.modules.network.nxos.storage import nxos_vsan
from ansible.modules.network.nxos.storage.nxos_vsan import GetVsanInfoFromSwitch

from ..nxos_module import TestNxosModule, load_fixture, set_module_args


class TestNxosVsanModule(TestNxosModule):
    module = nxos_vsan

    def setUp(self):
        super(TestNxosVsanModule, self).setUp()
        module_path = 'ansible.modules.network.nxos.storage.nxos_vsan.'

        self.mock_run_commands = patch(module_path + 'run_commands')
        self.run_commands = self.mock_run_commands.start()

        self.mock_execute_show_vsan_cmd = patch(module_path + 'GetVsanInfoFromSwitch.execute_show_vsan_cmd')
        self.execute_show_vsan_cmd = self.mock_execute_show_vsan_cmd.start()

        self.mock_execute_show_vsanmemcmd = patch(module_path + 'GetVsanInfoFromSwitch.execute_show_vsan_mem_cmd')
        self.execute_show_vsanmem_cmd = self.mock_execute_show_vsanmemcmd.start()

        self.mock_load_config = patch(module_path + 'load_config')
        self.load_config = self.mock_load_config.start()

        self.maxDiff = None

    def tearDown(self):
        super(TestNxosVsanModule, self).tearDown()
        self.mock_run_commands.stop()
        self.execute_show_vsan_cmd.stop()
        self.execute_show_vsanmem_cmd.stop()
        self.mock_load_config.stop()

    def load_fixtures(self, commands=None, device=''):
        self.load_config.return_value = None

    def test_vsan_add_remove_but_present_in_switch(self):
        margs = {
            "vsan": [
                {
                    "interface": [
                        "fc1/1",
                        "port-channel 55"
                    ],
                    "id": 922,
                    "remove": False,
                    "name": "vsan-SAN-A"
                },
                {
                    "interface": [
                        "fc1/11",
                        "fc1/21",
                        "port-channel 56"
                    ],
                    "id": 923,
                    "remove": False,
                    "name": "vsan-SAN-B"
                },
                {
                    "id": 1923,
                    "remove": True,
                    "name": "vsan-SAN-Old"
                }
            ]
        }
        set_module_args(margs, True)
        self.execute_show_vsan_cmd.return_value = load_fixture('nxos_vsan', 'shvsan.cfg')
        self.execute_show_vsanmem_cmd.return_value = load_fixture('nxos_vsan', 'shvsanmem.cfg')
        result = self.execute_module(changed=False)
        self.assertEqual(result['commands'], [])

    def test_vsan_remove(self):
        margs = {
            "vsan": [
                {
                    "id": 922,
                    "remove": True
                },
                {
                    "id": 923,
                    "remove": True
                }
            ]
        }
        set_module_args(margs, True)
        self.execute_show_vsan_cmd.return_value = load_fixture('nxos_vsan', 'shvsan.cfg')
        self.execute_show_vsanmem_cmd.return_value = load_fixture('nxos_vsan', 'shvsanmem.cfg')
        result = self.execute_module(changed=True)
        self.assertEqual(result['commands'], ["terminal dont-ask"] + ["vsan database"] +
                         ["no vsan 922", "no vsan 923"] + ["no terminal dont-ask"])

    def test_vsan_add(self):
        margs = {
            "vsan": [
                {
                    "interface": [
                        "fc1/1",
                        "port-channel 55"
                    ],
                    "id": 924,
                    "name": "vsan-SAN-924"
                },
                {
                    "interface": [
                        "fc1/11",
                        "fc1/21",
                        "port-channel 56"
                    ],
                    "id": 925,
                    "name": "vsan-SAN-925"
                }
            ]
        }
        set_module_args(margs, True)
        self.execute_show_vsan_cmd.return_value = load_fixture('nxos_vsan', 'shvsan.cfg')
        self.execute_show_vsanmem_cmd.return_value = load_fixture('nxos_vsan', 'shvsanmem.cfg')
        result = self.execute_module(changed=True)
        self.assertEqual(result['commands'], ["terminal dont-ask"] + ["vsan database"] +
                         ["vsan 924", "vsan 924 name vsan-SAN-924", "no vsan 924 suspend",
                          "vsan 924 interface fc1/1", "vsan 924 interface port-channel 55"] +
                         ["vsan 925", "vsan 925 name vsan-SAN-925", "no vsan 925 suspend", "vsan 925 interface fc1/11",
                          "vsan 925 interface fc1/21", "vsan 925 interface port-channel 56"] +
                         ["no terminal dont-ask"])

    def test_vsan_suspend(self):
        margs = {
            "vsan": [
                {
                    "interface": [
                        "fc1/1",
                        "port-channel 55"
                    ],
                    "id": 924,
                    "name": "vsan-SAN-924"
                },
                {
                    "id": 925,
                    "name": "vsan-SAN-925",
                    "suspend": True
                }
            ]
        }
        set_module_args(margs, True)
        self.execute_show_vsan_cmd.return_value = load_fixture('nxos_vsan', 'shvsan.cfg')
        self.execute_show_vsanmem_cmd.return_value = load_fixture('nxos_vsan', 'shvsanmem.cfg')
        result = self.execute_module(changed=True)
        self.assertEqual(result['commands'], ["terminal dont-ask"] + ["vsan database"] +
                         ["vsan 924", "vsan 924 name vsan-SAN-924", "no vsan 924 suspend",
                          "vsan 924 interface fc1/1", "vsan 924 interface port-channel 55"] +
                         ["vsan 925", "vsan 925 name vsan-SAN-925", "vsan 925 suspend"] +
                         ["no terminal dont-ask"])

    def test_vsan_invalid_vsan(self):
        margs = {
            "vsan": [
                {
                    "id": 4096,
                    "name": "vsan-SAN-925",
                    "suspend": True
                }
            ]
        }
        set_module_args(margs, True)
        self.execute_show_vsan_cmd.return_value = load_fixture('nxos_vsan', 'shvsan.cfg')
        self.execute_show_vsanmem_cmd.return_value = load_fixture('nxos_vsan', 'shvsanmem.cfg')
        with pytest.raises(AnsibleFailJson) as errinfo:
            self.execute_module()
            testdata = errinfo.value.args[0]
            assert 'invalid vsan' in str(testdata['msg'])
            assert testdata['failed']

    def test_vsan_change_reserved_vsan(self):
        margs = {
            "vsan": [
                {
                    "id": 4094,
                    "name": "vsan-SAN-925",
                    "suspend": True
                }
            ]
        }
        set_module_args(margs, True)
        self.execute_show_vsan_cmd.return_value = load_fixture('nxos_vsan', 'shvsan.cfg')
        self.execute_show_vsanmem_cmd.return_value = load_fixture('nxos_vsan', 'shvsanmem.cfg')
        result = self.execute_module(changed=False)
        assert 'reserved vsan' in str(result['messages'])
        self.assertEqual(result['commands'], [])

    def test_vsan_add_int_existing_vsan(self):
        margs = {
            "vsan": [
                {
                    "interface": [
                        "fc1/1",
                        "fc1/40",
                        "port-channel 155"
                    ],
                    "id": 922,
                },
            ]
        }
        set_module_args(margs, True)
        self.execute_show_vsan_cmd.return_value = load_fixture('nxos_vsan', 'shvsan.cfg')
        self.execute_show_vsanmem_cmd.return_value = load_fixture('nxos_vsan', 'shvsanmem.cfg')
        result = self.execute_module(changed=True)
        self.assertEqual(result['commands'], ["terminal dont-ask"] + ["vsan database"] +
                         ["vsan 922 interface fc1/40", "vsan 922 interface port-channel 155"] +
                         ["no terminal dont-ask"])

    def test_vsan_remove_non_existing_vsan(self):
        margs = {
            "vsan": [
                {
                    "id": 1111,
                    "remove": True
                },
            ]
        }
        set_module_args(margs, True)
        self.execute_show_vsan_cmd.return_value = load_fixture('nxos_vsan', 'shvsan.cfg')
        self.execute_show_vsanmem_cmd.return_value = load_fixture('nxos_vsan', 'shvsanmem.cfg')
        result = self.execute_module(changed=False)
        self.assertEqual(result['commands'], [])
        assert 'no vsan' in str(result['messages'])