From 2c292cebe1764adaca98b431a3c51a88012c10de Mon Sep 17 00:00:00 2001 From: xintli <986222045@qq.com> Date: Thu, 7 Nov 2024 16:02:28 +0800 Subject: [PATCH] FEAT: new custom filter 'fstab_mounted.dirs' to ls_lan spec (#4255) Signed-off-by: Xinting Li --- insights/collect.py | 4 ++++ insights/specs/datasources/ls.py | 27 +++++++++++++++++---------- insights/tests/datasources/test_ls.py | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/insights/collect.py b/insights/collect.py index 8efcee5e6c..76a833c0ac 100755 --- a/insights/collect.py +++ b/insights/collect.py @@ -241,6 +241,10 @@ enabled: true - name: insights.components.selinux.SELinuxEnabled enabled: true + + # needed for the 'fstab_mounted.dirs' to the 'ls_lan' spec + - name: insights.parsers.fstab.FSTab + enabled: true """.strip() EXCEPTIONS_TO_REPORT = set([ diff --git a/insights/specs/datasources/ls.py b/insights/specs/datasources/ls.py index 62a1ced4ef..96d79ff26a 100644 --- a/insights/specs/datasources/ls.py +++ b/insights/specs/datasources/ls.py @@ -6,7 +6,9 @@ from insights.core.exceptions import SkipComponent from insights.core.filters import get_filters from insights.core.plugins import datasource +from insights.parsers.fstab import FSTab from insights.specs import Specs +import os def _list_items(spec): @@ -36,50 +38,55 @@ def _list_items(spec): ============================================================== """ filters.append('_non_existing_') - return ' '.join(filters) + return filters raise SkipComponent @datasource(HostContext) def list_with_la(broker): - return _list_items(Specs.ls_la_dirs) + return ' '.join(_list_items(Specs.ls_la_dirs)) @datasource(HostContext) def list_with_la_filtered(broker): - return _list_items(Specs.ls_la_filtered_dirs) + return ' '.join(_list_items(Specs.ls_la_filtered_dirs)) @datasource(HostContext) def list_with_lan(broker): - return _list_items(Specs.ls_lan_dirs) + filters = set(_list_items(Specs.ls_lan_dirs)) + if 'fstab_mounted.dirs' in filters and FSTab in broker: + for mntp in broker[FSTab].mounted_on.keys(): + mnt_point = os.path.dirname(mntp) + filters.add(mnt_point) if mnt_point else None + return ' '.join(sorted(filters)) @datasource(HostContext) def list_with_lan_filtered(broker): - return _list_items(Specs.ls_lan_filtered_dirs) + return ' '.join(_list_items(Specs.ls_lan_filtered_dirs)) @datasource(HostContext) def list_with_lanL(broker): - return _list_items(Specs.ls_lanL_dirs) + return ' '.join(_list_items(Specs.ls_lanL_dirs)) @datasource(HostContext) def list_with_lanR(broker): - return _list_items(Specs.ls_lanR_dirs) + return ' '.join(_list_items(Specs.ls_lanR_dirs)) @datasource(HostContext) def list_with_lanRL(broker): - return _list_items(Specs.ls_lanRL_dirs) + return ' '.join(_list_items(Specs.ls_lanRL_dirs)) @datasource(HostContext) def list_with_laRZ(broker): - return _list_items(Specs.ls_laRZ_dirs) + return ' '.join(_list_items(Specs.ls_laRZ_dirs)) @datasource(HostContext) def list_with_laZ(broker): - return _list_items(Specs.ls_laZ_dirs) + return ' '.join(_list_items(Specs.ls_laZ_dirs)) diff --git a/insights/tests/datasources/test_ls.py b/insights/tests/datasources/test_ls.py index 8c1a171704..b25fe41283 100644 --- a/insights/tests/datasources/test_ls.py +++ b/insights/tests/datasources/test_ls.py @@ -4,12 +4,24 @@ from insights.core import filters from insights.core.exceptions import SkipComponent +from insights.parsers.fstab import FSTab from insights.specs import Specs from insights.specs.datasources.ls import ( list_with_la, list_with_la_filtered, list_with_lan, list_with_lan_filtered, list_with_lanL, list_with_lanR, list_with_lanRL, list_with_laRZ, list_with_laZ) +from insights.tests import context_wrap + + +FSTAB_CONTEXT = """ +/dev/mapper/rhel_rhel8-root / xfs defaults 0 0 +/dev/vda1 /boot xfs defaults 0 0 +/dev/mapper/rhel_rhel8-swap none swap defaults 0 0 +/dev/mapper/rhel_rhel7-hana1 /hana/data/rhel7-hana1 xfs defaults 0 0 +/dev/mapper/rhel_rhel7-hana2 /hana/data/rhel7-hana2 xfs defaults 0 0 +/dev/mapper/rhel_rhel7-hana3 /hana/data/rhel7-hana3 xfs defaults 0 0 +""".strip() def setup_function(func): @@ -33,6 +45,8 @@ def setup_function(func): filters.add_filter(Specs.ls_laRZ_dirs, ['/boot']) if func is test_lanZ: filters.add_filter(Specs.ls_laZ_dirs, ["/", '/mnt']) + if func is test_lan_with_fstab_mounted_filter: + filters.add_filter(Specs.ls_lan_dirs, ["/", '/boot', 'fstab_mounted.dirs']) def teardown_function(func): @@ -88,3 +102,12 @@ def test_lanRZ(): def test_lanZ(): ret = list_with_laZ({}) assert ret == '/ /mnt' + + +def test_lan_with_fstab_mounted_filter(): + fstab = FSTab(context_wrap(FSTAB_CONTEXT)) + broker = { + FSTab: fstab + } + ret = list_with_lan(broker) + assert ret == '/ /boot /hana/data fstab_mounted.dirs'