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

@ -21,12 +21,6 @@ def store_path(store: objectstore.ObjectStore, ref: str, path: str) -> bool:
return os.path.exists(os.path.join(obj, path))
@pytest.fixture(name="tmpdir")
def tmpdir_fixture():
with tempfile.TemporaryDirectory() as tmp:
yield tmp
@pytest.fixture(name="object_store")
def store_fixture():
with tempfile.TemporaryDirectory(
@ -83,8 +77,8 @@ def test_basic(object_store):
assert tree.mode == objectstore.Object.Mode.READ
def test_cleanup(tmpdir):
with objectstore.ObjectStore(tmpdir) as object_store:
def test_cleanup(tmp_path):
with objectstore.ObjectStore(tmp_path) as object_store:
object_store.maximum_size = 1024 * 1024 * 1024
stage = os.path.join(object_store, "stage")
@ -94,11 +88,11 @@ def test_cleanup(tmpdir):
p.touch()
# there should be no temporary Objects dirs anymore
with objectstore.ObjectStore(tmpdir) as object_store:
with objectstore.ObjectStore(tmp_path) as object_store:
assert object_store.get("A") is None
def test_metadata(tmpdir):
def test_metadata(tmp_path):
# test metadata object directly first
with tempfile.TemporaryDirectory() as tmp:
@ -154,8 +148,8 @@ def test_metadata(tmpdir):
md.set("a", data)
assert md.get("a") == data
# use tmpdir fixture from here on
with objectstore.ObjectStore(tmpdir) as store:
# use tmp_path fixture from here on
with objectstore.ObjectStore(tmp_path) as store:
store.maximum_size = 1024 * 1024 * 1024
obj = store.new("a")
p = Path(obj, "A")
@ -170,7 +164,7 @@ def test_metadata(tmpdir):
store.commit(obj, "a")
with objectstore.ObjectStore(tmpdir) as store:
with objectstore.ObjectStore(tmp_path) as store:
obj = store.get("a")
assert obj.meta.get("md") == data
@ -183,8 +177,8 @@ def test_metadata(tmpdir):
@pytest.mark.skipif(not test.TestBase.can_bind_mount(), reason="Need root for bind mount")
def test_host_tree(tmpdir):
with objectstore.ObjectStore(tmpdir) as store:
def test_host_tree(tmp_path):
with objectstore.ObjectStore(tmp_path) as store:
host = store.host_tree
assert host.tree
@ -256,10 +250,10 @@ def test_source_epoch(object_store):
@pytest.mark.skipif(not test.TestBase.can_bind_mount(), reason="Need root for bind mount")
def test_store_server(tmpdir):
def test_store_server(tmp_path):
with contextlib.ExitStack() as stack:
store = objectstore.ObjectStore(tmpdir)
store = objectstore.ObjectStore(tmp_path)
stack.enter_context(store)
tmp = tempfile.TemporaryDirectory()
@ -271,7 +265,7 @@ def test_store_server(tmpdir):
client = objectstore.StoreClient(server.socket_address)
have = client.source("org.osbuild.files")
want = os.path.join(tmpdir, "sources")
want = os.path.join(tmp_path, "sources")
assert have.startswith(want)
tmp = client.mkdtemp(suffix="suffix", prefix="prefix")
@ -288,7 +282,7 @@ def test_store_server(tmpdir):
p.mkdir()
obj.finalize()
mountpoint = Path(tmpdir, "mountpoint")
mountpoint = Path(tmp_path, "mountpoint")
mountpoint.mkdir()
assert store.contains("42")
@ -301,7 +295,7 @@ def test_store_server(tmpdir):
# check we can mount subtrees via `read_tree_at`
filemount = Path(tmpdir, "file")
filemount = Path(tmp_path, "file")
filemount.touch()
path = client.read_tree_at("42", filemount, "/file.txt")
@ -310,7 +304,7 @@ def test_store_server(tmpdir):
txt = filepath.read_text(encoding="utf8")
assert txt == "osbuild"
dirmount = Path(tmpdir, "dir")
dirmount = Path(tmp_path, "dir")
dirmount.mkdir()
path = client.read_tree_at("42", dirmount, "/directory")
@ -321,8 +315,8 @@ def test_store_server(tmpdir):
# mount points and sub-trees
with pytest.raises(RuntimeError):
nonexistent = os.path.join(tmpdir, "nonexistent")
nonexistent = os.path.join(tmp_path, "nonexistent")
_ = client.read_tree_at("42", nonexistent)
with pytest.raises(RuntimeError):
_ = client.read_tree_at("42", tmpdir, "/nonexistent")
_ = client.read_tree_at("42", tmp_path, "/nonexistent")

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)