From a1f02113cdbe242384d566a2bd08353e70c88ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Tue, 22 Oct 2024 16:32:16 +0200 Subject: [PATCH] stages/org.osbuild.tar: implement disk-full test this should be an example environment for more stages to test if they return a proper error in a "disk full scenario" --- stages/test/conftest.py | 15 +++++++++++++++ stages/test/test_tar.py | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/stages/test/conftest.py b/stages/test/conftest.py index f2f9d5a7..93811484 100644 --- a/stages/test/conftest.py +++ b/stages/test/conftest.py @@ -1,5 +1,6 @@ import os import pathlib +import subprocess from types import ModuleType import pytest @@ -43,3 +44,17 @@ def stage_schema(request: pytest.FixtureRequest) -> osbuild.meta.Schema: root = caller_dir.parent.parent mod_info = osbuild.meta.ModuleInfo.load(root, "Stage", stage_name) return osbuild.meta.Schema(mod_info.get_schema(version=schema_version), stage_name) + + +@pytest.fixture +def tmp_path_disk_full(tmp_path, tmp_path_disk_full_size): + small_dir = tmp_path / "small-dir" + small_dir.mkdir() + subprocess.run(["mount", + "-t", "tmpfs", + "-o", f"size={tmp_path_disk_full_size}", + "tmpfs", + small_dir + ], check=True) + yield small_dir + subprocess.run(["umount", small_dir], check=True) diff --git a/stages/test/test_tar.py b/stages/test/test_tar.py index 75fb0bd1..c8fa8cc7 100644 --- a/stages/test/test_tar.py +++ b/stages/test/test_tar.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 import os.path +import re import subprocess import pytest @@ -145,3 +146,17 @@ def test_tar_transform_no_sh(tmp_path, stage_module, fake_inputs): with pytest.raises(subprocess.CalledProcessError) as ex: stage_module.main(fake_inputs, tmp_path, options) assert "exit status 2" in str(ex.value) + + +@pytest.mark.parametrize('tmp_path_disk_full_size', [5 * 1024]) +@pytest.mark.skipif(os.getuid() != 0, reason="needs root") +def test_tar_disk_full(stage_module, fake_inputs, tmp_path_disk_full, capfd): + options = { + "filename": "out.tar", + } + + with pytest.raises(subprocess.CalledProcessError) as ex: + stage_module.main(fake_inputs, tmp_path_disk_full, options) + + assert ex.value.returncode == 2 + assert re.match(r".*Wrote only \d+ of \d+ bytes.*", str(capfd.readouterr().err))