tests: remove custom tmpdir() fixtures and use tmp_path

This commit removes some unnecessary custom tmpdir() fixtures
and uses the pytest buildin tmp_path instead.

Some custom tmpdir fixtures are left in place as they configure
the tmp location to be under `/var/tmp` which is not trivial to
do with pytests `tmp_path`. Not sure or not if the is a deep
reason there for using /var/tmp. I assume it's to ensure that
the tests run on a real FS not on a potential tmpfs but I don't
have the full background so didn't want to change anything.
This commit is contained in:
Michael Vogt 2023-11-23 10:42:50 +01:00 committed by Simon de Vlieger
parent d66b2faa41
commit 1374faa488
5 changed files with 37 additions and 67 deletions

View file

@ -19,12 +19,6 @@ from osbuild import devices, host, meta, mounts
from ..test import TestBase
@pytest.fixture(name="tmpdir")
def tmpdir_fixture():
with tempfile.TemporaryDirectory(prefix="test-devices-") as tmp:
yield tmp
@contextmanager
def make_arguments(opts):
os.makedirs("/run/osbuild/api")
@ -38,17 +32,17 @@ def make_arguments(opts):
@contextmanager
def make_dev_tmpfs(tmpdir):
dev_path = os.path.join(tmpdir, "dev")
def make_dev_tmpfs(tmp_path):
dev_path = os.path.join(tmp_path, "dev")
os.makedirs(dev_path)
subprocess.run(["mount", "-t", "tmpfs", "-o", "nosuid", "none", dev_path], check=True)
yield dev_path
subprocess.run(["umount", "--lazy", dev_path], check=True)
def create_image(tmpdir):
def create_image(tmp_path):
# create a file to contain an image
tree = os.path.join(tmpdir, "tree")
tree = os.path.join(tmp_path, "tree")
os.makedirs(tree)
size = 2 * 1024 * 1024
file = os.path.join(tree, "image.img")
@ -120,13 +114,13 @@ def mount(mgr, devpath, tree, size, mountpoint, options):
@pytest.mark.skipif(not TestBase.can_bind_mount(), reason="root only")
def test_without_options(tmpdir):
tree, size = create_image(tmpdir)
def test_without_options(tmp_path):
tree, size = create_image(tmp_path)
options = {}
with tempfile.TemporaryDirectory(dir=tmpdir) as mountpoint:
with tempfile.TemporaryDirectory(dir=tmp_path) as mountpoint:
with host.ServiceManager() as mgr:
with make_dev_tmpfs(tmpdir) as devpath:
with make_dev_tmpfs(tmp_path) as devpath:
mount(mgr, devpath, tree, size, mountpoint, options)
with open(os.path.join(mountpoint, "test"), "w", encoding="utf-8") as f:
f.write("should work")
@ -134,8 +128,8 @@ def test_without_options(tmpdir):
@pytest.mark.skipif(not TestBase.can_bind_mount(), reason="root only")
def test_all_options(tmpdir):
tree, size = create_image(tmpdir)
def test_all_options(tmp_path):
tree, size = create_image(tmp_path)
options = {
"readonly": True,
"uid": 0,
@ -145,9 +139,9 @@ def test_all_options(tmpdir):
}
print(options)
with tempfile.TemporaryDirectory(dir=tmpdir) as mountpoint:
with tempfile.TemporaryDirectory(dir=tmp_path) as mountpoint:
with host.ServiceManager() as mgr:
with make_dev_tmpfs(tmpdir) as devpath:
with make_dev_tmpfs(tmp_path) as devpath:
mount(mgr, devpath, tree, size, mountpoint, options)
# Check FS is read only