Skip to content

Commit

Permalink
Fix pytest for Python3.12: It does not support '-' in module names (#90)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
  • Loading branch information
bernhardkaindl authored Mar 22, 2024
1 parent ec15cea commit 99d9441
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ def import_from_file(module_name, file_path):
spec.loader.exec_module(module)
return module

bugtool = import_from_file("xen-bugtool", testdir + "/../../xen-bugtool")
#
# Import the xen-bugtool script as a module:
# Python3.12's mocker.patch() stopped supporting modules with a "-" in them.
#
# Use "bugtool" as the module name instead of "xen-bugtool" to avoid this
# problem with the mocker.patch() function in Python 3.12.
#
bugtool = import_from_file("bugtool", testdir + "/../../xen-bugtool")
bugtool.ProcOutput.debug = True

# Prepend tests/mocks to force the use of our mock xen.lowlevel.xc module:
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_dump_xapi_rrds.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def assert_mock_session1(bugtool, mock_urlopen):
def run_dump_xapi_rrds(mocker, bugtool, mock_session, mock_urlopen):
"""Run the bugtool function dump_xapi_rrds(entries) with the given mocks."""
# Patch the urlopen, xapi_local_session and entries
mocker.patch("xen-bugtool.urlopen", side_effect=mock_urlopen)
mocker.patch("xen-bugtool.xapi_local_session", return_value=mock_session)
mocker.patch("xen-bugtool.entries", [bugtool.CAP_PERSISTENT_STATS])
mocker.patch("bugtool.urlopen", side_effect=mock_urlopen)
mocker.patch("bugtool.xapi_local_session", return_value=mock_session)
mocker.patch("bugtool.entries", [bugtool.CAP_PERSISTENT_STATS])

# Run the function
bugtool.dump_xapi_rrds(bugtool.entries)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def assert_mock_bugtool_plugin_output(temporary_directory, subdir, names):
def minimal_bugtool(bugtool, dom0_template, archive, subdir, mocker):
"""Load the plugins from the template and include the generated inventory"""

mocker.patch("xen-bugtool.time.strftime", return_value="time.strftime")
mocker.patch("bugtool.time.strftime", return_value="time.strftime")
# Load the mock plugin from dom0_template and process the plugin's caps:
bugtool.PLUGIN_DIR = dom0_template + "/etc/xensource/bugtool"
bugtool.entries = ["mock"]
Expand Down

0 comments on commit 99d9441

Please sign in to comment.