Skip to content

Commit 97ce08d

Browse files
zanebjuliakreger
authored andcommitted
Fix getting memory size in some lshw output
Due to a regression in lshw introduced by lyonel/lshw#60, there are some versions in the wild that do not return sizes for memory banks <32GiB. In those cases, work around the problem by looking at the top-level size (if available) to find the total size. Previously we assumed that we only needed the top-level size when there was no list of memory banks. The issue is fixed upstream by lyonel/lshw#65, but the erroneous patch is still present in the lshw-B.02.19.2-5.el8 package in CentOS 8.4 and 8.5. Note from Backport to Victoria branch: The stable test data was moved sometime during the Wallaby development cycle to a separate file, where as in Victoria and earlier, it is based in the test file itself. The required content was moved into the test file in line with where it was in the later versions. Change-Id: I6eb5981d28b9ae368239af0c1d0ec32ff79d95b3 Story: #2008865 Task: 42395 (cherry picked from commit ed791d9) (cherry picked from commit bae3aec)
1 parent 2645240 commit 97ce08d

File tree

3 files changed

+210
-7
lines changed

3 files changed

+210
-7
lines changed

ironic_python_agent/hardware.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,16 @@ def _calc_memory(sys_dict):
217217
for core_child in sys_child['children']:
218218
if not _MEMORY_ID_RE.match(core_child['id']):
219219
continue
220-
if (not core_child.get("children")
221-
and core_child.get('size')):
220+
if core_child.get('size'):
222221
value = ("%(size)s %(units)s" % core_child)
223222
physical += int(UNIT_CONVERTER(value).to
224223
('MB').magnitude)
225-
for bank in core_child.get('children', ()):
226-
if bank.get('size'):
227-
value = ("%(size)s %(units)s" % bank)
228-
physical += int(UNIT_CONVERTER(value).to
229-
('MB').magnitude)
224+
else:
225+
for bank in core_child.get('children', ()):
226+
if bank.get('size'):
227+
value = ("%(size)s %(units)s" % bank)
228+
physical += int(UNIT_CONVERTER(value).to
229+
('MB').magnitude)
230230
return physical
231231

232232

