From ebe6ad75565a05f647d18147cd8a9e547bde8910 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 26 Sep 2024 21:28:36 -0700 Subject: [PATCH] storage: Skip tests that involve loopback device in a container - Insider a container, it is not possible to use loopback devices without additional permissions. Skips tests that need loopback devices. This will results in fewer errors when running './container run-tests'. Tests: - Run './container run-tests --pytest-args plinth/modules/stroage/test_storage.py'. Notice that all tests are either skipped or succeed. - Run the tests on host machine and they all run without skipping. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/modules/storage/tests/test_storage.py | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/plinth/modules/storage/tests/test_storage.py b/plinth/modules/storage/tests/test_storage.py index a203aea5a..2ad7fbf33 100644 --- a/plinth/modules/storage/tests/test_storage.py +++ b/plinth/modules/storage/tests/test_storage.py @@ -18,8 +18,23 @@ from plinth.diagnostic_check import DiagnosticCheck, Result from plinth.modules import storage from plinth.modules.storage import privileged + +def _is_container(): + """Return if we are running inside a container.""" + try: + process = subprocess.run(['systemd-detect-virt', '--container'], + check=False) + except FileNotFoundError: + # systemd is not installed on the machine. Likely a container. + return True # Skip just in case. + + return process.returncode == 0 + + pytestmark = pytest.mark.usefixtures('mock_privileged') privileged_modules_to_mock = ['plinth.modules.storage.privileged'] +skip_if_container = pytest.mark.skipif(_is_container(), + reason='running inside a container') class Disk(): @@ -163,6 +178,7 @@ def disk_space_available(): @pytest.mark.usefixtures('needs_root') @pytest.mark.skipif(disk_space_available() < 1024, reason='Needs 1024MiB of space') +@skip_if_container def test_simple_case(): """Test a simple with no complications""" disk_info = [ @@ -188,6 +204,7 @@ def test_simple_case(): @pytest.mark.usefixtures('needs_root') @pytest.mark.skipif(disk_space_available() < 512, reason='Needs 512MiB of space') +@skip_if_container def test_extended_partition_free_space(): """Test that free space does not show up when outside extended.""" disk_info = [ @@ -202,6 +219,7 @@ def test_extended_partition_free_space(): @pytest.mark.usefixtures('needs_root') @pytest.mark.skipif(disk_space_available() < 512, reason='Needs 512MiB of space') +@skip_if_container def test_gpt_partition_free_space(): """Test that GPT partitions can be expanded.""" # Specifically check for partition number > 4 @@ -225,6 +243,7 @@ def test_gpt_partition_free_space(): @pytest.mark.usefixtures('needs_root') @pytest.mark.parametrize('partition_table_type', ['gpt', 'msdos']) @pytest.mark.skipif(disk_space_available() < 32, reason='Needs 32MiB of space') +@skip_if_container def test_unsupported_file_system(partition_table_type): """Test that free space after unknown file system does not count.""" disk_info = [f'mktable {partition_table_type}', 'mkpart primary 1 8'] @@ -237,6 +256,7 @@ def test_unsupported_file_system(partition_table_type): @pytest.mark.parametrize('partition_table_type', ['gpt', 'msdos']) @pytest.mark.skipif(disk_space_available() < 512, reason='Needs 512MiB of space') +@skip_if_container def test_btrfs_expansion(partition_table_type): """Test that btrfs file system can be expanded.""" disk_info = [ @@ -252,6 +272,7 @@ def test_btrfs_expansion(partition_table_type): @pytest.mark.parametrize('partition_table_type', ['gpt', 'msdos']) @pytest.mark.skipif(disk_space_available() < 128, reason='Needs 128MiB of space') +@skip_if_container def test_ext4_expansion(partition_table_type): """Test that ext4 file system can be expanded.""" disk_info = [f'mktable {partition_table_type}', 'mkpart primary ext4 1 64']