From bb442f99a06c79bc8bf55dcc147715101801a425 Mon Sep 17 00:00:00 2001 From: Bernhard Kaindl <43588962+bernhardkaindl@users.noreply.github.com> Date: Tue, 12 Dec 2023 12:00:00 +0200 Subject: [PATCH] Add tests/unit/test_load_plugins.py and fix load_plugins() --- .pylintrc | 3 +- .../etc/xensource/bugtool/xapi.xml | 1 + .../etc/xensource/bugtool/xapi/stuff.xml | 6 +++ tests/unit/test_load_plugins.py | 42 +++++++++++++++++++ xen-bugtool | 6 ++- 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/integration/dom0-template/etc/xensource/bugtool/xapi.xml create mode 100644 tests/integration/dom0-template/etc/xensource/bugtool/xapi/stuff.xml create mode 100644 tests/unit/test_load_plugins.py diff --git a/.pylintrc b/.pylintrc index 78dfd299..6803cb0c 100644 --- a/.pylintrc +++ b/.pylintrc @@ -105,8 +105,7 @@ max-returns=11 # Maximum number of statements in function / method body. # defaults to: max-statements=50 -# xen-bugtool:main() is currently at 376, but it has a dedicated pylint disable comment now: -# max-statements=376 +max-statements=52 [FORMAT] diff --git a/tests/integration/dom0-template/etc/xensource/bugtool/xapi.xml b/tests/integration/dom0-template/etc/xensource/bugtool/xapi.xml new file mode 100644 index 00000000..583ed794 --- /dev/null +++ b/tests/integration/dom0-template/etc/xensource/bugtool/xapi.xml @@ -0,0 +1 @@ + diff --git a/tests/integration/dom0-template/etc/xensource/bugtool/xapi/stuff.xml b/tests/integration/dom0-template/etc/xensource/bugtool/xapi/stuff.xml new file mode 100644 index 00000000..3860fa76 --- /dev/null +++ b/tests/integration/dom0-template/etc/xensource/bugtool/xapi/stuff.xml @@ -0,0 +1,6 @@ + +/sys/kernel/reboot +/sys/kernel/reboot* +/sys/kernel/reboot +ls /sys/kernel/reboot + diff --git a/tests/unit/test_load_plugins.py b/tests/unit/test_load_plugins.py new file mode 100644 index 00000000..d5e15abb --- /dev/null +++ b/tests/unit/test_load_plugins.py @@ -0,0 +1,42 @@ +"""Regression tests for bugtool.load_plugins()""" + + +def test_load_plugins(bugtool, dom0_template): + """Assert () returning arrays of the in the dom0-template""" + + # Use the plugins found in the dom0_template "/etc/xensource/bugtool": + bugtool.PLUGIN_DIR = dom0_template + "/etc/xensource/bugtool" + # Only process the xapi bugtool plugin: + bugtool.entries = ["xapi"] + # Reset the predefined caps and cap_sizes, so we can check the plugin code: + # bugtool.caps = {} + # bugtool.cap_sizes = {} + # Load the xapi plugin: + bugtool.load_plugins(just_capabilities=False) + # Assert the set of properties of the created xapi capability: + assert bugtool.caps["xapi"] == ( + "xapi", + "yes", + -1, + 16384, + -1, + 60, + "text/plain", + True, + False, + 9, + ) + # Assert the size of the created xapi capability: + assert bugtool.cap_sizes["xapi"] == 4096 + print(bugtool.data) + # Assert the entries added to the bugtool.data dict: + assert bugtool.data == { + "ls -l /sys/kernel/reboot": { + "cap": "xapi", + "cmd_args": ["ls", "-l", "/sys/kernel/reboot"], + "filter": None, + }, + "/sys/kernel/reboot": {"cap": "xapi", "filename": "/sys/kernel/reboot"}, + "/sys/kernel/reboot/mode": {"cap": "xapi", "filename": "/sys/kernel/reboot/mode"}, + "tty": {"cap": "xapi", "cmd_args": "ls /sys/kernel/reboot", "filter": None}, + } diff --git a/xen-bugtool b/xen-bugtool index 119c91dd..a96cc938 100755 --- a/xen-bugtool +++ b/xen-bugtool @@ -518,7 +518,7 @@ def dir_list(cap, path_list, recursive = False): flags = '-lR' pl = [] - map(lambda x: pl.extend(glob.glob(x)), path_list) + list(map(lambda x: pl.extend(glob.glob(x)), path_list)) for p in pl: cmd_output(cap, [LS, flags, p]) @@ -526,7 +526,7 @@ def dir_list(cap, path_list, recursive = False): def file_output(cap, path_list): if cap in entries: pl = [] - map(lambda x: pl.extend(glob.glob(x)), path_list) + list(map(lambda x: pl.extend(glob.glob(x)), path_list)) for p in pl: try: @@ -1594,6 +1594,8 @@ def load_plugins(just_capabilities = False): for node in nodelist: if node.nodeType == node.TEXT_NODE: rc += node.data + if sys.version_info > (3, 0): + return rc return rc.encode() def getBoolAttr(el, attr, default = False):