debian-forge/test/test_objectstore.py
Christian Kellner 42a365d12f osbuild: no auto commit of the last stage
Do not automatically commit the last stage of the pipeline to the
store. The last stage is most likely not what should be cached,
because it will contain all the individual customization and thus
be very likely different for different users. Instead, the dnf or
rpm stages have a higher chance of being the same and thus are
better candidates for caching.
Technically this change is done via two big changes that build
upon new features introduces in the previous commits, most notably
the copy on write semantics of Object and that input/output is
being done via `objectstore.Object` instead of plain paths. The
first of the two big changes is  to create one new `Object` at
the beginning of `pipeline.run` and use that, in write mode via
`Object.write` across invocations of `stage.run` calls, with
checkpoints being created after each stage on demand.
The very same `Object` is then used in read mode via `Object.read`
as the input tree for the Assembler. After the assembler is done
the resulting image/tree is manually committed to the store.
The other big change is to remove the `ObjectStore.commit` call
from the `ObjectStore.new` method and thus the automatic commit
after the last stage is gone.
NB: since the build tree is being retrieved in `get_buildtree`
from the store, a checkpoint for the last stage of the build
pipeline is forced for now. Future commits will refactor will
do away with that forced commit as well.
Change osbuildtest.TestCase to always create a checkpoint at
the final tree (the last stage of the pipeline), since tests
need it to check the tree contents.
2020-02-28 16:11:49 +01:00

103 lines
3.6 KiB
Python

import os
import shutil
import tempfile
import unittest
from pathlib import Path
from osbuild import objectstore
class TestObjectStore(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.store = os.getenv("OSBUILD_TEST_STORE")
if not cls.store:
cls.store = tempfile.mkdtemp(prefix="osbuild-test-", dir="/var/tmp")
@classmethod
def tearDownClass(cls):
if not os.getenv("OSBUILD_TEST_STORE"):
shutil.rmtree(cls.store)
def test_basic(self):
# always use a temporary store so item counting works
with tempfile.TemporaryDirectory(dir="/var/tmp") as tmp:
object_store = objectstore.ObjectStore(tmp)
with object_store.new() as tree:
path = tree.write()
p = Path(f"{path}/A")
p.touch()
object_store.commit(tree, "a")
assert os.path.exists(f"{object_store.refs}/a")
assert os.path.exists(f"{object_store.refs}/a/A")
assert len(os.listdir(object_store.refs)) == 1
assert len(os.listdir(object_store.objects)) == 1
assert len(os.listdir(f"{object_store.refs}/a/")) == 1
with object_store.new() as tree:
path = tree.write()
p = Path(f"{path}/A")
p.touch()
p = Path(f"{path}/B")
p.touch()
object_store.commit(tree, "b")
assert os.path.exists(f"{object_store.refs}/b")
assert os.path.exists(f"{object_store.refs}/b/B")
assert len(os.listdir(object_store.refs)) == 2
assert len(os.listdir(object_store.objects)) == 2
assert len(os.listdir(f"{object_store.refs}/b/")) == 2
def test_duplicate(self):
with tempfile.TemporaryDirectory(dir="/var/tmp") as tmp:
object_store = objectstore.ObjectStore(tmp)
with object_store.new() as tree:
path = tree.write()
p = Path(f"{path}/A")
p.touch()
object_store.commit(tree, "a")
with object_store.new() as tree:
path = tree.write()
shutil.copy2(f"{object_store.refs}/a/A",
f"{path}/A")
object_store.commit(tree, "b")
assert os.path.exists(f"{object_store.refs}/a")
assert os.path.exists(f"{object_store.refs}/a/A")
assert os.path.exists(f"{object_store.refs}/b/A")
assert len(os.listdir(object_store.refs)) == 2
assert len(os.listdir(object_store.objects)) == 1
assert len(os.listdir(f"{object_store.refs}/a/")) == 1
assert len(os.listdir(f"{object_store.refs}/b/")) == 1
def test_snapshot(self):
object_store = objectstore.ObjectStore(self.store)
with object_store.new() as tree:
path = tree.write()
p = Path(f"{path}/A")
p.touch()
object_store.commit(tree, "a")
path = tree.write()
p = Path(f"{path}/B")
p.touch()
object_store.commit(tree, "b")
# check the references exist
assert os.path.exists(f"{object_store.refs}/a")
assert os.path.exists(f"{object_store.refs}/b")
# check the contents of the trees
assert os.path.exists(f"{object_store.refs}/a/A")
assert not os.path.exists(f"{object_store.refs}/a/B")
assert os.path.exists(f"{object_store.refs}/b/A")
assert os.path.exists(f"{object_store.refs}/b/B")
if __name__ == "__main__":
unittest.main()