Skip to content
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

fix: the parser "LvmConfig" raises exception #3476

Merged
merged 5 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions insights/parsers/lvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,17 +695,10 @@ def parse_content(self, content):
self.data = lvm_conf_dict


def _lvm_render(o):
if isinstance(o, dict):
parts = ['"%s": %s' % (k, _lvm_render(v)) for k, v in o.items()]
return "{%s}" % ",".join(parts)
return "%s" % o


@parser(Specs.lvmconfig)
class LvmConfig(CommandParser):
def parse_content(self, content):
dd = defaultdict(dict)
self.data = defaultdict(dict)
xiangce marked this conversation as resolved.
Show resolved Hide resolved
key = None
for line in content:
line = line.rstrip()
Expand All @@ -718,10 +711,16 @@ def parse_content(self, content):
key = None
elif line[0] == "\t":
k, v = line.strip().split("=", 1)
dd[key][k] = v
# umask=077 will raise exception, and also no need to
# transfer it, just keep it as string
if k != 'umask':
try:
v = json.loads(v)
except Exception:
raise SkipException("Failed to parse line %s." % line)
xiangce marked this conversation as resolved.
Show resolved Hide resolved
self.data[key][k] = v
else:
pass # inferring this a stderr, so skipping
self.data = json.loads(_lvm_render(dict(dd)))


@parser(Specs.lvm_system_devices)
Expand Down
12 changes: 12 additions & 0 deletions insights/tests/parsers/lvm_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,15 @@
system_id=""
host_id=0
}""".strip()

LVMCONFIG2 = """
global {
umask=077
test=0
units="r"
si_unit_consistency=1
suffix=1
activation=1
fallback_to_lvm1=0
}
""".strip()
7 changes: 6 additions & 1 deletion insights/tests/parsers/test_lvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from insights.parsers import SkipException
from insights.parsers import lvm
from insights.tests import context_wrap
from .lvm_test_data import LVMCONFIG
from .lvm_test_data import LVMCONFIG, LVMCONFIG2

WARNINGS_CONTENT = """
WARNING
Expand Down Expand Up @@ -189,6 +189,11 @@ def test_lvmconfig():
assert p.data["global"]["thin_check_options"] == ["-q", "--clear-needs-check-flag"]


def test_lvmconfig_2():
p = lvm.LvmConfig(context_wrap(LVMCONFIG2))
assert p.data['global']['umask'] == '077'


def test_vgsheading_warnings():
result = lvm.VgsHeadings(context_wrap(VGSHEADING_CONTENT))
assert len(result.warnings) == 6
Expand Down