Skip to content

Commit

Permalink
feat: deprecate GrubbyDefaultKernel with Grubby combiner
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxue Wang <[email protected]>
  • Loading branch information
JoySnow committed Jan 14, 2025
1 parent c1f020f commit 3dfcc1b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
13 changes: 9 additions & 4 deletions insights/combiners/grubby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
10 changes: 10 additions & 0 deletions insights/parsers/grubby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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::
Expand All @@ -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')
Expand Down
22 changes: 16 additions & 6 deletions insights/tests/combiners/test_grubby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand 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)

0 comments on commit 3dfcc1b

Please sign in to comment.