From 0ec8fc21cdcc52dca34b3079864ddf4616fbcebe Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Fri, 4 Nov 2022 16:08:30 +0800 Subject: [PATCH 1/6] Feat: New Parser for 'dotnet --version' Command Signed-off-by: Xinting Li --- insights/parsers/dotnet.py | 33 ++++++++++++++++++++++++- insights/specs/__init__.py | 1 + insights/specs/default.py | 1 + insights/tests/parsers/test_dotnet.py | 35 +++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/insights/parsers/dotnet.py b/insights/parsers/dotnet.py index bbc91d10d3..4eb64bf7e3 100644 --- a/insights/parsers/dotnet.py +++ b/insights/parsers/dotnet.py @@ -2,13 +2,20 @@ DotNet- Comand ``/usr/bin/dotnet`` ================================== -The parser for ``/usr/bin/dotnet --version`` is included in this module.. +The parsers related ``/usr/bin/dotnet --version`` is included in this module. + +DotNetVersion - command ``dotnet --version`` +-------------------------------------------- + +ContainerInstalledRpms - command ``dotnet --version`` +----------------------------------------------------- """ from insights import parser, CommandParser from insights.parsers import SkipException, ParseException from insights.specs import Specs +from insights import ContainerParser @parser(Specs.dotnet_version) @@ -44,3 +51,27 @@ def parse_content(self, content): if self.major is None: raise ParseException("Unrecognized version: {0}", self.raw) + + +@parser(Specs.container_dotnet_version) +class ContainerInstalledRpms(ContainerParser, DotNetVersion): + """ + Parses the output of the ``/usr/bin/dotnet --version`` command of the running + containers which are based on RHEL images. + + Sample output:: + + 3.1.108 + + Examples: + >>> type(con_dotnet_ver) + + >>> con_dotnet_ver.major + 3 + >>> con_dotnet_ver.minor + 1 + >>> con_dotnet_ver.raw + '3.1.108' + """ + + pass diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 21ae94d7e2..4941fd5fde 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -790,6 +790,7 @@ class Specs(SpecSet): zipl_conf = RegistryPoint() # container_specs + container_dotnet_version = RegistryPoint(multi_output=True) container_redhat_release = RegistryPoint(multi_output=True) container_nginx_conf = RegistryPoint(multi_output=True) container_installed_rpms = RegistryPoint(multi_output=True) diff --git a/insights/specs/default.py b/insights/specs/default.py index 8df436d858..cb903fe69f 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -673,6 +673,7 @@ class DefaultSpecs(Specs): zipl_conf = simple_file("/etc/zipl.conf") # Container collection specs + container_dotnet_version = container_execute(running_rhel_containers, "dotnet --version") container_installed_rpms = container_execute(running_rhel_containers, "rpm -qa --qf '%s'" % _rpm_format, context=HostContext, signum=signal.SIGTERM) container_nginx_conf = container_collect(container_nginx_conf_ds) container_redhat_release = container_collect(running_rhel_containers, "/etc/redhat-release") diff --git a/insights/tests/parsers/test_dotnet.py b/insights/tests/parsers/test_dotnet.py index 0afefe1b9f..7c4bd3549c 100644 --- a/insights/tests/parsers/test_dotnet.py +++ b/insights/tests/parsers/test_dotnet.py @@ -3,7 +3,7 @@ from insights.parsers import dotnet from insights.core.plugins import ContentException from insights.parsers import SkipException, ParseException -from insights.parsers.dotnet import DotNetVersion +from insights.parsers.dotnet import DotNetVersion, ContainerInstalledRpms from insights.tests import context_wrap dotnet_version_1 = "3.1.108" @@ -45,9 +45,40 @@ def test_dotnet_version_ab(): assert ret is None +def test_container_dotnet_version(): + ret = ContainerInstalledRpms( + context_wrap( + dotnet_version_1, + container_id='cc2883a1a369', + image='quay.io/rhel8', + engine='podman' + ) + ) + assert ret.major == 3 + assert ret.minor == 1 + assert ret.raw == dotnet_version_1 + assert ret.image == "quay.io/rhel8" + assert ret.engine == "podman" + assert ret.container_id == "cc2883a1a369" + + +def test_container_dotnet_version_error(): + with pytest.raises(ContentException): + ret = ContainerInstalledRpms( + context_wrap( + dotnet_version_3, + container_id='cc2883a1a369', + image='quay.io/rhel8', + engine='podman' + ) + ) + assert ret is None + + def test_doc_examples(): env = { - 'dotnet_ver': DotNetVersion(context_wrap(dotnet_version_1)) + 'dotnet_ver': DotNetVersion(context_wrap(dotnet_version_1)), + 'con_dotnet_ver': ContainerInstalledRpms(context_wrap(dotnet_version_1)) } failed, total = doctest.testmod(dotnet, globs=env) assert failed == 0 From c9f6e89d324274c37e80245d0e59ac04d7e199e6 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Mon, 7 Nov 2022 14:11:33 +0800 Subject: [PATCH 2/6] Update spec Signed-off-by: Xinting Li --- insights/specs/default.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/insights/specs/default.py b/insights/specs/default.py index cb903fe69f..3b1e349f88 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -673,7 +673,7 @@ class DefaultSpecs(Specs): zipl_conf = simple_file("/etc/zipl.conf") # Container collection specs - container_dotnet_version = container_execute(running_rhel_containers, "dotnet --version") + container_dotnet_version = container_execute(running_rhel_containers, "/usr/bin/dotnet --version") container_installed_rpms = container_execute(running_rhel_containers, "rpm -qa --qf '%s'" % _rpm_format, context=HostContext, signum=signal.SIGTERM) container_nginx_conf = container_collect(container_nginx_conf_ds) container_redhat_release = container_collect(running_rhel_containers, "/etc/redhat-release") From ccaa866742710d848e03d9abe544ff76e8e9bc82 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Tue, 8 Nov 2022 09:39:19 +0800 Subject: [PATCH 3/6] Fix Parser Name Signed-off-by: Xinting Li --- insights/parsers/dotnet.py | 4 ++-- insights/tests/parsers/test_dotnet.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/insights/parsers/dotnet.py b/insights/parsers/dotnet.py index 4eb64bf7e3..282ba6d0d8 100644 --- a/insights/parsers/dotnet.py +++ b/insights/parsers/dotnet.py @@ -54,7 +54,7 @@ def parse_content(self, content): @parser(Specs.container_dotnet_version) -class ContainerInstalledRpms(ContainerParser, DotNetVersion): +class ContainerDotNetVersion(ContainerParser, DotNetVersion): """ Parses the output of the ``/usr/bin/dotnet --version`` command of the running containers which are based on RHEL images. @@ -65,7 +65,7 @@ class ContainerInstalledRpms(ContainerParser, DotNetVersion): Examples: >>> type(con_dotnet_ver) - + >>> con_dotnet_ver.major 3 >>> con_dotnet_ver.minor diff --git a/insights/tests/parsers/test_dotnet.py b/insights/tests/parsers/test_dotnet.py index 7c4bd3549c..cb912d5489 100644 --- a/insights/tests/parsers/test_dotnet.py +++ b/insights/tests/parsers/test_dotnet.py @@ -3,7 +3,7 @@ from insights.parsers import dotnet from insights.core.plugins import ContentException from insights.parsers import SkipException, ParseException -from insights.parsers.dotnet import DotNetVersion, ContainerInstalledRpms +from insights.parsers.dotnet import DotNetVersion, ContainerDotNetVersion from insights.tests import context_wrap dotnet_version_1 = "3.1.108" @@ -46,7 +46,7 @@ def test_dotnet_version_ab(): def test_container_dotnet_version(): - ret = ContainerInstalledRpms( + ret = ContainerDotNetVersion( context_wrap( dotnet_version_1, container_id='cc2883a1a369', @@ -64,7 +64,7 @@ def test_container_dotnet_version(): def test_container_dotnet_version_error(): with pytest.raises(ContentException): - ret = ContainerInstalledRpms( + ret = ContainerDotNetVersion( context_wrap( dotnet_version_3, container_id='cc2883a1a369', @@ -78,7 +78,7 @@ def test_container_dotnet_version_error(): def test_doc_examples(): env = { 'dotnet_ver': DotNetVersion(context_wrap(dotnet_version_1)), - 'con_dotnet_ver': ContainerInstalledRpms(context_wrap(dotnet_version_1)) + 'con_dotnet_ver': ContainerDotNetVersion(context_wrap(dotnet_version_1)) } failed, total = doctest.testmod(dotnet, globs=env) assert failed == 0 From 767db3871bf577c8bf8dddde5b58da4fa4b94adb Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Tue, 8 Nov 2022 15:20:23 +0800 Subject: [PATCH 4/6] Update Doc string Signed-off-by: Xinting Li --- insights/parsers/dotnet.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/insights/parsers/dotnet.py b/insights/parsers/dotnet.py index 282ba6d0d8..9ff7c97e7b 100644 --- a/insights/parsers/dotnet.py +++ b/insights/parsers/dotnet.py @@ -7,8 +7,8 @@ DotNetVersion - command ``dotnet --version`` -------------------------------------------- -ContainerInstalledRpms - command ``dotnet --version`` ------------------------------------------------------ +ContainerInstalledRpms - command ``dotnet --version`` for containers +-------------------------------------------------------------------- """ From 3cd1d1e664c0ebbd72be11bbd0f236d8a7accd45 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Tue, 8 Nov 2022 15:55:45 +0800 Subject: [PATCH 5/6] Update doc title Signed-off-by: Xinting Li --- insights/parsers/dotnet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/insights/parsers/dotnet.py b/insights/parsers/dotnet.py index 9ff7c97e7b..eae8cc2c64 100644 --- a/insights/parsers/dotnet.py +++ b/insights/parsers/dotnet.py @@ -7,7 +7,7 @@ DotNetVersion - command ``dotnet --version`` -------------------------------------------- -ContainerInstalledRpms - command ``dotnet --version`` for containers +ContainerDotNetVersion - command ``dotnet --version`` for containers -------------------------------------------------------------------- """ From 71e8e3b84eb7fe0b89ef2fe59b75bc4b2e2f9811 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Tue, 8 Nov 2022 18:05:40 +0800 Subject: [PATCH 6/6] Add doctest Signed-off-by: Xinting Li --- insights/parsers/dotnet.py | 7 ++++++- insights/tests/parsers/test_dotnet.py | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/insights/parsers/dotnet.py b/insights/parsers/dotnet.py index eae8cc2c64..ef11575db0 100644 --- a/insights/parsers/dotnet.py +++ b/insights/parsers/dotnet.py @@ -9,7 +9,6 @@ ContainerDotNetVersion - command ``dotnet --version`` for containers -------------------------------------------------------------------- - """ from insights import parser, CommandParser @@ -66,6 +65,12 @@ class ContainerDotNetVersion(ContainerParser, DotNetVersion): Examples: >>> type(con_dotnet_ver) + >>> con_dotnet_ver.container_id + 'cc2883a1a369' + >>> con_dotnet_ver.image + 'quay.io/rhel8' + >>> con_dotnet_ver.engine + 'podman' >>> con_dotnet_ver.major 3 >>> con_dotnet_ver.minor diff --git a/insights/tests/parsers/test_dotnet.py b/insights/tests/parsers/test_dotnet.py index cb912d5489..f6044c39f0 100644 --- a/insights/tests/parsers/test_dotnet.py +++ b/insights/tests/parsers/test_dotnet.py @@ -78,7 +78,14 @@ def test_container_dotnet_version_error(): def test_doc_examples(): env = { 'dotnet_ver': DotNetVersion(context_wrap(dotnet_version_1)), - 'con_dotnet_ver': ContainerDotNetVersion(context_wrap(dotnet_version_1)) + 'con_dotnet_ver': ContainerDotNetVersion( + context_wrap( + dotnet_version_1, + container_id='cc2883a1a369', + image='quay.io/rhel8', + engine='podman' + ) + ) } failed, total = doctest.testmod(dotnet, globs=env) assert failed == 0