live: hw_inventory: fix empty memory bank bug

When a client's hardware presents an empty memory bank and invalid call
to _bytes_to_human is performed because None is passed as a parameter.

	size = _bytes_to_human(obj.get('size', None))

Fix this by checking if 'size' is present in the JSON output from lshw.
If size is present then map the bytes to a human readable string using
_bytes_to_human, if no size is present then use 'Empty slot' to indicate
that the memory bank is not being used.
master
Jose M. Guisado 2023-10-05 16:01:32 +02:00
parent 49038f125a
commit 39c13287c5
1 changed files with 9 additions and 6 deletions

View File

@ -64,7 +64,7 @@ class HardwareInventory():
def _bytes_to_human(size):
suffixes = ['B', 'MiB', 'GiB', 'TiB']
if type(size) is not int:
raise TypeError('Invalid type')
raise TypeError(f'Invalid type in _bytes_to_human, got: {size} {type(size)}')
for exponent, suffix in enumerate(suffixes, start=1):
conv = size / (1024**exponent)
if conv < 1024:
@ -106,11 +106,14 @@ def _process_core_cpu(inventory, obj):
def _process_core_mem_bank(inventory, obj):
slot = obj.get('slot', 'Unknown slot')
size = _bytes_to_human(obj.get('size', None))
if size:
mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), size, f'({slot})'])
mem_elem = HardwareElement(HardwareType.MEMORY, mem)
inventory.add_element(mem_elem)
if 'size' in obj:
size = obj['size']
human_size = _bytes_to_human(size)
mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), human_size, f'({slot})'])
else:
mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), 'Empty slot', f'({slot})'])
mem_elem = HardwareElement(HardwareType.MEMORY, mem)
inventory.add_element(mem_elem)
def _process_core_mem(inventory, obj):