debian-forge/test/mod/test_util_rmrf.py
David Rheinsberg 5c0e6f5964 test: convert to shared helpers
Use the `can_modify_immutable()` helper from the TestBase parent class
so we do not duplicate the code in multiple places. Similarly, make use
of the `have_rpm_ostree()` helper.
2020-05-13 14:26:05 +02:00

36 lines
956 B
Python

#
# Tests for the `osbuild.util.rmrf` module.
#
import os
import pathlib
import shutil
import subprocess
import tempfile
import unittest
from osbuild.util import rmrf
from .. import test
class TestUtilLinux(unittest.TestCase):
@unittest.skipUnless(test.TestBase.can_modify_immutable("/var/tmp"), "root-only")
def test_rmtree_immutable(self):
#
# Test the `rmrf.rmtree()` helper and verify it can correctly unlink
# files that are marked immutable.
#
with tempfile.TemporaryDirectory(dir="/var/tmp") as vartmpdir:
os.makedirs(f"{vartmpdir}/dir")
p = pathlib.Path(f"{vartmpdir}/dir/immutable")
p.touch()
subprocess.run(["chattr", "+i", f"{vartmpdir}/dir/immutable"],
check=True)
with self.assertRaises(PermissionError):
shutil.rmtree(f"{vartmpdir}/dir")
rmrf.rmtree(f"{vartmpdir}/dir")