From 3dfcc1b7aacb31b290896f6838d61fa8843f131d Mon Sep 17 00:00:00 2001 From: Xiaoxue Wang Date: Tue, 14 Jan 2025 19:46:32 +0800 Subject: [PATCH] feat: deprecate GrubbyDefaultKernel with Grubby combiner Signed-off-by: Xiaoxue Wang --- insights/combiners/grubby.py | 13 +++++++++---- insights/parsers/grubby.py | 10 ++++++++++ insights/tests/combiners/test_grubby.py | 22 ++++++++++++++++------ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/insights/combiners/grubby.py b/insights/combiners/grubby.py index 72733d384..1e003e426 100644 --- a/insights/combiners/grubby.py +++ b/insights/combiners/grubby.py @@ -21,7 +21,9 @@ class Grubby(object): Attributes: boot_entries (dict): All boot entries indexed by the entry "index" - default_index (int): the numeric index of the current default boot entry + default_index (int): The numeric index of the default boot entry + default_boot_entry (dict): The boot information for default kernel + default_kernel (str): The path of the default kernel Raises: ParseException: when parsing into error. @@ -30,9 +32,12 @@ def __init__(self, grubby_info_all, grubby_default_index): self.boot_entries = grubby_info_all.boot_entries self.default_index = grubby_default_index.default_index - @property - def default_boot_entry(self): if self.default_index not in self.boot_entries: raise ParseException("DEFAULT index %s not exist in parsed boot_entries: %s" % (self.default_index, list(self.boot_entries.keys()))) - return self.boot_entries[self.default_index] + self.default_boot_entry = self.boot_entries[self.default_index] + + self.default_kernel = self.default_boot_entry.get("kernel") + if not self.default_kernel: + raise ParseException("DEFAULT kernel-path not exist in default-index: %s" % + self.default_index) diff --git a/insights/parsers/grubby.py b/insights/parsers/grubby.py index 3c08205b0..4e0d3eb69 100644 --- a/insights/parsers/grubby.py +++ b/insights/parsers/grubby.py @@ -18,6 +18,7 @@ from insights.core.exceptions import ParseException, SkipComponent from insights.core.plugins import parser from insights.specs import Specs +from insights.util import deprecated @parser(Specs.grubby_default_index) @@ -51,6 +52,10 @@ def parse_content(self, content): @parser(Specs.grubby_default_kernel) class GrubbyDefaultKernel(CommandParser): """ + .. warning:: + This class is deprecated and will be removed from 3.7.0. + Please use the :class:`insights.combiners.grubby.Grubby` instead. + This parser parses the output of command ``grubby --default-kernel``. The typical output of this command is:: @@ -69,6 +74,11 @@ class GrubbyDefaultKernel(CommandParser): Attributes: default_kernel(str): The default kernel name for next boot """ + def __init__(self, context): + deprecated(GrubbyDefaultKernel, + "Please use the :class:`insights.combiners.grubby.Grubby` instead.", "3.7.0") + super(GrubbyDefaultKernel, self).__init__(context) + def parse_content(self, content): if not content: raise SkipComponent('Empty output') diff --git a/insights/tests/combiners/test_grubby.py b/insights/tests/combiners/test_grubby.py index c2fba64d1..0f4616639 100644 --- a/insights/tests/combiners/test_grubby.py +++ b/insights/tests/combiners/test_grubby.py @@ -31,6 +31,12 @@ id="4d684a4a6166439a867e701ded4f7e10-0-rescue" """.strip() +GRUBBY_INFO_ALL_AB_1 = """ +index=0 +title="Red Hat Enterprise Linux (5.14.0-162.6.1.el9_1.x86_64) 9.1 (Plow)" +id="4d684a4a6166439a867e701ded4f7e10-5.14.0-162.6.1.el9_1.x86_64" +""".strip() + def test_grubby(): grubby_info_all = GrubbyInfoAll(context_wrap(GRUBBY_INFO_ALL)) @@ -53,15 +59,19 @@ def test_grubby(): title="Red Hat Enterprise Linux (5.14.0-162.6.1.el9_1.x86_64) 9.1 (Plow)", id="4d684a4a6166439a867e701ded4f7e10-5.14.0-162.6.1.el9_1.x86_64", ) + assert result.default_kernel == "/boot/vmlinuz-5.14.0-162.6.1.el9_1.x86_64" assert len(result.boot_entries) == 3 + +def test_grubby_ab(): grubby_info_all = GrubbyInfoAll(context_wrap(GRUBBY_INFO_ALL)) grubby_default_index = GrubbyDefaultIndex(context_wrap(DEFAULT_INDEX_2)) - result = Grubby(grubby_info_all, grubby_default_index) - - assert result.default_index == 3 - assert len(result.boot_entries) == 3 - with pytest.raises(ParseException) as excinfo: - result.default_boot_entry + Grubby(grubby_info_all, grubby_default_index) assert "DEFAULT index 3 not exist in parsed boot_entries: [0, 1, 2]" in str(excinfo.value) + + grubby_info_all = GrubbyInfoAll(context_wrap(GRUBBY_INFO_ALL_AB_1)) + grubby_default_index = GrubbyDefaultIndex(context_wrap(DEFAULT_INDEX_1)) + with pytest.raises(ParseException) as excinfo: + Grubby(grubby_info_all, grubby_default_index) + assert "DEFAULT kernel-path not exist in default-index: 0" in str(excinfo.value)