1# Copyright: (c) 2018, Hewlett Packard Enterprise Development LP
2# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4from __future__ import (absolute_import, division, print_function)
5__metaclass__ = type
6
7import mock
8import sys
9sys.modules['hpe3par_sdk'] = mock.Mock()
10sys.modules['hpe3par_sdk.client'] = mock.Mock()
11sys.modules['hpe3parclient'] = mock.Mock()
12sys.modules['hpe3parclient.exceptions'] = mock.Mock()
13from ansible_collections.community.general.plugins.modules.storage.hpe3par import ss_3par_cpg
14from ansible_collections.community.general.plugins.module_utils.storage.hpe3par import hpe3par
15
16
17@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.client')
18@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.AnsibleModule')
19@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.create_cpg')
20def test_module_args(mock_create_cpg, mock_module, mock_client):
21    """
22    hpe3par CPG - test module arguments
23    """
24
25    PARAMS_FOR_PRESENT = {
26        'storage_system_ip': '192.168.0.1',
27        'storage_system_username': 'USER',
28        'storage_system_password': 'PASS',
29        'cpg_name': 'test_cpg',
30        'domain': 'test_domain',
31        'growth_increment': 32768,
32        'growth_increment_unit': 'MiB',
33        'growth_limit': 32768,
34        'growth_limit_unit': 'MiB',
35        'growth_warning': 32768,
36        'growth_warning_unit': 'MiB',
37        'raid_type': 'R6',
38        'set_size': 8,
39        'high_availability': 'MAG',
40        'disk_type': 'FC',
41        'state': 'present',
42        'secure': False
43    }
44    mock_module.params = PARAMS_FOR_PRESENT
45    mock_module.return_value = mock_module
46    mock_client.HPE3ParClient.login.return_value = True
47    mock_create_cpg.return_value = (True, True, "Created CPG successfully.")
48    ss_3par_cpg.main()
49    mock_module.assert_called_with(
50        argument_spec=hpe3par.cpg_argument_spec(),
51        required_together=[['raid_type', 'set_size']])
52
53
54@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.client')
55@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.AnsibleModule')
56@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.create_cpg')
57def test_main_exit_functionality_present_success_without_issue_attr_dict(mock_create_cpg, mock_module, mock_client):
58    """
59    hpe3par flash cache - success check
60    """
61    PARAMS_FOR_PRESENT = {
62        'storage_system_ip': '192.168.0.1',
63        'storage_system_name': '3PAR',
64        'storage_system_username': 'USER',
65        'storage_system_password': 'PASS',
66        'cpg_name': 'test_cpg',
67        'domain': 'test_domain',
68        'growth_increment': 32768,
69        'growth_increment_unit': 'MiB',
70        'growth_limit': 32768,
71        'growth_limit_unit': 'MiB',
72        'growth_warning': 32768,
73        'growth_warning_unit': 'MiB',
74        'raid_type': 'R6',
75        'set_size': 8,
76        'high_availability': 'MAG',
77        'disk_type': 'FC',
78        'state': 'present',
79        'secure': False
80    }
81    # This creates a instance of the AnsibleModule mock.
82    mock_module.params = PARAMS_FOR_PRESENT
83    mock_module.return_value = mock_module
84    instance = mock_module.return_value
85    mock_client.HPE3ParClient.login.return_value = True
86    mock_create_cpg.return_value = (
87        True, True, "Created CPG successfully.")
88    ss_3par_cpg.main()
89    # AnsibleModule.exit_json should be called
90    instance.exit_json.assert_called_with(
91        changed=True, msg="Created CPG successfully.")
92    # AnsibleModule.fail_json should not be called
93    assert instance.fail_json.call_count == 0
94
95
96@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.client')
97@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.AnsibleModule')
98@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.delete_cpg')
99def test_main_exit_functionality_absent_success_without_issue_attr_dict(mock_delete_cpg, mock_module, mock_client):
100    """
101    hpe3par flash cache - success check
102    """
103    PARAMS_FOR_DELETE = {
104        'storage_system_ip': '192.168.0.1',
105        'storage_system_name': '3PAR',
106        'storage_system_username': 'USER',
107        'storage_system_password': 'PASS',
108        'cpg_name': 'test_cpg',
109        'domain': None,
110        'growth_increment': None,
111        'growth_increment_unit': None,
112        'growth_limit': None,
113        'growth_limit_unit': None,
114        'growth_warning': None,
115        'growth_warning_unit': None,
116        'raid_type': None,
117        'set_size': None,
118        'high_availability': None,
119        'disk_type': None,
120        'state': 'absent',
121        'secure': False
122    }
123    # This creates a instance of the AnsibleModule mock.
124    mock_module.params = PARAMS_FOR_DELETE
125    mock_module.return_value = mock_module
126    instance = mock_module.return_value
127    mock_delete_cpg.return_value = (
128        True, True, "Deleted CPG test_cpg successfully.")
129    mock_client.HPE3ParClient.login.return_value = True
130    ss_3par_cpg.main()
131    # AnsibleModule.exit_json should be called
132    instance.exit_json.assert_called_with(
133        changed=True, msg="Deleted CPG test_cpg successfully.")
134    # AnsibleModule.fail_json should not be called
135    assert instance.fail_json.call_count == 0
136
137
138def test_convert_to_binary_multiple():
139    assert hpe3par.convert_to_binary_multiple(None) == -1
140    assert hpe3par.convert_to_binary_multiple('-1.0 MiB') == -1
141    assert hpe3par.convert_to_binary_multiple('-1.0GiB') == -1
142    assert hpe3par.convert_to_binary_multiple('1.0   MiB') == 1
143    assert hpe3par.convert_to_binary_multiple('1.5GiB') == 1.5 * 1024
144    assert hpe3par.convert_to_binary_multiple('1.5 TiB') == 1.5 * 1024 * 1024
145    assert hpe3par.convert_to_binary_multiple(' 1.5 TiB ') == 1.5 * 1024 * 1024
146
147
148@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.client')
149def test_validate_set_size(mock_client):
150    mock_client.HPE3ParClient.RAID_MAP = {'R0': {'raid_value': 1, 'set_sizes': [1]},
151                                          'R1': {'raid_value': 2, 'set_sizes': [2, 3, 4]},
152                                          'R5': {'raid_value': 3, 'set_sizes': [3, 4, 5, 6, 7, 8, 9]},
153                                          'R6': {'raid_value': 4, 'set_sizes': [6, 8, 10, 12, 16]}
154                                          }
155    raid_type = 'R0'
156    set_size = 1
157    assert ss_3par_cpg.validate_set_size(raid_type, set_size)
158
159    set_size = 2
160    assert not ss_3par_cpg.validate_set_size(raid_type, set_size)
161
162    raid_type = None
163    assert not ss_3par_cpg.validate_set_size(raid_type, set_size)
164
165
166@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.client')
167def test_cpg_ldlayout_map(mock_client):
168    mock_client.HPE3ParClient.PORT = 1
169    mock_client.HPE3ParClient.RAID_MAP = {'R0': {'raid_value': 1, 'set_sizes': [1]},
170                                          'R1': {'raid_value': 2, 'set_sizes': [2, 3, 4]},
171                                          'R5': {'raid_value': 3, 'set_sizes': [3, 4, 5, 6, 7, 8, 9]},
172                                          'R6': {'raid_value': 4, 'set_sizes': [6, 8, 10, 12, 16]}
173                                          }
174    ldlayout_dict = {'RAIDType': 'R6', 'HA': 'PORT'}
175    assert ss_3par_cpg.cpg_ldlayout_map(ldlayout_dict) == {
176        'RAIDType': 4, 'HA': 1}
177
178
179@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.client')
180def test_create_cpg(mock_client):
181    ss_3par_cpg.validate_set_size = mock.Mock(return_value=True)
182    ss_3par_cpg.cpg_ldlayout_map = mock.Mock(
183        return_value={'RAIDType': 4, 'HA': 1})
184
185    mock_client.HPE3ParClient.login.return_value = True
186    mock_client.HPE3ParClient.cpgExists.return_value = False
187    mock_client.HPE3ParClient.FC = 1
188    mock_client.HPE3ParClient.createCPG.return_value = True
189
190    assert ss_3par_cpg.create_cpg(mock_client.HPE3ParClient,
191                                  'test_cpg',
192                                  'test_domain',
193                                  '32768 MiB',
194                                  '32768 MiB',
195                                  '32768 MiB',
196                                  'R6',
197                                  8,
198                                  'MAG',
199                                  'FC'
200                                  ) == (True, True, "Created CPG %s successfully." % 'test_cpg')
201
202    mock_client.HPE3ParClient.cpgExists.return_value = True
203    assert ss_3par_cpg.create_cpg(mock_client.HPE3ParClient,
204                                  'test_cpg',
205                                  'test_domain',
206                                  '32768.0 MiB',
207                                  '32768.0 MiB',
208                                  '32768.0 MiB',
209                                  'R6',
210                                  8,
211                                  'MAG',
212                                  'FC'
213                                  ) == (True, False, 'CPG already present')
214
215    ss_3par_cpg.validate_set_size = mock.Mock(return_value=False)
216    assert ss_3par_cpg.create_cpg(mock_client.HPE3ParClient,
217                                  'test_cpg',
218                                  'test_domain',
219                                  '32768.0 MiB',
220                                  '32768 MiB',
221                                  '32768.0 MiB',
222                                  'R6',
223                                  3,
224                                  'MAG',
225                                  'FC'
226                                  ) == (False, False, 'Set size 3 not part of RAID set R6')
227
228
229@mock.patch('ansible_collections.community.general.plugins.modules.storage.hpe3par.ss_3par_cpg.client')
230def test_delete_cpg(mock_client):
231    mock_client.HPE3ParClient.login.return_value = True
232    mock_client.HPE3ParClient.cpgExists.return_value = True
233    mock_client.HPE3ParClient.FC = 1
234    mock_client.HPE3ParClient.deleteCPG.return_value = True
235
236    assert ss_3par_cpg.delete_cpg(mock_client.HPE3ParClient,
237                                  'test_cpg'
238                                  ) == (True, True, "Deleted CPG %s successfully." % 'test_cpg')
239
240    mock_client.HPE3ParClient.cpgExists.return_value = False
241
242    assert ss_3par_cpg.delete_cpg(mock_client.HPE3ParClient,
243                                  'test_cpg'
244                                  ) == (True, False, "CPG does not exist")
245    assert ss_3par_cpg.delete_cpg(mock_client.HPE3ParClient,
246                                  None
247                                  ) == (True, False, "CPG does not exist")
248