objectstore: implement a new metadata class

Implement a new class, nested inside `Object`, to read and write
metadata. It is indexed by a key and individual pieces of meta-
data are stored in separate files. Empty files are not created.
This commit is contained in:
Christian Kellner 2022-11-25 13:28:23 +01:00
parent d48f4eb4ff
commit fec9dcea97
2 changed files with 127 additions and 0 deletions

View file

@ -199,6 +199,61 @@ class TestObjectStore(unittest.TestCase):
assert store_path(store, "b", "A")
assert store_path(store, "b", "B")
def test_metadata(self):
# test metadata object directly first
with tempfile.TemporaryDirectory() as tmp:
md = objectstore.Object.Metadata(tmp)
assert os.fspath(md) == tmp
with self.assertRaises(KeyError):
with md.read("a"):
pass
# do not write anything to the file, it should not get stored
with md.write("a"):
pass
assert len(list(os.scandir(tmp))) == 0
# also we should not write anything if an exception was raised
with self.assertRaises(AssertionError):
with md.write("a") as f:
f.write("{}")
raise AssertionError
with md.write("a") as f:
f.write("{}")
assert len(list(os.scandir(tmp))) == 1
with md.read("a") as f:
assert f.read() == "{}"
data = {
"boolean": True,
"number": 42,
"string": "yes, please"
}
extra = {
"extra": "data"
}
with tempfile.TemporaryDirectory() as tmp:
md = objectstore.Object.Metadata(tmp)
d = md.get("a")
assert d is None
md.set("a", None)
with self.assertRaises(KeyError):
with md.read("a"):
pass
md.set("a", data)
assert md.get("a") == data
def test_host_tree(self):
with objectstore.ObjectStore(self.store) as store:
host = store.host_tree