test/util: convert util_linux to pytest

Convert the test from `unittest` to `pytest`. No semantic change.
This commit is contained in:
Christian Kellner 2022-04-20 16:23:33 +02:00 committed by Tom Gundersen
parent 4055774c7b
commit 46fd8958bb

View file

@ -5,66 +5,67 @@
import os import os
import subprocess import subprocess
import tempfile import tempfile
import unittest
import pytest
from osbuild.util import linux from osbuild.util import linux
from .. import test from .. import test
class TestUtilLinux(unittest.TestCase): @pytest.fixture(name="tmpdir")
def setUp(self): def tmpdir_fixture():
self.vartmpdir = tempfile.TemporaryDirectory(dir="/var/tmp") with tempfile.TemporaryDirectory(dir="/var/tmp") as tmp:
yield tmp
def tearDown(self):
self.vartmpdir.cleanup()
@unittest.skipUnless(test.TestBase.can_modify_immutable("/var/tmp"), "root-only") @pytest.mark.skipif(not test.TestBase.can_modify_immutable("/var/tmp"), reason="root-only")
def test_ioctl_get_immutable(self): def test_ioctl_get_immutable(tmpdir):
# #
# Test the `ioctl_get_immutable()` helper and make sure it works # Test the `ioctl_get_immutable()` helper and make sure it works
# as intended. # as intended.
# #
with open(f"{self.vartmpdir.name}/immutable", "x") as f: with open(f"{tmpdir}/immutable", "x") as f:
assert not linux.ioctl_get_immutable(f.fileno()) assert not linux.ioctl_get_immutable(f.fileno())
@unittest.skipUnless(test.TestBase.can_modify_immutable("/var/tmp"), "root-only")
def test_ioctl_toggle_immutable(self):
#
# Test the `ioctl_toggle_immutable()` helper and make sure it works
# as intended.
#
with open(f"{self.vartmpdir.name}/immutable", "x") as f: @pytest.mark.skipif(not test.TestBase.can_modify_immutable("/var/tmp"), reason="root-only")
# Check the file is mutable by default and if we clear it again. def test_ioctl_toggle_immutable(tmpdir):
assert not linux.ioctl_get_immutable(f.fileno()) #
linux.ioctl_toggle_immutable(f.fileno(), False) # Test the `ioctl_toggle_immutable()` helper and make sure it works
assert not linux.ioctl_get_immutable(f.fileno()) # as intended.
#
# Set immutable and check for it. Try again to verify with flag set. with open(f"{tmpdir}/immutable", "x") as f:
linux.ioctl_toggle_immutable(f.fileno(), True) # Check the file is mutable by default and if we clear it again.
assert linux.ioctl_get_immutable(f.fileno()) assert not linux.ioctl_get_immutable(f.fileno())
linux.ioctl_toggle_immutable(f.fileno(), True) linux.ioctl_toggle_immutable(f.fileno(), False)
assert linux.ioctl_get_immutable(f.fileno()) assert not linux.ioctl_get_immutable(f.fileno())
# Verify immutable files cannot be unlinked. # Set immutable and check for it. Try again to verify with flag set.
with self.assertRaises(OSError): linux.ioctl_toggle_immutable(f.fileno(), True)
os.unlink(f"{self.vartmpdir.name}/immutable") assert linux.ioctl_get_immutable(f.fileno())
linux.ioctl_toggle_immutable(f.fileno(), True)
assert linux.ioctl_get_immutable(f.fileno())
# Check again that clearing the flag works. # Verify immutable files cannot be unlinked.
linux.ioctl_toggle_immutable(f.fileno(), False) with pytest.raises(OSError):
assert not linux.ioctl_get_immutable(f.fileno()) os.unlink(f"{tmpdir}/immutable")
# This time, check that we actually set the same flag as `chattr`. # Check again that clearing the flag works.
subprocess.run(["chattr", "+i", linux.ioctl_toggle_immutable(f.fileno(), False)
f"{self.vartmpdir.name}/immutable"], check=True) assert not linux.ioctl_get_immutable(f.fileno())
assert linux.ioctl_get_immutable(f.fileno())
# Same for clearing it. # This time, check that we actually set the same flag as `chattr`.
subprocess.run(["chattr", "-i", subprocess.run(["chattr", "+i",
f"{self.vartmpdir.name}/immutable"], check=True) f"{tmpdir}/immutable"], check=True)
assert not linux.ioctl_get_immutable(f.fileno()) assert linux.ioctl_get_immutable(f.fileno())
# Verify we can unlink the file again, once the flag is cleared. # Same for clearing it.
os.unlink(f"{self.vartmpdir.name}/immutable") subprocess.run(["chattr", "-i",
f"{tmpdir}/immutable"], check=True)
assert not linux.ioctl_get_immutable(f.fileno())
# Verify we can unlink the file again, once the flag is cleared.
os.unlink(f"{tmpdir}/immutable")