-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add spec parser and combiner for grubby_info #4329
Open
JoySnow
wants to merge
1
commit into
RedHatInsights:master
Choose a base branch
from
JoySnow:add-grubby-info
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+317
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,38 @@ | ||
""" | ||
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 current default boot entry | ||
|
||
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 | ||
|
||
@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] |
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,67 @@ | ||
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() | ||
|
||
|
||
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 len(result.boot_entries) == 3 | ||
|
||
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 | ||
assert "DEFAULT index 3 not exist in parsed boot_entries: [0, 1, 2]" in str(excinfo.value) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a combiner, it would be good to combine more useful attribute here, e.g. devs can use the combiner rather than access each single parser to fetch particular info.