Testsuite: minor code cleanup

This commit is contained in:
Christian Poessinger 2021-01-30 15:56:41 +01:00
parent 37b270bab2
commit 2801efd101

View File

@ -43,7 +43,8 @@ import traceback
import logging import logging
import re import re
from io import BytesIO, StringIO from io import BytesIO
from io import StringIO
from datetime import datetime from datetime import datetime
EXCEPTION = 0 EXCEPTION = 0
@ -170,33 +171,33 @@ else:
output = sys.stdout.buffer output = sys.stdout.buffer
if not os.path.isfile(args.iso): if not os.path.isfile(args.iso):
log.error("Unable to find iso image to install") log.error('Unable to find iso image to install')
sys.exit(1) sys.exit(1)
if args.no_kvm: if args.no_kvm:
log.error("KVM forced off by command line") log.error('KVM forced off by command line')
kvm=False kvm=False
elif not os.path.exists("/dev/kvm"): elif not os.path.exists('/dev/kvm'):
log.error("KVM is not enabled on host, proceeding with software emulation") log.error('KVM not enabled on host, proceeding with software emulation')
kvm=False kvm=False
else: else:
kvm=True kvm=True
# Creating diskimage!! # Creating diskimage!!
if not os.path.isfile(args.disk): if not os.path.isfile(args.disk):
log.info("Creating Disk image {}".format(args.disk)) log.info(f'Creating Disk image {args.disk}')
c = subprocess.check_output(["qemu-img", "create", args.disk, "2G"]) c = subprocess.check_output(['qemu-img', 'create', args.disk, '2G'])
log.debug(c.decode()) log.debug(c.decode())
else: else:
log.info("Diskimage already exists, using the existing one") log.info('Diskimage already exists, using the existing one')
try: try:
################################################# #################################################
# Installing image to disk # Installing image to disk
################################################# #################################################
log.info("Installing system") log.info('Installing system')
cmd = get_qemu_cmd("TESTVM", kvm, args.disk, args.iso) cmd = get_qemu_cmd('TESTVM', kvm, args.disk, args.iso)
log.debug("Executing command: {}".format(cmd)) log.debug('Executing command: {cmd}')
c = pexpect.spawn(cmd, logfile=stl) c = pexpect.spawn(cmd, logfile=stl)
################################################# #################################################
@ -206,7 +207,7 @@ try:
c.expect('Automatic boot in', timeout=10) c.expect('Automatic boot in', timeout=10)
c.sendline('') c.sendline('')
except pexpect.TIMEOUT: except pexpect.TIMEOUT:
log.warning("Did not find grub countdown window, ignoring") log.warning('Did not find GRUB countdown window, ignoring')
log.info('Waiting for login prompt') log.info('Waiting for login prompt')
c.expect('[Ll]ogin:', timeout=300) c.expect('[Ll]ogin:', timeout=300)
@ -219,11 +220,11 @@ try:
################################################# #################################################
# Installing into VyOS system # Installing into VyOS system
################################################# #################################################
log.info("Starting installer") log.info('Starting installer')
c.sendline('install image') c.sendline('install image')
c.expect('\nWould you like to continue?.*:') c.expect('\nWould you like to continue?.*:')
c.sendline('yes') c.sendline('yes')
log.info("Partitioning disk") log.info('Partitioning disk')
c.expect('\nPartition.*:') c.expect('\nPartition.*:')
c.sendline('') c.sendline('')
c.expect('\nInstall the image on.*:') c.expect('\nInstall the image on.*:')
@ -251,26 +252,26 @@ try:
################################################# #################################################
# Powering down installer # Powering down installer
################################################# #################################################
log.info("Shutting down installation system") log.info('Shutting down installation system')
c.sendline('poweroff') c.sendline('poweroff')
c.expect(r'\nAre you sure you want to poweroff this system.*\]') c.expect(r'\nAre you sure you want to poweroff this system.*\]')
c.sendline('Y') c.sendline('Y')
for i in range(30): for i in range(30):
log.info("Waiting for shutdown...") log.info('Waiting for shutdown...')
if not c.isalive(): if not c.isalive():
log.info("VM is shut down!") log.info('VM shutdown!')
break break
time.sleep(10) time.sleep(10)
else: else:
log.error("VM Did not shut down after 300sec, killing") log.error('VM Did not shutdown after 300sec, killing it!')
c.close() c.close()
################################################# #################################################
# Booting installed system # Booting installed system
################################################# #################################################
log.info("Booting installed system") log.info('Booting installed system')
cmd = get_qemu_cmd("TESTVM", kvm, args.disk) cmd = get_qemu_cmd('TESTVM', kvm, args.disk)
log.debug('Executing command: {}'.format(cmd)) log.debug(f'Executing command: {cmd}')
c = pexpect.spawn(cmd, logfile=stl) c = pexpect.spawn(cmd, logfile=stl)
################################################# #################################################
@ -280,7 +281,7 @@ try:
c.expect('The highlighted entry will be executed automatically in', timeout=10) c.expect('The highlighted entry will be executed automatically in', timeout=10)
c.sendline('') c.sendline('')
except pexpect.TIMEOUT: except pexpect.TIMEOUT:
log.warning("Did not find grub countdown window, ignoring") log.warning('Did not find GRUB countdown window, ignoring')
log.info('Waiting for login prompt') log.info('Waiting for login prompt')
c.expect('[Ll]ogin:', timeout=300) c.expect('[Ll]ogin:', timeout=300)
@ -304,15 +305,14 @@ try:
################################################# #################################################
if args.configd: if args.configd:
c.sendline('sudo systemctl start vyos-configd.service &> /dev/null') c.sendline('sudo systemctl start vyos-configd.service &> /dev/null')
c.expect(r'vyos@vyos:~\$')
else: else:
c.sendline('sudo systemctl stop vyos-configd.service &> /dev/null') c.sendline('sudo systemctl stop vyos-configd.service &> /dev/null')
c.expect(r'vyos@vyos:~\$') c.expect(r'vyos@vyos:~\$')
################################################# #################################################
# Basic Configmode/Opmode switch # Basic Configmode/Opmode switch
################################################# #################################################
log.info("Basic CLI configuration mode test") log.info('Basic CLI configuration mode test')
c.sendline('configure') c.sendline('configure')
c.expect(r'vyos@vyos#') c.expect(r'vyos@vyos#')
c.sendline('run show version') c.sendline('run show version')
@ -322,58 +322,55 @@ try:
################################################# #################################################
# Executing test-suite # Executing test-suite
################################################# #################################################
log.info("Executing test-suite ")
# run default smoketest suite # run default smoketest suite
if not args.configtest: if not args.configtest:
log.info('Executing VyOS smoketests')
c.sendline('/usr/bin/vyos-smoketest') c.sendline('/usr/bin/vyos-smoketest')
i = c.expect(['\n +Invalid command:', i = c.expect(['\n +Invalid command:', '\n +Set failed',
'\n +Set failed', 'No such file or directory', r'\n\S+@\S+[$#]'], timeout=7200)
'No such file or directory',
r'\n\S+@\S+[$#]'], timeout=7200)
if i==0: if i == 0:
raise Exception('Invalid command detected') raise Exception('Invalid command detected')
elif i==1: elif i == 1:
raise Exception('Set syntax failed :/') raise Exception('Set syntax failed :/')
elif i==2: elif i == 2:
log.error("Did not find VyOS smoketest, this should be an exception") tmp = '(W)hy (T)he (F)ace? VyOS smoketest not found!'
raise Exception("WTF? did not find VyOS smoketest, this should be an exception") log.error(tmp)
raise Exception(tmp)
c.sendline('echo EXITCODE:$\x16?') c.sendline('echo EXITCODE:$\x16?')
i = c.expect(['EXITCODE:0', 'EXITCODE:\d+'], timeout=20) i = c.expect(['EXITCODE:0', 'EXITCODE:\d+'], timeout=20)
if i==0: if i == 0:
log.info('Smoketest finished successfully!') log.info('Smoketest finished successfully!')
pass pass
if i==1: elif i == 1:
log.error('Smoketest failed :/') log.error('Smoketest failed :/')
raise Exception("Smoketest-failed, please look into debug output") raise Exception("Smoketest-failed, please look into debug output")
#log.info("Smoke test status")
#data = c.before.decode()
# else, run configtest suite # else, run configtest suite
else: else:
log.info("Executing load config tests") log.info("Executing load config tests")
c.sendline('/usr/bin/vyos-configtest') c.sendline('/usr/bin/vyos-configtest')
i = c.expect(['\n +Invalid command:', i = c.expect(['\n +Invalid command:', 'No such file or directory',
'No such file or directory', r'\n\S+@\S+[$#]'], timeout=3600)
r'\n\S+@\S+[$#]'], timeout=3600)
if i==0: if i==0:
raise Exception('Invalid command detected') raise Exception('Invalid command detected')
elif i==1: elif i==1:
log.error("Did not find VyOS configtest, this should be an exception") tmp = '(W)hy (T)he (F)ace? VyOS smoketest not found!'
raise Exception("WTF? did not find VyOS configtest, this should be an exception") log.error(tmp)
raise Exception(tmp)
c.sendline('echo EXITCODE:$\x16?') c.sendline('echo EXITCODE:$\x16?')
i = c.expect(['EXITCODE:0', 'EXITCODE:\d+'], timeout=10) i = c.expect(['EXITCODE:0', 'EXITCODE:\d+'], timeout=10)
if i==0: if i == 0:
log.info('Configtest finished successfully!') log.info('Configtest finished successfully!')
pass pass
if i==1: elif i == 1:
log.error('Configtest failed :/') tmp = 'Configtest failed :/ - check debug output'
raise Exception("Configtest failed, please look into debug output") log.error(tmp)
raise Exception(tmp)
################################################# #################################################
# Powering off system # Powering off system
@ -382,31 +379,32 @@ try:
c.sendline('poweroff') c.sendline('poweroff')
c.expect(r'\nAre you sure you want to poweroff this system.*\]') c.expect(r'\nAre you sure you want to poweroff this system.*\]')
c.sendline('Y') c.sendline('Y')
log.info("Shutting down virtual machine") log.info('Shutting down virtual machine')
for i in range(30): for i in range(30):
log.info("Waiting for shutdown...") log.info('Waiting for shutdown...')
if not c.isalive(): if not c.isalive():
log.info("VM is shut down!") log.info('VM is shut down!')
break break
time.sleep(10) time.sleep(10)
else: else:
log.error("VM Did not shut down after 300sec") tmp = 'VM Did not shut down after 300sec'
raise Exception("VM Did not shut down after 300sec") log.error(tmp)
raise Exception(tmp)
c.close() c.close()
except pexpect.exceptions.TIMEOUT: except pexpect.exceptions.TIMEOUT:
log.error("Timeout waiting for VyOS system") log.error('Timeout waiting for VyOS system')
log.error(traceback.format_exc()) log.error(traceback.format_exc())
EXCEPTION = 1 EXCEPTION = 1
except pexpect.exceptions.ExceptionPexpect: except pexpect.exceptions.ExceptionPexpect:
log.error("Exeption while executing QEMU") log.error('Exeption while executing QEMU')
log.error("Is qemu working on this system?") log.error('Is qemu working on this system?')
log.error(traceback.format_exc()) log.error(traceback.format_exc())
EXCEPTION = 1 EXCEPTION = 1
except Exception: except Exception:
log.error("An unknown error occured when installing the VyOS system") log.error('Unknown error occured while VyOS!')
traceback.print_exc() traceback.print_exc()
EXCEPTION = 1 EXCEPTION = 1
@ -416,15 +414,15 @@ except Exception:
log.info("Cleaning up") log.info("Cleaning up")
if not args.keep: if not args.keep:
log.info("Removing disk file: {}".format(args.disk)) log.info(f'Removing disk file: {args.disk}')
try: try:
os.remove(args.disk) os.remove(args.disk)
except Exception: except Exception:
log.error("Exception while removing diskimage") log.error('Exception while removing diskimage!')
log.error(traceback.format_exc()) log.error(traceback.format_exc())
EXCEPTION = 1 EXCEPTION = 1
if EXCEPTION: if EXCEPTION:
log.error("Hmm... System got an exception while processing") log.error('Hmm... system got an exception while processing.')
log.error("The ISO is not considered usable") log.error('The ISO image is not considered usable!')
sys.exit(1) sys.exit(1)