From e1d279ecd2401e7e6e49d368ab7064766d41f68b Mon Sep 17 00:00:00 2001 From: Xiangce Liu Date: Thu, 3 Nov 2022 13:33:46 +0800 Subject: [PATCH] feat: new spec and parser for 'subscription-manage facts' (#3555) * feat: new spec and parser for 'subscription-manage facts' Signed-off-by: Xiangce Liu * add fixed filter Signed-off-by: Xiangce Liu * miss the filterable=True Signed-off-by: Xiangce Liu * use ': ' as the separator instead of ':' Signed-off-by: Xiangce Liu Signed-off-by: Xiangce Liu --- .../subscription_manager.rst | 3 ++ insights/parsers/subscription_manager.py | 47 +++++++++++++++++++ insights/specs/__init__.py | 1 + insights/specs/default.py | 1 + insights/specs/insights_archive.py | 1 + .../parsers/test_subscription_manager.py | 45 ++++++++++++++++++ 6 files changed, 98 insertions(+) create mode 100644 docs/shared_parsers_catalog/subscription_manager.rst create mode 100644 insights/parsers/subscription_manager.py create mode 100644 insights/tests/parsers/test_subscription_manager.py diff --git a/docs/shared_parsers_catalog/subscription_manager.rst b/docs/shared_parsers_catalog/subscription_manager.rst new file mode 100644 index 0000000000..6428343a3e --- /dev/null +++ b/docs/shared_parsers_catalog/subscription_manager.rst @@ -0,0 +1,3 @@ +.. automodule:: insights.parsers.subscription_manager + :members: + :show-inheritance: diff --git a/insights/parsers/subscription_manager.py b/insights/parsers/subscription_manager.py new file mode 100644 index 0000000000..b01b609066 --- /dev/null +++ b/insights/parsers/subscription_manager.py @@ -0,0 +1,47 @@ +""" +subscription-manager commands +============================= + +Parsers for parsing output of the ``subscription-manager`` commands. + +SubscriptionManagerFacts - command ``subscription-manager facts`` +----------------------------------------------------------------- +""" +from insights import parser, CommandParser +from insights.core.filters import add_filter +from insights.specs import Specs +from insights.parsers import SkipException, ParseException + + +add_filter(Specs.subscription_manager_facts, ['instance_id']) + + +@parser(Specs.subscription_manager_facts) +class SubscriptionManagerFacts(CommandParser, dict): + """ + Class for parsing the output of `subscription-manager facts` command. + + Typical output of the command is:: + + aws_instance_id: 567890567890 + network.ipv6_address: ::1 + uname.sysname: Linux + uname.version: #1 SMP PREEMPT Fri Sep 2 16:07:40 EDT 2022 + virt.host_type: rhev, kvm + virt.is_guest: True + + Examples: + >>> type(rhsm_facts) + + >>> rhsm_facts['aws_instance_id'] + '567890567890' + """ + def parse_content(self, content): + for line in content: + if ': ' not in line: + raise ParseException('Incorrect line: {0}'.format(line)) + key, val = [_l.strip() for _l in line.split(': ', 1)] + self[key] = val + + if len(self) == 0: + raise SkipException diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 00ae212506..1ff396827d 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -663,6 +663,7 @@ class Specs(SpecSet): sshd_config_perms = RegistryPoint() sssd_config = RegistryPoint() sssd_logs = RegistryPoint(multi_output=True, filterable=True) + subscription_manager_facts = RegistryPoint(filterable=True) subscription_manager_id = RegistryPoint() subscription_manager_installed_product_ids = RegistryPoint(filterable=True) subscription_manager_list_consumed = RegistryPoint() diff --git a/insights/specs/default.py b/insights/specs/default.py index daa40a7e21..22b23806f8 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -595,6 +595,7 @@ class DefaultSpecs(Specs): sshd_config = simple_file("/etc/ssh/sshd_config") sshd_config_perms = simple_command("/bin/ls -lH /etc/ssh/sshd_config") sssd_config = simple_file("/etc/sssd/sssd.conf") + subscription_manager_facts = simple_command("/usr/sbin/subscription-manager facts") subscription_manager_id = simple_command("/usr/sbin/subscription-manager identity") # use "/usr/sbin" here, BZ#1690529 subscription_manager_installed_product_ids = simple_command("/usr/bin/find /etc/pki/product-default/ /etc/pki/product/ -name '*pem' -exec rct cat-cert --no-content '{}' \;") sudoers = glob_file(["/etc/sudoers", "/etc/sudoers.d/*"]) diff --git a/insights/specs/insights_archive.py b/insights/specs/insights_archive.py index 15b76673c9..d2c57d7e83 100644 --- a/insights/specs/insights_archive.py +++ b/insights/specs/insights_archive.py @@ -249,6 +249,7 @@ class InsightsArchiveSpecs(Specs): spamassassin_channels = simple_file('insights_commands/grep_-r_s_CHANNELURL_.etc.mail.spamassassin.channel.d') ss = simple_file("insights_commands/ss_-tupna") sshd_config_perms = first_file(["insights_commands/ls_-lH_.etc.ssh.sshd_config", "insights_commands/ls_-l_.etc.ssh.sshd_config"]) + subscription_manager_facts = simple_file("insights_commands/subscription-manager_facts") subscription_manager_id = simple_file("insights_commands/subscription-manager_identity") subscription_manager_installed_product_ids = simple_file("insights_commands/find_.etc.pki.product-default._.etc.pki.product._-name_pem_-exec_rct_cat-cert_--no-content") sysctl = simple_file("insights_commands/sysctl_-a") diff --git a/insights/tests/parsers/test_subscription_manager.py b/insights/tests/parsers/test_subscription_manager.py new file mode 100644 index 0000000000..dbb38e5de7 --- /dev/null +++ b/insights/tests/parsers/test_subscription_manager.py @@ -0,0 +1,45 @@ +from insights.parsers import ParseException, SkipException, subscription_manager +from insights.parsers.subscription_manager import SubscriptionManagerFacts +from insights.tests import context_wrap +import pytest +import doctest + +INPUT_NORMAL_1 = """ +aws_instance_id: 567890567890 +network.ipv6_address: ::1 +uname.sysname: Linux +uname.version: #1 SMP PREEMPT Fri Sep 2 16:07:40 EDT 2022 +virt.host_type: rhev, kvm +virt.is_guest: True +""".strip() + +INPUT_NG_1 = """ +XYC +Release not set +""".strip() + +INPUT_NG_2 = "" + + +def test_subscription_manager_facts(): + ret = SubscriptionManagerFacts(context_wrap(INPUT_NORMAL_1)) + for line in INPUT_NORMAL_1.splitlines(): + key, value = line.split(': ', 1) + assert ret[key] == value + + +def test_subscription_manager_release_show_ng(): + with pytest.raises(ParseException): + SubscriptionManagerFacts(context_wrap(INPUT_NG_1)) + + with pytest.raises(SkipException): + SubscriptionManagerFacts(context_wrap(INPUT_NG_2)) + + +def test_doc_examples(): + env = { + 'rhsm_facts': + SubscriptionManagerFacts(context_wrap(INPUT_NORMAL_1)), + } + failed, total = doctest.testmod(subscription_manager, globs=env) + assert failed == 0