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

@ -14,21 +14,15 @@ from osbuild import devices, host, loop, meta
from ..test import TestBase
@pytest.fixture(name="tmpdir")
def tmpdir_fixture():
with tempfile.TemporaryDirectory(prefix="test-devices-") as tmp:
yield tmp
@pytest.mark.skipif(not TestBase.can_bind_mount(), reason="root only")
def test_loopback_basic(tmpdir):
def test_loopback_basic(tmp_path):
index = meta.Index(os.curdir)
info = index.get_module_info("Device", "org.osbuild.loopback")
tree = os.path.join(tmpdir, "tree")
tree = os.path.join(tmp_path, "tree")
os.makedirs(tree)
devpath = os.path.join(tmpdir, "dev")
devpath = os.path.join(tmp_path, "dev")
os.makedirs(devpath)
size = 1024 * 1024
@ -37,7 +31,7 @@ def test_loopback_basic(tmpdir):
f.truncate(size)
sb = os.fstat(f.fileno())
testfile = os.path.join(tmpdir, "test.img")
testfile = os.path.join(tmp_path, "test.img")
options = {
"filename": "image.img",

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

View file

@ -37,12 +37,6 @@ def jsondata_fixture():
})
@pytest.fixture(name="tmpdir", scope="module")
def tmpdir_fixture():
with tempfile.TemporaryDirectory() as tmp:
yield tmp
@pytest.fixture(name="osb", scope="module")
def osbuild_fixture():
with test.OSBuild() as osb:
@ -69,5 +63,5 @@ def test_noop2(osb):
@pytest.mark.skipif(not test.TestBase.can_bind_mount(), reason="root-only")
def test_noop_v2(osb, tmpdir, jsondata):
osb.compile(jsondata, output_dir=tmpdir, exports=["noop"])
def test_noop_v2(osb, tmp_path, jsondata):
osb.compile(jsondata, output_dir=tmp_path, exports=["noop"])

View file

@ -114,15 +114,9 @@ def check_case(source, case, store, libdir):
raise ValueError(f"invalid expectation: {expects}")
@pytest.fixture(name="tmpdir")
def tmpdir_fixture():
with tempfile.TemporaryDirectory() as tmp:
yield tmp
@pytest.mark.skipif(not can_setup_netns(), reason="network namespace setup failed")
@pytest.mark.parametrize("source,case", make_test_cases())
def test_sources(source, case, tmpdir):
def test_sources(source, case, tmp_path):
index = osbuild.meta.Index(os.curdir)
sources = os.path.join(test.TestBase.locate_test_data(), "sources")
@ -136,7 +130,7 @@ def test_sources(source, case, tmpdir):
src = osbuild.sources.Source(info, items, options)
with osbuild.objectstore.ObjectStore(tmpdir) as store, \
with osbuild.objectstore.ObjectStore(tmp_path) as store, \
fileServer(test.TestBase.locate_test_data()):
check_case(src, case_options, store, index.path)
check_case(src, case_options, store, index.path)