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

FEAT: add new parser LSlad #4262

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions insights/parsers/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
-----------------------------------
"""
from insights.core import ls_parser, Parser
from insights.core.exceptions import SkipComponent
from insights.core.filters import add_filter
from insights.core.plugins import parser
from insights.specs import Specs
Expand Down Expand Up @@ -352,3 +353,34 @@ class LSlaZ(FileListing):
See :py:class:`FileListing` for more information.
"""
pass


@parser(Specs.ls_lad)
class LSlad(Parser):
"""
Parses output of ``ls -lad <dirs>`` command.

Sample directory listing::
dr-xr-xr-x. 21 root root 4096 Oct 15 08:19 /
drwxr-xr-x. 3 root root 17 Apr 13 2023 /mnt

Examples:
>>> type(lslad)
<class 'insights.parsers.ls.LSlad'>
>>> '/' in lslad.entries
True
>>> lslad.entries.get('/').get('owner')
'root'
>>> lslad.entries.get('/mnt').get('perms')
'rwxr-xr-x.'
"""

def parse_content(self, content):
parsed_content = []
for line in content:
if 'No such file or directory' not in line:
parsed_content.append(line)
if not parsed_content:
raise SkipComponent
ls_data = ls_parser.parse(parsed_content, '').get('')
self.entries = ls_data.get('entries')
2 changes: 2 additions & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ class Specs(SpecSet):
ls_laRZ_dirs = RegistryPoint(filterable=True)
ls_laZ = RegistryPoint()
ls_laZ_dirs = RegistryPoint(filterable=True)
ls_lad = RegistryPoint()
ls_lad_dirs = RegistryPoint(filterable=True)
# Old `ls` Specs
ls_R_var_lib_nova_instances = RegistryPoint()
ls_boot = RegistryPoint()
Expand Down
5 changes: 5 additions & 0 deletions insights/specs/datasources/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ def list_with_laRZ(broker):
@datasource(HostContext)
def list_with_laZ(broker):
return _list_items(Specs.ls_laZ_dirs)


@datasource(HostContext)
def list_with_lad(broker):
return _list_items(Specs.ls_lad_dirs)
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ class DefaultSpecs(Specs):
ls_lanRL = command_with_args('/bin/ls -lanRl %s', ls.list_with_lanRL, keep_rc=True)
ls_laRZ = command_with_args('/bin/ls -laRZ %s', ls.list_with_laRZ, keep_rc=True)
ls_laZ = command_with_args('/bin/ls -laZ %s', ls.list_with_laZ, keep_rc=True)
ls_lad = command_with_args('/bin/ls -lad %s', ls.list_with_lad, keep_rc=True)
# Old `ls` Specs
ls_R_var_lib_nova_instances = simple_command("/bin/ls -laR /var/lib/nova/instances")
ls_boot = simple_command("/bin/ls -lanR /boot")
Expand Down
13 changes: 10 additions & 3 deletions insights/tests/datasources/test_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
list_with_la, list_with_la_filtered,
list_with_lan, list_with_lan_filtered,
list_with_lanL, list_with_lanR, list_with_lanRL,
list_with_laRZ, list_with_laZ)
list_with_laRZ, list_with_laZ, list_with_lad)


def setup_function(func):
Expand All @@ -31,14 +31,16 @@ def setup_function(func):
filters.add_filter(Specs.ls_laRZ_dirs, ['/boot'])
if func is test_lanZ:
filters.add_filter(Specs.ls_laZ_dirs, ["/", '/mnt'])
if func is test_lad:
filters.add_filter(Specs.ls_lad_dirs, ["/", '/mnt'])


