test: move test_objectstore into module-tests

Move `test_objectstore` into the module-level tests. This allows us to
run it as part of `make test-module.

Make sure to properly guard it as root-only module.
This commit is contained in:
David Rheinsberg 2020-05-27 17:56:26 +02:00
parent 9bb6123963
commit 3cf8b79e80
3 changed files with 52 additions and 9 deletions

View file

@ -74,11 +74,6 @@ jobs:
cd osbuild
python3 -m unittest -v test.test_osbuild
- name: Run test_objectstore
run: |
cd osbuild
python3 -m unittest -v test.test_objectstore
sample_validation:
name: "sample validation"
runs-on: ubuntu-latest

View file

@ -1,3 +1,7 @@
#
# Tests for the 'osbuild.objectstore' module.
#
import os
import shutil
import tempfile
@ -5,8 +9,10 @@ import unittest
from pathlib import Path
from osbuild import objectstore
from .. import test
@unittest.skipUnless(test.TestBase.can_bind_mount(), "root-only")
class TestObjectStore(unittest.TestCase):
@classmethod
@ -279,7 +285,3 @@ class TestObjectStore(unittest.TestCase):
p = Path(f"{path}/osbuild-test-file")
with self.assertRaises(OSError):
p.touch()
if __name__ == "__main__":
unittest.main()

View file

@ -119,6 +119,52 @@ class TestBase(unittest.TestCase):
return True
@staticmethod
def can_bind_mount() -> bool:
"""Check Bind-Mount Capability
Test whether we can bind-mount file-system objects. If yes, return
`True`, otherwise return `False`.
"""
with tempfile.TemporaryDirectory() as tmpdir:
original = os.path.join(tmpdir, "original")
mnt = os.path.join(tmpdir, "mnt")
with open(original, "w") as f:
f.write("foo")
with open(mnt, "w") as f:
f.write("bar")
try:
subprocess.run(
[
"mount",
"--make-private",
"-o",
"bind,ro",
original,
mnt,
],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
with open(mnt, "r") as f:
assert f.read() == "foo"
return True
except subprocess.CalledProcessError:
return False
finally:
subprocess.run(
["umount", mnt],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
@staticmethod
def have_rpm_ostree() -> bool:
"""Check rpm-ostree Availability