mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-29 03:30:22 -07:00
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.
This commit is contained in:
parent
402f725424
commit
a1781d09dd
84 changed files with 4043 additions and 4302 deletions
|
@ -309,30 +309,30 @@ class TestWdcRedfishCommand(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': 'FWActivate',
|
||||
'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_chassis_power_mode_low(self):
|
||||
"""Test setting chassis power mode to low (happy path)."""
|
||||
|
@ -344,15 +344,15 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
'resource_id': 'Enclosure',
|
||||
'baseuri': 'example.com'
|
||||
}
|
||||
set_module_args(module_args)
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
with set_module_args(module_args):
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
|
||||
def test_module_chassis_power_mode_normal_when_already_normal(self):
|
||||
"""Test setting chassis power mode to normal when it already is. Verify we get changed=False."""
|
||||
|
@ -364,14 +364,14 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
'resource_id': 'Enclosure',
|
||||
'baseuri': 'example.com'
|
||||
}
|
||||
set_module_args(module_args)
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertFalse(is_changed(ansible_exit_json))
|
||||
with set_module_args(module_args):
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertFalse(is_changed(ansible_exit_json))
|
||||
|
||||
def test_module_chassis_power_mode_invalid_command(self):
|
||||
"""Test that we get an error when issuing an invalid PowerMode command."""
|
||||
|
@ -383,14 +383,14 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
'resource_id': 'Enclosure',
|
||||
'baseuri': 'example.com'
|
||||
}
|
||||
set_module_args(module_args)
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request):
|
||||
with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
|
||||
module.main()
|
||||
expected_error_message = "Invalid Command 'PowerModeExtraHigh'"
|
||||
self.assertIn(expected_error_message,
|
||||
get_exception_message(ansible_fail_json))
|
||||
with set_module_args(module_args):
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request):
|
||||
with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
|
||||
module.main()
|
||||
expected_error_message = "Invalid Command 'PowerModeExtraHigh'"
|
||||
self.assertIn(expected_error_message,
|
||||
get_exception_message(ansible_fail_json))
|
||||
|
||||
def test_module_enclosure_led_indicator_on(self):
|
||||
"""Test turning on a valid LED indicator (in this case we use the Enclosure resource)."""
|
||||
|
@ -402,16 +402,15 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"resource_id": "Enclosure",
|
||||
"baseuri": "example.com"
|
||||
}
|
||||
set_module_args(module_args)
|
||||
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
with set_module_args(module_args):
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
|
||||
def test_module_invalid_resource_led_indicator_on(self):
|
||||
"""Test turning LED on for an invalid resource id."""
|
||||
|
@ -423,16 +422,15 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"resource_id": "Disk99",
|
||||
"baseuri": "example.com"
|
||||
}
|
||||
set_module_args(module_args)
|
||||
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
|
||||
module.main()
|
||||
expected_error_message = "Chassis resource Disk99 not found"
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(ansible_fail_json))
|
||||
with set_module_args(module_args):
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleFailJson) as ansible_fail_json:
|
||||
module.main()
|
||||
expected_error_message = "Chassis resource Disk99 not found"
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(ansible_fail_json))
|
||||
|
||||
def test_module_enclosure_led_off_already_off(self):
|
||||
"""Test turning LED indicator off when it's already off. Confirm changed is False and no POST occurs."""
|
||||
|
@ -444,15 +442,14 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"resource_id": "Enclosure",
|
||||
"baseuri": "example.com"
|
||||
}
|
||||
set_module_args(module_args)
|
||||
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertFalse(is_changed(ansible_exit_json))
|
||||
with set_module_args(module_args):
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_request=mock_get_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertFalse(is_changed(ansible_exit_json))
|
||||
|
||||
def test_module_fw_activate_first_iom_unavailable(self):
|
||||
"""Test that if the first IOM is not available, the 2nd one is used."""
|
||||
|
@ -467,26 +464,26 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
'password': 'PASSW0RD=21',
|
||||
'ioms': ioms
|
||||
}
|
||||
set_module_args(module_args)
|
||||
with set_module_args(module_args):
|
||||
|
||||
def mock_get_request(*args, **kwargs):
|
||||
"""Mock for get_request that will fail on the 'bad' IOM."""
|
||||
if "bad.example.com" in args[1]:
|
||||
return MOCK_URL_ERROR
|
||||
else:
|
||||
return mock_get_request_enclosure_single_tenant(*args, **kwargs)
|
||||
def mock_get_request(*args, **kwargs):
|
||||
"""Mock for get_request that will fail on the 'bad' IOM."""
|
||||
if "bad.example.com" in args[1]:
|
||||
return MOCK_URL_ERROR
|
||||
else:
|
||||
return mock_get_request_enclosure_single_tenant(*args, **kwargs)
|
||||
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
_firmware_activate_uri=mock_fw_activate_url,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleExitJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(cm))
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
_firmware_activate_uri=mock_fw_activate_url,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleExitJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(cm))
|
||||
|
||||
def test_module_fw_activate_pass(self):
|
||||
"""Test the FW Activate command in a passing scenario."""
|
||||
|
@ -508,69 +505,68 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
'password': 'PASSW0RD=21',
|
||||
}
|
||||
module_args.update(uri_specifier)
|
||||
set_module_args(module_args)
|
||||
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
_firmware_activate_uri=mock_fw_activate_url,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_single_tenant,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
with set_module_args(module_args):
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
_firmware_activate_uri=mock_fw_activate_url,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_single_tenant,
|
||||
post_request=mock_post_request):
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE,
|
||||
get_exception_message(ansible_exit_json))
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
|
||||
def test_module_fw_activate_service_does_not_support_fw_activate(self):
|
||||
"""Test FW Activate when it is not supported."""
|
||||
expected_error_message = "Service does not support FWActivate"
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'FWActivate',
|
||||
'username': 'USERID',
|
||||
'password': 'PASSW0RD=21',
|
||||
'ioms': ["example1.example.com"]
|
||||
})
|
||||
}):
|
||||
|
||||
def mock_update_uri_response(*args, **kwargs):
|
||||
return {
|
||||
"ret": True,
|
||||
"data": {} # No Actions
|
||||
}
|
||||
def mock_update_uri_response(*args, **kwargs):
|
||||
return {
|
||||
"ret": True,
|
||||
"data": {} # No Actions
|
||||
}
|
||||
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_update_uri_response):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_update_uri_response):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
|
||||
def test_module_update_and_activate_image_uri_not_http(self):
|
||||
"""Test Update and Activate when URI is not http(s)"""
|
||||
expected_error_message = "Bundle URI must be HTTP or HTTPS"
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
'password': 'PASSW0RD=21',
|
||||
'ioms': ["example1.example.com"],
|
||||
'update_image_uri': "ftp://example.com/image"
|
||||
})
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
}):
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
|
||||
def test_module_update_and_activate_target_not_ready_for_fw_update(self):
|
||||
"""Test Update and Activate when target is not in the correct state."""
|
||||
|
@ -580,38 +576,38 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
mock_status_code,
|
||||
mock_status_description
|
||||
)
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
'password': 'PASSW0RD=21',
|
||||
'ioms': ["example1.example.com"],
|
||||
'update_image_uri': "http://example.com/image"
|
||||
})
|
||||
with patch.object(module.WdcRedfishUtils, "get_simple_update_status") as mock_get_simple_update_status:
|
||||
mock_get_simple_update_status.return_value = {
|
||||
"ret": True,
|
||||
"entries": {
|
||||
"StatusCode": mock_status_code,
|
||||
"Description": mock_status_description
|
||||
}):
|
||||
with patch.object(module.WdcRedfishUtils, "get_simple_update_status") as mock_get_simple_update_status:
|
||||
mock_get_simple_update_status.return_value = {
|
||||
"ret": True,
|
||||
"entries": {
|
||||
"StatusCode": mock_status_code,
|
||||
"Description": mock_status_description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
|
||||
def test_module_update_and_activate_bundle_not_a_tarfile(self):
|
||||
"""Test Update and Activate when bundle is not a tarfile"""
|
||||
mock_filename = os.path.abspath(__file__)
|
||||
expected_error_message = ERROR_MESSAGE_UNABLE_TO_EXTRACT_BUNDLE_VERSION
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
|
@ -622,24 +618,24 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"username": "image_user",
|
||||
"password": "image_password"
|
||||
}
|
||||
})
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = mock_filename
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
}):
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = mock_filename
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
|
||||
def test_module_update_and_activate_bundle_contains_no_firmware_version(self):
|
||||
"""Test Update and Activate when bundle contains no firmware version"""
|
||||
expected_error_message = ERROR_MESSAGE_UNABLE_TO_EXTRACT_BUNDLE_VERSION
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
|
@ -650,29 +646,29 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"username": "image_user",
|
||||
"password": "image_password"
|
||||
}
|
||||
})
|
||||
}):
|
||||
|
||||
tar_name = "empty_tarfile{0}.tar".format(uuid.uuid4())
|
||||
empty_tarfile = tarfile.open(os.path.join(self.tempdir, tar_name), "w")
|
||||
empty_tarfile.close()
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
tar_name = "empty_tarfile{0}.tar".format(uuid.uuid4())
|
||||
empty_tarfile = tarfile.open(os.path.join(self.tempdir, tar_name), "w")
|
||||
empty_tarfile.close()
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return):
|
||||
with self.assertRaises(AnsibleFailJson) as cm:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(cm))
|
||||
|
||||
def test_module_update_and_activate_version_already_installed(self):
|
||||
"""Test Update and Activate when the bundle version is already installed"""
|
||||
mock_firmware_version = "1.2.3"
|
||||
expected_error_message = ACTION_WAS_SUCCESSFUL_MESSAGE
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
|
@ -683,31 +679,31 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"username": "image_user",
|
||||
"password": "image_password"
|
||||
}
|
||||
})
|
||||
}):
|
||||
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=False)
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3,
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_single_tenant):
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(result))
|
||||
self.assertFalse(is_changed(result))
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=False)
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3,
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_single_tenant):
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(result))
|
||||
self.assertFalse(is_changed(result))
|
||||
|
||||
def test_module_update_and_activate_version_already_installed_multi_tenant(self):
|
||||
"""Test Update and Activate on multi-tenant when version is already installed"""
|
||||
mock_firmware_version = "1.2.3"
|
||||
expected_error_message = ACTION_WAS_SUCCESSFUL_MESSAGE
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
|
@ -718,30 +714,30 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"username": "image_user",
|
||||
"password": "image_password"
|
||||
}
|
||||
})
|
||||
}):
|
||||
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=True)
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3,
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_multi_tenant):
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(result))
|
||||
self.assertFalse(is_changed(result))
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=True)
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3,
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_multi_tenant):
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(result))
|
||||
self.assertFalse(is_changed(result))
|
||||
|
||||
def test_module_update_and_activate_pass(self):
|
||||
"""Test Update and Activate (happy path)"""
|
||||
mock_firmware_version = "1.2.2"
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
|
@ -752,34 +748,34 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"username": "image_user",
|
||||
"password": "image_password"
|
||||
}
|
||||
})
|
||||
}):
|
||||
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=False)
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=False)
|
||||
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3,
|
||||
simple_update=mock_simple_update,
|
||||
_simple_update_status_uri=mocked_url_response,
|
||||
# _find_updateservice_resource=empty_return,
|
||||
# _find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_single_tenant,
|
||||
post_request=mock_post_request):
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils",
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3,
|
||||
simple_update=mock_simple_update,
|
||||
_simple_update_status_uri=mocked_url_response,
|
||||
# _find_updateservice_resource=empty_return,
|
||||
# _find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_single_tenant,
|
||||
post_request=mock_post_request):
|
||||
|
||||
with patch("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils.get_simple_update_status"
|
||||
) as mock_get_simple_update_status:
|
||||
mock_get_simple_update_status.side_effect = MOCK_SIMPLE_UPDATE_STATUS_LIST
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json))
|
||||
with patch("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils.get_simple_update_status"
|
||||
) as mock_get_simple_update_status:
|
||||
mock_get_simple_update_status.side_effect = MOCK_SIMPLE_UPDATE_STATUS_LIST
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json))
|
||||
|
||||
def test_module_update_and_activate_pass_multi_tenant(self):
|
||||
"""Test Update and Activate with multi-tenant (happy path)"""
|
||||
mock_firmware_version = "1.2.2"
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
|
@ -790,34 +786,34 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"username": "image_user",
|
||||
"password": "image_password"
|
||||
}
|
||||
})
|
||||
}):
|
||||
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=True)
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=True)
|
||||
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3,
|
||||
simple_update=mock_simple_update,
|
||||
_simple_update_status_uri=mocked_url_response,
|
||||
# _find_updateservice_resource=empty_return,
|
||||
# _find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_multi_tenant,
|
||||
post_request=mock_post_request):
|
||||
with patch("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils.get_simple_update_status"
|
||||
) as mock_get_simple_update_status:
|
||||
mock_get_simple_update_status.side_effect = MOCK_SIMPLE_UPDATE_STATUS_LIST
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json))
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3,
|
||||
simple_update=mock_simple_update,
|
||||
_simple_update_status_uri=mocked_url_response,
|
||||
# _find_updateservice_resource=empty_return,
|
||||
# _find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_multi_tenant,
|
||||
post_request=mock_post_request):
|
||||
with patch("ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.WdcRedfishUtils.get_simple_update_status"
|
||||
) as mock_get_simple_update_status:
|
||||
mock_get_simple_update_status.side_effect = MOCK_SIMPLE_UPDATE_STATUS_LIST
|
||||
with self.assertRaises(AnsibleExitJson) as ansible_exit_json:
|
||||
module.main()
|
||||
self.assertTrue(is_changed(ansible_exit_json))
|
||||
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json))
|
||||
|
||||
def test_module_fw_update_multi_tenant_firmware_single_tenant_enclosure(self):
|
||||
"""Test Update and Activate using multi-tenant bundle on single-tenant enclosure"""
|
||||
mock_firmware_version = "1.1.1"
|
||||
expected_error_message = "Enclosure multi-tenant is False but bundle multi-tenant is True"
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
|
@ -828,30 +824,30 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"username": "image_user",
|
||||
"password": "image_password"
|
||||
}
|
||||
})
|
||||
}):
|
||||
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=True)
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3(),
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_single_tenant):
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(result))
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=True)
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3(),
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_single_tenant):
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(result))
|
||||
|
||||
def test_module_fw_update_single_tentant_firmware_multi_tenant_enclosure(self):
|
||||
"""Test Update and Activate using singe-tenant bundle on multi-tenant enclosure"""
|
||||
mock_firmware_version = "1.1.1"
|
||||
expected_error_message = "Enclosure multi-tenant is True but bundle multi-tenant is False"
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
'category': 'Update',
|
||||
'command': 'UpdateAndActivate',
|
||||
'username': 'USERID',
|
||||
|
@ -862,24 +858,24 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
|||
"username": "image_user",
|
||||
"password": "image_password"
|
||||
}
|
||||
})
|
||||
}):
|
||||
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=False)
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3(),
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_multi_tenant):
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(result))
|
||||
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version,
|
||||
is_multi_tenant=False)
|
||||
with patch('ansible_collections.community.general.plugins.module_utils.wdc_redfish_utils.fetch_file') as mock_fetch_file:
|
||||
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name)
|
||||
with patch.multiple(module.WdcRedfishUtils,
|
||||
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3(),
|
||||
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update,
|
||||
_firmware_activate_uri=mocked_url_response,
|
||||
_update_uri=mock_update_url,
|
||||
_find_updateservice_resource=empty_return,
|
||||
_find_updateservice_additional_uris=empty_return,
|
||||
get_request=mock_get_request_enclosure_multi_tenant):
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
module.main()
|
||||
self.assertEqual(expected_error_message,
|
||||
get_exception_message(result))
|
||||
|
||||
def generate_temp_bundlefile(self,
|
||||
mock_firmware_version,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue