-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add spec parser and combiner for grubby_info (#4329)
Signed-off-by: Xiaoxue Wang <[email protected]>
- Loading branch information
Showing
7 changed files
with
356 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.. automodule:: insights.combiners.grubby | ||
:members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Grubby | ||
====== | ||
Combiner for command ``/usr/sbin/grubby`` parsers. | ||
This combiner uses the parsers: | ||
:class:`insights.parsers.grubby.GrubbyDefaultIndex`, | ||
:class:`insights.parsers.grubby.GrubbyInfoAll`. | ||
""" | ||
|
||
from insights.core.exceptions import ParseException | ||
from insights.core.plugins import combiner | ||
from insights.parsers.grubby import GrubbyDefaultIndex, GrubbyInfoAll | ||
|
||
|
||
@combiner(GrubbyInfoAll, GrubbyDefaultIndex) | ||
class Grubby(object): | ||
""" | ||
Combine command "grubby" parsers into one Combiner. | ||
Attributes: | ||
boot_entries (dict): All boot entries indexed by the entry "index" | ||
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. | ||
""" | ||
|
||
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 | ||
|
||
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())) | ||
) | ||
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from insights.combiners.grubby import Grubby | ||
from insights.core.exceptions import ParseException | ||
from insights.parsers.grubby import GrubbyInfoAll, GrubbyDefaultIndex | ||
from insights.tests import context_wrap | ||
import pytest | ||
|
||
DEFAULT_INDEX_1 = '0' | ||
DEFAULT_INDEX_2 = '3' | ||
|
||
GRUBBY_INFO_ALL = """ | ||
index=0 | ||
kernel="/boot/vmlinuz-5.14.0-162.6.1.el9_1.x86_64" | ||
args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet retbleed=stuff" | ||
root="/dev/mapper/rhel-root" | ||
initrd="/boot/initramfs-5.14.0-162.6.1.el9_1.x86_64.img" | ||
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" | ||
index=1 | ||
kernel="/boot/vmlinuz-5.14.0-70.13.1.el9_0.x86_64" | ||
args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet retbleed=stuff" | ||
root="/dev/mapper/rhel-root" | ||
initrd="/boot/initramfs-5.14.0-70.13.1.el9_0.x86_64.img" | ||
title="Red Hat Enterprise Linux (5.14.0-70.13.1.el9_0.x86_64) 9.0 (Plow)" | ||
id="4d684a4a6166439a867e701ded4f7e10-5.14.0-70.13.1.el9_0.x86_64" | ||
index=2 | ||
kernel="/boot/vmlinuz-0-rescue-4d684a4a6166439a867e701ded4f7e10" | ||
args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet retbleed=stuff" | ||
root="/dev/mapper/rhel-root" | ||
initrd="/boot/initramfs-0-rescue-4d684a4a6166439a867e701ded4f7e10.img" | ||
title="Red Hat Enterprise Linux (0-rescue-4d684a4a6166439a867e701ded4f7e10) 9.0 (Plow)" | ||
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)) | ||
grubby_default_index = GrubbyDefaultIndex(context_wrap(DEFAULT_INDEX_1)) | ||
result = Grubby(grubby_info_all, grubby_default_index) | ||
|
||
assert result.default_index == 0 | ||
assert result.default_boot_entry == dict( | ||
index=0, | ||
kernel="/boot/vmlinuz-5.14.0-162.6.1.el9_1.x86_64", | ||
args={ | ||
'ro': [True], | ||
'crashkernel': ['1G-4G:192M,4G-64G:256M,64G-:512M'], | ||
'resume': ['/dev/mapper/rhel-swap'], | ||
'rd.lvm.lv': ['rhel/root', 'rhel/swap'], | ||
'rhgb': [True], | ||
'quiet': [True], | ||
'retbleed': ['stuff'], | ||
}, | ||
root="/dev/mapper/rhel-root", | ||
initrd="/boot/initramfs-5.14.0-162.6.1.el9_1.x86_64.img", | ||
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)) | ||
with pytest.raises(ParseException) as excinfo: | ||
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) |
Oops, something went wrong.