1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-26 21:52:48 +00:00

[PR #9838/a1781d09 backport][stable-10] Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9839)

Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9838)

Make set_module_args() a context manager, and remove copies of set_module_args().

Prepares for Data Tagging.

(cherry picked from commit a1781d09dd)

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
patchback[bot] 2025-03-07 07:27:55 +01:00 committed by GitHub
parent cd0ca389ed
commit 7d45b678e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 4043 additions and 4302 deletions

View file

@ -77,140 +77,140 @@ class TestWdcRedfishInfo(unittest.TestCase):
def test_module_fail_when_required_args_missing(self):
with self.assertRaises(AnsibleFailJson):
set_module_args({})
module.main()
with set_module_args({}):
module.main()
def test_module_fail_when_unknown_category(self):
with self.assertRaises(AnsibleFailJson):
set_module_args({
with set_module_args({
'category': 'unknown',
'command': 'SimpleUpdateStatus',
'username': 'USERID',
'password': 'PASSW0RD=21',
'ioms': [],
})
module.main()
}):
module.main()
def test_module_fail_when_unknown_command(self):
with self.assertRaises(AnsibleFailJson):
set_module_args({
with set_module_args({
'category': 'Update',
'command': 'unknown',
'username': 'USERID',
'password': 'PASSW0RD=21',
'ioms': [],
})
module.main()
}):
module.main()
def test_module_simple_update_status_pass(self):
set_module_args({
with set_module_args({
'category': 'Update',
'command': 'SimpleUpdateStatus',
'username': 'USERID',
'password': 'PASSW0RD=21',
'ioms': ["example1.example.com"],
})
}):
def mock_simple_update_status(*args, **kwargs):
return {
"ret": True,
"data": {
"Description": "Ready for FW update",
"ErrorCode": 0,
"EstimatedRemainingMinutes": 0,
"StatusCode": 0
}
}
def mocked_string_response(*args, **kwargs):
return "mockedUrl"
def empty_return(*args, **kwargs):
return {"ret": True}
with patch.multiple(module.WdcRedfishUtils,
_simple_update_status_uri=mocked_string_response,
_find_updateservice_resource=empty_return,
_find_updateservice_additional_uris=empty_return,
get_request=mock_simple_update_status):
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
module.main()
redfish_facts = get_redfish_facts(ansible_exit_json)
self.assertEqual(mock_simple_update_status()["data"],
redfish_facts["simple_update_status"]["entries"])
def test_module_simple_update_status_updateservice_resource_not_found(self):
set_module_args({
'category': 'Update',
'command': 'SimpleUpdateStatus',
'username': 'USERID',
'password': 'PASSW0RD=21',
'ioms': ["example1.example.com"],
})
with patch.object(module.WdcRedfishUtils, 'get_request') as mock_get_request:
mock_get_request.return_value = {
"ret": True,
"data": {} # Missing UpdateService property
}
with self.assertRaises(AnsibleFailJson) as ansible_exit_json:
module.main()
self.assertEqual("UpdateService resource not found",
get_exception_message(ansible_exit_json))
def test_module_simple_update_status_service_does_not_support_simple_update(self):
set_module_args({
'category': 'Update',
'command': 'SimpleUpdateStatus',
'username': 'USERID',
'password': 'PASSW0RD=21',
'ioms': ["example1.example.com"],
})
def mock_get_request_function(uri):
mock_url_string = "mockURL"
if mock_url_string in uri:
def mock_simple_update_status(*args, **kwargs):
return {
"ret": True,
"data": {
"Actions": { # No #UpdateService.SimpleUpdate
}
"Description": "Ready for FW update",
"ErrorCode": 0,
"EstimatedRemainingMinutes": 0,
"StatusCode": 0
}
}
else:
return {
"ret": True,
"data": mock_url_string
}
with patch.object(module.WdcRedfishUtils, 'get_request') as mock_get_request:
mock_get_request.side_effect = mock_get_request_function
with self.assertRaises(AnsibleFailJson) as ansible_exit_json:
module.main()
self.assertEqual("UpdateService resource not found",
get_exception_message(ansible_exit_json))
def mocked_string_response(*args, **kwargs):
return "mockedUrl"
def test_module_simple_update_status_service_does_not_support_fw_activate(self):
set_module_args({
def empty_return(*args, **kwargs):
return {"ret": True}
with patch.multiple(module.WdcRedfishUtils,
_simple_update_status_uri=mocked_string_response,
_find_updateservice_resource=empty_return,
_find_updateservice_additional_uris=empty_return,
get_request=mock_simple_update_status):
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
module.main()
redfish_facts = get_redfish_facts(ansible_exit_json)
self.assertEqual(mock_simple_update_status()["data"],
redfish_facts["simple_update_status"]["entries"])
def test_module_simple_update_status_updateservice_resource_not_found(self):
with set_module_args({
'category': 'Update',
'command': 'SimpleUpdateStatus',
'username': 'USERID',
'password': 'PASSW0RD=21',
'ioms': ["example1.example.com"],
})
}):
with patch.object(module.WdcRedfishUtils, 'get_request') as mock_get_request:
mock_get_request.return_value = {
"ret": True,
"data": {} # Missing UpdateService property
}
with self.assertRaises(AnsibleFailJson) as ansible_exit_json:
module.main()
self.assertEqual("UpdateService resource not found",
get_exception_message(ansible_exit_json))
def mock_get_request_function(uri):
if uri.endswith("/redfish/v1") or uri.endswith("/redfish/v1/"):
return MOCK_SUCCESSFUL_RESPONSE_WITH_UPDATE_SERVICE_RESOURCE
elif uri.endswith("/mockedUrl"):
return MOCK_SUCCESSFUL_HTTP_EMPTY_RESPONSE
elif uri.endswith("/UpdateService"):
return MOCK_SUCCESSFUL_RESPONSE_WITH_SIMPLE_UPDATE_BUT_NO_FW_ACTIVATE
else:
raise RuntimeError("Illegal call to get_request in test: " + uri)
def test_module_simple_update_status_service_does_not_support_simple_update(self):
with set_module_args({
'category': 'Update',
'command': 'SimpleUpdateStatus',
'username': 'USERID',
'password': 'PASSW0RD=21',
'ioms': ["example1.example.com"],
}):
with patch("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils.get_request") as mock_get_request:
mock_get_request.side_effect = mock_get_request_function
with self.assertRaises(AnsibleFailJson) as ansible_exit_json:
module.main()
self.assertEqual("Service does not support FWActivate",
get_exception_message(ansible_exit_json))
def mock_get_request_function(uri):
mock_url_string = "mockURL"
if mock_url_string in uri:
return {
"ret": True,
"data": {
"Actions": { # No #UpdateService.SimpleUpdate
}
}
}
else:
return {
"ret": True,
"data": mock_url_string
}
with patch.object(module.WdcRedfishUtils, 'get_request') as mock_get_request:
mock_get_request.side_effect = mock_get_request_function
with self.assertRaises(AnsibleFailJson) as ansible_exit_json:
module.main()
self.assertEqual("UpdateService resource not found",
get_exception_message(ansible_exit_json))
def test_module_simple_update_status_service_does_not_support_fw_activate(self):
with set_module_args({
'category': 'Update',
'command': 'SimpleUpdateStatus',
'username': 'USERID',
'password': 'PASSW0RD=21',
'ioms': ["example1.example.com"],
}):
def mock_get_request_function(uri):
if uri.endswith("/redfish/v1") or uri.endswith("/redfish/v1/"):
return MOCK_SUCCESSFUL_RESPONSE_WITH_UPDATE_SERVICE_RESOURCE
elif uri.endswith("/mockedUrl"):
return MOCK_SUCCESSFUL_HTTP_EMPTY_RESPONSE
elif uri.endswith("/UpdateService"):
return MOCK_SUCCESSFUL_RESPONSE_WITH_SIMPLE_UPDATE_BUT_NO_FW_ACTIVATE
else:
raise RuntimeError("Illegal call to get_request in test: " + uri)
with patch("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils.get_request") as mock_get_request:
mock_get_request.side_effect = mock_get_request_function
with self.assertRaises(AnsibleFailJson) as ansible_exit_json:
module.main()
self.assertEqual("Service does not support FWActivate",
get_exception_message(ansible_exit_json))