From 05f1a06193bca8814ce25b367debef60e5c0d3ad 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 | 11 ++++++++++- test/unit_tests/test_interface.py | 12 +++++++++++- test/unit_tests/test_kickstart.py | 7 ++++++- 4 files changed, 28 insertions(+), 4 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..eea56a5 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.""" @@ -78,6 +79,14 @@ def fadump_enabled(self, value): self.fadump_enabled_changed.emit() log.debug("Fadump enabled is set to '%s'.", value) + def check_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)) + return value + @property def reserved_memory(self): """Amount of memory in MB to reserve for kdump.""" @@ -85,7 +94,7 @@ def reserved_memory(self): @reserved_memory.setter def reserved_memory(self, value): - self._reserved_memory = value + self._reserved_memory = self.check_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..4689767 100644 --- a/test/unit_tests/test_interface.py +++ b/test/unit_tests/test_interface.py @@ -1,7 +1,9 @@ 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 @@ -48,7 +50,15 @@ def test_fadump_enabled(self): self._check_properties_changed("FadumpEnabled", True) self.assertEqual(self._interface.FadumpEnabled, True) - def test_reserved_memory(self): + @patch("com_redhat_kdump.service.kdump.KdumpService.check_reserved_memory", return_value="256") + def test_reserved_memory(self, _mock_read): self._interface.ReservedMemory = "256" self._check_properties_changed("ReservedMemory", "256") self.assertEqual(self._interface.ReservedMemory, "256") + + @patch("com_redhat_kdump.common.getMemoryBounds", return_value=(500, 800, 1)) + def test_check_reserved_memory(self, _mock_read): + self._service._lower , self._service._upper, self._service._step = common.getMemoryBounds() + self.assertEqual(self._service.check_reserved_memory("900"), "800") + self.assertEqual(self._service.check_reserved_memory("400"), "500") + self.assertEqual(self._service.check_reserved_memory("600"), "600") \ No newline at end of file diff --git a/test/unit_tests/test_kickstart.py b/test/unit_tests/test_kickstart.py index 746c242..4f063de 100644 --- a/test/unit_tests/test_kickstart.py +++ b/test/unit_tests/test_kickstart.py @@ -1,6 +1,8 @@ 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 @@ -68,7 +70,10 @@ def test_ks_disable(self): %end """) - def test_ks_reserve_mb(self): + @patch("com_redhat_kdump.common.getMemoryBounds", return_value=(160, 800, 1)) + def test_ks_reserve_mb(self, _mock_read): + self._service._lower , self._service._upper, self._service._step = common.getMemoryBounds() + self.assertEqual((self._service._lower , self._service._upper), (160, 800)) self._check_ks_input(""" %addon com_redhat_kdump --enable --reserve-mb=256 %end