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
|
# Copyright 2016-2022 Hewlett Packard Enterprise Company, L.P.
# All Rights Reserved.
#
# 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.
"""Test class for Utils Module."""
import hashlib
from unittest import mock
import requests
import six
import six.moves.builtins as __builtin__
from six.moves import http_client
import testtools
from proliantutils import exception
from proliantutils.ilo import client
from proliantutils.ilo import firmware_controller
from proliantutils.ilo import ribcl
from proliantutils import utils
class UtilsTestCase(testtools.TestCase):
@mock.patch.object(ribcl.RIBCLOperations, 'get_product_name')
def setUp(self, product_mock):
super(UtilsTestCase, self).setUp()
product_mock.return_value = 'Gen8'
self.some_compact_fw_file = 'some_compact_fw_file.scexe'
self.client = client.IloClient("1.2.3.4", "admin", "Admin")
@mock.patch.object(firmware_controller, 'get_fw_extractor',
spec_set=True, autospec=True)
def test_process_firmware_image_throws_for_unknown_firmware_file_format(
self, get_extractor_mock):
# | GIVEN |
get_extractor_mock.side_effect = exception.InvalidInputError
# | WHEN | & | THEN |
self.assertRaises(exception.InvalidInputError,
utils.process_firmware_image,
'invalid_compact_fw_file',
self.client)
@mock.patch.object(firmware_controller, 'get_fw_extractor',
spec_set=True, autospec=True)
def test_process_firmware_image_throws_for_failed_extraction(
self, get_extractor_mock):
# | GIVEN |
exc = exception.ImageExtractionFailed(
image_ref='some_file', reason='God only knows!')
get_extractor_mock.return_value.extract.side_effect = exc
# | WHEN | & | THEN |
self.assertRaises(exception.ImageExtractionFailed,
utils.process_firmware_image,
self.some_compact_fw_file,
self.client)
@mock.patch.object(firmware_controller, 'get_fw_extractor',
spec_set=True, autospec=True)
def test_process_firmware_image_calls_extract_of_fw_extractor_object(
self, get_extractor_mock):
# process_firmware_image calls extract on the firmware_extractor
# instance
# | GIVEN |
get_extractor_mock.return_value.extract.return_value = (
'core_fw_file.bin', True)
# | WHEN |
raw_fw_file, to_upload, is_extracted = (
utils.process_firmware_image(self.some_compact_fw_file,
self.client))
# | THEN |
get_extractor_mock.assert_called_once_with(self.some_compact_fw_file)
get_extractor_mock.return_value.extract.assert_called_once_with()
@mock.patch.object(firmware_controller, 'get_fw_extractor',
spec_set=True, autospec=True)
def test_process_firmware_image_asks_not_to_upload_firmware_file(
self, get_extractor_mock):
# | GIVEN |
get_extractor_mock.return_value.extract.return_value = (
'core_fw_file.bin', True)
self.client.model = 'Gen8'
# | WHEN |
raw_fw_file, to_upload, is_extracted = (
utils.process_firmware_image(self.some_compact_fw_file,
self.client))
# | THEN |
self.assertEqual('core_fw_file.bin', raw_fw_file)
self.assertFalse(to_upload)
@mock.patch.object(firmware_controller, 'get_fw_extractor',
spec_set=True, autospec=True)
def test_process_firmware_image_asks_to_upload_firmware_file(
self, get_extractor_mock):
# if fw_version is greater than or equal to 2.0
# | GIVEN |
get_extractor_mock.return_value.extract.return_value = (
'core_fw_file.bin', True)
self.client.model = 'Gen9'
# | WHEN |
raw_fw_file, to_upload, is_extracted = (
utils.process_firmware_image(self.some_compact_fw_file,
self.client))
# | THEN |
self.assertEqual('core_fw_file.bin', raw_fw_file)
self.assertTrue(to_upload)
@mock.patch.object(firmware_controller, 'get_fw_extractor',
spec_set=True, autospec=True)
def test_process_firmware_image_asks_to_upload_firmware_file_Gen10(
self, get_extractor_mock):
# if fw_version is greater than or equal to 2.0
# | GIVEN |
get_extractor_mock.return_value.extract.return_value = (
'core_fw_file.bin', True)
self.client.model = 'Gen10'
# | WHEN |
raw_fw_file, to_upload, is_extracted = (
utils.process_firmware_image(self.some_compact_fw_file,
self.client))
# | THEN |
self.assertEqual('core_fw_file.bin', raw_fw_file)
self.assertTrue(to_upload)
@mock.patch.object(utils, 'hashlib', autospec=True)
def test__get_hash_object(self, hashlib_mock):
algorithms_available = ('md5', 'sha1', 'sha224',
'sha256', 'sha384', 'sha512')
hashlib_mock.algorithms_guaranteed = algorithms_available
hashlib_mock.algorithms = algorithms_available
# | WHEN |
utils._get_hash_object('md5')
utils._get_hash_object('sha1')
utils._get_hash_object('sha224')
utils._get_hash_object('sha256')
utils._get_hash_object('sha384')
utils._get_hash_object('sha512')
# | THEN |
calls = [mock.call.md5(), mock.call.sha1(), mock.call.sha224(),
mock.call.sha256(), mock.call.sha384(), mock.call.sha512()]
hashlib_mock.assert_has_calls(calls)
def test__get_hash_object_throws_for_invalid_or_unsupported_hash_name(
self):
# | WHEN | & | THEN |
self.assertRaises(exception.InvalidInputError,
utils._get_hash_object,
'hickory-dickory-dock')
def test_hash_file_for_md5(self):
# | GIVEN |
data = b'Mary had a little lamb, its fleece as white as snow'
file_like_object = six.BytesIO(data)
expected = hashlib.md5(data).hexdigest()
# | WHEN |
actual = utils.hash_file(file_like_object) # using default, 'md5'
# | THEN |
self.assertEqual(expected, actual)
def test_hash_file_for_sha1(self):
# | GIVEN |
data = b'Mary had a little lamb, its fleece as white as snow'
file_like_object = six.BytesIO(data)
expected = hashlib.sha1(data).hexdigest()
# | WHEN |
actual = utils.hash_file(file_like_object, 'sha1')
# | THEN |
self.assertEqual(expected, actual)
def test_hash_file_for_sha512(self):
# | GIVEN |
data = b'Mary had a little lamb, its fleece as white as snow'
file_like_object = six.BytesIO(data)
expected = hashlib.sha512(data).hexdigest()
# | WHEN |
actual = utils.hash_file(file_like_object, 'sha512')
# | THEN |
self.assertEqual(expected, actual)
def test_hash_file_throws_for_invalid_or_unsupported_hash(self):
# | GIVEN |
data = b'Mary had a little lamb, its fleece as white as snow'
file_like_object = six.BytesIO(data)
# | WHEN | & | THEN |
self.assertRaises(exception.InvalidInputError, utils.hash_file,
file_like_object, 'hickory-dickory-dock')
@mock.patch.object(__builtin__, 'open', autospec=True)
def test_verify_image_checksum(self, open_mock):
# | GIVEN |
data = b'Yankee Doodle went to town riding on a pony;'
file_like_object = six.BytesIO(data)
open_mock().__enter__.return_value = file_like_object
actual_hash = hashlib.md5(data).hexdigest()
# | WHEN |
utils.verify_image_checksum(file_like_object, actual_hash)
# | THEN |
# no any exception thrown
def test_verify_image_checksum_throws_for_nonexistent_file(self):
# | GIVEN |
invalid_file_path = '/some/invalid/file/path'
# | WHEN | & | THEN |
self.assertRaises(exception.ImageRefValidationFailed,
utils.verify_image_checksum,
invalid_file_path, 'hash_xxx')
@mock.patch.object(__builtin__, 'open', autospec=True)
def test_verify_image_checksum_throws_for_failed_validation(self,
open_mock):
# | GIVEN |
data = b'Yankee Doodle went to town riding on a pony;'
file_like_object = six.BytesIO(data)
open_mock().__enter__.return_value = file_like_object
invalid_hash = 'invalid_hash_value'
# | WHEN | & | THEN |
self.assertRaises(exception.ImageRefValidationFailed,
utils.verify_image_checksum,
file_like_object,
invalid_hash)
@mock.patch.object(requests, 'head', autospec=True)
def test_validate_href(self, head_mock):
href = 'http://1.2.3.4/abc.iso'
response = head_mock.return_value
response.status_code = http_client.OK
utils.validate_href(href)
head_mock.assert_called_once_with(href)
response.status_code = http_client.NO_CONTENT
self.assertRaises(exception.ImageRefValidationFailed,
utils.validate_href,
href)
response.status_code = http_client.BAD_REQUEST
self.assertRaises(exception.ImageRefValidationFailed,
utils.validate_href, href)
@mock.patch.object(requests, 'head', autospec=True)
def test_validate_href_error_code(self, head_mock):
href = 'http://1.2.3.4/abc.iso'
head_mock.return_value.status_code = http_client.BAD_REQUEST
self.assertRaises(exception.ImageRefValidationFailed,
utils.validate_href, href)
head_mock.assert_called_once_with(href)
@mock.patch.object(requests, 'head', autospec=True)
def test_validate_href_error(self, head_mock):
href = 'http://1.2.3.4/abc.iso'
head_mock.side_effect = requests.ConnectionError()
self.assertRaises(exception.ImageRefValidationFailed,
utils.validate_href, href)
head_mock.assert_called_once_with(href)
@mock.patch.object(requests, 'head', autospec=True)
def test_validate_href_error_no_base_image(self, head_mock):
href = 'http://1.2.3.4/'
head_mock.return_value.status_code = http_client.OK
self.assertRaises(exception.ImageRefValidationFailed,
utils.validate_href, href)
head_mock.assert_called_once_with(href)
def test_apply_bios_properties_filter(self):
data = {
"AdminName": "Administrator",
"BootMode": "LEGACY",
"ServerName": "Gen9 server",
"TimeFormat": "Ist",
"BootOrderPolicy": "RetryIndefinitely",
"ChannelInterleaving": "Enabled",
"CollabPowerControl": "Enabled",
"ConsistentDevNaming": "LomsOnly",
"CustomPostMessage": ""
}
filter_to_be_applied = {
"AdminName",
"BootMode",
"ServerName",
"TimeFormat",
"CustomPostMessage"
}
expected = {
"AdminName": "Administrator",
"BootMode": "LEGACY",
"ServerName": "Gen9 server",
"TimeFormat": "Ist",
"CustomPostMessage": ""
}
actual = utils.apply_bios_properties_filter(
data, filter_to_be_applied)
self.assertEqual(expected, actual)
def test_apply_bios_properties_filter_no_filter(self):
data = {
"AdminName": "Administrator",
"BootMode": "LEGACY",
"ServerName": "Gen9 server",
"TimeFormat": "Ist",
"BootOrderPolicy": "RetryIndefinitely",
"ChannelInterleaving": "Enabled",
"CollabPowerControl": "Enabled",
"ConsistentDevNaming": "LomsOnly",
"CustomPostMessage": ""
}
filter_to_be_applied = None
expected = {
"AdminName": "Administrator",
"BootMode": "LEGACY",
"ServerName": "Gen9 server",
"TimeFormat": "Ist",
"BootOrderPolicy": "RetryIndefinitely",
"ChannelInterleaving": "Enabled",
"CollabPowerControl": "Enabled",
"ConsistentDevNaming": "LomsOnly",
"CustomPostMessage": ""
}
actual = utils.apply_bios_properties_filter(
data, filter_to_be_applied)
self.assertEqual(expected, actual)
def test_apply_bios_properties_filter_no_settings(self):
data = None
filter_to_be_applied = {
"AdminName",
"BootMode",
"ServerName",
"TimeFormat",
"CustomPostMessage"
}
expected = None
actual = utils.apply_bios_properties_filter(
data, filter_to_be_applied)
self.assertEqual(expected, actual)
|