From 7f2a21711b311d32d893a1b21f67881a36fead40 Mon Sep 17 00:00:00 2001 From: Huanhuan Li Date: Wed, 13 Apr 2022 16:35:39 +0800 Subject: [PATCH 1/3] fix: Update the spec "du_dirs" to filterable Signed-off-by: Huanhuan Li --- insights/specs/__init__.py | 2 +- insights/specs/default.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 7690a8edba..888f4d3e98 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -143,7 +143,7 @@ class Specs(SpecSet): doveconf = RegistryPoint(filterable=True) dracut_kdump_capture_service = RegistryPoint() dse_ldif = RegistryPoint(multi_output=True, filterable=True) - du_dirs = RegistryPoint(multi_output=True) + du_dirs = RegistryPoint(multi_output=True, filterable=True) dumpe2fs_h = RegistryPoint(multi_output=True) engine_config_all = RegistryPoint() engine_db_query_vdsm_version = RegistryPoint() diff --git a/insights/specs/default.py b/insights/specs/default.py index 53c2db77e4..506780bde4 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -25,6 +25,7 @@ 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 first_file, listdir +from insights.core.filters import get_filters from insights.combiners.ps import Ps from insights.components.rhel_version import IsRhel7, IsRhel8, IsRhel9 from insights.components.cloud_provider import IsAWS, IsAzure, IsGCP @@ -214,7 +215,8 @@ def corosync_cmapctl_cmd_list(broker): @datasource(HostContext) def du_dirs_list(broker): """ Provide a list of directorys for the ``du_dirs`` spec to scan """ - return ['/var/lib/candlepin/activemq-artemis'] + filters = get_filters(Specs.du_dirs) + return list(filters) du_dirs = foreach_execute(du_dirs_list, "/bin/du -s -k %s") engine_db_query_vdsm_version = simple_command('engine-db-query --statement "SELECT vs.vds_name, rpm_version FROM vds_dynamic vd, vds_static vs WHERE vd.vds_id = vs.vds_id" --json') engine_log = simple_file("/var/log/ovirt-engine/engine.log") From 24b223c5be2b12a043cd8d003aa2db8c0674cfb0 Mon Sep 17 00:00:00 2001 From: Huanhuan Li Date: Thu, 14 Apr 2022 10:50:23 +0800 Subject: [PATCH 2/3] Move the "du_dirs_list" datasource to the datasource directory Signed-off-by: Huanhuan Li --- docs/custom_datasources_index.rst | 8 ++++++ insights/specs/datasources/get_dirs.py | 15 +++++++++++ insights/specs/default.py | 14 +++------- insights/tests/datasources/test_get_dirs.py | 29 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 insights/specs/datasources/get_dirs.py create mode 100644 insights/tests/datasources/test_get_dirs.py diff --git a/docs/custom_datasources_index.rst b/docs/custom_datasources_index.rst index c83c94c2d6..15b442a13d 100644 --- a/docs/custom_datasources_index.rst +++ b/docs/custom_datasources_index.rst @@ -35,6 +35,14 @@ insights.specs.datasources.ethernet :show-inheritance: :undoc-members: +insights.specs.datasources.get_dirs +----------------------------------- + +.. automodule:: insights.specs.datasources.get_dirs + :members: du_dirs_list + :show-inheritance: + :undoc-members: + insights.specs.datasources.ipcs ------------------------------- diff --git a/insights/specs/datasources/get_dirs.py b/insights/specs/datasources/get_dirs.py new file mode 100644 index 0000000000..298668133b --- /dev/null +++ b/insights/specs/datasources/get_dirs.py @@ -0,0 +1,15 @@ +""" +Custom datasources to get a list of directories to check disk size. +""" + +from insights.core.context import HostContext +from insights.core.plugins import datasource +from insights.core.filters import get_filters +from insights.specs import Specs + + +@datasource(HostContext) +def du_dirs_list(broker): + """ Return a list of directories from the spec filter """ + filters = get_filters(Specs.du_dirs) + return list(filters) diff --git a/insights/specs/default.py b/insights/specs/default.py index 506780bde4..bcbe6a99e8 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -25,7 +25,6 @@ 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 first_file, listdir -from insights.core.filters import get_filters from insights.combiners.ps import Ps from insights.components.rhel_version import IsRhel7, IsRhel8, IsRhel9 from insights.components.cloud_provider import IsAWS, IsAzure, IsGCP @@ -33,8 +32,9 @@ from insights.combiners.satellite_version import SatelliteVersion, CapsuleVersion from insights.specs import Specs from insights.specs.datasources import ( - awx_manage, cloud_init, candlepin_broker, ethernet, get_running_commands, ipcs, lpstat, package_provides, - ps as ps_datasource, sap, satellite_missed_queues, ssl_certificate, yum_updates) + awx_manage, cloud_init, candlepin_broker, ethernet, get_dirs, + get_running_commands, ipcs, lpstat, package_provides, ps as ps_datasource, + sap, satellite_missed_queues, ssl_certificate, yum_updates) from insights.specs.datasources.sap import sap_hana_sid, sap_hana_sid_SID_nr from insights.specs.datasources.pcp import pcp_enabled, pmlog_summary_args @@ -211,13 +211,7 @@ def corosync_cmapctl_cmd_list(broker): docker_sysconfig = simple_file("/etc/sysconfig/docker") dotnet_version = simple_command("/usr/bin/dotnet --version") dracut_kdump_capture_service = simple_file("/usr/lib/dracut/modules.d/99kdumpbase/kdump-capture.service") - - @datasource(HostContext) - def du_dirs_list(broker): - """ Provide a list of directorys for the ``du_dirs`` spec to scan """ - filters = get_filters(Specs.du_dirs) - return list(filters) - du_dirs = foreach_execute(du_dirs_list, "/bin/du -s -k %s") + du_dirs = foreach_execute(get_dirs.du_dirs_list, "/bin/du -s -k %s") engine_db_query_vdsm_version = simple_command('engine-db-query --statement "SELECT vs.vds_name, rpm_version FROM vds_dynamic vd, vds_static vs WHERE vd.vds_id = vs.vds_id" --json') engine_log = simple_file("/var/log/ovirt-engine/engine.log") etc_journald_conf = simple_file(r"etc/systemd/journald.conf") diff --git a/insights/tests/datasources/test_get_dirs.py b/insights/tests/datasources/test_get_dirs.py new file mode 100644 index 0000000000..66eed22fb8 --- /dev/null +++ b/insights/tests/datasources/test_get_dirs.py @@ -0,0 +1,29 @@ +from insights.specs import Specs +from insights.core import filters +from insights.specs.datasources.get_dirs import du_dirs_list + + +def setup_function(func): + if Specs.du_dirs in filters._CACHE: + del filters._CACHE[Specs.du_dirs] + if Specs.du_dirs in filters.FILTERS: + del filters.FILTERS[Specs.du_dirs] + + if func is test_du_dirs_list: + filters.add_filter(Specs.du_dirs, ["/var/lib/pulp", "/etc/httpd"]) + if func is test_du_dirs_list_no_filter: + filters.add_filter(Specs.du_dirs, []) + + +def test_du_dirs_list(): + broker = {} + result = du_dirs_list(broker) + assert len(result) == 2 + assert '/var/lib/pulp' in result + assert '/etc/httpd' in result + + +def test_du_dirs_list_no_filter(): + broker = {} + result = du_dirs_list(broker) + assert not result From c372cb92e72fde8fa994440b61720d5d2ad3da96 Mon Sep 17 00:00:00 2001 From: Huanhuan Li Date: Thu, 14 Apr 2022 15:57:44 +0800 Subject: [PATCH 3/3] Rename to "dir_list" * Also raise SkipComponent if there are no filters Signed-off-by: Huanhuan Li --- docs/custom_datasources_index.rst | 16 ++++++++-------- .../datasources/{get_dirs.py => dir_list.py} | 9 ++++++--- insights/specs/default.py | 4 ++-- .../{test_get_dirs.py => test_dir_list.py} | 11 +++++++---- 4 files changed, 23 insertions(+), 17 deletions(-) rename insights/specs/datasources/{get_dirs.py => dir_list.py} (65%) rename insights/tests/datasources/{test_get_dirs.py => test_dir_list.py} (75%) diff --git a/docs/custom_datasources_index.rst b/docs/custom_datasources_index.rst index 15b442a13d..869d190abc 100644 --- a/docs/custom_datasources_index.rst +++ b/docs/custom_datasources_index.rst @@ -27,19 +27,19 @@ insights.specs.datasources.cloud_init :show-inheritance: :undoc-members: -insights.specs.datasources.ethernet -------------------------------------- +insights.specs.datasources.dir_list +----------------------------------- -.. automodule:: insights.specs.datasources.ethernet - :members: interfaces, LocalSpecs +.. automodule:: insights.specs.datasources.dir_list + :members: du_dir_list :show-inheritance: :undoc-members: -insights.specs.datasources.get_dirs ------------------------------------ +insights.specs.datasources.ethernet +------------------------------------- -.. automodule:: insights.specs.datasources.get_dirs - :members: du_dirs_list +.. automodule:: insights.specs.datasources.ethernet + :members: interfaces, LocalSpecs :show-inheritance: :undoc-members: diff --git a/insights/specs/datasources/get_dirs.py b/insights/specs/datasources/dir_list.py similarity index 65% rename from insights/specs/datasources/get_dirs.py rename to insights/specs/datasources/dir_list.py index 298668133b..adcd67467f 100644 --- a/insights/specs/datasources/get_dirs.py +++ b/insights/specs/datasources/dir_list.py @@ -3,13 +3,16 @@ """ from insights.core.context import HostContext +from insights.core.dr import SkipComponent from insights.core.plugins import datasource from insights.core.filters import get_filters from insights.specs import Specs @datasource(HostContext) -def du_dirs_list(broker): +def du_dir_list(broker): """ Return a list of directories from the spec filter """ - filters = get_filters(Specs.du_dirs) - return list(filters) + filters = list(get_filters(Specs.du_dirs)) + if filters: + return filters + raise SkipComponent diff --git a/insights/specs/default.py b/insights/specs/default.py index bcbe6a99e8..49cba52e53 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -32,7 +32,7 @@ from insights.combiners.satellite_version import SatelliteVersion, CapsuleVersion from insights.specs import Specs from insights.specs.datasources import ( - awx_manage, cloud_init, candlepin_broker, ethernet, get_dirs, + awx_manage, cloud_init, candlepin_broker, dir_list, ethernet, get_running_commands, ipcs, lpstat, package_provides, ps as ps_datasource, sap, satellite_missed_queues, ssl_certificate, yum_updates) from insights.specs.datasources.sap import sap_hana_sid, sap_hana_sid_SID_nr @@ -211,7 +211,7 @@ def corosync_cmapctl_cmd_list(broker): docker_sysconfig = simple_file("/etc/sysconfig/docker") dotnet_version = simple_command("/usr/bin/dotnet --version") dracut_kdump_capture_service = simple_file("/usr/lib/dracut/modules.d/99kdumpbase/kdump-capture.service") - du_dirs = foreach_execute(get_dirs.du_dirs_list, "/bin/du -s -k %s") + du_dirs = foreach_execute(dir_list.du_dir_list, "/bin/du -s -k %s") engine_db_query_vdsm_version = simple_command('engine-db-query --statement "SELECT vs.vds_name, rpm_version FROM vds_dynamic vd, vds_static vs WHERE vd.vds_id = vs.vds_id" --json') engine_log = simple_file("/var/log/ovirt-engine/engine.log") etc_journald_conf = simple_file(r"etc/systemd/journald.conf") diff --git a/insights/tests/datasources/test_get_dirs.py b/insights/tests/datasources/test_dir_list.py similarity index 75% rename from insights/tests/datasources/test_get_dirs.py rename to insights/tests/datasources/test_dir_list.py index 66eed22fb8..0d5521938c 100644 --- a/insights/tests/datasources/test_get_dirs.py +++ b/insights/tests/datasources/test_dir_list.py @@ -1,6 +1,9 @@ +import pytest + from insights.specs import Specs from insights.core import filters -from insights.specs.datasources.get_dirs import du_dirs_list +from insights.specs.datasources.dir_list import du_dir_list +from insights.core.dr import SkipComponent def setup_function(func): @@ -17,7 +20,7 @@ def setup_function(func): def test_du_dirs_list(): broker = {} - result = du_dirs_list(broker) + result = du_dir_list(broker) assert len(result) == 2 assert '/var/lib/pulp' in result assert '/etc/httpd' in result @@ -25,5 +28,5 @@ def test_du_dirs_list(): def test_du_dirs_list_no_filter(): broker = {} - result = du_dirs_list(broker) - assert not result + with pytest.raises(SkipComponent): + du_dir_list(broker)