From 3cf8b79e807225b415cc7e992afae060c8a6e4a5 Mon Sep 17 00:00:00 2001 From: David Rheinsberg Date: Wed, 27 May 2020 17:56:26 +0200 Subject: [PATCH] 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. --- .github/workflows/tests.yml | 5 ---- test/{ => mod}/test_objectstore.py | 10 ++++--- test/test.py | 46 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 9 deletions(-) rename test/{ => mod}/test_objectstore.py (98%) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 336b5508..632ca792 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/test/test_objectstore.py b/test/mod/test_objectstore.py similarity index 98% rename from test/test_objectstore.py rename to test/mod/test_objectstore.py index 1d87a585..9f2f8929 100644 --- a/test/test_objectstore.py +++ b/test/mod/test_objectstore.py @@ -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() diff --git a/test/test.py b/test/test.py index 612fe5bf..032c53c8 100644 --- a/test/test.py +++ b/test/test.py @@ -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