From 79d9a021f37f3b2cc1e0389ebff83c944dcc84c7 Mon Sep 17 00:00:00 2001 From: Huanhuan Li Date: Wed, 30 Nov 2022 17:53:07 +0800 Subject: [PATCH 1/2] feat: New spec "ls -lZ /var/lib/rsyslog" and the parser Signed-off-by: Huanhuan Li --- .../ls_var_lib_rsyslog.rst | 3 ++ insights/parsers/ls_var_lib_rsyslog.py | 28 ++++++++++++++++ insights/specs/__init__.py | 1 + insights/specs/default.py | 1 + insights/specs/insights_archive.py | 1 + .../tests/parsers/test_ls_var_lib_rsyslog.py | 32 +++++++++++++++++++ 6 files changed, 66 insertions(+) create mode 100644 docs/shared_parsers_catalog/ls_var_lib_rsyslog.rst create mode 100644 insights/parsers/ls_var_lib_rsyslog.py create mode 100644 insights/tests/parsers/test_ls_var_lib_rsyslog.py diff --git a/docs/shared_parsers_catalog/ls_var_lib_rsyslog.rst b/docs/shared_parsers_catalog/ls_var_lib_rsyslog.rst new file mode 100644 index 0000000000..abad60b550 --- /dev/null +++ b/docs/shared_parsers_catalog/ls_var_lib_rsyslog.rst @@ -0,0 +1,3 @@ +.. automodule:: insights.parsers.ls_var_lib_rsyslog + :members: + :show-inheritance: diff --git a/insights/parsers/ls_var_lib_rsyslog.py b/insights/parsers/ls_var_lib_rsyslog.py new file mode 100644 index 0000000000..292a27ec3e --- /dev/null +++ b/insights/parsers/ls_var_lib_rsyslog.py @@ -0,0 +1,28 @@ +""" +LsVarLibRsyslog - command ``ls -lZ /var/lib/rsyslog`` +====================================================== +""" + + +from insights.specs import Specs +from insights import CommandParser, parser, FileListing + + +@parser(Specs.ls_var_lib_rsyslog) +class LsVarLibRsyslog(CommandParser, FileListing): + """ + Parses output of ``ls -lZ /var/lib/rsyslog`` command. + + Sample output:: + + total 4 + -rw-------. 1 root root system_u:object_r:syslogd_var_lib_t:s0 127 Nov 30 03:40 imjournal.state + + Examples: + >>> rsyslog_obj.dir_contains('/var/lib/rsyslog', 'imjournal.state') + True + >>> imjournal_entry = rsyslog_obj.dir_entry('/var/lib/rsyslog', 'imjournal.state') + >>> imjournal_entry['se_type'] + 'syslogd_var_lib_t' + """ + pass diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 89277d3a46..3bf2db3cba 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -341,6 +341,7 @@ class Specs(SpecSet): ls_var_lib_mongodb = RegistryPoint() ls_var_lib_nova_instances = RegistryPoint() ls_var_lib_pcp = RegistryPoint() + ls_var_lib_rsyslog = RegistryPoint() ls_var_log = RegistryPoint() ls_var_opt_mssql = RegistryPoint() ls_var_opt_mssql_log = RegistryPoint() diff --git a/insights/specs/default.py b/insights/specs/default.py index 8db853d60a..3f98d6147b 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -331,6 +331,7 @@ class DefaultSpecs(Specs): ls_var_lib_mongodb = simple_command("/bin/ls -la /var/lib/mongodb") ls_var_lib_nova_instances = simple_command("/bin/ls -laRZ /var/lib/nova/instances") ls_var_lib_pcp = simple_command("/bin/ls -la /var/lib/pcp") + ls_var_lib_rsyslog = simple_command("/bin/ls -lZ /var/lib/rsyslog") ls_var_log = simple_command("/bin/ls -la /var/log /var/log/audit") ls_var_opt_mssql = simple_command("/bin/ls -ld /var/opt/mssql") ls_var_opt_mssql_log = simple_command("/bin/ls -la /var/opt/mssql/log") diff --git a/insights/specs/insights_archive.py b/insights/specs/insights_archive.py index cf78f373c0..a085cd256b 100644 --- a/insights/specs/insights_archive.py +++ b/insights/specs/insights_archive.py @@ -136,6 +136,7 @@ class InsightsArchiveSpecs(Specs): ls_var_lib_mongodb = simple_file("insights_commands/ls_-la_.var.lib.mongodb") ls_var_lib_nova_instances = simple_file("insights_commands/ls_-laRZ_.var.lib.nova.instances") ls_var_lib_pcp = simple_file("insights_commands/ls_-la_.var.lib.pcp") + ls_var_lib_rsyslog = simple_file("insights_commands/ls_-lZ_.var.lib.rsyslog") ls_var_log = simple_file("insights_commands/ls_-la_.var.log_.var.log.audit") ls_var_opt_mssql = simple_file("insights_commands/ls_-ld_.var.opt.mssql") ls_var_opt_mssql_log = simple_file("insights_commands/ls_-la_.var.opt.mssql.log") diff --git a/insights/tests/parsers/test_ls_var_lib_rsyslog.py b/insights/tests/parsers/test_ls_var_lib_rsyslog.py new file mode 100644 index 0000000000..1e668fffff --- /dev/null +++ b/insights/tests/parsers/test_ls_var_lib_rsyslog.py @@ -0,0 +1,32 @@ +import doctest + +from insights.parsers import ls_var_lib_rsyslog +from insights.tests import context_wrap + + +LS_VAR_LIB_RSYSLOG_1 = """ +total 4 +-rw-------. 1 root root system_u:object_r:syslogd_var_lib_t:s0 127 Nov 30 03:40 imjournal.state +""" + +LS_VAR_LIB_RSYSLOG_2 = """ +total 4 +-rw-------. root root system_u:object_r:syslogd_var_lib_t:s0 imjournal.state +""" + + +def test_ls_var_lib_rsyslog(): + rsyslog_obj = ls_var_lib_rsyslog.LsVarLibRsyslog(context_wrap(LS_VAR_LIB_RSYSLOG_2, path="insights_commands/ls_-lZ_.var.lib.rsyslog")) + assert rsyslog_obj.files_of('/var/lib/rsyslog') == ['imjournal.state'] + journal_obj = rsyslog_obj.dir_entry('/var/lib/rsyslog', 'imjournal.state') + assert journal_obj is not None + assert journal_obj['se_type'] == 'syslogd_var_lib_t' + assert journal_obj['owner'] == 'root' + + +def test_ls_var_lib_pcp_doc_examples(): + env = { + 'rsyslog_obj': ls_var_lib_rsyslog.LsVarLibRsyslog(context_wrap(LS_VAR_LIB_RSYSLOG_1, path="insights_commands/ls_-lZ_.var.lib.rsyslog")), + } + failed, total = doctest.testmod(ls_var_lib_rsyslog, globs=env) + assert failed == 0 From 146a4a85875b5f2f9710b2154f4110a5e94e89f1 Mon Sep 17 00:00:00 2001 From: Huanhuan Li Date: Tue, 6 Dec 2022 09:17:40 +0800 Subject: [PATCH 2/2] Update to full path import Signed-off-by: Huanhuan Li --- insights/parsers/ls_var_lib_rsyslog.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/insights/parsers/ls_var_lib_rsyslog.py b/insights/parsers/ls_var_lib_rsyslog.py index 292a27ec3e..4bec296b4a 100644 --- a/insights/parsers/ls_var_lib_rsyslog.py +++ b/insights/parsers/ls_var_lib_rsyslog.py @@ -4,8 +4,9 @@ """ +from insights.core import CommandParser, FileListing +from insights.core.plugins import parser from insights.specs import Specs -from insights import CommandParser, parser, FileListing @parser(Specs.ls_var_lib_rsyslog)