From 2296c09b35d18cb2875c9eaab1aa4c5bdf3d3886 Mon Sep 17 00:00:00 2001 From: Jakub Klama Date: Wed, 7 Sep 2016 22:43:10 +0200 Subject: [PATCH] Set up a DHCP server for tested bhyve instance. --- tests/freenas/config.pyd | 1 + tests/freenas/run.py | 73 ++++------------------ tests/freenas/vm.py | 128 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 62 deletions(-) create mode 100644 tests/freenas/vm.py diff --git a/tests/freenas/config.pyd b/tests/freenas/config.pyd index e214f53..8de5c81 100644 --- a/tests/freenas/config.pyd +++ b/tests/freenas/config.pyd @@ -27,5 +27,6 @@ CORES = "1" MEMSIZE = "4096" HOST_IP = "192.168.100.1" +FREENAS_IP = "192.168.100.2" NETMASK = "255.255.255.0" VM_NAME = "freenas" diff --git a/tests/freenas/run.py b/tests/freenas/run.py index a338eaa..67d2501 100644 --- a/tests/freenas/run.py +++ b/tests/freenas/run.py @@ -25,80 +25,29 @@ # import os -import subprocess from dsl import load_file, load_profile_config from utils import sh, sh_str, sh_spawn, info, objdir, e load_profile_config() load_file(e('${BUILD_ROOT}/tests/freenas/config.pyd'), os.environ) -destdir = objdir('tests') -isopath = objdir('${NAME}.iso') -tapdev = None +testdir = objdir('tests') +venvdir = objdir('tests/venv') def cleanup(): sh('bhyvectl --destroy --vm=${VM_NAME}', nofail=True) -def setup_files(): - sh('mkdir -p ${destdir}') - sh('truncate -s 8G ${destdir}/boot.img') - sh('truncate -s 20G ${destdir}/hd1.img') - sh('truncate -s 20G ${destdir}/hd2.img') - - -def setup_network(): - global tapdev - - info('Configuring VM networking') - tapdev = sh_str('ifconfig tap create') - info('Using tap device {0}', tapdev) - sh('ifconfig ${tapdev} inet ${HOST_IP} ${NETMASK} up') - - -def do_install(): - info('Starting up VM for unattended install') - vm_proc = sh_spawn( - 'bhyve -m ${MEMSIZE} -c ${CORES} -A -H -P', - '-s 3:0,ahci-hd,${destdir}/boot.img', - '-s 4:0,ahci-hd,${destdir}/hd1.img', - '-s 5:0,ahci-hd,${destdir}/hd2.img', - '-s 6:0,ahci-cd,${isopath}', - '-s 7:0,virtio-net,${tapdev}', - '-s 8:0,fbuf,tcp=5900,w=1024,h=768', - '-s 31,lpc', - '-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd', - '${VM_NAME}' - ) - - try: - vm_proc.wait(timeout=3600) - except subprocess.TimeoutExpired: - fail('Install timed out after 1 hour') - - -def do_run(): - info('Starting up VM for testing') - vm_proc = sh_spawn( - 'bhyve -m ${MEMSIZE} -c ${CORES} -A -H -P', - '-s 3:0,ahci-hd,${destdir}/boot.img', - '-s 4:0,ahci-hd,${destdir}/hd1.img', - '-s 5:0,ahci-hd,${destdir}/hd2.img', - '-s 6:0,virtio-net,${tapdev}', - '-s 7:0,fbuf,tcp=5900,w=1024,h=768', - '-s 31,lpc', - '-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd', - '${VM_NAME}' - ) - - vm_proc.wait() +def setup_venv(): + sh('virtualenv ${venvdir}') + sh('${venvdir}/bin/pip install -U cython six') + sh('${venvdir}/bin/pip install -U ${BE_ROOT}/py-bsd') + sh('${venvdir}/bin/pip install -U ${BE_ROOT}/py-netif') + sh('${venvdir}/bin/pip install -U ${BE_ROOT}/py-dhcp') + sh('${venvdir}/bin/python ${BUILD_ROOT}/tests/freenas/vm.py') if __name__ == '__main__': - info('Starting up test schedule') - cleanup() - setup_files() - setup_network() - do_install() - do_run() \ No newline at end of file + info('Setting up test environment') + setup_venv() diff --git a/tests/freenas/vm.py b/tests/freenas/vm.py new file mode 100644 index 0000000..1b1f3a7 --- /dev/null +++ b/tests/freenas/vm.py @@ -0,0 +1,128 @@ +#+ +# Copyright 2016 iXsystems, Inc. +# All rights reserved +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted providing that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +import os +import ipaddress +import subprocess +import threading +from dhcp.server import Server +from dhcp.lease import Lease +from dsl import load_file, load_profile_config +from utils import sh, sh_str, sh_spawn, info, objdir, e + + +load_profile_config() +load_file(e('${BUILD_ROOT}/tests/freenas/config.pyd'), os.environ) +destdir = objdir('tests') +isopath = objdir('${NAME}.iso') +tapdev = None +dhcp_server = None + + +def cleanup(): + sh('bhyvectl --destroy --vm=${VM_NAME}', nofail=True) + + +def setup_files(): + sh('mkdir -p ${destdir}') + sh('truncate -s 8G ${destdir}/boot.img') + sh('truncate -s 20G ${destdir}/hd1.img') + sh('truncate -s 20G ${destdir}/hd2.img') + + +def setup_network(): + global tapdev + + info('Configuring VM networking') + tapdev = sh_str('ifconfig tap create') + info('Using tap device {0}', tapdev) + sh('ifconfig ${tapdev} inet ${HOST_IP} ${NETMASK} up') + + +def setup_dhcp_server(): + global dhcp_server + + def dhcp_request(mac, hostname): + info('DHCP request from ${hostname} (${mac})') + lease = Lease() + lease.client_mac = mac + lease.client_ip = ipaddress.ip_address(e('${FREENAS_IP}')) + lease.client_mask = ipaddress.ip_address(e('${NETMASK}')) + return lease + + dhcp_server = Server() + dhcp_server.server_name = 'FreeNAS_test_env' + dhcp_server.on_request = dhcp_request + dhcp_server.start(tapdev, ipaddress.ip_address(e('${HOST_IP}'))) + threading.Thread(target=dhcp_server.serve, daemon=True).start() + info('Started DHCP server on {0}', tapdev) + +def do_install(): + info('Starting up VM for unattended install') + vm_proc = sh_spawn( + 'bhyve -m ${MEMSIZE} -c ${CORES} -A -H -P', + '-s 3:0,ahci-hd,${destdir}/boot.img', + '-s 4:0,ahci-hd,${destdir}/hd1.img', + '-s 5:0,ahci-hd,${destdir}/hd2.img', + '-s 6:0,ahci-cd,${isopath}', + '-s 7:0,virtio-net,${tapdev}', + '-s 8:0,fbuf,tcp=5900,w=1024,h=768', + '-s 31,lpc', + '-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd', + '${VM_NAME}' + ) + + try: + vm_proc.wait(timeout=3600) + except subprocess.TimeoutExpired: + fail('Install timed out after 1 hour') + + +def do_run(): + info('Starting up VM for testing') + vm_proc = sh_spawn( + 'bhyve -m ${MEMSIZE} -c ${CORES} -A -H -P', + '-s 3:0,ahci-hd,${destdir}/boot.img', + '-s 4:0,ahci-hd,${destdir}/hd1.img', + '-s 5:0,ahci-hd,${destdir}/hd2.img', + '-s 6:0,virtio-net,${tapdev}', + '-s 7:0,fbuf,tcp=5900,w=1024,h=768', + '-s 31,lpc', + '-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd', + '${VM_NAME}' + ) + + vm_proc.wait() + + +if __name__ == '__main__': + info('Starting up test schedule') + cleanup() + setup_files() + setup_network() + setup_dhcp_server() + do_install() + do_run()