Skip to content

Commit 2cb2302

Browse files
authored
Merge pull request #80 from jepler/test-with-pytest
Demonstrate basic possibility to test with pytest
2 parents e457b5f + b7755b4 commit 2cb2302

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

.github/workflows/python.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
run: python -c "import sys; print(sys.version)"
2424

2525
- name: Install Python build dependencies
26-
run: python -m pip install --upgrade pip setuptools wheel
26+
run: python -m pip install --upgrade pip setuptools wheel pytest
2727

2828
- name: Install libfuse-dev and pkg-config
2929
run: sudo apt install -y libfuse-dev pkg-config
@@ -34,5 +34,4 @@ jobs:
3434
- name: Test
3535
run: |
3636
sudo modprobe fuse
37-
mkdir /tmp/foo && python example/hello.py /tmp/foo && [ "$(cat /tmp/foo/hello)" = "Hello World!" ]
38-
mkdir /tmp/bar && python example/fioc.py /tmp/bar && [ -f /tmp/bar/fioc ]
37+
python -m pytest

example/fioc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def _IOWR(cls,t,nr,size):
7171
FIOC_GET_SIZE = IOCTL._IOR(ord('E'),0, struct.calcsize("L"));
7272
FIOC_SET_SIZE = IOCTL._IOW(ord('E'),1, struct.calcsize("L"));
7373

74+
print(f"{FIOC_GET_SIZE:x} {FIOC_SET_SIZE:x}")
7475
# object type
7576
FIOC_NONE = 0
7677
FIOC_ROOT = 1

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
markers =
3+
fstype: Type of filesystem to mount

tests/test_basic.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
import time
3+
import sys
4+
import struct
5+
import subprocess
6+
import pathlib
7+
import tempfile
8+
import pytest
9+
import fcntl
10+
11+
topdir = pathlib.Path(__file__).parent.parent
12+
13+
@pytest.fixture
14+
def filesystem(request):
15+
fstype = request.node.get_closest_marker("fstype").args[0]
16+
17+
with tempfile.TemporaryDirectory() as tmpdir:
18+
st_dev = os.stat(tmpdir).st_dev
19+
proc = subprocess.Popen([sys.executable, "-d", topdir / "example" / f"{fstype}.py", tmpdir], stdin=subprocess.DEVNULL)
20+
21+
deadline = time.time() + 1
22+
while time.time() < deadline:
23+
new_st_dev = os.stat(tmpdir).st_dev
24+
if new_st_dev != st_dev:
25+
break
26+
time.sleep(.01)
27+
if new_st_dev == st_dev:
28+
proc.terminate()
29+
raise RuntimeError("Filesystem did not mount within 1s")
30+
31+
32+
yield pathlib.Path(tmpdir)
33+
34+
subprocess.call(["fusermount", "-u", "-q", "-z", tmpdir])
35+
36+
deadline = time.time() + 1
37+
while time.time() < deadline:
38+
result = proc.poll()
39+
if result is not None:
40+
if result != 0:
41+
raise RuntimeError("Filesystem exited with an error: {result}")
42+
return
43+
time.sleep(.01)
44+
45+
proc.terminate()
46+
raise RuntimeError("Filesystem failed to exit within 1s after unmount")
47+
48+
@pytest.mark.fstype("hello")
49+
def test_hello(filesystem):
50+
content = (filesystem / "hello").read_text(encoding="utf-8")
51+
assert content == "Hello World!\n"
52+
53+
@pytest.mark.fstype("fioc")
54+
def test_fioc(filesystem):
55+
FIOC_GET_SIZE, FIOC_SET_SIZE = 0x80084500, 0x40084501
56+
with (filesystem / "fioc").open("rb") as f:
57+
b = struct.pack("L", 42)
58+
fcntl.ioctl(f.fileno(), FIOC_SET_SIZE, b)
59+
60+
b = bytearray(struct.calcsize('l'))
61+
fcntl.ioctl(f.fileno(), FIOC_GET_SIZE, b)
62+
assert struct.unpack("L", b)[0] == 42
63+

0 commit comments

Comments
 (0)