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.
This commit is contained in:
David Rheinsberg 2020-05-06 11:26:05 +02:00
parent 851d949027
commit 5c0e6f5964
3 changed files with 9 additions and 37 deletions

View file

@ -9,18 +9,7 @@ import unittest
from osbuild.util import linux
def can_set_immutable():
with tempfile.TemporaryDirectory(dir="/var/tmp") as tmp:
try:
os.makedirs(f"{tmp}/f")
# fist they give it ...
subprocess.run(["chattr", "+i", f"{tmp}/f"], check=True)
# ... then they take it away
subprocess.run(["chattr", "-i", f"{tmp}/f"], check=True)
except (subprocess.CalledProcessError, FileNotFoundError):
return False
return True
from .. import test
class TestUtilLinux(unittest.TestCase):
@ -30,6 +19,7 @@ class TestUtilLinux(unittest.TestCase):
def tearDown(self):
self.vartmpdir.cleanup()
@unittest.skipUnless(test.TestBase.can_modify_immutable("/var/tmp"), "root-only")
def test_ioctl_get_immutable(self):
#
# Test the `ioctl_get_immutable()` helper and make sure it works
@ -39,7 +29,7 @@ class TestUtilLinux(unittest.TestCase):
with open(f"{self.vartmpdir.name}/immutable", "x") as f:
assert not linux.ioctl_get_immutable(f.fileno())
@unittest.skipUnless(can_set_immutable(), "root-only")
@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

View file

@ -8,6 +8,8 @@ import subprocess
from osbuild.util import ostree
from .. import test
def run(*args, check=True, encoding="utf-8", **kwargs):
res = subprocess.run(*args,
@ -17,19 +19,10 @@ def run(*args, check=True, encoding="utf-8", **kwargs):
return res
def have_rpm_ostree():
try:
r = run(["rpm-ostree", "--version"],
capture_output=True, check=False)
except FileNotFoundError:
return False
return r.returncode == 0 and "compose" in r.stdout
class TestObjectStore(unittest.TestCase):
# pylint: disable=no-self-use
@unittest.skipIf(not have_rpm_ostree(), "rpm-ostree missing")
@unittest.skipUnless(test.TestBase.have_rpm_ostree(), "rpm-ostree missing")
def test_treefile_empty(self):
# check we produce a valid treefile from an empty object
tf = ostree.Treefile()
@ -64,7 +57,7 @@ class TestObjectStore(unittest.TestCase):
self.assertEqual(js["ref"], test_ref)
self.assertEqual(tf["ref"], test_ref)
@unittest.skipIf(not have_rpm_ostree(), "rpm-ostree missing")
@unittest.skipUnless(test.TestBase.have_rpm_ostree(), "rpm-ostree missing")
def test_treefile_full(self):
params = {
"ref": "osbuild/ostree/devel",

View file

@ -11,22 +11,11 @@ import unittest
from osbuild.util import rmrf
def can_set_immutable():
with tempfile.TemporaryDirectory(dir="/var/tmp") as tmp:
try:
os.makedirs(f"{tmp}/f")
# fist they give it ...
subprocess.run(["chattr", "+i", f"{tmp}/f"], check=True)
# ... then they take it away
subprocess.run(["chattr", "-i", f"{tmp}/f"], check=True)
except (subprocess.CalledProcessError, FileNotFoundError):
return False
return True
from .. import test
class TestUtilLinux(unittest.TestCase):
@unittest.skipUnless(can_set_immutable(), "Need root permissions")
@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