Commit e25ccb20 authored by Michael Pöhn's avatar Michael Pöhn

add some dry-run test cases

parent d7dd6132
......@@ -116,24 +116,25 @@ def init_params(provider, debver, workdir='.', verbose=False, dry_run=False):
return params
def write_bootstrap_script(path, deb_mirror, deb_distro, username, provider):
with open(path, 'w') as f:
f.write(get_resource_as_string('debootstrap-custom.sh')
.format(username=username,
deb_mirror=deb_mirror,
deb_distro=deb_distro,
provider=provider))
st = os.stat(path)
os.chmod(path, st.st_mode |
stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def write_bootstrap_script(params, provider):
if not params['dry_run']:
path = params['boostrapscript']
with open(path, 'w') as f:
f.write(get_resource_as_string('debootstrap-custom.sh')
.format(username=params['username'],
deb_mirror=params['deb_mirror'],
deb_distro=params['deb_distro'],
provider=provider))
st = os.stat(path)
os.chmod(path, st.st_mode |
stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
else:
logging.info("(dry run) Skip writing bootstrap script to workdir.")
def exec_vmdebootstrap(params, provider):
write_bootstrap_script(params['boostrapscript'],
params['deb_mirror'],
params['deb_distro'],
params['username'],
write_bootstrap_script(params,
provider)
cmd = [shutil.which('vmdebootstrap') or 'vmdebootstrap',
......@@ -299,10 +300,10 @@ def vbox_package_box(params):
logging.info('(dry run) Skip packaging box file.')
def main(provider='virtualbox', debver='jessie64',
verbose=False, dry_run=False):
def main(provider='virtualbox', debver='jessie64', workdir=None,
verbose=False, dry_run=False, skip_checks=False):
if not os.geteuid() == 0 and not dry_run:
if not os.geteuid() == 0 and not skip_checks:
raise BaseboxException('This script requires super user privileges. '
'(Please use sudo or run as root.)')
......@@ -321,35 +322,33 @@ def main(provider='virtualbox', debver='jessie64',
"{supvers}".format(debver=debver,
supvers=supvers))
if not shutil.which('vmdebootstrap') and not dry_run:
if not shutil.which('vmdebootstrap') and not skip_checks:
raise BaseboxException("Can not find 'vmdebootstrap' executable. "
"(Please install vmdebootstrap.)")
if provider == 'virtualbox' and not dry_run:
if provider == 'virtualbox' and not skip_checks:
if not shutil.which('VBoxManage'):
raise BaseboxException("Can not find 'VBoxManage' executable. "
"(Please install virtualbox.)")
elif provider == 'libvirt' and not dry_run:
elif provider == 'libvirt' and not skip_checks:
if not shutil.which('qemu-img'):
raise BaseboxException("Can not find 'qemu-img' executable. "
"(Please install qemu-utils.)")
if provider == 'virtualbox' and debver == 'stretch64':
if provider == 'virtualbox' and debver == 'stretch64' and not skip_checks:
if not shutil.which('systemd-nspawn'):
raise BaseboxException("Can not find 'systemd-nspawn' executable. "
"(Please install systemd-container.)")
with tempfile.TemporaryDirectory() as tmpdir:
if options.workdir:
workdir = options.workdir
else:
if not workdir:
workdir = tmpdir
# main parameters for this image
params = init_params(provider, debver, workdir=workdir,
verbose=verbose, dry_run=dry_run)
logging.debug('image parameters: {}'
.format(json.dumps(params, indent=2)))
.format(json.dumps(params, indent=2, sort_keys=True)))
exec_vmdebootstrap(params, provider)
......@@ -387,6 +386,10 @@ if __name__ == '__main__':
parser.add_argument('--workdir', default=None,
help="put intermediate steps into requested path, "
"instead of temporary dir")
parser.add_argument('-S', '--skip-checks',
action='store_true', default=False,
help="do not check for dependencies, "
"user permissions, etc.")
options = parser.parse_args()
# Helpful to differentiate warnings from errors even when on quiet
......@@ -402,7 +405,9 @@ if __name__ == '__main__':
main(dry_run=options.dry_run,
provider=options.provider,
debver=options.target,
verbose=options.verbose)
verbose=options.verbose,
workdir=options.workdir,
skip_checks=options.skip_checks)
except BaseboxException as e:
logging.critical(e)
exit(1)
......@@ -33,20 +33,64 @@ class MkBsBoxTest(unittest.TestCase):
def test_write_bootstrap_script(self):
with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, 'script.sh')
fdroid_basebox.write_bootstrap_script(
path, 'http://deb.debian.org/debian', 'jessie',
'theUsersName', 'libvirt')
params = {'dry_run': False,
'boostrapscript': path,
'deb_distro': 'jessie',
'deb_mirror': 'http://deb.debian.org/debian',
'username': 'theUserName'}
fdroid_basebox.write_bootstrap_script(params, 'libvirt')
self.assertTrue(os.path.isfile(path))
self.assertTrue(os.access(path, os.X_OK))
# testcase disabled because gitlab ci actually runs as root.
#
# def test_main(self):
# self.assertRaisesRegex(fdroid_basebox.BaseboxException,
# "This script requires super user privileges.+",
# fdroid_basebox.main,
# provider='unsupported_value')
def test_main_dry_run(self):
fdroid_basebox.main(dry_run=True, skip_checks=True, workdir='.')
def test_main_dry_run_jessie64_virtualbox(self):
fdroid_basebox.main(dry_run=True,
skip_checks=True,
workdir='.',
provider='virtualbox',
debver='jessie64')
def test_main_dry_run_stretch64_virtualbox(self):
fdroid_basebox.main(dry_run=True,
skip_checks=True,
workdir='.',
provider='virtualbox',
debver='stretch64')
def test_main_dry_run_jessie64_libvirt(self):
fdroid_basebox.main(dry_run=True,
skip_checks=True,
workdir='.',
provider='libvirt',
debver='jessie64')
def test_main_dry_run_stretch64_libvirt(self):
fdroid_basebox.main(dry_run=True,
skip_checks=True,
workdir='.',
provider='libvirt',
debver='stretch64')
def test_main_dry_run_bad_provider(self):
self.assertRaisesRegex(fdroid_basebox.BaseboxException,
"provider 'hyper-v' not supported. "
"Supported providers are: libvirt, virtualbox",
fdroid_basebox.main,
provider='hyper-v',
dry_run=True,
skip_checks=True)
def test_main_dry_run_bad_debver(self):
self.assertRaisesRegex(fdroid_basebox.BaseboxException,
"target debian version 'etch32' not supported. "
"Supported versions are: jessie64, stretch64",
fdroid_basebox.main,
debver='etch32',
dry_run=True,
skip_checks=True)
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment