From 77a63ceb41faa351407c10ca6e5586ffa53253f7 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Wed, 19 Oct 2022 16:01:56 +0800 Subject: [PATCH 1/5] New Parser for container_installed_rpms Signed-off-by: Xinting Li --- insights/parsers/installed_rpms.py | 10 ++++++++++ insights/specs/__init__.py | 1 + insights/specs/default.py | 3 ++- insights/tests/parsers/test_installed_rpms.py | 13 ++++++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/insights/parsers/installed_rpms.py b/insights/parsers/installed_rpms.py index b8c1d486be..f934f357d7 100644 --- a/insights/parsers/installed_rpms.py +++ b/insights/parsers/installed_rpms.py @@ -79,6 +79,7 @@ from .. import parser, get_active_lines, CommandParser from .rpm_vercmp import rpm_version_compare from insights.specs import Specs +from insights import ContainerParser # This list of architectures is taken from PDC (Product Definition Center): # https://pdc.fedoraproject.org/rest_api/v1/arches/ @@ -628,3 +629,12 @@ def __hash__(self): from_package = InstalledRpm.from_package Rpm = InstalledRpm Installed = InstalledRpms + + +@parser(Specs.container_installed_rpms) +class ContainerInstalledRpms(ContainerParser, InstalledRpms): + """ + Parses the data for list of installed rpms of the running + containers which are based on RHEL images. + """ + diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 8e7772ba9f..262de96c05 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -792,3 +792,4 @@ class Specs(SpecSet): # container_specs 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 453ec533a1..a21122b09e 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -16,7 +16,7 @@ from insights.core.spec_factory import simple_file, simple_command, glob_file from insights.core.spec_factory import first_of, command_with_args from insights.core.spec_factory import foreach_collect, foreach_execute -from insights.core.spec_factory import container_collect +from insights.core.spec_factory import container_collect, container_execute from insights.core.spec_factory import first_file, listdir from insights.components.cloud_provider import IsAzure, IsGCP from insights.components.ceph import IsCephMonitor @@ -678,3 +678,4 @@ class DefaultSpecs(Specs): # container_specs container_redhat_release = container_collect(running_rhel_containers, "/etc/redhat-release") container_nginx_conf = container_collect(container_nginx_conf_ds) + container_installed_rpms = container_execute(running_rhel_containers, "rpm -qa", context=HostContext, signum=signal.SIGTERM) \ No newline at end of file diff --git a/insights/tests/parsers/test_installed_rpms.py b/insights/tests/parsers/test_installed_rpms.py index a6f0636cdc..40e29e9efe 100644 --- a/insights/tests/parsers/test_installed_rpms.py +++ b/insights/tests/parsers/test_installed_rpms.py @@ -1,5 +1,5 @@ import pytest -from insights.parsers.installed_rpms import InstalledRpms, InstalledRpm, pad_version +from insights.parsers.installed_rpms import InstalledRpms, InstalledRpm, pad_version, ContainerInstalledRpms from insights.tests import context_wrap @@ -539,3 +539,14 @@ def test_vmaas(): assert isinstance(rpm, InstalledRpm) assert rpm.version == "5.2.2" assert rpm.release == "1.el7" + + +def test_container_installed_rpms(): + rpms = ContainerInstalledRpms(context_wrap(RPMS_PACKAGE)) + assert rpms.get_min('openldap').package == 'openldap-2.4.23-31.el6' + pkg_rpm = rpms.packages['openssh-server'][0] + rpm = InstalledRpm.from_package(pkg_rpm.package) + assert rpm.package == 'openssh-server-5.3p1-104.el6' + assert pkg_rpm.package == 'openssh-server-5.3p1-104.el6' + assert rpm == pkg_rpm + assert rpm.epoch == '0' From 8aea0f571096d8d9936b24a8c897d83bb2d5aa70 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Wed, 19 Oct 2022 16:20:05 +0800 Subject: [PATCH 2/5] Fix flake8 error Signed-off-by: Xinting Li --- insights/parsers/installed_rpms.py | 1 - insights/specs/default.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/insights/parsers/installed_rpms.py b/insights/parsers/installed_rpms.py index f934f357d7..9dae2703be 100644 --- a/insights/parsers/installed_rpms.py +++ b/insights/parsers/installed_rpms.py @@ -637,4 +637,3 @@ class ContainerInstalledRpms(ContainerParser, InstalledRpms): Parses the data for list of installed rpms of the running containers which are based on RHEL images. """ - diff --git a/insights/specs/default.py b/insights/specs/default.py index a21122b09e..5937e6f580 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -678,4 +678,4 @@ class DefaultSpecs(Specs): # container_specs container_redhat_release = container_collect(running_rhel_containers, "/etc/redhat-release") container_nginx_conf = container_collect(container_nginx_conf_ds) - container_installed_rpms = container_execute(running_rhel_containers, "rpm -qa", context=HostContext, signum=signal.SIGTERM) \ No newline at end of file + container_installed_rpms = container_execute(running_rhel_containers, "rpm -qa", context=HostContext, signum=signal.SIGTERM) From 0949105964bfd2a6409e05c069d65a8658f30e9c Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Tue, 25 Oct 2022 11:46:37 +0800 Subject: [PATCH 3/5] Update spec Signed-off-by: Xinting Li --- insights/specs/default.py | 2 +- insights/tests/parsers/test_installed_rpms.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/insights/specs/default.py b/insights/specs/default.py index 5937e6f580..ae41f64368 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -678,4 +678,4 @@ class DefaultSpecs(Specs): # container_specs container_redhat_release = container_collect(running_rhel_containers, "/etc/redhat-release") container_nginx_conf = container_collect(container_nginx_conf_ds) - container_installed_rpms = container_execute(running_rhel_containers, "rpm -qa", context=HostContext, signum=signal.SIGTERM) + container_installed_rpms = container_execute(running_rhel_containers, "rpm -qa --qf '%s'" % rpm_format, context=HostContext, signum=signal.SIGTERM) diff --git a/insights/tests/parsers/test_installed_rpms.py b/insights/tests/parsers/test_installed_rpms.py index 40e29e9efe..ee10101c9f 100644 --- a/insights/tests/parsers/test_installed_rpms.py +++ b/insights/tests/parsers/test_installed_rpms.py @@ -550,3 +550,8 @@ def test_container_installed_rpms(): assert pkg_rpm.package == 'openssh-server-5.3p1-104.el6' assert rpm == pkg_rpm assert rpm.epoch == '0' + + rpms_json = InstalledRpms(context_wrap(RPMS_JSON)) + assert isinstance(rpms_json.get_max("log4j").source, InstalledRpm) + assert rpms_json.get_max("libteam").source.name == "libteam" + assert rpms_json.get_max("libteam").source.version == "1.17" \ No newline at end of file From 8727fe842d06471eeb48bad8c2d06aadb46fd5d7 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Tue, 25 Oct 2022 11:55:33 +0800 Subject: [PATCH 4/5] Fix flak8 error Signed-off-by: Xinting Li --- insights/tests/parsers/test_installed_rpms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/insights/tests/parsers/test_installed_rpms.py b/insights/tests/parsers/test_installed_rpms.py index ee10101c9f..a0afebb1b4 100644 --- a/insights/tests/parsers/test_installed_rpms.py +++ b/insights/tests/parsers/test_installed_rpms.py @@ -554,4 +554,4 @@ def test_container_installed_rpms(): rpms_json = InstalledRpms(context_wrap(RPMS_JSON)) assert isinstance(rpms_json.get_max("log4j").source, InstalledRpm) assert rpms_json.get_max("libteam").source.name == "libteam" - assert rpms_json.get_max("libteam").source.version == "1.17" \ No newline at end of file + assert rpms_json.get_max("libteam").source.version == "1.17" From 128077c5a3b31aa5cb83a21ae39b483f2c62ef21 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Tue, 25 Oct 2022 17:28:33 +0800 Subject: [PATCH 5/5] Update test Signed-off-by: Xinting Li --- insights/parsers/installed_rpms.py | 1 + insights/tests/parsers/test_installed_rpms.py | 26 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/insights/parsers/installed_rpms.py b/insights/parsers/installed_rpms.py index 9dae2703be..cfa7e51075 100644 --- a/insights/parsers/installed_rpms.py +++ b/insights/parsers/installed_rpms.py @@ -637,3 +637,4 @@ class ContainerInstalledRpms(ContainerParser, InstalledRpms): Parses the data for list of installed rpms of the running containers which are based on RHEL images. """ + pass diff --git a/insights/tests/parsers/test_installed_rpms.py b/insights/tests/parsers/test_installed_rpms.py index a0afebb1b4..f87c4fff06 100644 --- a/insights/tests/parsers/test_installed_rpms.py +++ b/insights/tests/parsers/test_installed_rpms.py @@ -542,7 +542,18 @@ def test_vmaas(): def test_container_installed_rpms(): - rpms = ContainerInstalledRpms(context_wrap(RPMS_PACKAGE)) + rpms = ContainerInstalledRpms( + context_wrap( + RPMS_PACKAGE, + container_id='39310f3ccc12', + image='registry.access.redhat.com/rhel9', + engine='podman', + path='insights_containers/39310f3ccc12/insights_commands/rpm_-qa_--qf_name_NAME_epoch_EPOCH_version_VERSION_release_RELEASE_arch_ARCH_installtime_INSTALLTIME_date_buildtime_BUILDTIME_vendor_VENDOR_buildhost_BUILDHOST_sigpgp_SIGPGP_pgpsig' + ) + ) + assert rpms.image == "registry.access.redhat.com/rhel9" + assert rpms.engine == "podman" + assert rpms.container_id == "39310f3ccc12" assert rpms.get_min('openldap').package == 'openldap-2.4.23-31.el6' pkg_rpm = rpms.packages['openssh-server'][0] rpm = InstalledRpm.from_package(pkg_rpm.package) @@ -551,7 +562,18 @@ def test_container_installed_rpms(): assert rpm == pkg_rpm assert rpm.epoch == '0' - rpms_json = InstalledRpms(context_wrap(RPMS_JSON)) + rpms_json = ContainerInstalledRpms( + context_wrap( + RPMS_JSON, + container_id='cc2883a1a369', + image='quay.io/rhel8', + engine='podman', + path='insights_containers/cc2883a1a369/insights_commands/rpm_-qa_--qf_name_NAME_epoch_EPOCH_version_VERSION_release_RELEASE_arch_ARCH_installtime_INSTALLTIME_date_buildtime_BUILDTIME_vendor_VENDOR_buildhost_BUILDHOST_sigpgp_SIGPGP_pgpsig' + ) + ) + assert rpms_json.image == "quay.io/rhel8" + assert rpms_json.engine == "podman" + assert rpms_json.container_id == "cc2883a1a369" assert isinstance(rpms_json.get_max("log4j").source, InstalledRpm) assert rpms_json.get_max("libteam").source.name == "libteam" assert rpms_json.get_max("libteam").source.version == "1.17"