test/ostree: use pytest and split out tests

Convert the test to use `pytest` and split out the individual
tests. The temp-directory fixture has the session scope so
that checkpoints can be shared between the individual tests.
This commit is contained in:
Christian Kellner 2021-06-07 22:02:09 +00:00
parent a47a40cf26
commit 8cf35b4728

View file

@ -4,50 +4,69 @@
import os import os
import tempfile import tempfile
import unittest import pytest
from .. import test from .. import test
@unittest.skipUnless(test.TestBase.have_test_data(), "no test-data access") @pytest.fixture(name="tmpdir", scope="module")
@unittest.skipUnless(test.TestBase.can_bind_mount(), "root-only") def tmpdir_fixture():
class TestOSTree(test.TestBase): with tempfile.TemporaryDirectory() as tmp:
def setUp(self): yield tmp
self.osbuild = test.OSBuild(self)
def test_ostree(self):
with self.osbuild as osb:
with tempfile.TemporaryDirectory(dir="/var/tmp") as temp_dir:
# Build a container @pytest.fixture(name="osb", scope="module")
manifest = os.path.join(self.locate_test_data(), def osbuild_fixture():
"manifests/fedora-ostree-container.json") with test.OSBuild() as osb:
osb.compile_file(manifest, yield osb
output_dir=temp_dir,
checkpoints=["build", "ostree-tree", "ostree-commit"],
exports=["container"])
oci_archive = os.path.join(temp_dir, "container", "fedora-container.tar")
self.assertTrue(os.path.exists(oci_archive))
# build a bootable ISO @pytest.fixture(name="testdata", scope="module")
manifest = os.path.join(self.locate_test_data(), def testdata_fixture():
"manifests/fedora-ostree-bootiso.json") return test.TestBase.locate_test_data()
osb.compile_file(manifest,
output_dir=temp_dir,
checkpoints=["build", "ostree-tree", "ostree-commit"],
exports=["bootiso"])
bootiso = os.path.join(temp_dir, "bootiso", "fedora-ostree-boot.iso")
self.assertTrue(os.path.exists(bootiso))
# build a qemu image @pytest.mark.skipif(not test.TestBase.have_test_data(), reason="no test-data access")
manifest = os.path.join(self.locate_test_data(), @pytest.mark.skipif(not test.TestBase.can_bind_mount(), reason="root-only")
"manifests/fedora-ostree-image.json") def test_ostree_container(osb, tmpdir, testdata):
osb.compile_file(manifest,
output_dir=temp_dir,
checkpoints=["build", "ostree-tree", "ostree-commit"],
exports=["qcow2"])
bootiso = os.path.join(temp_dir, "qcow2", "disk.qcow2") # Build a container
self.assertTrue(os.path.exists(bootiso)) manifest = os.path.join(testdata,
"manifests/fedora-ostree-container.json")
osb.compile_file(manifest,
output_dir=tmpdir,
checkpoints=["build", "ostree-tree", "ostree-commit"],
exports=["container"])
oci_archive = os.path.join(tmpdir, "container", "fedora-container.tar")
assert os.path.exists(oci_archive)
@pytest.mark.skipif(not test.TestBase.have_test_data(), reason="no test-data access")
@pytest.mark.skipif(not test.TestBase.can_bind_mount(), reason="root-only")
def test_ostree_bootiso(osb, tmpdir, testdata):
# build a bootable ISO
manifest = os.path.join(testdata,
"manifests/fedora-ostree-bootiso.json")
osb.compile_file(manifest,
output_dir=tmpdir,
checkpoints=["build", "ostree-tree", "ostree-commit"],
exports=["bootiso"])
bootiso = os.path.join(tmpdir, "bootiso", "fedora-ostree-boot.iso")
assert os.path.exists(bootiso)
@pytest.mark.skipif(not test.TestBase.have_test_data(), reason="no test-data access")
@pytest.mark.skipif(not test.TestBase.can_bind_mount(), reason="root-only")
def test_ostree_image(osb, tmpdir, testdata):
# build a qemu image
manifest = os.path.join(testdata,
"manifests/fedora-ostree-image.json")
osb.compile_file(manifest,
output_dir=tmpdir,
checkpoints=["build", "ostree-tree", "ostree-commit"],
exports=["qcow2"])
bootiso = os.path.join(tmpdir, "qcow2", "disk.qcow2")
assert os.path.exists(bootiso)