Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA-388587: Fix filtering the xapi-clusterd db and log any errors #67

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,25 @@ log_cli=True
log_cli_level=INFO
# By default, run the tests in the tests directory:
testpaths=tests/

#
# Make @pytest.mark.xfail(strict=True) the default:
#
# pytest.mark.xfail(strict=True) will fail if the test passes, but the default
# is to not fail if the test passes.
#
# This is a problem because it can lead to tests that are marked as XFAIL, but
# are actually passing, and we don't notice because they are marked as XFAIL.
#
# For example, if we have a test that is marked as XFAIL, but it passes, we
# will see a XPASS instead of a FAILED. This is a problem because we don't
# notice that the test is actually passing, and we don't update the test to
# now be fixed and require it to pass instead from now on.
#
# To fix this, we can set the strict parameter to True, so that if a test is
# marked as XFAIL, but passes, it will fail. This way, we will see a FAILED
# instead of a PASSED (or XPASS).
#
# Make @pytest.mark.xfail(strict=True) the default:
#
xfail_strict=True
89 changes: 89 additions & 0 deletions tests/unit/conftest.py
bernhardkaindl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""tests/unit/conftest.py: pytest fixtures for unit-testing functions in the xen-bugtool script"""
from __future__ import print_function

import os
import shutil
import sys

import pytest
Expand Down Expand Up @@ -72,3 +75,89 @@ def bugtool(imported_bugtool):
imported_bugtool.data = {}
sys.argv = ["xen-bugtool", "--unlimited"]
return imported_bugtool


@pytest.fixture(scope="function")
def in_tmpdir(tmpdir):
"""
Run each test function in it's own temporary directory

This fixture warps pytest's built-in tmpdir fixture with a chdir()
to the tmpdir and a check for leftover files after the test returns.

Usage in a test function:

@pytest.usefixtures("in_tmpdir")
def test_foo(other_fixtures):
# ... test code that runs with the tmpdir as its working directory ...

# If the test function wants to use the in_tmpdir variable:
def test_foo(in_tmpdir):
in_tmpdir.mkdir("subdir").join("filename").write("content_for_testing")
# code under test, that runs with the tmpdir as its working directory:
with open("subdir/filename") as f:
assert f.read() == "content_for_testing"

Documentation on the wrapped tmpdir fixture:
https://docs.pytest.org/en/6.2.x/tmpdir.html#the-tmpdir-fixture
"""

# Get the current directory:
curdir = os.getcwd()

# Change to the temporary directory:
os.chdir(str(tmpdir))

# Run the test:
yield tmpdir # provide the fixture value to the pytest test function

# Change back to the original directory:
os.chdir(curdir)

# upon return, the tmpdir fixture will cleanup the temporary directory


@pytest.fixture(scope="function")
def bugtool_log(in_tmpdir, bugtool):
"""Like in_tmpdir and check that no logs are left in XEN_BUGTOOL_LOG"""

in_tmpdir.mkdir("tmp") # Create a tmp directory for use by test cases

in_tmpdir.join(bugtool.XEN_BUGTOOL_LOG).write("") # create the log file

# Run the test:
yield bugtool # provide the bugtool to the test function

log = in_tmpdir.join(bugtool.XEN_BUGTOOL_LOG).read() # read the log file
if log: # pragma: no cover
print("Content of " + bugtool.XEN_BUGTOOL_LOG + ":" + log, file=sys.stderr)
pytest.fail("Code under test left logs in " + bugtool.XEN_BUGTOOL_LOG)

# Cleanup the temporary directory to prepare to check leftover files:
os.remove(bugtool.XEN_BUGTOOL_LOG)
shutil.rmtree("tmp")

# Check for files that the code under test might have left:
files = in_tmpdir.listdir()
if files: # pragma: no cover
print("Files left in temporary working dir:", files, file=sys.stderr)
pytest.fail("Code under test left files in the its working directory")

# upon return, the in_tmpdir switches back to the original directory


@pytest.fixture(scope="function")
def isolated_bugtool(bugtool_log):
"""
Like `bugtool_log` and make the temporary working directory read-only
to prevent creating files in it
"""

# Make the current cwd (a temporary directory) read-only:
os.chmod(".", 0o555)

yield bugtool_log # runs the test function in the read-only directory

os.chmod(".", 0o777) # restore write permissions (for cleanup)

# upon return, bugtool_log continues with its cleanup
Loading
Loading