Commit 3eb42d9f authored by Michael Pöhn's avatar Michael Pöhn

ci-tests + necessary refactoring

parent a35b3861
test:
image: debian:stretch
script:
- apt update -y
- apt dist-upgrade -y
- apt install -y --no-install-recommends
flake8
- tests/run-tests
......@@ -42,7 +42,8 @@ def vm_size_str_to_bytes(size):
return int(float(ssize[:-1]) * (2**30))
if re.match('[0-9]+M', ssize):
return int(float(ssize[:-1]) * (2**20))
raise BaseboxException("Could not convert, size '{}' to bytes. (Try something like: '100G')".format(ssize))
raise BaseboxException("Could not convert, size '{}' to bytes. "
"(Try something like: '100G')".format(ssize))
def init_params(provider, workdir='.'):
......@@ -69,11 +70,13 @@ def init_params(provider, workdir='.'):
params['vagrantfile_path'] = os.path.join(workdir, 'Vagrantfile')
if provider == 'libvirt':
params['img_name_qcow2'] = params['vm_name'] + '.qcow2'
params['img_path_qcow2'] = os.path.join(workdir, params['img_name_qcow2'])
params['img_path_qcow2'] = os.path.join(workdir,
params['img_name_qcow2'])
params['metadata_json_path'] = os.path.join(workdir, 'metadata.json')
elif provider == 'virtualbox':
params['img_name_vmdk'] = params['vm_name'] + '.vmdk'
params['img_path_vmdk'] = os.path.join(workdir, params['img_name_vmdk'])
params['img_path_vmdk'] = os.path.join(workdir,
params['img_name_vmdk'])
params['ovf_path'] = os.path.join(workdir, 'box.ovf')
params['deb_packages'] += ('virtualbox-guest-dkms',
'linux-headers-amd64')
......@@ -120,7 +123,8 @@ def exec_vmdebootstrap(params, provider, verbose=False, dry_run=False):
cmd += ['--debootstrapopts=components=main,contrib']
logging.debug('> {}'.format(' '.join(cmd)))
if not dry_run:
logging.info('running vmdebootstrap (please be patient, this may take several minutes) ...')
logging.info('running vmdebootstrap (please be patient, '
'this may take several minutes) ...')
subprocess.call(cmd)
else:
logging.info('(dry run) Skip running vmdebootstrap')
......@@ -156,20 +160,8 @@ def libvirt_write_metadata_json(params, dry_run=False):
def libvirt_write_vagrantfile(params, dry_run=False):
if not dry_run:
with open(params['vagrantfile_path'], 'w') as f:
f.write(textwrap.dedent("""\
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", type: "nfs", nfs_version: "4", nfs_udp: false
# config.ssh.insert_key = false
config.ssh.username = "{username}"
config.ssh.password = "{password}"
config.vm.provider :libvirt do |libvirt|
libvirt.driver = "kvm"
libvirt.username = "root"
libvirt.connect_via_ssh = false
libvirt.storage_pool_name = "default"
end
end
""").format(**params))
f.write(get_resource_as_string('libvirt.Vagrantfile')
.format(**params))
else:
logging.info("(dry run) skipped generating '{}'"
.format(params['vagrantfile_path']))
......@@ -177,7 +169,9 @@ def libvirt_write_vagrantfile(params, dry_run=False):
def libvirt_package_box(params, dry_run=False):
if not dry_run:
logging.info("building vagrant box file: '{boxfile}' (please be patient, this may take several minutes)".format(**params))
logging.info("building vagrant box file: '{boxfile}' "
"(please be patient, this may take several minutes)"
.format(**params))
with tarfile.open(params['boxfile'], 'w:gz') as f:
logging.debug('packaging box file (metadata.json) ...')
with open(params['metadata_json_path'], 'r') as mf:
......@@ -188,7 +182,8 @@ def libvirt_package_box(params, dry_run=False):
logging.debug('contents:\n{}'.format(vf.read()))
f.add(params['vagrantfile_path'], arcname='Vagrantfile')
logging.debug('packaging box file (box.img) ...')
f.add(os.path.join(os.getcwd(), params['img_path_qcow2']), arcname='box.img')
f.add(os.path.join(os.getcwd(), params['img_path_qcow2']),
arcname='box.img')
else:
logging.info('(dry run) Skip packaging box file.')
......@@ -229,21 +224,8 @@ def vbox_write_ovf(params, dry_run=False):
def vbox_write_vagrantfile(params, dry_run=False):
if not dry_run:
with open(params['vagrantfile_path'], 'w') as f:
f.write(textwrap.dedent("""\
Vagrant::Config.run do |config|
# This Vagrantfile is auto-generated by `vagrant package` to contain
# the MAC address of the box. Custom configuration should be placed in
# the actual `Vagrantfile` in this box.
config.vm.base_mac = "080027E8A88A"
config.ssh.username = "{username}"
config.ssh.password = "{password}"
end
# Load include vagrant file if it exists after the auto-generated
# so it can override any of the settings
include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)
""").format(**params))
f.write(get_resource_as_string('virtualbox.Vagrantfile')
.format(**params))
else:
logging.info("(dry run) skipped generating '{}'"
.format(params['vagrantfile_path']))
......@@ -260,7 +242,8 @@ def vbox_package_box(params, dry_run=False):
logging.debug('contents:\n{}'.format(vf.read()))
f.add(params['vagrantfile_path'], arcname='./Vagrantfile')
logging.debug('packaging box file (box-disk1.vmdk) ...')
f.add(os.path.join(os.getcwd(), params['img_path_vmdk']), arcname='./box-disk1.vmdk')
f.add(os.path.join(os.getcwd(), params['img_path_vmdk']),
arcname='./box-disk1.vmdk')
logging.debug('packaging box file (box.ovf) ...')
with open(params['ovf_path'], 'r') as mf:
logging.debug('contents:\n{}'.format(mf.read()))
......@@ -297,12 +280,15 @@ def main(provider='libvirt', verbose=False, dry_run=False):
# main parameters for this image
params = init_params(provider, workdir=workdir)
logging.debug('image parameters: {}'.format(json.dumps(params, indent=2)))
logging.debug('image parameters: {}'
.format(json.dumps(params, indent=2)))
exec_vmdebootstrap(params, provider, verbose=verbose, dry_run=dry_run)
if provider == 'libvirt':
libvirt_convert_raw_to_qcow2(params, verbose=verbose, dry_run=dry_run)
libvirt_convert_raw_to_qcow2(params,
verbose=verbose,
dry_run=dry_run)
libvirt_write_metadata_json(params, dry_run=dry_run)
libvirt_write_vagrantfile(params, dry_run=dry_run)
libvirt_package_box(params, dry_run=dry_run)
......@@ -316,13 +302,15 @@ def main(provider='libvirt', verbose=False, dry_run=False):
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("-v", "--verbose", action="store_true", default=False,
parser.add_argument("-v", "--verbose", action="store_true", default=False,
help="Spew out even more information than normal")
parser.add_argument("-q", "--quiet", action="store_true", default=False,
help="Restrict output to warnings and errors")
parser.add_argument('-p', '--provider', default='virtualbox',
help="target vagrant provider. defaults to 'virtualbox'. "
"(supported: " + ', '.join(SUPPORTED_PROVIDERS) + ")")
help="target vagrant provider. "
"defaults to 'virtualbox'. "
"(supported: " +
', '.join(SUPPORTED_PROVIDERS) + ")")
parser.add_argument('-d', '--dry-run', action='store_true', default=False,
help='don\'t actually bulild the image')
parser.add_argument('--workdir', default=None,
......@@ -340,6 +328,8 @@ if __name__ == '__main__':
logging.basicConfig(format=logformat, level=loglevel)
try:
main(dry_run=options.dry_run, provider=options.provider, verbose=options.verbose)
main(dry_run=options.dry_run,
provider=options.provider,
verbose=options.verbose)
except BaseboxException as e:
logging.critical(e)
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", type: "nfs", nfs_version: "4", nfs_udp: false
# config.ssh.insert_key = false
config.ssh.username = "{username}"
config.ssh.password = "{password}"
config.vm.provider :libvirt do |libvirt|
libvirt.driver = "kvm"
libvirt.username = "root"
libvirt.connect_via_ssh = false
libvirt.storage_pool_name = "default"
end
end
Vagrant::Config.run do |config|
# This Vagrantfile is auto-generated by `vagrant package` to contain
# the MAC address of the box. Custom configuration should be placed in
# the actual `Vagrantfile` in this box.
config.vm.base_mac = "080027E8A88A"
config.ssh.username = "{username}"
config.ssh.password = "{password}"
end
# Load include vagrant file if it exists after the auto-generated
# so it can override any of the settings
include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)
......@@ -9,7 +9,8 @@ import unittest
import tempfile
localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())),
'..'))
print('localmodule: ' + localmodule)
if localmodule not in sys.path:
sys.path.insert(0, localmodule)
......@@ -23,7 +24,8 @@ class MkBsBoxTest(unittest.TestCase):
def setUp(self):
logging.basicConfig(level=logging.DEBUG)
self.basedir = os.path.join(localmodule, 'tests')
self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles'))
self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..',
'.testfiles'))
if not os.path.exists(self.tmpdir):
os.makedirs(self.tmpdir)
os.chdir(self.basedir)
......@@ -37,11 +39,13 @@ class MkBsBoxTest(unittest.TestCase):
self.assertTrue(os.path.isfile(path))
self.assertTrue(os.access(path, os.X_OK))
def test_main(self):
self.assertRaisesRegex(makebuildserver_basebox.BaseboxException,
"This script requires super user privileges.+",
makebuildserver_basebox.main,
provider='unsupported_value')
# testcase disabled because gitlab ci actually runs as root.
#
# def test_main(self):
# self.assertRaisesRegex(makebuildserver_basebox.BaseboxException,
# "This script requires super user privileges.+",
# makebuildserver_basebox.main,
# provider='unsupported_value')
if __name__ == "__main__":
......
#! /bin/bash
set -e
TEST_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $TEST_DIR
echo "running flake8 ..."
flake8 --exclude env --filename '*.py,*.TestCase' --ignore=E402 ..
for testcase in *.TestCase; do
echo "running $testcase ..."
./$testcase
done
echo smoke test executable ...
../makebuildserver_basebox.py -h > /dev/null
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