ironic_python_agent/tests/unit/test_hardware.py

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,190 @@
581581
}
582582
""", "")
583583

584+
LSHW_JSON_OUTPUT_NO_MEMORY_BANK_SIZE = ("""
585+
{
586+
"id" : "bumblebee",
587+
"class" : "system",
588+
"claimed" : true,
589+
"handle" : "DMI:0001",
590+
"description" : "Rack Mount Chassis",
591+
"product" : "ABCD",
592+
"vendor" : "ABCD",
593+
"version" : "1234",
594+
"serial" : "1234",
595+
"width" : 64,
596+
"configuration" : {
597+
"boot" : "normal",
598+
"chassis" : "rackmount",
599+
"family" : "Intel Grantley EP",
600+
"sku" : "NULL",
601+
"uuid" : "00010002-0003-0004-0005-000600070008"
602+
},
603+
"capabilities" : {
604+
"smbios-2.8" : "SMBIOS version 2.8",
605+
"dmi-2.7" : "DMI version 2.7",
606+
"vsyscall32" : "32-bit processes"
607+
},
608+
"children" : [
609+
{
610+
"id" : "core",
611+
"class" : "bus",
612+
"claimed" : true,
613+
"handle" : "DMI:0002",
614+
"description" : "Motherboard",
615+
"product" : "ABCD",
616+
"vendor" : "ABCD",
617+
"physid" : "0",
618+
"version" : "1234",
619+
"serial" : "1234",
620+
"slot" : "NULL",
621+
"children" : [
622+
{
623+
"id" : "memory:0",
624+
"class" : "memory",
625+
"claimed" : true,
626+
"handle" : "DMI:004A",
627+
"description" : "System Memory",
628+
"physid" : "4a",
629+
"slot" : "System board or motherboard",
630+
"units" : "bytes",
631+
"size" : 34359738368,
632+
"children" : [
633+
{
634+
"id" : "bank:0",
635+
"class" : "memory",
636+
"claimed" : true,
637+
"handle" : "DMI:004C",
638+
"description" : "DIMM Synchronous 2133 MHz (0.5 ns)",
639+
"product" : "36ASF2G72PZ-2G1A2",
640+
"vendor" : "Micron",
641+
"physid" : "0",
642+
"serial" : "101B6543",
643+
"slot" : "DIMM_A0",
644+
"width" : 64,
645+
"clock" : 2133000000
646+
},
647+
{
648+
"id" : "bank:1",
649+
"class" : "memory",
650+
"claimed" : true,
651+
"handle" : "DMI:004E",
652+
"description" : "DIMM Synchronous [empty]",
653+
"product" : "NO DIMM",
654+
"vendor" : "NO DIMM",
655+
"physid" : "1",
656+
"serial" : "NO DIMM",
657+
"slot" : "DIMM_A1"
658+
},
659+
{
660+
"id" : "bank:2",
661+
"class" : "memory",
662+
"claimed" : true,
663+
"handle" : "DMI:004F",
664+
"description" : "DIMM Synchronous 2133 MHz (0.5 ns)",
665+
"product" : "36ASF2G72PZ-2G1A2",
666+
"vendor" : "Micron",
667+
"physid" : "2",
668+
"serial" : "101B654E",
669+
"slot" : "DIMM_A2",
670+
"width" : 64,
671+
"clock" : 2133000000
672+
},
673+
{
674+
"id" : "bank:3",
675+
"class" : "memory",
676+
"claimed" : true,
677+
"handle" : "DMI:0051",
678+
"description" : "DIMM Synchronous [empty]",
679+
"product" : "NO DIMM",
680+
"vendor" : "NO DIMM",
681+
"physid" : "3",
682+
"serial" : "NO DIMM",
683+
"slot" : "DIMM_A3"
684+
}
685+
]
686+
},
687+
{
688+
"id" : "memory:1",
689+
"class" : "memory",
690+
"claimed" : true,
691+
"handle" : "DMI:0052",
692+
"description" : "System Memory",
693+
"physid" : "52",
694+
"slot" : "System board or motherboard",
695+
"units" : "bytes",
696+
"size" : 34359738368,
697+
"children" : [
698+
{
699+
"id" : "bank:0",
700+
"class" : "memory",
701+
"claimed" : true,
702+
"handle" : "DMI:0054",
703+
"description" : "DIMM Synchronous 2133 MHz (0.5 ns)",
704+
"product" : "36ASF2G72PZ-2G1A2",
705+
"vendor" : "Micron",
706+
"physid" : "0",
707+
"serial" : "101B6545",
708+
"slot" : "DIMM_A4",
709+
"width" : 64,
710+
"clock" : 2133000000
711+
},
712+
{
713+
"id" : "bank:1",
714+
"class" : "memory",
715+
"claimed" : true,
716+
"handle" : "DMI:0056",
717+
"description" : "DIMM Synchronous [empty]",
718+
"product" : "NO DIMM",
719+
"vendor" : "NO DIMM",
720+
"physid" : "1",
721+
"serial" : "NO DIMM",
722+
"slot" : "DIMM_A5"
723+
},
724+
{
725+
"id" : "bank:2",
726+
"class" : "memory",
727+
"claimed" : true,
728+
"handle" : "DMI:0057",
729+
"description" : "DIMM Synchronous 2133 MHz (0.5 ns)",
730+
"product" : "36ASF2G72PZ-2G1A2",
731+
"vendor" : "Micron",
732+
"physid" : "2",
733+
"serial" : "101B6540",
734+
"slot" : "DIMM_A6",
735+
"width" : 64,
736+
"clock" : 2133000000
737+
},
738+
{
739+
"id" : "bank:3",
740+
"class" : "memory",
741+
"claimed" : true,
742+
"handle" : "DMI:0059",
743+
"description" : "DIMM Synchronous [empty]",
744+
"product" : "NO DIMM",
745+
"vendor" : "NO DIMM",
746+
"physid" : "3",
747+
"serial" : "NO DIMM",
748+
"slot" : "DIMM_A7"
749+
}
750+
]
751+
},
752+
{
753+
"id" : "memory:4",
754+
"class" : "memory",
755+
"physid" : "1"
756+
},
757+
{
758+
"id" : "memory:5",
759+
"class" : "memory",
760+
"physid" : "2"
761+
}
762+
]
763+
}
764+
]
765+
}
766+
""", "")
767+
584768
LSHW_JSON_OUTPUT_ARM64 = ("""
585769
{
586770
"id" : "debian",
@@ -1838,6 +2022,16 @@ def test_get_memory_psutil_v2(self, mocked_execute, mocked_psutil):
18382022
self.assertEqual(3952 * 1024 * 1024, mem.total)
18392023
self.assertEqual(65536, mem.physical_mb)
18402024

2025+
@mock.patch('psutil.virtual_memory', autospec=True)
2026+
@mock.patch.object(utils, 'execute', autospec=True)
2027+
def test_get_memory_psutil_bank_size(self, mocked_execute, mocked_psutil):
2028+
mocked_psutil.return_value.total = 3952 * 1024 * 1024
2029+
mocked_execute.return_value = LSHW_JSON_OUTPUT_NO_MEMORY_BANK_SIZE
2030+
mem = self.hardware.get_memory()
2031+
2032+
self.assertEqual(3952 * 1024 * 1024, mem.total)
2033+
self.assertEqual(65536, mem.physical_mb)
2034+
18412035
@mock.patch('psutil.virtual_memory', autospec=True)
18422036
@mock.patch.object(utils, 'execute', autospec=True)
18432037
def test_get_memory_psutil_exception_v1(self, mocked_execute,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
fixes:
3+
- |
4+
The lshw package version B.02.19.2-5 on CentOS 8.4 and 8.5 contains a `bug
5+
<https://bugzilla.redhat.com/show_bug.cgi?id=1955250>`_ that prevents the
6+
size of individual memory banks from being reported, with the result that
7+
the total memory size would be reported as 0 in some places. The total
8+
memory size is now taken from lshw's total memory size output (which does
9+
not suffer from the same problem) when available.

0 commit comments

Comments
 (0)