Skip to content

Commit

Permalink
fix: update ls specs, parser definition & test files to support new o…
Browse files Browse the repository at this point in the history
…ptions

Signed-off-by: Rahul Sharma <rahushar@redhat.com>
  • Loading branch information
rahulXs committed Oct 1, 2024
1 parent 7ff8c5e commit ad7b2d1
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 63 deletions.
9 changes: 5 additions & 4 deletions insights/parsers/ls_boot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
LsBoot - command ``ls -lanR /boot``
===================================
LsBoot - command ``ls -lanR /boot`` or ``ls -alZR /boot``
=========================================================
The ``ls -lanR /boot`` command provides information for the listing of the
The ``ls -lanR /boot`` or ``ls -alZR /boot`` command provides information for the listing of the
``/boot`` directory.
See :class:`insights.parsers.ls.FileListing` for more information.
Expand Down Expand Up @@ -54,4 +54,5 @@ class LsBoot(CommandParser, FileListing):
>>> bootdir.dir_contains('/boot/grub2', 'menu.lst')
True
"""
__root_path = '/boot'

__root_path = "/boot"
14 changes: 8 additions & 6 deletions insights/parsers/ls_dev.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
"""
LsDev - Command ``ls -lanR /dev``
=================================
LsDev - Command ``ls -lanR /dev`` or ``ls -alZR /dev``
======================================================
The ``ls -lanR /dev`` command provides information for the listing of the
The ``ls -lanR /dev`` or ``ls -alZR /dev`` command provides information for the listing of the
``/dev`` directory.
See :class:`insights.parsers.ls.FileListing` for more information.
"""
from insights import CommandParser, FileListing, parser
from insights import CommandParser, parser
from insights.parsers.ls import FileListing
from insights.specs import Specs


@parser(Specs.ls_dev)
class LsDev(CommandParser, FileListing):
"""
Parses output of ``ls -lanR /dev`` command.
Parse the /dev directory listing using a standard FileListing parser.
.. warning::
Expand Down Expand Up @@ -59,4 +60,5 @@ class LsDev(CommandParser, FileListing):
>>> ls_dev.listing_of('/dev/rhel')['home']['link']
'../dm-2'
"""
__root_path = '/dev'

__root_path = "/dev"
10 changes: 6 additions & 4 deletions insights/parsers/ls_sys_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
============================================
The ``ls -lanR /sys/firmware`` command provides information for the listing of
the ``/sys/firmware`` directory.
the ``/sys/firmware`` directory. The parser class can also process ``ls -alZR /sys/firmware`` command output.
See :class:`insights.parsers.ls.FileListing` for more information.
"""

from insights import CommandParser, FileListing, parser
from insights import CommandParser, parser
from insights.parsers.ls import FileListing
from insights.specs import Specs


@parser(Specs.ls_sys_firmware)
class LsSysFirmware(CommandParser, FileListing):
"""
Parses output of ``ls -lanR /sys/firmware`` command.
Parses output of ``ls -lanR /sys/firmware`` or ``ls -alZR`` command.
.. warning::
Expand Down Expand Up @@ -55,4 +56,5 @@ class LsSysFirmware(CommandParser, FileListing):
>>> ls_sys_firmware.files_of("/sys/firmware/acpi")
['pm_profile']
"""
__root_path = '/sys/firmware'

