objectstore: add HostTree class to access host fs

Simple new object that should expose the root file system with the
same API as `objectstore.Object` but as read-only. This means that
the `read` call works exactly as for `Object` but `write` raises
an exception.
Add tests to specifically check the read-only properties.
This commit is contained in:
Christian Kellner 2020-03-06 14:20:32 +01:00 committed by Tom Gundersen
parent 01efa9f817
commit 457f21a336
2 changed files with 42 additions and 0 deletions

View file

@ -203,6 +203,33 @@ class Object:
return exc_type is None
class HostTree:
"""Read-only access to the host file system
An object that provides the same interface as
`objectstore.Object` that can be used to read
the host file-system.
"""
def __init__(self, store):
self.store = store
@staticmethod
def write():
raise ValueError("Cannot write to host")
@contextlib.contextmanager
def read(self):
with self.store.tempdir() as tmp:
mount("/", tmp)
try:
yield tmp
finally:
umount(tmp)
def cleanup(self):
pass # noop for the host
class ObjectStore:
def __init__(self, store):
self.store = store

View file

@ -263,6 +263,21 @@ class TestObjectStore(unittest.TestCase):
assert os.path.exists(f"{object_store.refs}/b/A")
assert os.path.exists(f"{object_store.refs}/b/B")
def test_host_tree(self):
object_store = objectstore.ObjectStore(self.store)
host = objectstore.HostTree(object_store)
# check we cannot call `write`
with self.assertRaises(ValueError):
with host.write() as _:
pass
# check we actually cannot write to the path
with host.read() as path:
p = Path(f"{path}/osbuild-test-file")
with self.assertRaises(OSError):
p.touch()
if __name__ == "__main__":
unittest.main()