From 7c05837788ec30ea8600f8a386076f39b612cd11 Mon Sep 17 00:00:00 2001 From: huali027 <44796653+huali027@users.noreply.github.com> Date: Mon, 18 Apr 2022 16:01:51 +0800 Subject: [PATCH] fix: Update the spec "du_dirs" to filterable (#3384) * fix: Update the spec "du_dirs" to filterable Signed-off-by: Huanhuan Li * Move the "du_dirs_list" datasource to the datasource directory Signed-off-by: Huanhuan Li * Rename to "dir_list" * Also raise SkipComponent if there are no filters Signed-off-by: Huanhuan Li (cherry picked from commit 7d78fc53a08ad537f9499ee99c4f47e46581f1c0) --- docs/custom_datasources_index.rst | 8 ++++++ insights/specs/__init__.py | 2 +- insights/specs/datasources/dir_list.py | 18 ++++++++++++ insights/specs/default.py | 12 +++----- insights/tests/datasources/test_dir_list.py | 32 +++++++++++++++++++++ 5 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 insights/specs/datasources/dir_list.py create mode 100644 insights/tests/datasources/test_dir_list.py diff --git a/docs/custom_datasources_index.rst b/docs/custom_datasources_index.rst index c83c94c2d6..869d190abc 100644 --- a/docs/custom_datasources_index.rst +++ b/docs/custom_datasources_index.rst @@ -27,6 +27,14 @@ insights.specs.datasources.cloud_init :show-inheritance: :undoc-members: +insights.specs.datasources.dir_list +----------------------------------- + +.. automodule:: insights.specs.datasources.dir_list + :members: du_dir_list + :show-inheritance: + :undoc-members: + insights.specs.datasources.ethernet ------------------------------------- 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/datasources/dir_list.py b/insights/specs/datasources/dir_list.py new file mode 100644 index 0000000000..adcd67467f --- /dev/null +++ b/insights/specs/datasources/dir_list.py @@ -0,0 +1,18 @@ +""" +Custom datasources to get a list of directories to check disk size. +""" + +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_dir_list(broker): + """ Return a list of directories from the spec filter """ + 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 53c2db77e4..49cba52e53 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -32,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, 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 from insights.specs.datasources.pcp import pcp_enabled, pmlog_summary_args @@ -210,12 +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 """ - return ['/var/lib/candlepin/activemq-artemis'] - du_dirs = foreach_execute(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_dir_list.py b/insights/tests/datasources/test_dir_list.py new file mode 100644 index 0000000000..0d5521938c --- /dev/null +++ b/insights/tests/datasources/test_dir_list.py @@ -0,0 +1,32 @@ +import pytest + +from insights.specs import Specs +from insights.core import filters +from insights.specs.datasources.dir_list import du_dir_list +from insights.core.dr import SkipComponent + + +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_dir_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 = {} + with pytest.raises(SkipComponent): + du_dir_list(broker)