__root_path = "/sys/firmware"
6 changes: 3 additions & 3 deletions insights/specs/sos_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ class SosSpecs(Specs):
kubelet_conf = simple_file("/etc/kubernetes/kubelet.conf")
libvirtd_qemu_log = glob_file(r"/var/log/libvirt/qemu/*.log")
locale = simple_file("sos_commands/i18n/locale")
ls_boot = simple_file("sos_commands/boot/ls_-lanR_.boot")
ls_dev = first_file(["sos_commands/block/ls_-lanR_.dev", "sos_commands/devicemapper/ls_-lanR_.dev"])
ls_sys_firmware = simple_file("sos_commands/boot/ls_-lanR_.sys.firmware")
ls_boot = first_file(["sos_commands/boot/ls_-lanR_.boot", "sos_commands/boot/ls_-alZR_.boot"])
ls_dev = first_file(["sos_commands/block/ls_-lanR_.dev", "sos_commands/devicemapper/ls_-lanR_.dev", "sos_commands/block/ls_-alZR_.dev", "sos_commands/devicemapper/ls_-alZR_.dev"])
ls_sys_firmware = first_file(["sos_commands/boot/ls_-lanR_.sys.firmware", "sos_commands/boot/ls_-alZR_.sys.firmware"])
lsblk = first_file(["sos_commands/block/lsblk", "sos_commands/filesys/lsblk"])
lsblk_pairs = simple_file("sos_commands/block/lsblk_-O_-P")
lscpu = simple_file("sos_commands/processor/lscpu")
Expand Down
110 changes: 90 additions & 20 deletions insights/tests/parsers/test_ls_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from insights.parsers.ls_boot import LsBoot
from insights.tests import context_wrap

LS_BOOT = """
LS_LANR_BOOT = """
/boot:
total 187380
dr-xr-xr-x. 3 0 0 4096 Mar 4 16:19 .
Expand All @@ -21,7 +21,24 @@
-rw-r--r--. 1 0 0 7040 Mar 29 13:30 grub.cfg
"""

LS_BOOT_LINKS = """
LS_ALZR_BOOT = """
/boot:
total 187380
dr-xr-xr-x. 3 root root system_u:object_r:boot_t:s0 4096 Oct 4 2023 .
dr-xr-xr-x. 19 root root system_u:object_r:boot_t:s0 4096 Apr 21 2023 ..
-rw-r--r--. 1 root root system_u:object_r:boot_t:s0 123891 Sep 30 2022 config-5.14.0-162.6.1.el9_1.aarch64
drwxr-xr-x. 6 root root system_u:object_r:boot_t:s0 111 Aug 28 16:42 grub2
/boot/grub2:
total 16
drwx------. 3 root root system_u:object_r:boot_t:s0 50 Aug 28 16:42 .
dr-xr-xr-x. 6 root root system_u:object_r:boot_t:s0 4096 Oct 4 2023 ..
drwx------. 2 root root system_u:object_r:boot_t:s0 25 Apr 21 2023 fonts
-rwx------. 1 root root system_u:object_r:boot_t:s0 6535 Apr 21 2023 grub.cfg
-rw-------. 1 root root unconfined_u:object_r:boot_t:s0 1024 Aug 28 16:42 grubenv
"""

LS_LANR_BOOT_LINKS = """
/boot:
total 187380
-rw-------. 1 root root 19143244 Mar 3 14:31 initramfs-2.6.32-504.el6.x86_64.img
Expand All @@ -30,35 +47,88 @@
-rwxr-xr-x. 1 root root 4153904 Sep 16 2014 vmlinuz-2.6.32-504.el6.x86_64
"""

LS_ALZR_BOOT_LINKS = """
/boot:
total 187380
lrwxrwxrwx. 1 root root system_u:object_r:boot_t:s0 32 Apr 21 2023 dtb -> dtb-5.14.0-162.6.1.el9_1.aarch64
drwxr-xr-x. 11 root root system_u:object_r:boot_t:s0 129 Apr 21 2023 dtb-5.14.0-162.6.1.el9_1.aarch64
-rw-------. 1 root root system_u:object_r:boot_t:s0 103122590 Apr 21 2023 initramfs-0-rescue-6c7d8d74767a419884156df2b0005e3e.img
-rw-------. 1 root root system_u:object_r:boot_t:s0 54790153 Apr 21 2023 initramfs-5.14.0-162.6.1.el9_1.aarch64.img
-rw-------. 1 root root system_u:object_r:boot_t:s0 32324096 Oct 4 2023 initramfs-5.14.0-162.6.1.el9_1.aarch64kdump.img
drwxr-xr-x. 3 root root system_u:object_r:boot_t:s0 21 Apr 21 2023 loader
lrwxrwxrwx. 1 root root system_u:object_r:boot_t:s0 52 Apr 21 2023 symvers-5.14.0-162.6.1.el9_1.aarch64.gz -> /lib/modules/5.14.0-162.6.1.el9_1.aarch64/symvers.gz
"""


