Skip to content

Commit

Permalink
Merge pull request #115 from xenserver-next/CP-49944-multiple-pattern…
Browse files Browse the repository at this point in the history
…s-for-one-directory

CA-394409: plugins: Fix multiple file patterns for the same directory
  • Loading branch information
bernhardkaindl authored Jun 18, 2024
2 parents 1c8d544 + 3a316bd commit 07fff31
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions .vscode/ltex.dictionary.en-US.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ init
initialisation
initialised
initiatorname
inotify
iomem
ioports
iscsi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
<files>/etc/passwd</files>
<files>/etc/group</files>
<files>/proc/self/status</files>

<!--
Test that multiple patterns work together and both are collected.
Two patterns for the same directory, and each pattern matches one file:
- the 1st pattern is negated and matches the file max_queued_events
- the 2nd pattern matches the file max_user_instances:
-->
<directory pattern=".*user_.*" negate="yes">/proc/sys/fs/inotify</directory>
<directory pattern=".*max_user_instances.*">/proc/sys/fs/inotify</directory>
<!--
Expected result, asserted by tests/unit/test_output.py:
- 1st entry: /proc/sys/fs/inotify/max_queued_events
- 2nd entry: /proc/sys/fs/inotify/max_user_instances
-->

<!--
Test that the 2nd directory pattern does not affect the first directory pattern,
even when it matches no file:
- the 1st pattern matches the file /proc/sys/fs/epoll/max_user_watches
- the 2nd pattern matches no file
-->
<directory pattern=".*ax_user_watches">/proc/sys/fs/epoll</directory>
<directory pattern="no" negate="false">/proc/sys/fs/epoll</directory>
<!--
Expected result, asserted by tests/unit/test_output.py:
- /proc/sys/fs/epoll/max_user_watches
-->

<command label="proc_version">cat /proc/version</command>
</collect>
19 changes: 18 additions & 1 deletion tests/unit/test_load_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,25 @@ def test_load_plugins(bugtool, dom0_template):
"filter": None,
},
}

# Assert the tree_output entries for /proc/sys/fs/inotify:
entry_one, entry_two = bugtool.directory_specifications["/proc/sys/fs/inotify"]
cap, regex, negate = entry_one
assert cap == "mock"
assert regex.pattern == ".*user_.*"
assert negate
cap, regex, negate = entry_two
assert cap == "mock"
assert regex.pattern == ".*max_user_instances.*"
assert not negate

# Assert the tree_output entry for /proc/sys/fs/epoll:
cap, regex, negate = bugtool.directory_specifications["/proc/sys/fs/epoll"]
entry_one, entry_two = bugtool.directory_specifications["/proc/sys/fs/epoll"]
cap, regex, negate = entry_one
assert cap == "mock"
assert regex.pattern == ".*ax_user_watches"
assert not negate
cap, regex, negate = entry_two
assert cap == "mock"
assert regex.pattern == "no"
assert not negate
2 changes: 2 additions & 0 deletions tests/unit/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def assert_mock_bugtool_plugin_output(temporary_directory, subdir, names):
subdir + "/ls-l-%etc.out",
subdir + "/proc/self/status",
subdir + "/proc/sys/fs/epoll/max_user_watches",
subdir + "/proc/sys/fs/inotify/max_queued_events",
subdir + "/proc/sys/fs/inotify/max_user_instances",
subdir + "/proc_version.out",
]
assert sorted(names) == expected_names
Expand Down
21 changes: 13 additions & 8 deletions xen-bugtool
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,10 @@ def file_output(cap, path_list):

def tree_output(cap, path, pattern = None, negate = False):
if cap in entries:
directory_specifications[path] = (cap, pattern, negate)
if path in directory_specifications:
directory_specifications[path].append((cap, pattern, negate))
else:
directory_specifications[path] = [(cap, pattern, negate)]


def traverse_directory_specifications(directory_specs, requested_capabilities):
Expand All @@ -611,13 +614,15 @@ def traverse_directory_specifications(directory_specs, requested_capabilities):
:param directory_specs: Directories to lookup with cap, pattern, and negate.
:param requested_capabilities: The list of requested capabilities.
"""
for directory, directory_items in directory_specs.items():
# Unpack the stored capability, pattern and negate flag of each row:
capability, pattern, negate = directory_items
# If the capability is in the requested inventory entries, check it:
if capability in requested_capabilities:
if os.path.isdir(directory):
lookup_tree_recursively(capability, directory, pattern, negate)
for directory, tree_output_entries in directory_specs.items():
# Multiple tree_output calls may have appended multiple output entries:
for directory_items in tree_output_entries:
# Unpack the stored capability, pattern and negate flag of each row:
capability, pattern, negate = directory_items
# If the capability is in the requested inventory entries, check it:
if capability in requested_capabilities:
if os.path.isdir(directory):
lookup_tree_recursively(capability, directory, pattern, negate)


def lookup_tree_recursively(cap, path, pattern, negate):
Expand Down

0 comments on commit 07fff31

Please sign in to comment.