From 993a3f9e6cdb6ec32f10559449ede99c04582b13 Mon Sep 17 00:00:00 2001 From: iasunsea Date: Tue, 9 May 2023 20:00:07 +0800 Subject: [PATCH] check 'reserve-mb' number with kickstart --- com_redhat_kdump/gui/spokes/kdump.py | 2 +- com_redhat_kdump/service/kdump.py | 6 ++++++ test/unit_tests/test_interface.py | 21 ++++++++++++++++++--- test/unit_tests/test_kickstart.py | 23 +++++++++++++++++++---- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/com_redhat_kdump/gui/spokes/kdump.py b/com_redhat_kdump/gui/spokes/kdump.py index f9de748..c313f43 100644 --- a/com_redhat_kdump/gui/spokes/kdump.py +++ b/com_redhat_kdump/gui/spokes/kdump.py @@ -144,7 +144,7 @@ def apply(self): if self._autoButton.get_active(): self._proxy.ReservedMemory = "auto" else: - self._proxy.ReservedMemory = "%dM" % self._toBeReservedSpin.get_value_as_int() + self._proxy.ReservedMemory = "%d" % self._toBeReservedSpin.get_value_as_int() self._proxy.FadumpEnabled = self._fadumpButton.get_active() # This hub have been visited, use should now be aware of the crypted devices issue diff --git a/com_redhat_kdump/service/kdump.py b/com_redhat_kdump/service/kdump.py index 3f2a14c..db743f6 100755 --- a/com_redhat_kdump/service/kdump.py +++ b/com_redhat_kdump/service/kdump.py @@ -49,6 +49,7 @@ def __init__(self): self._reserved_memory = "auto" self.reserved_memory_changed = Signal() + self._lower, self._upper, self._step = getMemoryBounds() def publish(self): """Publish the DBus objects.""" @@ -85,6 +86,11 @@ def reserved_memory(self): @reserved_memory.setter def reserved_memory(self, value): + if value != "auto": + if int(value) > self._upper: + value = str(int(self._upper)) + if int(value) < self._lower: + value = str(int(self._lower)) self._reserved_memory = value self.reserved_memory_changed.emit() log.debug("Reserved memory is set to '%s'.", value) diff --git a/test/unit_tests/test_interface.py b/test/unit_tests/test_interface.py index dbac003..bc2c541 100644 --- a/test/unit_tests/test_interface.py +++ b/test/unit_tests/test_interface.py @@ -1,11 +1,22 @@ from unittest.case import TestCase +from unittest.mock import patch from unittest.mock import Mock from com_redhat_kdump import common +from .mock import MockBuiltinRead from com_redhat_kdump.constants import KDUMP from com_redhat_kdump.service.kdump import KdumpService from com_redhat_kdump.service.kdump_interface import KdumpInterface +SYS_CRASH_SIZE = '/sys/kernel/kexec_crash_size' +PROC_MEMINFO = '/proc/meminfo' + +AARCH64_NFO_FIXTURE = { + SYS_CRASH_SIZE: "536870912", # 512MB + PROC_MEMINFO:"""MemTotal: 1572864 kB +""" # 2GB - 512MB +} + class PropertiesChangedCallback(Mock): @@ -48,7 +59,11 @@ def test_fadump_enabled(self): self._check_properties_changed("FadumpEnabled", True) self.assertEqual(self._interface.FadumpEnabled, True) - def test_reserved_memory(self): + @patch("builtins.open", MockBuiltinRead(AARCH64_NFO_FIXTURE)) + @patch("blivet.arch.get_arch", return_value="aarch64") + def test_reserved_memory_aarch64(self, _mock_read): + common._reservedMemory = None + self._service.__init__() self._interface.ReservedMemory = "256" - self._check_properties_changed("ReservedMemory", "256") - self.assertEqual(self._interface.ReservedMemory, "256") + self.assertEqual(self._interface.ReservedMemory, "512") + diff --git a/test/unit_tests/test_kickstart.py b/test/unit_tests/test_kickstart.py index 746c242..305f78a 100644 --- a/test/unit_tests/test_kickstart.py +++ b/test/unit_tests/test_kickstart.py @@ -1,8 +1,19 @@ from textwrap import dedent from unittest.case import TestCase +from unittest.mock import patch from com_redhat_kdump import common +from .mock import MockBuiltinRead from com_redhat_kdump.service.kdump import KdumpService +SYS_CRASH_SIZE = '/sys/kernel/kexec_crash_size' +PROC_MEMINFO = '/proc/meminfo' + +X86_LOWER_INFO_FIXTURE = { + SYS_CRASH_SIZE: "167772160", # 160MB + PROC_MEMINFO:"""MemTotal: 884736 kB +""" # 1GB - 160MB +} + class KdumpKickstartTestCase(TestCase): @@ -68,18 +79,22 @@ def test_ks_disable(self): %end """) - def test_ks_reserve_mb(self): + @patch("builtins.open", MockBuiltinRead(X86_LOWER_INFO_FIXTURE)) + @patch("blivet.arch.get_arch", return_value="x86_64") + def test_ks_reserve_mb_x86_with_up(self, _mock_read): + common._reservedMemory = None + self._service.__init__() self._check_ks_input(""" - %addon com_redhat_kdump --enable --reserve-mb=256 + %addon com_redhat_kdump --enable --reserve-mb=600 %end """) self.assertEqual(self._service.kdump_enabled, True) self.assertEqual(self._service.fadump_enabled, False) - self.assertEqual(self._service.reserved_memory, "256") + self.assertEqual(self._service.reserved_memory, "512") # upper = 1024M - 512M self._check_ks_output(""" - %addon com_redhat_kdump --enable --reserve-mb='256' + %addon com_redhat_kdump --enable --reserve-mb='512' %end """)