| 1 |
# Copyright (c) Ansible project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import unittest |
| 8 |
from unittest.mock import patch |
| 9 |
|
| 10 |
from ansible.module_utils import basic |
| 11 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 12 |
AnsibleExitJson, |
| 13 |
AnsibleFailJson, |
| 14 |
exit_json, |
| 15 |
fail_json, |
| 16 |
set_module_args, |
| 17 |
) |
| 18 |
|
| 19 |
import ansible_collections.community.general.plugins.modules.wdc_redfish_info as module |
| 20 |
|
| 21 |
MOCK_SUCCESSFUL_RESPONSE_WITH_ACTIONS = {"ret": True, "data": {"Actions": {}}} |
| 22 |
|
| 23 |
MOCK_SUCCESSFUL_HTTP_EMPTY_RESPONSE = {"ret": True, "data": {}} |
| 24 |
|
| 25 |
MOCK_SUCCESSFUL_RESPONSE_WITH_UPDATE_SERVICE_RESOURCE = { |
| 26 |
"ret": True, |
| 27 |
"data": {"UpdateService": {"@odata.id": "/UpdateService"}}, |
| 28 |
} |
| 29 |
|
| 30 |
MOCK_SUCCESSFUL_RESPONSE_WITH_SIMPLE_UPDATE_BUT_NO_FW_ACTIVATE = { |
| 31 |
"ret": True, |
| 32 |
"data": { |
| 33 |
"Actions": { |
| 34 |
"#UpdateService.SimpleUpdate": {"target": "mocked value"}, |
| 35 |
"Oem": { |
| 36 |
"WDC": {} # No #UpdateService.FWActivate |
| 37 |
}, |
| 38 |
} |
| 39 |
}, |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
def get_bin_path(self, arg, required=False): |
| 44 |
"""Mock AnsibleModule.get_bin_path""" |
| 45 |
return arg |
| 46 |
|
| 47 |
|
| 48 |
def get_redfish_facts(ansible_exit_json): |
| 49 |
"""From an AnsibleExitJson exception, get the redfish facts dict.""" |
| 50 |
return ansible_exit_json.exception.args[0]["redfish_facts"] |
| 51 |
|
| 52 |
|
| 53 |
def get_exception_message(ansible_exit_json): |
| 54 |
"""From an AnsibleExitJson exception, get the message string.""" |
| 55 |
return ansible_exit_json.exception.args[0]["msg"] |
| 56 |
|
| 57 |
|
| 58 |
class TestWdcRedfishInfo(unittest.TestCase): |
| 59 |
def setUp(self): |
| 60 |
self.mock_module_helper = patch.multiple( |
| 61 |
basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json, get_bin_path=get_bin_path |
| 62 |
) |
| 63 |
self.mock_module_helper.start() |
| 64 |
self.addCleanup(self.mock_module_helper.stop) |
| 65 |
|
| 66 |
def test_module_fail_when_required_args_missing(self): |
| 67 |
with self.assertRaises(AnsibleFailJson): |
| 68 |
with set_module_args({}): |
| 69 |
module.main() |
| 70 |
|
| 71 |
def test_module_fail_when_unknown_category(self): |
| 72 |
with self.assertRaises(AnsibleFailJson): |
| 73 |
with set_module_args( |
| 74 |
{ |
| 75 |
"category": "unknown", |
| 76 |
"command": "SimpleUpdateStatus", |
| 77 |
"username": "USERID", |
| 78 |
"password": "PASSW0RD=21", |
| 79 |
"ioms": [], |
| 80 |
} |
| 81 |
): |
| 82 |
module.main() |
| 83 |
|
| 84 |
def test_module_fail_when_unknown_command(self): |
| 85 |
with self.assertRaises(AnsibleFailJson): |
| 86 |
with set_module_args( |
| 87 |
{ |
| 88 |
"category": "Update", |
| 89 |
"command": "unknown", |
| 90 |
"username": "USERID", |
| 91 |
"password": "PASSW0RD=21", |
| 92 |
"ioms": [], |
| 93 |
} |
| 94 |
): |
| 95 |
module.main() |
| 96 |
|
| 97 |
def test_module_simple_update_status_pass(self): |
| 98 |
with set_module_args( |
| 99 |
{ |
| 100 |
"category": "Update", |
| 101 |
"command": "SimpleUpdateStatus", |
| 102 |
"username": "USERID", |
| 103 |
"password": "PASSW0RD=21", |
| 104 |
"ioms": ["example1.example.com"], |
| 105 |
} |
| 106 |
): |
| 107 |
|
| 108 |
def mock_simple_update_status(*args, **kwargs): |
| 109 |
return { |
| 110 |
"ret": True, |
| 111 |
"data": { |
| 112 |
"Description": "Ready for FW update", |
| 113 |
"ErrorCode": 0, |
| 114 |
"EstimatedRemainingMinutes": 0, |
| 115 |
"StatusCode": 0, |
| 116 |
}, |
| 117 |
} |
| 118 |
|
| 119 |
def mocked_string_response(*args, **kwargs): |
| 120 |
return "mockedUrl" |
| 121 |
|
| 122 |
def empty_return(*args, **kwargs): |
| 123 |
return {"ret": True} |
| 124 |
|
| 125 |
with patch.multiple( |
| 126 |
module.WdcRedfishUtils, |
| 127 |
_simple_update_status_uri=mocked_string_response, |
| 128 |
_find_updateservice_resource=empty_return, |
| 129 |
_find_updateservice_additional_uris=empty_return, |
| 130 |
get_request=mock_simple_update_status, |
| 131 |
): |
| 132 |
with self.assertRaises(AnsibleExitJson) as ansible_exit_json: |
| 133 |
module.main() |
| 134 |
redfish_facts = get_redfish_facts(ansible_exit_json) |
| 135 |
self.assertEqual(mock_simple_update_status()["data"], redfish_facts["simple_update_status"]["entries"]) |
| 136 |
|
| 137 |
def test_module_simple_update_status_updateservice_resource_not_found(self): |
| 138 |
with set_module_args( |
| 139 |
{ |
| 140 |
"category": "Update", |
| 141 |
"command": "SimpleUpdateStatus", |
| 142 |
"username": "USERID", |
| 143 |
"password": "PASSW0RD=21", |
| 144 |
"ioms": ["example1.example.com"], |
| 145 |
} |
| 146 |
): |
| 147 |
with patch.object(module.WdcRedfishUtils, "get_request") as mock_get_request: |
| 148 |
mock_get_request.return_value = { |
| 149 |
"ret": True, |
| 150 |
"data": {}, # Missing UpdateService property |
| 151 |
} |
| 152 |
with self.assertRaises(AnsibleFailJson) as ansible_exit_json: |
| 153 |
module.main() |
| 154 |
self.assertEqual("UpdateService resource not found", get_exception_message(ansible_exit_json)) |
| 155 |
|
| 156 |
def test_module_simple_update_status_service_does_not_support_simple_update(self): |
| 157 |
with set_module_args( |
| 158 |
{ |
| 159 |
"category": "Update", |
| 160 |
"command": "SimpleUpdateStatus", |
| 161 |
"username": "USERID", |
| 162 |
"password": "PASSW0RD=21", |
| 163 |
"ioms": ["example1.example.com"], |
| 164 |
} |
| 165 |
): |
| 166 |
|
| 167 |
def mock_get_request_function(uri): |
| 168 |
mock_url_string = "mockURL" |
| 169 |
if mock_url_string in uri: |
| 170 |
return { |
| 171 |
"ret": True, |
| 172 |
"data": { |
| 173 |
"Actions": { # No #UpdateService.SimpleUpdate |
| 174 |
} |
| 175 |
}, |
| 176 |
} |
| 177 |
else: |
| 178 |
return {"ret": True, "data": mock_url_string} |
| 179 |
|
| 180 |
with patch.object(module.WdcRedfishUtils, "get_request") as mock_get_request: |
| 181 |
mock_get_request.side_effect = mock_get_request_function |
| 182 |
with self.assertRaises(AnsibleFailJson) as ansible_exit_json: |
| 183 |
module.main() |
| 184 |
self.assertEqual("UpdateService resource not found", get_exception_message(ansible_exit_json)) |
| 185 |
|
| 186 |
def test_module_simple_update_status_service_does_not_support_fw_activate(self): |
| 187 |
with set_module_args( |
| 188 |
{ |
| 189 |
"category": "Update", |
| 190 |
"command": "SimpleUpdateStatus", |
| 191 |
"username": "USERID", |
| 192 |
"password": "PASSW0RD=21", |
| 193 |
"ioms": ["example1.example.com"], |
| 194 |
} |
| 195 |
): |
| 196 |
|
| 197 |
def mock_get_request_function(uri): |
| 198 |
if uri.endswith("/redfish/v1") or uri.endswith("/redfish/v1/"): |
| 199 |
return MOCK_SUCCESSFUL_RESPONSE_WITH_UPDATE_SERVICE_RESOURCE |
| 200 |
elif uri.endswith("/mockedUrl"): |
| 201 |
return MOCK_SUCCESSFUL_HTTP_EMPTY_RESPONSE |
| 202 |
elif uri.endswith("/UpdateService"): |
| 203 |
return MOCK_SUCCESSFUL_RESPONSE_WITH_SIMPLE_UPDATE_BUT_NO_FW_ACTIVATE |
| 204 |
else: |
| 205 |
raise RuntimeError(f"Illegal call to get_request in test: {uri}") |
| 206 |
|
| 207 |
with patch( |
| 208 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils.get_request" |
| 209 |
) as mock_get_request: |
| 210 |
mock_get_request.side_effect = mock_get_request_function |
| 211 |
with self.assertRaises(AnsibleFailJson) as ansible_exit_json: |
| 212 |
module.main() |
| 213 |
self.assertEqual("Service does not support FWActivate", get_exception_message(ansible_exit_json)) |