Skip to content

Commit 9716171

Browse files
Fixing pyTest failure in UT (#723)
1 parent c989c56 commit 9716171

4 files changed

+30
-28
lines changed

tests/unit/plugins/modules/test_idrac_certificates.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -259,30 +259,30 @@ def test_get_idrac_service(
259259
idrac_redfish_mock_for_certs, params.get('res_id'))
260260
assert idrac_srv == params['idrac_srv']
261261

262-
def test_write_to_file(self, idrac_default_args):
262+
def test_write_to_file(self, idrac_default_args, mocker):
263+
temp_dir = 'XX/YY/'
263264
inv_dir = "invalid_temp_dir"
264265
idrac_default_args.update({"certificate_path": inv_dir})
265266
f_module = self.get_module_mock(params=idrac_default_args)
266267
with pytest.raises(Exception) as ex:
267268
self.module.write_to_file(f_module, {}, "dkey")
268269
assert ex.value.args[0] == f"Provided directory path '{inv_dir}' is not valid."
269-
temp_dir = tempfile.mkdtemp()
270-
os.chmod(temp_dir, 0o000)
270+
mocker.patch('os.path.exists', return_value=True)
271+
mocker.patch('os.access', return_value=False)
271272
idrac_default_args.update({"certificate_path": temp_dir})
272273
with pytest.raises(Exception) as ex:
273274
self.module.write_to_file(f_module, {}, "dkey")
274275
assert ex.value.args[0] == f"Provided directory path '{temp_dir}' is not writable. Please check if you have appropriate permissions."
275-
os.removedirs(temp_dir)
276276

277-
def test_upload_ssl_key(self, idrac_default_args):
277+
def test_upload_ssl_key(self, idrac_default_args, mocker):
278278
temp_ssl = tempfile.NamedTemporaryFile(delete=False)
279279
temp_ssl.write(b'ssl_key')
280280
temp_ssl.close()
281281
f_module = self.get_module_mock(params=idrac_default_args)
282282
with pytest.raises(Exception) as ex:
283283
self.module.upload_ssl_key(f_module, {}, {}, temp_ssl.name, "res_id")
284284
assert ex.value.args[0] == "Upload of SSL key not supported"
285-
os.chmod(temp_ssl.name, 0o000)
285+
mocker.patch('builtins.open', side_effect=OSError(0, "Permission denied"))
286286
with pytest.raises(Exception) as ex:
287287
self.module.upload_ssl_key(f_module, {}, {}, temp_ssl.name, "res_id")
288288
assert "Permission denied" in ex.value.args[0]

tests/unit/plugins/modules/test_ome_application_network_settings.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError
1919
from ansible.module_utils.urls import SSLValidationError
2020
from io import StringIO
21+
from copy import deepcopy
2122
from ansible.module_utils._text import to_text
2223
from ansible_collections.dellemc.openmanage.plugins.modules import ome_application_network_settings
2324
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule
@@ -177,7 +178,7 @@ def test_update_payload_no_change(self, params, ome_connection_mock_for_ns, ome_
177178
"api_timeout": 2
178179
}
179180
},
180-
"payload": responseData.get("value")
181+
"payload": deepcopy(responseData).get("value")
181182
}
182183
])
183184
def test_update_payload_timeout_change(self, params, ome_connection_mock_for_ns, ome_response_mock):
@@ -212,7 +213,7 @@ def test_update_payload_max_sessions_change(self, params, ome_connection_mock_fo
212213
"api_sessions": 90
213214
}
214215
},
215-
"payload": responseData.get("value")
216+
"payload": deepcopy(responseData).get("value")
216217
}
217218
])
218219
def test_update_payload_timeout_and_max_session_change(self, params, ome_connection_mock_for_ns, ome_response_mock):

tests/unit/plugins/modules/test_ome_device_mgmt_network.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import json
1616
from io import StringIO
17-
17+
from copy import deepcopy
1818
import pytest
1919
from ansible.module_utils._text import to_text
2020
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError
@@ -206,9 +206,9 @@ def test_validate_input_success(self, addr_param):
206206
f_module = self.get_module_mock(params=addr_param["in"])
207207
self.module.validate_input(f_module)
208208

