Skip to content

Commit efd6a75

Browse files
test_mountingaccessor.py: use caplog to capture logs
1 parent fafb6cd commit efd6a75

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

tests/test_accessor.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77

88
if TYPE_CHECKING:
99
import pyfakefs
10+
import pytest
1011

1112

12-
def test_file_accessor(fs):
13-
# type(pyfakefs.fake_filesystem.FakeFilesystem) -> None
13+
def test_file_accessor(fs, caplog):
14+
# type(pyfakefs.fake_filesystem.FakeFilesystem, pytest.LogCaptureFixture) -> None
1415
"""Test FileAccessor.writeFile(), .openAddress and .access using pyfakefs"""
1516
accessor = xcp.accessor.createAccessor("file://repo/", False)
1617
assert isinstance(accessor, xcp.accessor.FileAccessor)
1718
check_binary_read(accessor, "/repo", fs)
18-
check_binary_write(accessor, "/repo", fs)
19+
check_binary_write(accessor, "/repo", fs, caplog)
1920

2021

2122
class TestAccessor(unittest.TestCase):

tests/test_mountingaccessor.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""pytest tests testing subclasses of xcp.accessor.MountingAccessor using pyfakefs"""
2+
3+
import logging
24
import os
35
import sys
46
from io import BytesIO
57
from typing import TYPE_CHECKING, cast
68

9+
import pytest
10+
from pytest import LogCaptureFixture as LogCap
711
from mock import patch
812
from pyfakefs.fake_filesystem import FakeFileOpen, FakeFilesystem
913

@@ -18,8 +22,6 @@
1822
if TYPE_CHECKING:
1923
from typing_extensions import Literal
2024
else:
21-
import pytest
22-
2325
pytest.skip(allow_module_level=True)
2426
sys.exit(0) # Let pyright know that this is a dead end
2527

@@ -30,8 +32,8 @@ def expect(fp, mount):
3032
fp.register_subprocess(mount) # type: ignore[arg-type]
3133

3234

33-
def test_device_accessor(fs, fp, mount_dir):
34-
# type: (FakeFilesystem, FakeProcess, bytes) -> None
35+
def test_device_accessor(fs, fp, mount_dir, caplog):
36+
# type: (FakeFilesystem, FakeProcess, bytes, LogCap) -> None
3537
assert isinstance(fp, FakeProcess)
3638

3739
# Test xcp.mount.bindMount()
@@ -43,11 +45,11 @@ def test_device_accessor(fs, fp, mount_dir):
4345
accessor = xcp.accessor.createAccessor("dev:///dev/device", (True, [""]))
4446

4547
assert isinstance(accessor, xcp.accessor.MountingAccessorTypes)
46-
check_mounting_accessor(accessor, fs, fp, mount_dir)
48+
check_mounting_accessor(accessor, fs, fp, mount_dir, caplog)
4749

4850

49-
def test_nfs_accessor(fs, fp, mount_dir):
50-
# type: (FakeFilesystem, FakeProcess, bytes) -> None
51+
def test_nfs_accessor(fs, fp, mount_dir, caplog):
52+
# type: (FakeFilesystem, FakeProcess, bytes, LogCap) -> None
5153
assert isinstance(fp, FakeProcess)
5254
mount = [
5355
b"/bin/mount",
@@ -61,11 +63,11 @@ def test_nfs_accessor(fs, fp, mount_dir):
6163
expect(fp, mount)
6264
accessor = xcp.accessor.createAccessor("nfs://server/path", False)
6365
assert isinstance(accessor, xcp.accessor.NFSAccessor)
64-
check_mounting_accessor(accessor, fs, fp, mount_dir)
66+
check_mounting_accessor(accessor, fs, fp, mount_dir, caplog)
6567

6668

67-
def check_mounting_accessor(accessor, fs, fp, mount_point):
68-
# type: (xcp.accessor.Mount, FakeFilesystem, FakeProcess, bytes) -> None
69+
def check_mounting_accessor(accessor, fs, fp, mount_point, caplog):
70+
# type: (xcp.accessor.Mount, FakeFilesystem, FakeProcess, bytes, LogCap) -> None
6971
"""Test subclasses of MountingAccessor (with xcp.cmd.runCmd in xcp.mount mocked)"""
7072

7173
assert isinstance(accessor, xcp.accessor.MountingAccessorTypes)
@@ -83,7 +85,7 @@ def check_mounting_accessor(accessor, fs, fp, mount_point):
8385
fs.add_mount_point(location)
8486

8587
assert check_binary_read(accessor, location, fs)
86-
assert check_binary_write(accessor, location, fs)
88+
assert check_binary_write(accessor, location, fs, caplog)
8789
assert open_text(accessor, location, fs, UTF8TEXT_LITERAL) == UTF8TEXT_LITERAL
8890

8991
if sys.version_info.major >= 3:
@@ -119,15 +121,20 @@ def check_binary_read(accessor, location, fs):
119121
return cast(bytes, binary_file.read()) == binary_data
120122

121123

122-
def check_binary_write(accessor, location, fs):
123-
# type: (Literal[False] | xcp.accessor.AnyAccessor, str, FakeFilesystem) -> bool
124+
def check_binary_write(accessor, location, fs, caplog):
125+
# type: (xcp.accessor.AnyAccessor, str, FakeFilesystem, LogCap) -> bool
124126
"""Test the writeFile() method of different types of local Accessor classes"""
125127

126128
assert isinstance(accessor, xcp.accessor.LocalTypes)
127129
name = "binary_file_written_by_accessor"
128-
accessor.writeFile(BytesIO(binary_data), name)
129-
130-
assert accessor.access(name)
130+
with caplog.at_level(logging.FATAL):
131+
accessor.writeFile(BytesIO(binary_data), name)
132+
if caplog.record_tuples:
133+
logger, level, message = caplog.record_tuples[0]
134+
assert logger == "root"
135+
assert level == logging.FATAL
136+
assert message == "Copying to " + name
137+
assert accessor.access(name)
131138

132139
with FakeFileOpen(fs, delete_on_close=True)(location + "/" + name, "rb") as written:
133140
return cast(bytes, written.read()) == binary_data

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ extras = coverage
129129
commands =
130130
coverage xml -o {envlogdir}/coverage.xml --fail-under {env:XCP_COV_MIN:68}
131131
coverage html -d {envlogdir}/htmlcov
132-
coverage html -d {envlogdir}/htmlcov-tests --fail-under {env:TESTS_COV_MIN:96} \
132+
coverage html -d {envlogdir}/htmlcov-tests --fail-under {env:TESTS_COV_MIN:95} \
133133
--include="tests/*"
134134
diff-cover --compare-branch=origin/master --include-untracked \
135135
{env:PY3_DIFFCOVER_OPTIONS} --fail-under {env:DIFF_COV_MIN:92} \

xcp/accessor.py

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def openAddress(self, address):
134134

135135
class MountingAccessor(FilesystemAccessor):
136136
def __init__(self, mount_types, mount_source, mount_options=None):
137+
# type: (List[str], str, List[str] | None) -> None
137138
ro = isinstance(mount_options, list) and 'ro' in mount_options
138139
super(MountingAccessor, self).__init__(None, ro)
139140

0 commit comments

Comments
 (0)