def teardown_function(func):
for spec in (
Specs.ls_la_dirs, Specs.ls_la_filtered, Specs.ls_la_filtered_dirs,
Specs.ls_lan_dirs, Specs.ls_lan_filtered, Specs.ls_lan_filtered_dirs,
Specs.ls_lanL_dirs, Specs.ls_lanR_dirs, Specs.ls_lanRL_dirs,
Specs.ls_laRZ_dirs, Specs.ls_laZ_dirs):
Specs.ls_laRZ_dirs, Specs.ls_laZ_dirs, Specs.ls_lad_dirs):
if spec in filters._CACHE:
del filters._CACHE[spec]

Expand Down Expand Up @@ -75,7 +77,7 @@ def test_lanL():

def test_lanR():
ret = list_with_lanR({})
assert '/ ' in ret
assert '/' in ret


def test_lanRL():
Expand All @@ -91,3 +93,8 @@ def test_lanRZ():
def test_lanZ():
ret = list_with_laZ({})
assert ret == '/ /mnt'


def test_lad():
ret = list_with_lad({})
assert ret == '/ /mnt'
14 changes: 13 additions & 1 deletion insights/tests/parsers/test_ls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from insights.parsers.ls import (
LSla, LSlaFiltered, LSlan, LSlanFiltered,
LSlanL, LSlanR, LSlanRL, LSlaRZ, LSlaZ
LSlanL, LSlanR, LSlanRL, LSlaRZ, LSlaZ, LSlad
)
from insights.tests import context_wrap

Expand Down Expand Up @@ -290,6 +290,11 @@
crw-rw-rw-. 1 root root system_u:object_r:vfio_device_t:s0 10, 196 May 10 09:54 vfio -> false_link_2
"""

LS_LAD_CONTENT = """
dr-xr-xr-x. 21 root root 4096 Oct 15 08:19 /
drwxr-xr-x. 3 root root 17 Apr 13 2023 /mnt
""".strip()


def test_ls_la():
ls = LSla(context_wrap(LS_LA))
Expand Down Expand Up @@ -537,3 +542,10 @@ def test_ls_laZ_on_dev():
dev_listings = ls.listing_of('/dev/vfio')
assert 'vfio' in dev_listings
assert dev_listings["vfio"]['link'] == 'false_link_2'


def test_ls_lad():
ls_lad = LSlad(context_wrap(LS_LAD_CONTENT))
assert len(ls_lad.entries) == 2
assert '/' in ls_lad.entries
assert ls_lad.entries.get('/') == {'type': 'd', 'perms': 'r-xr-xr-x.', 'links': 21, 'owner': 'root', 'group': 'root', 'size': 4096, 'date': 'Oct 15 08:19', 'name': '/', 'raw_entry': 'dr-xr-xr-x. 21 root root 4096 Oct 15 08:19 /', 'dir': ''}
12 changes: 10 additions & 2 deletions insights/tests/parsers/test_ls_file_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import doctest

from insights.parsers import ls as ls_module
from insights.parsers.ls import FileListing
from insights.parsers.ls import FileListing, LSlad
from insights.tests import context_wrap

SINGLE_DIRECTORY = """
Expand Down Expand Up @@ -137,6 +137,12 @@
# but still fit the patterns? What should the parser do with such entries?


LS_LAD_CONTENT = """
dr-xr-xr-x. 21 root root 4096 Oct 15 08:19 /
drwxr-xr-x. 3 root root 17 Apr 13 2023 /mnt
""".strip()


def test_single_directory():
# ctx = context_wrap(SINGLE_DIRECTORY, path='ls_-la_.etc.pki.tls')
ctx = context_wrap(SINGLE_DIRECTORY)
Expand Down Expand Up @@ -320,6 +326,8 @@ def test_files_created_with_selinux_disabled():


def test_doc_example():
env = {'ls_lan': FileListing(context_wrap(FILE_LISTING_DOC))}
env = {'ls_lan': FileListing(context_wrap(FILE_LISTING_DOC)),
"lslad": LSlad(context_wrap(LS_LAD_CONTENT)),
}
failed, total = doctest.testmod(ls_module, globs=env)
assert failed == 0
Loading