Browse Source

hardware: Raise useful error for invalid mempage size

The 'hw:mem_page_size' extra spec allows you to specify values in three
ways: via a relative size alias (large, small, any), via exact byte
values, and via an SI/IEC/mixed representation (e.g. 2MB, 1GB).
Currently, attempting to use an invalid variant of the latter, such as
'2M', will raise a rather confusing error message:

  Invalid memory page size '0'

Correct this so we instead see something like so:

  Invalid memory page size '2M'

Changes:
	nova/virt/hardware.py

NOTE(stephenfin): We need to drop use of 'raise...from', which is Python
3-only syntax. The net effect of this change is uglier (but still valid)
tracebacks.

Change-Id: I3af99f03514904da09f7b71f430b43bf75e8be67
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Closes-Bug: #1879964
(cherry picked from commit f76d09b19d)
(cherry picked from commit eedc0b5ff3)
changes/50/735950/5
Stephen Finucane 2 months ago
parent
commit
58c9c3f238
2 changed files with 16 additions and 14 deletions
  1. +10
    -2
      nova/tests/unit/virt/test_hardware.py
  2. +6
    -12
      nova/virt/hardware.py

+ 10
- 2
nova/tests/unit/virt/test_hardware.py View File

@@ -2429,15 +2429,23 @@ class VirtMemoryPagesTestCase(test.NoDBTestCase):
spec={"hw:mem_page_size": "2048"}))

def test_get_requested_mempages_pagesize_from_flavor_invalid(self):
self.assertRaises(
ex = self.assertRaises(
exception.MemoryPageSizeInvalid,
self._test_get_requested_mempages_pagesize,
{"hw:mem_page_size": "foo"})
self.assertIn("foo", str(ex))

self.assertRaises(
ex = self.assertRaises(
exception.MemoryPageSizeInvalid,
self._test_get_requested_mempages_pagesize,
{"hw:mem_page_size": "-42"})
self.assertIn("-42", str(ex))

ex = self.assertRaises(
exception.MemoryPageSizeInvalid,
self._test_get_requested_mempages_pagesize,
{"hw:mem_page_size": "2M"})
self.assertIn("2M", str(ex))

def test_get_requested_mempages_pagesizes_from_flavor_suffix_sweep(self):
self.assertEqual(


+ 6
- 12
nova/virt/hardware.py View File

@@ -1341,21 +1341,15 @@ def _get_numa_pagesize_constraint(flavor, image_meta):
return MEMPAGES_LARGE
elif request == "small":
return MEMPAGES_SMALL
else:
try:
request = int(request)
except ValueError:
try:
request = strutils.string_to_bytes(
request, return_int=True) / units.Ki
except ValueError:
request = 0
elif request.isdigit():
return int(request)

if request <= 0:
try:
return strutils.string_to_bytes(
request, return_int=True) / units.Ki
except ValueError:
raise exception.MemoryPageSizeInvalid(pagesize=request)

return request

flavor_request, image_request = _get_flavor_image_meta(
'mem_page_size', flavor, image_meta)



Loading…
Cancel
Save