209-
@pytest.mark.parametrize("param", [{"in": inp_param, "device": chassis, "enable_nic": False, "delay": 5,
209+
@pytest.mark.parametrize("param", [{"in": deepcopy(inp_param), "device": deepcopy(chassis), "enable_nic": False, "delay": 5,
210210
"diff": {'EnableNIC': False, 'Delay': 5}},
211-
{"in": inp_param, "device": chassis, "enable_nic": True,
211+
{"in": deepcopy(inp_param), "device": deepcopy(chassis), "enable_nic": True,
212212
"diff": {'StaticAlternateDNSServer': '2607:f2b1:f081:9:1c8c:f1c7:47e:f121',
213213
'StaticPreferredDNSServer': '2607:f2b1:f081:9:1c8c:f1c7:47e:f122',
214214
'StaticGateway': '0000::ffff', 'StaticSubnetMask': '255.255.255.0',
@@ -217,24 +217,24 @@ def test_validate_input_success(self, addr_param):
217217
'StaticPrefixLength': 0, 'EnableIPv6': True, 'NetworkSpeed': '10_MB',
218218
'DnsName': 'openmanage-enterprise', 'AutoNegotiation': False,
219219
'DnsDomainName': 'localdomain', 'MgmtVLANId': 0}},
220-
{"in": {"ipv6_configuration": ipv6_configuration}, "device": chassis,
220+
{"in": {"ipv6_configuration": ipv6_configuration}, "device": deepcopy(chassis),
221221
"enable_nic": True,
222222
"diff": {'StaticAlternateDNSServer': '2607:f2b1:f081:9:1c8c:f1c7:47e:f121',
223223
'StaticPreferredDNSServer': '2607:f2b1:f081:9:1c8c:f1c7:47e:f122',
224224
'StaticGateway': '0000::ffff',
225225
'StaticIPv6Address': '2607:f2b1:f081:9:1c8c:f1c7:47e:f120',
226226
'StaticPrefixLength': 0, 'EnableIPv6': True}},
227-
{"in": {"ipv4_configuration": ipv4_configuration}, "device": chassis,
227+
{"in": {"ipv4_configuration": ipv4_configuration}, "device": deepcopy(chassis),
228228
"enable_nic": True,
229229
"diff": {'StaticAlternateDNSServer': '192.168.11.3',
230230
'StaticPreferredDNSServer': '192.168.11.2',
231231
'StaticGateway': '192.168.11.1', 'StaticSubnetMask': '255.255.255.0',
232232
'StaticIPAddress': '192.168.11.20'}},
233-
{"in": {"dns_configuration": dns_configuration}, "device": chassis,
233+
{"in": {"dns_configuration": dns_configuration}, "device": deepcopy(chassis),
234234
"enable_nic": True,
235235
"diff": {'NetworkSpeed': '10_MB', 'DnsName': 'openmanage-enterprise',
236236
'AutoNegotiation': False, 'DnsDomainName': 'localdomain'}},
237-
{"in": {"management_vlan": management_vlan}, "device": chassis,
237+
{"in": {"management_vlan": management_vlan}, "device": deepcopy(chassis),
238238
"enable_nic": True,
239239
"diff": {'MgmtVLANId': 0}}])
240240
def test_update_chassis_payload_success(self, param):
@@ -245,9 +245,9 @@ def test_update_chassis_payload_success(self, param):
245245
diff = self.module.update_chassis_payload(f_module, param["device"])
246246
assert diff == param.get("diff")
247247

248-
@pytest.mark.parametrize("param", [{"in": inp_param, "device": server, "enable_nic": False,
248+
@pytest.mark.parametrize("param", [{"in": deepcopy(inp_param), "device": deepcopy(server), "enable_nic": False,
249249
"diff": {'enableNIC': 'Disabled'}},
250-
{"in": inp_param, "device": server, "enable_nic": True,
250+
{"in": deepcopy(inp_param), "device": deepcopy(server), "enable_nic": True,
251251
"diff": {'staticIPAddressIPv4': '192.168.11.20',
252252
'staticSubnetMaskIPv4': '255.255.255.0',
253253
'staticGatewayIPv4': '192.168.11.1',
@@ -261,20 +261,20 @@ def test_update_chassis_payload_success(self, param):
261261
'staticPrefixLengthIPv6': 0, 'staticGatewayIPv6': '0000::ffff',
262262
'enableIPv6': 'Enabled',
263263
'vlanId': 0}},
264-
{"in": {"ipv6_configuration": ipv6_configuration}, "device": server,
264+
{"in": {"ipv6_configuration": ipv6_configuration}, "device": deepcopy(server),
265265
"enable_nic": True,
266266
"diff": {'staticPreferredDNSIPv6': '2607:f2b1:f081:9:1c8c:f1c7:47e:f122',
267267
'staticAlternateDNSIPv6': '2607:f2b1:f081:9:1c8c:f1c7:47e:f121',
268268
'staticIPAddressIPv6': '2607:f2b1:f081:9:1c8c:f1c7:47e:f120',
269269
'staticPrefixLengthIPv6': 0, 'staticGatewayIPv6': '0000::ffff',
270270
'enableAutoConfigurationIPv6': 'Disabled', 'enableIPv6': 'Enabled'}},
271-
{"in": {"ipv4_configuration": ipv4_configuration}, "device": server,
271+
{"in": {"ipv4_configuration": ipv4_configuration}, "device": deepcopy(server),
272272
"enable_nic": True, "diff": {'staticIPAddressIPv4': '192.168.11.20',
273273
'staticSubnetMaskIPv4': '255.255.255.0',
274274
'staticGatewayIPv4': '192.168.11.1',
275275
'staticPreferredDNSIPv4': '192.168.11.2',
276276
'staticAlternateDNSIPv4': '192.168.11.3'}},
277-
{"in": {"management_vlan": management_vlan}, "device": server,
277+
{"in": {"management_vlan": management_vlan}, "device": deepcopy(server),
278278
"enable_nic": True, "diff": {'vlanEnable': 'Enabled', 'vlanId': 0}}
279279
])
280280
def test_update_server_payload_success(self, param):
@@ -284,30 +284,30 @@ def test_update_server_payload_success(self, param):
284284
diff = self.module.update_server_payload(f_module, param["device"])
285285
assert diff == param.get("diff")
286286

287-
@pytest.mark.parametrize("param", [{"in": inp_param, "device": iom, "enable_nic": False,
287+
@pytest.mark.parametrize("param", [{"in": deepcopy(inp_param), "device": deepcopy(iom), "enable_nic": False,
288288
"diff": {'StaticGateway': '0000::ffff', 'StaticIPAddress': '192.168.11.20',
289289
'StaticSubnetMask': '255.255.255.0', 'EnableDHCP': False,
290290
'EnableMgmtVLANId': True,
291291
'StaticPrefixLength': 0,
292292
'StaticIPv6Address': '2607:f2b1:f081:9:1c8c:f1c7:47e:f120',
293293
'MgmtVLANId': 0, 'SecondaryDNS': '192.96.20.182',
294294
'PrimaryDNS': '192.96.20.181'}},
295-
{"in": inp_param, "device": iom, "enable_nic": True,
295+
{"in": deepcopy(inp_param), "device": deepcopy(iom), "enable_nic": True,
296296
"diff": {'StaticGateway': '0000::ffff', 'StaticIPAddress': '192.168.11.20',
297297
'StaticSubnetMask': '255.255.255.0', 'EnableDHCP': False,
298298
'StaticPrefixLength': 0, 'EnableMgmtVLANId': True,
299299
'StaticIPv6Address': '2607:f2b1:f081:9:1c8c:f1c7:47e:f120',
300300
'MgmtVLANId': 0, 'SecondaryDNS': '192.96.20.182',
301301
'PrimaryDNS': '192.96.20.181'}},
302-
{"in": {"ipv6_configuration": ipv6_configuration}, "device": iom,
302+
{"in": {"ipv6_configuration": ipv6_configuration}, "device": deepcopy(iom),
303303
"enable_nic": True, "diff": {'StaticGateway': '0000::ffff',
304304
'StaticPrefixLength': 0,
305305
'StaticIPv6Address': '2607:f2b1:f081:9:1c8c:f1c7:47e:f120'}},
306-
{"in": {"ipv4_configuration": ipv4_configuration}, "device": iom,
306+
{"in": {"ipv4_configuration": ipv4_configuration}, "device": deepcopy(iom),
307307
"enable_nic": True,
308308
"diff": {'StaticGateway': '192.168.11.1', 'StaticIPAddress': '192.168.11.20',
309309
'StaticSubnetMask': '255.255.255.0', 'EnableDHCP': False}},
310-
{"in": {"management_vlan": management_vlan}, "device": iom,
310+
{"in": {"management_vlan": management_vlan}, "device": deepcopy(iom),
311311
"enable_nic": True, "diff": {'EnableMgmtVLANId': True, 'MgmtVLANId': 0}}
312312
])
313313
def test_update_iom_payload_success(self, param):

tests/unit/plugins/modules/test_ome_network_vlan_info.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import pytest
1616
import json
17+
from copy import deepcopy
1718
from ansible_collections.dellemc.openmanage.plugins.modules import ome_network_vlan_info
1819
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule
1920
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
@@ -87,7 +88,7 @@ def ome_connection_network_vlan_info_mock(self, mocker, ome_response_mock):
8788

8889
def test_get_network_vlan_info_success_case(self, mocker, ome_default_args, ome_connection_network_vlan_info_mock,
8990
ome_response_mock):
90-
ome_response_mock.json_data = response
91+
ome_response_mock.json_data = deepcopy(response)
9192
ome_response_mock.status_code = 200
9293
mocker.patch(
9394
MODULE_PATH + 'ome_network_vlan_info.get_network_type_and_qos_type_information',
@@ -101,7 +102,7 @@ def test_get_network_vlan_info_by_id_success_case(self, mocker, ome_default_args
101102
ome_connection_network_vlan_info_mock, ome_response_mock):
102103
ome_default_args.update({"id": 20057})
103104
ome_response_mock.success = True
104-
ome_response_mock.json_data = response
105+
ome_response_mock.json_data = deepcopy(response)
105106
ome_response_mock.status_code = 200
106107
mocker.patch(
107108
MODULE_PATH + 'ome_network_vlan_info.get_network_type_and_qos_type_information',
@@ -115,7 +116,7 @@ def test_get_network_vlan_info_by_name_success_case(self, mocker, ome_default_ar
115116
ome_connection_network_vlan_info_mock, ome_response_mock):
116117
ome_default_args.update({"name": "Logical Network - 1"})
117118
ome_response_mock.success = True
118-
ome_response_mock.json_data = response
119+
ome_response_mock.json_data = deepcopy(response)
119120
ome_response_mock.status_code = 200
120121
mocker.patch(
121122
MODULE_PATH + 'ome_network_vlan_info.get_network_type_and_qos_type_information',

0 commit comments

Comments
 (0)