Debian Codegraph @

ansible_collections/netapp_eseries/santricity/tests/unit/modules/test_na_santricity_discover.py

raw symbols SCIP
1 # (c) 2020, NetApp, Inc
2 # BSD-3 Clause (see COPYING or https://opensource.org/licenses/BSD-3-Clause)
3 from __future__ import absolute_import, division, print_function
4 __metaclass__ = type
5
6 from contextlib import contextmanager
7 from ansible.module_utils.testing import patch_module_args
8 from ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_discover import NetAppESeriesDiscover
9 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
10 AnsibleFailJson, AnsibleExitJson, ModuleTestCase
11 )
12 from ansible_collections.community.internal_test_tools.tests.unit.compat import mock
13
14
15 class AlertsTest(ModuleTestCase):
16 REQUIRED_PARAMS = {"subnet_mask": "192.168.1.0/24"}
17 BASE_REQ_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_discover.request'
18 SLEEP_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_discover.sleep'
19
20 @contextmanager
21 def _set_args(self, args=None):
22 module_args = self.REQUIRED_PARAMS.copy()
23 if args is not None:
24 module_args.update(args)
25 with patch_module_args(module_args):
26 yield
27
28 def test_valid_options_pass(self):
29 """Verify constructor accepts valid options."""
30 options_list = [{"ports": [1, 8443]},
31 {"ports": [8080, 65535]},
32 {"ports": [8443], "proxy_url": "https://192.168.1.1:8443/devmgr/v2/", "proxy_username": "admin", "proxy_password": "adminpass"},
33 {"ports": [8443], "proxy_url": "https://192.168.1.1:8443/devmgr/v2/", "proxy_username": "admin", "proxy_password": "adminpass",
34 "prefer_embedded": True},
35 {"ports": [8443], "proxy_url": "https://192.168.1.1:8443/devmgr/v2/", "proxy_username": "admin", "proxy_password": "adminpass",
36 "prefer_embedded": False},
37 {"ports": [8443], "proxy_url": "https://192.168.1.1:8443/devmgr/v2/", "proxy_username": "admin", "proxy_password": "adminpass",
38 "proxy_validate_certs": True},
39 {"ports": [8443], "proxy_url": "https://192.168.1.1:8443/devmgr/v2/", "proxy_username": "admin", "proxy_password": "adminpass",
40 "proxy_validate_certs": False}]
41
42 for options in options_list:
43 with self._set_args(options):
44 discover = NetAppESeriesDiscover()
45
46 def test_valid_options_fail(self):
47 """Verify constructor throws expected exceptions."""
48 # options_list = [{"ports": [0, 8443]}, {"ports": [8080, 65536]}, {"ports": [8080, "port"]}, {"ports": [8080, -10]}, {"ports": [8080, 70000]}]
49 options_list = [{"ports": [0, 8443]}]
50
51 for options in options_list:
52 with self._set_args(options):
53 with self.assertRaisesRegex(
54 AnsibleFailJson,
55 "Invalid port! Ports must be positive numbers between 0 and 65536."
56 ):
57 discover = NetAppESeriesDiscover()
58
59 def test_check_ip_address_pass(self):
60 """Verify check_ip_address successfully completes."""
61 with self._set_args():
62 with mock.patch(
63 self.BASE_REQ_FUNC,
64 return_value=(200, {"chassisSerialNumber": "012345678901", "storageArrayLabel": "array_label"})
65 ):
66 discover = NetAppESeriesDiscover()
67 discover.check_ip_address(discover.systems_found, "192.168.1.100")
68
69 self.assertEqual(
70 discover.systems_found,
71 {
72 "012345678901": {
73 "api_urls": ["https://192.168.1.100:8443/"],
74 "label": "array_label",
75 "addresses": [],
76 "proxy_required": False,
77 "proxy_ssid": ""
78 }
79 }
80 )
81
82 for ports in [{"ports": [8080]}, {"ports": [8443]}]:
83 with self._set_args(ports):
84 port = ports["ports"][0]
85 url_protocol = "https"
86 if port == 8080:
87 url_protocol = "http"
88
89 with mock.patch(
90 self.BASE_REQ_FUNC,
91 side_effect=[
92 (404, None),
93 (401, None),
94 (200, {"sa": {"saData": {
95 "chassisSerialNumber": "012345678901",
96 "storageArrayLabel": "array_label"
97 }}})
98 ]
99 ):
100 discover = NetAppESeriesDiscover()
101 discover.check_ip_address(discover.systems_found, "192.168.1.101")
102
103 self.assertEqual(
104 discover.systems_found,
105 {
106 "012345678901": {
107 "api_urls": [f"{url_protocol}://192.168.1.101:{port}/"],
108 "label": "array_label",
109 "addresses": [],
110 "proxy_required": False,
111 "proxy_ssid": ""
112 }
113 }
114 )
115
116 def test_no_proxy_discover_pass(self):
117 """Verify no_proxy_discover completes successfully."""
118 with self._set_args():
119 discover = NetAppESeriesDiscover()
120 discover.check_ip_address = None
121 discover.no_proxy_discover()
122
123 def test_verify_proxy_service_pass(self):
124 """Verify verify_proxy_service completes successfully."""
125 with self._set_args({"proxy_url": "https://192.168.1.200", "proxy_username": "admin", "proxy_password": "adminpass"}):
126 discover = NetAppESeriesDiscover()
127 with mock.patch(self.BASE_REQ_FUNC, return_value=(200, {"runningAsProxy": True})):
128 discover.verify_proxy_service()
129
130 def test_verify_proxy_service_fail(self):
131 """Verify verify_proxy_service throws expected exception."""
132 with self._set_args({"proxy_url": "https://192.168.1.200", "proxy_username": "admin", "proxy_password": "adminpass"}):
133 discover = NetAppESeriesDiscover()
134 with self.assertRaisesRegex(AnsibleFailJson, "Web Services is not running as a proxy!"):
135 with mock.patch(self.BASE_REQ_FUNC, return_value=(200, {"runningAsProxy": False})):
136 discover.verify_proxy_service()
137
138 with self._set_args({"proxy_url": "https://192.168.1.200", "proxy_username": "admin", "proxy_password": "adminpass"}):
139 discover = NetAppESeriesDiscover()
140 with self.assertRaisesRegex(AnsibleFailJson, "Proxy is not available! Check proxy_url."):
141 with mock.patch(self.BASE_REQ_FUNC, return_value=Exception()):
142 discover.verify_proxy_service()
143
144 def test_test_systems_found_pass(self):
145 """Verify test_systems_found adds to systems_found dictionary."""
146 with self._set_args({
147 "proxy_url": "https://192.168.1.200",
148 "proxy_username": "admin",
149 "proxy_password": "adminpass",
150 "prefer_embedded": True
151 }):
152 discover = NetAppESeriesDiscover()
153 with mock.patch(
154 self.BASE_REQ_FUNC,
155 return_value=(200, {"runningAsProxy": True})
156 ):
157 discover.test_systems_found(
158 discover.systems_found,
159 "012345678901",
160 "array_label", ["192.168.1.100", "192.168.1.102"]
161 )
162
163 self.assertEqual(
164 discover.systems_found,
165 {
166 "012345678901": {
167 "api_urls": [
168 "https://192.168.1.100:8443/devmgr/v2/",
169 "https://192.168.1.102:8443/devmgr/v2/"
170 ],
171 "label": "array_label",
172 "addresses": ["192.168.1.100", "192.168.1.102"],
173 "proxy_required": False,
174 "proxy_ssid": ""
175 }
176 }
177 )
178
179 def test_proxy_discover_pass(self):
180 """Verify proxy_discover completes successfully."""
181 with self._set_args({"subnet_mask": "192.168.1.0/30", "proxy_url": "https://192.168.1.200", "proxy_username": "admin", "proxy_password": "adminpass"}):
182 discover = NetAppESeriesDiscover()
183 discover.verify_proxy_service = lambda: None
184 with mock.patch(self.SLEEP_FUNC, return_value=None):
185 with mock.patch(
186 self.BASE_REQ_FUNC,
187 side_effect=[
188 (200, {"requestId": "1"}),
189 (200, {"discoverProcessRunning": True}),
190 (200, {
191 "discoverProcessRunning": False,
192 "storageSystems": [{
193 "controllers": [{"ipAddresses": ["192.168.1.100", "192.168.1.102"]}],
194 "supportedManagementPorts": ["https"],
195 "serialNumber": "012345678901",
196 "label": "array_label"
197 }]
198 })
199 ]
200 ):
201 discover.proxy_discover()
202
203 with self._set_args({"subnet_mask": "192.168.1.0/30", "proxy_url": "https://192.168.1.200", "proxy_username": "admin", "proxy_password": "adminpass"}):
204 discover = NetAppESeriesDiscover()
205 discover.verify_proxy_service = lambda: None
206 with mock.patch(self.SLEEP_FUNC, return_value=None):
207 with mock.patch(
208 self.BASE_REQ_FUNC,
209 side_effect=[
210 (200, {"requestId": "1"}),
211 (200, {"discoverProcessRunning": True}),
212 (200, {
213 "discoverProcessRunning": False,
214 "storageSystems": [{
215 "controllers": [{"ipAddresses": ["192.168.1.100", "192.168.1.102"]}],
216 "supportedManagementPorts": [],
217 "serialNumber": "012345678901",
218 "label": "array_label"
219 }]
220 })
221 ]
222 ):
223 discover.proxy_discover()
224
225 def test_proxy_discover_fail(self):
226 """Verify proxy_discover throws expected exceptions."""
227 with self._set_args({"subnet_mask": "192.168.1.0/30", "proxy_url": "https://192.168.1.200", "proxy_username": "admin", "proxy_password": "adminpass"}):
228 discover = NetAppESeriesDiscover()
229 discover.verify_proxy_service = lambda: None
230 with self.assertRaisesRegex(AnsibleFailJson, "Failed to initiate array discovery."):
231 with mock.patch(self.SLEEP_FUNC, return_value=None):
232 with mock.patch(self.BASE_REQ_FUNC, return_value=Exception()):
233 discover.proxy_discover()
234
235 with self._set_args({"subnet_mask": "192.168.1.0/30", "proxy_url": "https://192.168.1.200", "proxy_username": "admin", "proxy_password": "adminpass"}):
236 discover = NetAppESeriesDiscover()
237 discover.verify_proxy_service = lambda: None
238 with self.assertRaisesRegex(AnsibleFailJson, "Failed to get the discovery results."):
239 with mock.patch(self.SLEEP_FUNC, return_value=None):
240 with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"requestId": "1"}), Exception()]):
241 discover.proxy_discover()
242
243 with self._set_args({"subnet_mask": "192.168.1.0/30", "proxy_url": "https://192.168.1.200", "proxy_username": "admin", "proxy_password": "adminpass"}):
244 discover = NetAppESeriesDiscover()
245 discover.verify_proxy_service = lambda: None
246 with self.assertRaisesRegex(AnsibleFailJson, "Timeout waiting for array discovery process."):
247 with mock.patch(self.SLEEP_FUNC, return_value=None):
248 with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"requestId": "1"})] + [(200, {"discoverProcessRunning": True})] * 300):
249 discover.proxy_discover()
250
251 def test_discover_pass(self):
252 """Verify discover successfully completes."""
253 with self._set_args({
254 "subnet_mask": "192.168.1.0/30",
255 "proxy_url": "https://192.168.1.200",
256 "proxy_username": "admin",
257 "proxy_password": "adminpass"
258 }):
259 with mock.patch(
260 self.BASE_REQ_FUNC,
261 side_effect=[
262 (200, {
263 "012345678901": {
264 "api_urls": ["https://192.168.1.200:8443/"],
265 "label": "array_label",
266 "addresses": [],
267 "proxy_required": True,
268 "proxy_ssid": "1"
269 }
270 })
271 ]
272 ):
273 discover = NetAppESeriesDiscover()
274 discover.proxy_discover = lambda: None
275 with self.assertRaisesRegex(AnsibleExitJson, "Discover process complete."):
276 discover.discover()
277
278 with self._set_args():
279 discover = NetAppESeriesDiscover()
280 discover.no_proxy_discover = lambda: None
281 with self.assertRaisesRegex(AnsibleExitJson, "Discover process complete."):
282 discover.discover()