Skip to content

Commit

Permalink
test: extract a common osbuild_fixture() and share
Browse files Browse the repository at this point in the history
We currently have three copies of the osbuild_fixture() fixture
that are slightly different. This commit extract it into a single
place in `test.py` and shares it accross the various callers.

Note that it would be even nicer to put it into `conftest.py`
but there is a circular import right now when trying this. We
should still do it eventually it just requires a bit more
preperation work.
  • Loading branch information
mvo5 committed Sep 10, 2024
1 parent 9b80862 commit e3773c0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
24 changes: 9 additions & 15 deletions test/run/test_assemblers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@
from osbuild import loop

from .. import test
from ..test import osbuild_fixture # noqa: F401, pylint: disable=unused-import

MEBIBYTE = 1024 * 1024


@pytest.fixture(name="osbuild")
def osbuild_fixture():
store = os.getenv("OSBUILD_TEST_STORE")
osb = test.OSBuild(cache_from=store)
yield osb


def assertImageFile(filename, fmt, expected_size):
info = json.loads(subprocess.check_output(["qemu-img", "info", "--output", "json", filename]))
assert info["format"] == fmt
Expand Down Expand Up @@ -83,7 +77,7 @@ def read_partition_table(device):
@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")
@pytest.mark.parametrize("fs_type", ["ext4", "xfs", "btrfs"])
def test_rawfs(osbuild, fs_type):
def test_rawfs(osb, fs_type):
if not test.TestBase.has_filesystem_support(fs_type):
pytest.skip(f"The {fs_type} was explicitly marked as unsupported on this platform.")
options = {
Expand All @@ -92,7 +86,7 @@ def test_rawfs(osbuild, fs_type):
"size": 1024 * MEBIBYTE,
"fs_type": fs_type,
}
with osbuild as osb:
with osb:
with run_assembler(osb, "org.osbuild.rawfs", options, "image.raw") as (tree, image):
assertImageFile(image, "raw", options["size"])
assertFilesystem(image, options["root_fs_uuid"], fs_type, tree)
Expand All @@ -102,8 +96,8 @@ def test_rawfs(osbuild, fs_type):
@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")
@pytest.mark.skipif(not test.TestBase.have_rpm_ostree(), reason="rpm-ostree missing")
def test_ostree(osbuild):
with osbuild as osb:
def test_ostree(osb):
with osb:
with open(os.path.join(test.TestBase.locate_test_data(),
"manifests/fedora-ostree-commit.json"),
encoding="utf8") as f:
Expand Down Expand Up @@ -158,9 +152,9 @@ def test_ostree(osbuild):
@pytest.mark.skipif(not test.TestBase.can_bind_mount(), reason="root-only")
@pytest.mark.parametrize("fmt,", ["raw", "raw.xz", "qcow2", "vmdk", "vdi"])
@pytest.mark.parametrize("fs_type", ["ext4", "xfs", "btrfs"])
def test_qemu(osbuild, fmt, fs_type):
def test_qemu(osb, fmt, fs_type):
loctl = loop.LoopControl()
with osbuild as osb:
with osb:
if not test.TestBase.has_filesystem_support(fs_type):
pytest.skip(f"The {fs_type} was explicitly marked as unsupported on this platform.")
options = {
Expand Down Expand Up @@ -213,8 +207,8 @@ def test_qemu(osbuild, fmt, fs_type):
[("tree.tar.gz", None, ["application/x-tar"]),
("tree.tar.gz", "gzip", ["application/x-gzip", "application/gzip"])]
)
def test_tar(osbuild, filename, compression, expected_mimetypes):
with osbuild as osb:
def test_tar(osb, filename, compression, expected_mimetypes):
with osb:
options = {"filename": filename}
if compression:
options["compression"] = compression
Expand Down
8 changes: 1 addition & 7 deletions test/run/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from .. import test
from ..test import osbuild_fixture # noqa: F401, pylint: disable=unused-import


@pytest.fixture(name="jsondata", scope="module")
Expand Down Expand Up @@ -42,12 +42,6 @@ def jsondata_fixture():
})


@pytest.fixture(name="osb", scope="module")
def osbuild_fixture():
with test.OSBuild() as osb:
yield osb


@pytest.fixture(name="testing_libdir", scope="module")
def testing_libdir_fixture(tmpdir_factory):
tests_path = pathlib.Path(__file__).parent.parent
Expand Down
6 changes: 1 addition & 5 deletions test/run/test_noop.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import osbuild.main_cli

from .. import test
from ..test import osbuild_fixture # noqa: F401, pylint: disable=unused-import


@pytest.fixture(name="jsondata", scope="module")
Expand Down Expand Up @@ -39,11 +40,6 @@ def jsondata_fixture():
})


@pytest.fixture(name="osb", scope="module")
def osbuild_fixture():
with test.OSBuild() as osb:
yield osb

#
# Run a noop Pipeline. Run twice to verify the cache does not affect
# the operation (we do not have checkpoints, nor any stages that could
Expand Down
4 changes: 2 additions & 2 deletions test/run/test_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def mapping_is_subset(subset, other):
return False


def assertTreeDiffsEqual(tree_diff1, tree_diff2):
def assert_tree_diffs_equal(tree_diff1, tree_diff2):
"""
Asserts two tree diffs for equality.
Expand Down Expand Up @@ -240,7 +240,7 @@ def run_stage_diff_test(self, test_dir: str):
with open(os.path.join(test_dir, "diff.json"), encoding="utf8") as f:
expected_diff = json.load(f)

assertTreeDiffsEqual(expected_diff, actual_diff)
assert_tree_diffs_equal(expected_diff, actual_diff)

md_path = os.path.join(test_dir, "metadata.json")
if os.path.exists(md_path):
Expand Down
9 changes: 9 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import tempfile
import unittest

import pytest

import osbuild.meta
from osbuild.objectstore import ObjectStore
from osbuild.util import linux
Expand Down Expand Up @@ -496,3 +498,10 @@ def copy_source_data(self, target, source):
"cp", "--reflink=auto", "-a",
os.path.join(from_path, "."), to_path
], check=True)


@pytest.fixture(name="osb", scope="module")
def osbuild_fixture():
store = os.getenv("OSBUILD_TEST_STORE")
with OSBuild(cache_from=store) as osb:
yield osb

0 comments on commit e3773c0

Please sign in to comment.