1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
# Copyright 2017 Hewlett Packard Enterprise Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import shutil
import tarfile
import tempfile
import time
from unittest import mock
from oslo_concurrency import processutils
from oslo_serialization import base64
import testtools
from proliantutils import exception
from proliantutils.sum import sum_controller
from proliantutils.tests.sum import sum_sample_output as constants
from proliantutils import utils
class SUMFirmwareUpdateTest(testtools.TestCase):
def setUp(self):
super(SUMFirmwareUpdateTest, self).setUp()
self.info = {'ilo_address': '1.2.3.4',
'ilo_password': '12345678',
'ilo_username': 'admin'}
clean_step = {
'interface': 'management',
'step': 'update_firmware_sum',
'args': {'url': 'http://1.2.3.4/SPP.iso',
'checksum': '1234567890'}}
self.node = {'driver_info': self.info,
'clean_step': clean_step}
@mock.patch.object(sum_controller,
'_get_log_file_data_as_encoded_content')
@mock.patch.object(sum_controller, 'open',
mock.mock_open(read_data=constants.SUM_OUTPUT_DATA))
@mock.patch.object(os.path, 'exists')
@mock.patch.object(processutils, 'execute')
def test_execute_sum(self, execute_mock, exists_mock, log_mock):
exists_mock.return_value = True
log_mock.return_value = "aaabbbcccdddd"
value = ("hpsum_service_x64 started successfully. Sending Shutdown "
"request to engine. Successfully shutdown the service.")
execute_mock.side_effect = processutils.ProcessExecutionError(
stdout=value, stderr=None, exit_code=0)
ret_value = {
'Log Data': 'aaabbbcccdddd',
'Summary': ("The smart component was installed successfully."
" Status of updated components: Total: 2 Success: 2 "
"Failed: 0.")
}
stdout = sum_controller._execute_sum("hpsum", "/tmp/hpsum",
components=None)
self.assertEqual(ret_value, stdout)
execute_mock.assert_called_once_with('hpsum', '--s', '--romonly', '')
@mock.patch.object(processutils, 'execute')
def test_execute_sum_with_args(self, execute_mock):
value = ("hpsum_service_x64 started successfully. Sending Shutdown "
"request to engine. Successfully shutdown the service.")
execute_mock.side_effect = processutils.ProcessExecutionError(
stdout=value, stderr=None, exit_code=3)
ret_value = ("Summary: The smart component was not installed. Node is "
"already up-to-date.")
stdout = sum_controller._execute_sum("/tmp/sum/packages/smartupdate",
"/tmp/sum",
components=["foo", "bar"])
execute_mock.assert_called_once_with(
"./launch_sum.sh", "--s", "--romonly", "--use_location",
"/tmp/sum/packages", " --c foo --c bar", cwd='/tmp/sum')
self.assertEqual(ret_value, stdout)
@mock.patch.object(sum_controller,
'_get_log_file_data_as_encoded_content')
@mock.patch.object(
sum_controller, 'open',
mock.mock_open(read_data=constants.SUM_OUTPUT_DATA_FAILURE))
@mock.patch.object(os.path, 'exists')
@mock.patch.object(processutils, 'execute')
def test_execute_sum_update_fails(self, execute_mock, exists_mock,
log_mock):
exists_mock.return_value = True
log_mock.return_value = "aaabbbcccdddd"
ret = {
'Log Data': 'aaabbbcccdddd',
'Summary': ("The installation of the component failed. Status "
"of updated components: Total: 2 Success: 1 "
"Failed: 1.")
}
value = ("hpsum_service_x64 started successfully. Sending Shutdown "
"request to engine. Successfully shutdown the service.")
execute_mock.side_effect = processutils.ProcessExecutionError(
stdout=value, stderr=None, exit_code=253)
stdout = sum_controller._execute_sum("hpsum", "/tmp/hpsum",
components=None)
self.assertEqual(ret, stdout)
execute_mock.assert_called_once_with(
"hpsum", "--s", "--romonly", "")
@mock.patch.object(os.path, 'exists')
@mock.patch.object(processutils, 'execute')
def test_execute_sum_fails(self, execute_mock, exists_mock):
exists_mock.return_value = False
value = ("Error: Cannot launch hpsum_service_x64 locally. Reason: "
"General failure.")
execute_mock.side_effect = processutils.ProcessExecutionError(
stdout=value, stderr=None, exit_code=255)
ex = self.assertRaises(exception.SUMOperationError,
sum_controller._execute_sum, "hpsum",
"/tmp/hpsum",
None)
self.assertIn(value, str(ex))
def test_get_log_file_data_as_encoded_content(self):
log_file_content = b'Sample Data for testing SUM log output'
file_object = tempfile.NamedTemporaryFile(delete=False)
file_object.write(log_file_content)
file_object.close()
sum_controller.OUTPUT_FILES = [file_object.name]
base64_encoded_text = (sum_controller.
_get_log_file_data_as_encoded_content())
tar_gzipped_content = base64.decode_as_bytes(base64_encoded_text)
tar_file = tempfile.NamedTemporaryFile(suffix='.tar.gz', delete=False)
tar_file.write(tar_gzipped_content)
tar_file.close()
with tarfile.open(name=tar_file.name) as tar:
f = tar.extractfile(file_object.name.lstrip('/'))
self.assertEqual(log_file_content, f.read())
os.remove(file_object.name)
os.remove(tar_file.name)
@mock.patch.object(time, 'sleep')
@mock.patch.object(utils, 'validate_href')
@mock.patch.object(utils, 'verify_image_checksum')
@mock.patch.object(sum_controller, '_execute_sum')
@mock.patch.object(os, 'listdir')
@mock.patch.object(shutil, 'rmtree', autospec=True)
@mock.patch.object(tempfile, 'mkdtemp', autospec=True)
@mock.patch.object(os.path, 'exists')
@mock.patch.object(os, 'mkdir')
@mock.patch.object(processutils, 'execute')
def test_update_firmware(self, execute_mock, mkdir_mock,
exists_mock, mkdtemp_mock, rmtree_mock,
listdir_mock, execute_sum_mock,
verify_image_mock, validate_mock, sleep_mock):
execute_sum_mock.return_value = 'SUCCESS'
listdir_mock.return_value = ['SPP_LABEL']
mkdtemp_mock.return_value = "/tempdir"
null_output = ["", ""]
exists_mock.side_effect = [True, False]
execute_mock.side_effect = [null_output, null_output]
url = "http://a.b.c.d/spp.iso"
checksum = "12345678"
components = ['abc', 'pqr']
ret_val = sum_controller.update_firmware(self.node, url, checksum,
components)
execute_mock.assert_any_call('mount', "/dev/disk/by-label/SPP_LABEL",
"/tempdir")
execute_sum_mock.assert_any_call('/tempdir/hp/swpackages/hpsum',
'/tempdir', components=components)
calls = [mock.call("/dev/disk/by-label/SPP_LABEL"),
mock.call("/tempdir/packages/smartupdate")]
exists_mock.assert_has_calls(calls, any_order=False)
execute_mock.assert_any_call('umount', "/tempdir")
mkdtemp_mock.assert_called_once_with()
rmtree_mock.assert_called_once_with("/tempdir", ignore_errors=True)
self.assertEqual('SUCCESS', ret_val)
@mock.patch.object(time, 'sleep')
@mock.patch.object(utils, 'validate_href')
@mock.patch.object(utils, 'verify_image_checksum')
@mock.patch.object(sum_controller, '_execute_sum')
@mock.patch.object(os, 'listdir')
@mock.patch.object(shutil, 'rmtree', autospec=True)
@mock.patch.object(tempfile, 'mkdtemp', autospec=True)
@mock.patch.object(os.path, 'exists')
@mock.patch.object(os, 'mkdir')
@mock.patch.object(processutils, 'execute')
def test_update_firmware_sum(self, execute_mock, mkdir_mock,
exists_mock, mkdtemp_mock, rmtree_mock,
listdir_mock, execute_sum_mock,
verify_image_mock, validate_mock, sleep_mock):
execute_sum_mock.return_value = 'SUCCESS'
listdir_mock.return_value = ['SPP_LABEL']
mkdtemp_mock.return_value = "/tempdir"
null_output = ["", ""]
exists_mock.side_effect = [True, True, True, True]
execute_mock.side_effect = [null_output, null_output,
null_output, null_output]
url = "http://a.b.c.d/spp.iso"
checksum = "12345678"
ret_val = sum_controller.update_firmware(
self.node, url, checksum)
execute_mock.assert_any_call('mount',
"/dev/disk/by-label/SPP_LABEL",
"/tempdir")
execute_sum_mock.assert_any_call('/tempdir/packages/smartupdate',
'/tempdir',
components=None)
calls = [mock.call("/dev/disk/by-label/SPP_LABEL"),
mock.call("/tempdir/packages/smartupdate")]
exists_mock.assert_has_calls(calls, any_order=False)
execute_mock.assert_any_call('umount', "/tempdir")
mkdtemp_mock.assert_called_once_with()
rmtree_mock.assert_called_once_with("/tempdir", ignore_errors=True)
self.assertEqual('SUCCESS', ret_val)
@mock.patch.object(utils, 'validate_href')
def test_update_firmware_throws_for_nonexistent_file(self,
validate_href_mock):
invalid_file_path = '/some/invalid/file/path'
url = "http://a.b.c.d/spp.iso"
checksum = "12345678"
components = ['abc', 'pqr']
value = ("Got HTTP code 503 instead of 200 in response to "
"HEAD request.")
validate_href_mock.side_effect = exception.ImageRefValidationFailed(
reason=value, image_href=invalid_file_path)
exc = self.assertRaises(exception.SUMOperationError,
sum_controller.update_firmware, self.node,
url, checksum, components)
self.assertIn(value, str(exc))
@mock.patch.object(time, 'sleep')
@mock.patch.object(utils, 'validate_href')
@mock.patch.object(os.path, 'exists')
@mock.patch.object(os, 'listdir')
def test_update_firmware_device_file_not_found(self,
listdir_mock, exists_mock,
validate_mock, sleep_mock):
listdir_mock.return_value = ['SPP_LABEL']
exists_mock.return_value = False
url = "http://a.b.c.d/spp.iso"
checksum = "12345678"
components = ['abc', 'pqr']
msg = ("An error occurred while performing SUM based firmware "
"update, reason: Unable to find the virtual media device "
"for SUM")
exc = self.assertRaises(exception.SUMOperationError,
sum_controller.update_firmware, self.node,
url, checksum, components=components)
self.assertEqual(msg, str(exc))
exists_mock.assert_called_once_with("/dev/disk/by-label/SPP_LABEL")
@mock.patch.object(time, 'sleep')
@mock.patch.object(utils, 'validate_href')
@mock.patch.object(utils, 'verify_image_checksum')
@mock.patch.object(os, 'listdir')
@mock.patch.object(os.path, 'exists')
def test_update_firmware_invalid_checksum(self, exists_mock,
listdir_mock, verify_image_mock,
validate_mock, sleep_mock):
listdir_mock.return_value = ['SPP_LABEL']
exists_mock.side_effect = [True, False]
url = "http://1.2.3.4/SPP.iso"
checksum = "1234567890"
components = ['abc', 'pqr']
value = ("Error verifying image checksum. Image "
"http://1.2.3.4/SPP.iso failed to verify against checksum "
"123456789. Actual checksum is: xxxxxxxx")
verify_image_mock.side_effect = exception.ImageRefValidationFailed(
reason=value, image_href='http://1.2.3.4/SPP.iso')
self.assertRaisesRegex(exception.SUMOperationError, value,
sum_controller.update_firmware, self.node,
url, checksum, components=components)
verify_image_mock.assert_called_once_with(
'/dev/disk/by-label/SPP_LABEL', '1234567890')
exists_mock.assert_called_once_with("/dev/disk/by-label/SPP_LABEL")
@mock.patch.object(time, 'sleep')
@mock.patch.object(utils, 'validate_href')
@mock.patch.object(utils, 'verify_image_checksum')
@mock.patch.object(processutils, 'execute')
@mock.patch.object(tempfile, 'mkdtemp', autospec=True)
@mock.patch.object(os, 'mkdir')
@mock.patch.object(os.path, 'exists')
@mock.patch.object(os, 'listdir')
def test_update_firmware_mount_fails(self, listdir_mock,
exists_mock, mkdir_mock,
mkdtemp_mock, execute_mock,
verify_image_mock, validate_mock,
sleep_mock):
listdir_mock.return_value = ['SPP_LABEL']
exists_mock.return_value = True
mkdtemp_mock.return_value = "/tempdir"
execute_mock.side_effect = processutils.ProcessExecutionError
url = "http://a.b.c.d/spp.iso"
checksum = "12345678"
msg = ("Unable to mount virtual media device "
"/dev/disk/by-label/SPP_LABEL")
exc = self.assertRaises(exception.SUMOperationError,
sum_controller.update_firmware, self.node,
url, checksum)
self.assertIn(msg, str(exc))
exists_mock.assert_called_once_with("/dev/disk/by-label/SPP_LABEL")
@mock.patch.object(sum_controller,
'_get_log_file_data_as_encoded_content')
@mock.patch.object(sum_controller, 'open',
mock.mock_open(read_data=constants.SUM_OUTPUT_DATA))
@mock.patch.object(os.path, 'exists')
def test__parse_sum_ouput(self, exists_mock, log_mock):
exists_mock.return_value = True
log_mock.return_value = "aaabbbcccdddd"
expt_ret = {'Log Data': 'aaabbbcccdddd',
'Summary': ("The smart component was installed "
"successfully. Status of updated components: "
"Total: 2 Success: 2 Failed: 0.")}
ret = sum_controller._parse_sum_ouput(0)
exists_mock.assert_called_once_with(sum_controller.OUTPUT_FILES[0])
self.assertEqual(expt_ret, ret)
@mock.patch.object(sum_controller,
'_get_log_file_data_as_encoded_content')
@mock.patch.object(
sum_controller, 'open',
mock.mock_open(read_data=constants.SUM_OUTPUT_DATA_FAILURE))
@mock.patch.object(os.path, 'exists')
def test__parse_sum_ouput_some_failed(self, exists_mock, log_mock):
exists_mock.return_value = True
log_mock.return_value = "aaabbbcccdddd"
expt_ret = {'Log Data': 'aaabbbcccdddd',
'Summary': ("The installation of the component failed. "
"Status of updated components: Total: 2 "
"Success: 1 Failed: 1.")}
ret = sum_controller._parse_sum_ouput(253)
exists_mock.assert_called_once_with(sum_controller.OUTPUT_FILES[0])
self.assertEqual(expt_ret, ret)
@mock.patch.object(os.path, 'exists')
def test__parse_sum_ouput_fails(self, exists_mock):
exists_mock.return_value = False
expt_ret = ("UPDATE STATUS: UNKNOWN")
ret = sum_controller._parse_sum_ouput(1)
exists_mock.assert_called_once_with(sum_controller.OUTPUT_FILES[0])
self.assertEqual(expt_ret, ret)
|