From 2ab76c7dfb307c3fbf58457eb22d698c4ae423d0 Mon Sep 17 00:00:00 2001 From: Bernhard Kaindl <43588962+bernhardkaindl@users.noreply.github.com> Date: Mon, 11 Dec 2023 12:00:00 +0200 Subject: [PATCH] Add dom0_template fixture, test_module_info, multipathd_topology --- .../dom0-template/usr/sbin/modinfo | 2 ++ .../dom0-template/usr/sbin/multipathd | 2 ++ tests/unit/conftest.py | 8 ++++++++ tests/unit/test_mdadm_arrays.py | 13 ------------- tests/unit/test_process_output.modules | 3 +++ tests/unit/test_process_output.py | 19 +++++++++++++++++++ xen-bugtool | 12 +++++++++--- 7 files changed, 43 insertions(+), 16 deletions(-) create mode 100755 tests/integration/dom0-template/usr/sbin/modinfo create mode 100755 tests/integration/dom0-template/usr/sbin/multipathd delete mode 100644 tests/unit/test_mdadm_arrays.py create mode 100644 tests/unit/test_process_output.modules create mode 100644 tests/unit/test_process_output.py diff --git a/tests/integration/dom0-template/usr/sbin/modinfo b/tests/integration/dom0-template/usr/sbin/modinfo new file mode 100755 index 00000000..024a6e69 --- /dev/null +++ b/tests/integration/dom0-template/usr/sbin/modinfo @@ -0,0 +1,2 @@ +#!/bin/sh +echo "modinfo for $*" diff --git a/tests/integration/dom0-template/usr/sbin/multipathd b/tests/integration/dom0-template/usr/sbin/multipathd new file mode 100755 index 00000000..2e8c4d48 --- /dev/null +++ b/tests/integration/dom0-template/usr/sbin/multipathd @@ -0,0 +1,2 @@ +#!/bin/sh +echo -n "${0##*/}$*" diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 0c120e73..4ad0529f 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -10,6 +10,14 @@ def testdir(): return os.path.dirname(__file__) +@pytest.fixture(scope="session") +def dom0_template(testdir): + """Test fixture to get the directory of the dom0 template and adding it's /usr/sbin to the PATH""" + dom0_rootdir = testdir + "/../integration/dom0-template" + os.environ["PATH"] = dom0_rootdir + "/usr/sbin" # for modinfo, mdadm, etc + return dom0_rootdir + + @pytest.fixture(scope="session") def bugtool(testdir): """Test fixture to import the xen-bugtool script as a module for executing unit tests on functions""" diff --git a/tests/unit/test_mdadm_arrays.py b/tests/unit/test_mdadm_arrays.py deleted file mode 100644 index b1f68303..00000000 --- a/tests/unit/test_mdadm_arrays.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Regression tests for the bugtool helper function mdadm_arrays()""" -import os -import sys - -import pytest - - -@pytest.mark.skipif(sys.version_info >= (3, 0), reason="limited to python2 until fixed") -def test_mdadm_arrays(bugtool, testdir): - """Assert mdadm_arrays() returning arrays of the mdadm mockup in the dom0-template""" - - os.environ["PATH"] = testdir + "/../integration/dom0-template/usr/sbin" - assert list(bugtool.mdadm_arrays()) == ["/dev/md0", "/dev/md1"] diff --git a/tests/unit/test_process_output.modules b/tests/unit/test_process_output.modules new file mode 100644 index 00000000..6ca24d1d --- /dev/null +++ b/tests/unit/test_process_output.modules @@ -0,0 +1,3 @@ +tcp_diag 16384 0 - Live 0xffffffffc05c0000 +udp_diag 16384 0 - Live 0xffffffffc059c000 +inet_diag 24576 2 tcp_diag,udp_diag, Live 0xffffffffc0533000 diff --git a/tests/unit/test_process_output.py b/tests/unit/test_process_output.py new file mode 100644 index 00000000..eaf1ba5b --- /dev/null +++ b/tests/unit/test_process_output.py @@ -0,0 +1,19 @@ +"""Regression tests for the bugtool helper function mdadm_arrays()""" + + +def test_mdadm_arrays(bugtool, dom0_template): + """Assert mdadm_arrays() returning arrays of the mdadm mockup in the dom0-template""" + assert list(bugtool.mdadm_arrays()) == ["/dev/md0", "/dev/md1"] + + +def test_module_info(bugtool, dom0_template): + """Assert module_info() returning module names from mockup file in the dom0-template""" + + bugtool.PROC_MODULES = __file__.replace(".py", ".modules") + output = bugtool.module_info(bugtool.CAP_KERNEL_INFO) + assert output == "modinfo for tcp_diag\nmodinfo for udp_diag\nmodinfo for inet_diag\n" + + +def test_multipathd_topology(bugtool, dom0_template): + """Assert multipathd_topology() returning the output of the faked multipathd tool""" + assert bugtool.multipathd_topology(bugtool.CAP_MULTIPATH) == bugtool.MULTIPATHD + "-k" diff --git a/xen-bugtool b/xen-bugtool index 6debf65d..75847001 100755 --- a/xen-bugtool +++ b/xen-bugtool @@ -1517,9 +1517,15 @@ def csl_logs(cap): return output.getvalue() def multipathd_topology(cap): - pipe = Popen([MULTIPATHD, '-k'], bufsize=1, stdin=PIPE, - stdout=PIPE, stderr=dev_null) - stdout, _ = pipe.communicate('show topology') + pipe = Popen( + [MULTIPATHD, "-k"], + universal_newlines=sys.version_info > (3, 0), + bufsize=1, + stdin=PIPE, + stdout=PIPE, + stderr=dev_null, + ) + stdout, _ = pipe.communicate("show topology") return stdout