Skip to content

Commit

Permalink
CA-388587: Add fixtures and tests for testing filter_xapi_clusterd_db…
Browse files Browse the repository at this point in the history
…() (#76)

Signed-off-by: Bernhard Kaindl <[email protected]>
(cherry picked from commit 9683fca)
  • Loading branch information
bernhardkaindl authored Feb 14, 2024
1 parent 6ff440e commit bc49be2
Show file tree
Hide file tree
Showing 2 changed files with 400 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,89 @@ def bugtool(imported_bugtool):
# Cleanup the bugtool data dict after each test as tests may modify it:
imported_bugtool.data = {}
sys.argv = ["xen-bugtool", "--unlimited"]


@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

0 comments on commit bc49be2

Please sign in to comment.