def test_ls_boot():
ls_boot = LsBoot(context_wrap(LS_BOOT))
assert '/boot' in ls_boot
assert '/boot/grub2' in ls_boot
assert ls_boot.dirs_of('/boot') == ['.', '..', 'grub2']

grub2_files = ls_boot.files_of('/boot/grub2')
assert 'menu.lst' in grub2_files
assert 'device.map' in grub2_files
assert 'grub.cfg' in grub2_files
ls_boot = LsBoot(context_wrap(LS_LANR_BOOT))
assert "/boot" in ls_boot
assert "/boot/grub2" in ls_boot
assert ls_boot.dirs_of("/boot") == [".", "..", "grub2"]

grub2_files = ls_boot.files_of("/boot/grub2")
assert "menu.lst" in grub2_files
assert "device.map" in grub2_files
assert "grub.cfg" in grub2_files
assert len(grub2_files) == 3

boot_files = ls_boot.files_of('/boot')
boot_files = ls_boot.files_of("/boot")
assert "config-3.10.0-229.14.1.el7.x86_64" in boot_files
assert len(boot_files) == 1

ls_alzr_boot = LsBoot(context_wrap(LS_ALZR_BOOT))
assert "/boot" in ls_alzr_boot
assert ls_alzr_boot.files_of("/boot") == ["config-5.14.0-162.6.1.el9_1.aarch64"]
assert ls_alzr_boot.dirs_of("/boot") == [".", "..", "grub2"]
assert ls_alzr_boot.get("/boot/grub2")["entries"].get("grub.cfg") == {
"type": "-",
"perms": "rwx------.",
"links": 1,
"owner": "root",
"group": "root",
"size": 6535,
"se_user": "system_u",
"se_role": "object_r",
"se_type": "boot_t",
"se_mls": "s0",
"name": "grub.cfg",
"date": "Apr 21 2023",
"raw_entry": "-rwx------. 1 root root system_u:object_r:boot_t:s0 6535 Apr 21 2023 grub.cfg",
"dir": "/boot/grub2",
}


def test_boot_links():
ls_boot = LsBoot(context_wrap(LS_BOOT_LINKS))
boot_files = ls_boot.files_of('/boot')
assert '/boot' in ls_boot
assert 'initramfs-2.6.32-504.el6.x86_64.img' in boot_files
assert 'initrd' in boot_files
assert 'vmlinuz' in boot_files
assert 'vmlinuz-2.6.32-504.el6.x86_64' in boot_files
ls_boot = LsBoot(context_wrap(LS_LANR_BOOT_LINKS))
boot_files = ls_boot.files_of("/boot")
assert "/boot" in ls_boot
assert "initramfs-2.6.32-504.el6.x86_64.img" in boot_files
assert "initrd" in boot_files
assert "vmlinuz" in boot_files
assert "vmlinuz-2.6.32-504.el6.x86_64" in boot_files

ls_alzr_boot = LsBoot(context_wrap(LS_ALZR_BOOT_LINKS))
assert ls_alzr_boot.total_of("/boot") == 187380
assert ls_alzr_boot.get("/boot")["entries"].get("dtb") == {
"type": "l",
"perms": "rwxrwxrwx.",
"links": 1,
"owner": "root",
"group": "root",
"size": 32,
"se_user": "system_u",
"se_role": "object_r",
"se_type": "boot_t",
"se_mls": "s0",
"name": "dtb",
"date": "Apr 21 2023",
"link": "dtb-5.14.0-162.6.1.el9_1.aarch64",
"raw_entry": "lrwxrwxrwx. 1 root root system_u:object_r:boot_t:s0 32 Apr 21 2023 dtb -> dtb-5.14.0-162.6.1.el9_1.aarch64",
"dir": "/boot",
}


