From 87d0e5a10b9131ea63c1b9dc147d5782da73553d Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 14 Oct 2025 10:16:41 +0200 Subject: [PATCH] specify virt_container_type via config and CLI options --- fdroidserver/common.py | 23 ++++++++++++++++++ tests/test_common.py | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 247ee733..96deb5ea 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -108,6 +108,8 @@ from .looseversion import LooseVersion # The path to this fdroidserver distribution FDROID_PATH = os.path.realpath(os.path.join(os.path.dirname(__file__), '..')) +SUPPORTED_VIRT_CONTAINER_TYPES = ('podman', 'vagrant') + # There needs to be a default, and this is the most common for software. DEFAULT_LOCALE = 'en-US' @@ -280,6 +282,27 @@ def setup_global_opts(parser): ) +def setup_virt_container_type_opts(parser): + parser.add_argument( + "--virt-container-type", + choices=SUPPORTED_VIRT_CONTAINER_TYPES, + help="Set the VM/container type used by the build process.", + ) + + +def get_virt_container_type(options): + if options.virt_container_type: + return options.virt_container_type + vct = get_config().get('virt_container_type') + if vct not in SUPPORTED_VIRT_CONTAINER_TYPES: + supported = ', '.join(sorted(SUPPORTED_VIRT_CONTAINER_TYPES)) + logging.error( + f"'virt_container_type: {vct}' not supported, try: {supported}" + ) + sys.exit(1) + return vct + + class ColorFormatter(logging.Formatter): def __init__(self, msg): diff --git a/tests/test_common.py b/tests/test_common.py index 1dbe79ba..c32a5b35 100755 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -3651,3 +3651,57 @@ class GetHeadCommitIdTest(unittest.TestCase): git_repo.git.add(all=True) git_repo.index.commit("add code") self.assertIsNotNone(fdroidserver.common.get_head_commit_id(git_repo)) + + +class VirtContainerTypeTest(unittest.TestCase): + """Test the logic for choosing which VM/container system to use.""" + + def setUp(self): + self._td = mkdtemp() + self.testdir = self._td.name + os.chdir(self.testdir) + fdroidserver.common.config = None + fdroidserver.common.options = None + # self.options represents the output of argparse.parser() + self.options = mock.Mock() + self.options.virt_container_type = None + + def tearDown(self): + os.chdir(basedir) + self._td.cleanup() + + def test_get_virt_container_type_unset(self): + with self.assertLogs(level=logging.ERROR) as logs: + with self.assertRaises(SystemExit): + fdroidserver.common.get_virt_container_type(self.options) + self.assertIn('virt_container_type', logs.output[0]) + + def test_get_virt_container_type_config(self): + testvalue = 'podman' + Path('config.yml').write_text(f'virt_container_type: {testvalue}\n') + self.assertEqual( + testvalue, fdroidserver.common.get_virt_container_type(self.options) + ) + + def test_get_virt_container_type_options(self): + testvalue = 'podman' + self.options.virt_container_type = testvalue + self.assertEqual( + testvalue, fdroidserver.common.get_virt_container_type(self.options) + ) + + def test_get_virt_container_type_options_override_config(self): + testvalue = 'podman' + self.options.virt_container_type = testvalue + Path('config.yml').write_text('virt_container_type: vagrant\n') + self.assertEqual( + testvalue, fdroidserver.common.get_virt_container_type(self.options) + ) + + def test_get_virt_container_type_config_bad_value(self): + testvalue = 'doesnotexist' + Path('config.yml').write_text(f'virt_container_type: {testvalue}\n') + with self.assertLogs(level=logging.ERROR) as logs: + with self.assertRaises(SystemExit): + fdroidserver.common.get_virt_container_type(self.options) + self.assertIn(testvalue, logs.output[0])