specify virt_container_type via config and CLI options

This commit is contained in:
Hans-Christoph Steiner 2025-10-14 10:16:41 +02:00
parent 6cbef943ac
commit 87d0e5a10b
2 changed files with 77 additions and 0 deletions

View file

@ -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):

View file

@ -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])