def test_doc_examples():
env = {'bootdir': LsBoot(context_wrap(LS_BOOT))}
env = {"bootdir": LsBoot(context_wrap(LS_LANR_BOOT))}
failed, total = doctest.testmod(ls_boot, globs=env)
assert failed == 0
141 changes: 118 additions & 23 deletions insights/tests/parsers/test_ls_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,132 @@
lrwxrwxrwx. 1 0 0 7 Jul 25 10:00 rhel-swap -> ../dm-1
"""

LS_ALZR_DEV = """
/dev:
total 0
drwxr-xr-x. 19 root root system_u:object_r:device_t:s0 3300 Sep 30 16:58 .
dr-xr-xr-x. 18 root root system_u:object_r:root_t:s0 235 Apr 21 2023 ..
crw-r--r--. 1 root root system_u:object_r:autofs_device_t:s0 10, 235 Sep 30 16:58 autofs
drwxr-xr-x. 2 root root system_u:object_r:device_t:s0 180 Sep 30 16:58 block
lrwxrwxrwx. 1 root root system_u:object_r:device_t:s0 12 Sep 30 16:58 initctl -> /run/initctl
brw-rw----. 1 root disk system_u:object_r:fixed_disk_device_t:s0 252, 0 Sep 30 16:58 vda
brw-rw----. 1 root disk system_u:object_r:fixed_disk_device_t:s0 252, 1 Sep 30 16:58 vda1
brw-rw----. 1 root disk system_u:object_r:fixed_disk_device_t:s0 252, 2 Sep 30 16:58 vda2
/dev/mapper:
total 0
drwxr-xr-x. 2 root root system_u:object_r:device_t:s0 100 Sep 30 16:58 .
drwxr-xr-x. 19 root root system_u:object_r:device_t:s0 3300 Sep 30 16:58 ..
crw-------. 1 root root system_u:object_r:lvm_control_t:s0 10, 236 Sep 30 16:58 control
lrwxrwxrwx. 1 root root system_u:object_r:device_t:s0 7 Sep 30 16:58 rhel-root -> ../dm-0
lrwxrwxrwx. 1 root root system_u:object_r:device_t:s0 7 Sep 30 16:58 rhel-swap -> ../dm-1
/dev/rhel:
total 0
drwxr-xr-x. 2 root root system_u:object_r:device_t:s0 80 Sep 30 16:58 .
drwxr-xr-x. 19 root root system_u:object_r:device_t:s0 3300 Sep 30 16:58 ..
lrwxrwxrwx. 1 root root system_u:object_r:device_t:s0 7 Sep 30 16:58 root -> ../dm-0
lrwxrwxrwx. 1 root root system_u:object_r:device_t:s0 7 Sep 30 16:58 swap -> ../dm-1
"""


def test_ls_dev():
lsdev = LsDev(context_wrap(LS_DEV))
assert lsdev.listing_of("/dev/rhel") == {
'home': {'group': '0', 'name': 'home', 'links': 1, 'perms': 'rwxrwxrwx.',
'raw_entry': 'lrwxrwxrwx. 1 0 0 7 Jul 25 10:00 home -> ../dm-2',
'owner': '0', 'link': '../dm-2', 'date': 'Jul 25 10:00',
'type': 'l', 'size': 7, 'dir': '/dev/rhel'},
'root': {'group': '0', 'name': 'root', 'links': 1, 'perms': 'rwxrwxrwx.',
'raw_entry': 'lrwxrwxrwx. 1 0 0 7 Jul 25 10:00 root -> ../dm-0',
'owner': '0', 'link': '../dm-0', 'date': 'Jul 25 10:00',
'type': 'l', 'size': 7, 'dir': '/dev/rhel'},
'swap': {'group': '0', 'name': 'swap', 'links': 1, 'perms': 'rwxrwxrwx.',
'raw_entry': 'lrwxrwxrwx. 1 0 0 7 Jul 25 10:00 swap -> ../dm-1',
'owner': '0', 'link': '../dm-1', 'date': 'Jul 25 10:00',
'type': 'l', 'size': 7, 'dir': '/dev/rhel'},
'..': {'group': '0', 'name': '..', 'links': 23, 'perms': 'rwxr-xr-x.',
'raw_entry': 'drwxr-xr-x. 23 0 0 3720 Jul 25 12:43 ..',
'owner': '0', 'date': 'Jul 25 12:43', 'type': 'd', 'size': 3720,
'dir': '/dev/rhel'},
'.': {'group': '0', 'name': '.', 'links': 2, 'perms': 'rwxr-xr-x.',
'raw_entry': 'drwxr-xr-x. 2 0 0 100 Jul 25 10:00 .', 'owner': '0',
'date': 'Jul 25 10:00', 'type': 'd', 'size': 100, 'dir': '/dev/rhel'}}
expected = ['docker-253:0-1443032-pool', 'rhel-home', 'rhel-root', 'rhel-swap']
actual = lsdev.listings.get("/dev/mapper")['files']
"home": {
"group": "0",
"name": "home",
"links": 1,
"perms": "rwxrwxrwx.",
"raw_entry": "lrwxrwxrwx. 1 0 0 7 Jul 25 10:00 home -> ../dm-2",
"owner": "0",
"link": "../dm-2",
"date": "Jul 25 10:00",
"type": "l",
"size": 7,
"dir": "/dev/rhel",
},
"root": {
"group": "0",
"name": "root",
"links": 1,
"perms": "rwxrwxrwx.",
"raw_entry": "lrwxrwxrwx. 1 0 0 7 Jul 25 10:00 root -> ../dm-0",
"owner": "0",
"link": "../dm-0",
"date": "Jul 25 10:00",
"type": "l",
"size": 7,
"dir": "/dev/rhel",
},
"swap": {
"group": "0",
"name": "swap",
"links": 1,
"perms": "rwxrwxrwx.",
"raw_entry": "lrwxrwxrwx. 1 0 0 7 Jul 25 10:00 swap -> ../dm-1",
"owner": "0",
"link": "../dm-1",
"date": "Jul 25 10:00",
"type": "l",
"size": 7,
"dir": "/dev/rhel",
},
"..": {
"group": "0",
"name": "..",
"links": 23,
"perms": "rwxr-xr-x.",
"raw_entry": "drwxr-xr-x. 23 0 0 3720 Jul 25 12:43 ..",
"owner": "0",
"date": "Jul 25 12:43",
"type": "d",
"size": 3720,
"dir": "/dev/rhel",
},
".": {
"group": "0",
"name": ".",
"links": 2,
"perms": "rwxr-xr-x.",
"raw_entry": "drwxr-xr-x. 2 0 0 100 Jul 25 10:00 .",
"owner": "0",
"date": "Jul 25 10:00",
"type": "d",
"size": 100,
"dir": "/dev/rhel",
},
}
expected = ["docker-253:0-1443032-pool", "rhel-home", "rhel-root", "rhel-swap"]
actual = lsdev.files_of("/dev/mapper")
assert actual == expected

assert lsdev.listings.get("/dev/mapper")['entries']['rhel-home']['link'] == "../dm-2"
assert lsdev.get("/dev/mapper")["entries"]["rhel-home"]["link"] == "../dm-2"

ls_alzr_dev = LsDev(context_wrap(LS_ALZR_DEV))
assert "/dev/rhel" in ls_alzr_dev
assert ls_alzr_dev.files_of("/dev") == ["initctl"]
assert ls_alzr_dev.get("/dev")["specials"] == ["autofs", "vda", "vda1", "vda2"]
assert ls_alzr_dev.get("/dev/rhel")["entries"].get("root") == {
"type": "l",
"perms": "rwxrwxrwx.",
"links": 1,
"owner": "root",
"group": "root",
"size": 7,
"se_user": "system_u",
"se_role": "object_r",
"se_type": "device_t",
"se_mls": "s0",
"name": "root",
"date": "Sep 30 16:58",
"link": "../dm-0",
"raw_entry": "lrwxrwxrwx. 1 root root system_u:object_r:device_t:s0 7 Sep 30 16:58 root -> ../dm-0",
"dir": "/dev/rhel",
}


def test_doc_examples():
env = {'ls_dev': LsDev(context_wrap(LS_DEV))}
env = {"ls_dev": LsDev(context_wrap(LS_DEV))}
failed, total = doctest.testmod(ls_dev, globs=env)
assert failed == 0
Loading

0 comments on commit ad7b2d1

Please sign in to comment.