From nobody Wed Nov 5 17:45:11 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496689757446550.7867793052029; Mon, 5 Jun 2017 12:09:17 -0700 (PDT) Received: from localhost ([::1]:34758 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHxNK-0000pk-RF for importer@patchew.org; Mon, 05 Jun 2017 15:09:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:32931) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dHxEX-0002Bu-Kq for qemu-devel@nongnu.org; Mon, 05 Jun 2017 15:00:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dHxEP-0008A6-Ft for qemu-devel@nongnu.org; Mon, 05 Jun 2017 15:00:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:10092) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dHxEP-00089Q-5F for qemu-devel@nongnu.org; Mon, 05 Jun 2017 15:00:01 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0A40E19CBD1; Mon, 5 Jun 2017 19:00:00 +0000 (UTC) Received: from localhost (ovpn-116-6.gru2.redhat.com [10.97.116.6]) by smtp.corp.redhat.com (Postfix) with ESMTP id 58C2A78C0B; Mon, 5 Jun 2017 18:59:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0A40E19CBD1 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=ehabkost@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 0A40E19CBD1 From: Eduardo Habkost To: Peter Maydell , Stefan Hajnoczi Date: Mon, 5 Jun 2017 15:59:27 -0300 Message-Id: <20170605185927.12111-11-ehabkost@redhat.com> In-Reply-To: <20170605185927.12111-1-ehabkost@redhat.com> References: <20170605185927.12111-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 05 Jun 2017 19:00:00 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 10/10] scripts: Test script to look for -device crashes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Test code to check if we can crash QEMU using -device. It will test all accel/machine/device combinations by default, which may take a few hours (it's more than 90k test cases). There's a "-r" option that makes it test a random sample of combinations. The scripts contains a whitelist for: 1) known error messages that make QEMU exit cleanly; 2) known QEMU crashes. This is the behavior when the script finds a failure: * Known clean (exitcode=3D1) errors generate DEBUG messages (hidden by default) * Unknown clean (exitcode=3D1) errors will generate INFO messages (visible by default) * Known crashes generate error messages, but are not fatal (unless --strict mode is used) * Unknown crashes generate fatal error messages Having an updated whitelist of known clean errors is useful to make the script less verbose and run faster when in --quick mode, but the whitelist doesn't need to be always up to date. Signed-off-by: Eduardo Habkost Message-Id: <20170526181200.17227-4-ehabkost@redhat.com> Signed-off-by: Eduardo Habkost --- scripts/device-crash-test | 624 ++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 624 insertions(+) create mode 100755 scripts/device-crash-test diff --git a/scripts/device-crash-test b/scripts/device-crash-test new file mode 100755 index 0000000000..5f90e9bb54 --- /dev/null +++ b/scripts/device-crash-test @@ -0,0 +1,624 @@ +#!/usr/bin/env python2.7 +# +# Copyright (c) 2017 Red Hat Inc +# +# Author: +# Eduardo Habkost +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +""" +Run QEMU with all combinations of -machine and -device types, +check for crashes and unexpected errors. +""" + +import sys +import os +import glob +import logging +import traceback +import re +import random +import argparse +from itertools import chain + +sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'scripts')) +from qemu import QEMUMachine + +logger =3D logging.getLogger('device-crash-test') +dbg =3D logger.debug + + +# Purposes of the following whitelist: +# * Avoiding verbose log messages when we find known non-fatal +# (exitcode=3D1) errors +# * Avoiding fatal errors when we find known crashes +# * Skipping machines/devices that are known not to work out of +# the box, when running in --quick mode +# +# Keeping the whitelist updated is desirable, but not required, +# because unexpected cases where QEMU exits with exitcode=3D1 will +# just trigger a INFO message. + +# Valid whitelist entry keys: +# * accel: regexp, full match only +# * machine: regexp, full match only +# * device: regexp, full match only +# * log: regexp, partial match allowed +# * exitcode: if not present, defaults to 1. If None, matches any exitcode +# * warn: if True, matching failures will be logged as warnings +# * expected: if True, QEMU is expected to always fail every time +# when testing the corresponding test case +# * loglevel: log level of log output when there's a match. +ERROR_WHITELIST =3D [ + # Machines that won't work out of the box: + # MACHINE | ERROR MESSAGE + {'machine':'niagara', 'expected':True}, # Unable to load a firmw= are for -M niagara + {'machine':'boston', 'expected':True}, # Please provide either = a -kernel or -bios argument + {'machine':'leon3_generic', 'expected':True}, # Can't read bios image = (null) + + # devices that don't work out of the box because they require extra op= tions to "-device DEV": + # DEVICE | ERROR MESSAGE + {'device':'.*-(i386|x86_64)-cpu', 'expected':True}, # CPU socket-id= is not set + {'device':'ARM,bitband-memory', 'expected':True}, # source-memory= property not set + {'device':'arm.cortex-a9-global-timer', 'expected':True}, # a9_gtimer_= realize: num-cpu must be between 1 and 4 + {'device':'arm_mptimer', 'expected':True}, # num-cpu must = be between 1 and 4 + {'device':'armv7m', 'expected':True}, # memory proper= ty was not set + {'device':'aspeed.scu', 'expected':True}, # Unknown silic= on revision: 0x0 + {'device':'aspeed.sdmc', 'expected':True}, # Unknown silic= on revision: 0x0 + {'device':'bcm2835-dma', 'expected':True}, # bcm2835_dma_r= ealize: required dma-mr link not found: Property '.dma-mr' not found + {'device':'bcm2835-fb', 'expected':True}, # bcm2835_fb_re= alize: required vcram-base property not set + {'device':'bcm2835-mbox', 'expected':True}, # bcm2835_mbox_= realize: required mbox-mr link not found: Property '.mbox-mr' not found + {'device':'bcm2835-peripherals', 'expected':True}, # bcm2835_perip= herals_realize: required ram link not found: Property '.ram' not found + {'device':'bcm2835-property', 'expected':True}, # bcm2835_prope= rty_realize: required fb link not found: Property '.fb' not found + {'device':'bcm2835_gpio', 'expected':True}, # bcm2835_gpio_= realize: required sdhci link not found: Property '.sdbus-sdhci' not found + {'device':'bcm2836', 'expected':True}, # bcm2836_reali= ze: required ram link not found: Property '.ram' not found + {'device':'cfi.pflash01', 'expected':True}, # attribute "se= ctor-length" not specified or zero. + {'device':'cfi.pflash02', 'expected':True}, # attribute "se= ctor-length" not specified or zero. + {'device':'icp', 'expected':True}, # icp_realize: = required link 'xics' not found: Property '.xics' not found + {'device':'ics', 'expected':True}, # ics_base_real= ize: required link 'xics' not found: Property '.xics' not found + # "-device ide-cd" does work on more recent QEMU versions, so it doesn= 't have expected=3DTrue + {'device':'ide-cd'}, # No drive specif= ied + {'device':'ide-drive', 'expected':True}, # No drive spec= ified + {'device':'ide-hd', 'expected':True}, # No drive spec= ified + {'device':'ipmi-bmc-extern', 'expected':True}, # IPMI external= bmc requires chardev attribute + {'device':'isa-debugcon', 'expected':True}, # Can't create = serial device, empty char device + {'device':'isa-ipmi-bt', 'expected':True}, # IPMI device r= equires a bmc attribute to be set + {'device':'isa-ipmi-kcs', 'expected':True}, # IPMI device r= equires a bmc attribute to be set + {'device':'isa-parallel', 'expected':True}, # Can't create = serial device, empty char device + {'device':'isa-serial', 'expected':True}, # Can't create = serial device, empty char device + {'device':'ivshmem', 'expected':True}, # You must spec= ify either 'shm' or 'chardev' + {'device':'ivshmem-doorbell', 'expected':True}, # You must spec= ify a 'chardev' + {'device':'ivshmem-plain', 'expected':True}, # You must spec= ify a 'memdev' + {'device':'kvm-pci-assign', 'expected':True}, # no host devic= e specified + {'device':'loader', 'expected':True}, # please includ= e valid arguments + {'device':'nand', 'expected':True}, # Unsupported N= AND block size 0x1 + {'device':'nvdimm', 'expected':True}, # 'memdev' prop= erty is not set + {'device':'nvme', 'expected':True}, # Device initia= lization failed + {'device':'pc-dimm', 'expected':True}, # 'memdev' prop= erty is not set + {'device':'pci-bridge', 'expected':True}, # Bridge chassi= s not specified. Each bridge is required to be assigned a unique chassis id= > 0. + {'device':'pci-bridge-seat', 'expected':True}, # Bridge chassi= s not specified. Each bridge is required to be assigned a unique chassis id= > 0. + {'device':'pci-serial', 'expected':True}, # Can't create = serial device, empty char device + {'device':'pci-serial-2x', 'expected':True}, # Can't create = serial device, empty char device + {'device':'pci-serial-4x', 'expected':True}, # Can't create = serial device, empty char device + {'device':'pxa2xx-dma', 'expected':True}, # channels valu= e invalid + {'device':'pxb', 'expected':True}, # Bridge chassi= s not specified. Each bridge is required to be assigned a unique chassis id= > 0. + {'device':'scsi-block', 'expected':True}, # drive propert= y not set + {'device':'scsi-disk', 'expected':True}, # drive propert= y not set + {'device':'scsi-generic', 'expected':True}, # drive propert= y not set + {'device':'scsi-hd', 'expected':True}, # drive propert= y not set + {'device':'spapr-pci-host-bridge', 'expected':True}, # BUID not spec= ified for PHB + {'device':'spapr-pci-vfio-host-bridge', 'expected':True}, # BUID not s= pecified for PHB + {'device':'spapr-rng', 'expected':True}, # spapr-rng nee= ds an RNG backend! + {'device':'spapr-vty', 'expected':True}, # chardev prope= rty not set + {'device':'tpm-tis', 'expected':True}, # tpm_tis: back= end driver with id (null) could not be found + {'device':'unimplemented-device', 'expected':True}, # property 'siz= e' not specified or zero + {'device':'usb-braille', 'expected':True}, # Property char= dev is required + {'device':'usb-mtp', 'expected':True}, # x-root proper= ty must be configured + {'device':'usb-redir', 'expected':True}, # Parameter 'ch= ardev' is missing + {'device':'usb-serial', 'expected':True}, # Property char= dev is required + {'device':'usb-storage', 'expected':True}, # drive propert= y not set + {'device':'vfio-amd-xgbe', 'expected':True}, # -device vfio-= amd-xgbe: vfio error: wrong host device name + {'device':'vfio-calxeda-xgmac', 'expected':True}, # -device vfio-= calxeda-xgmac: vfio error: wrong host device name + {'device':'vfio-pci', 'expected':True}, # No provided h= ost device + {'device':'vfio-pci-igd-lpc-bridge', 'expected':True}, # VFIO dummy IS= A/LPC bridge must have address 1f.0 + {'device':'vhost-scsi.*', 'expected':True}, # vhost-scsi: m= issing wwpn + {'device':'vhost-vsock-device', 'expected':True}, # guest-cid pro= perty must be greater than 2 + {'device':'vhost-vsock-pci', 'expected':True}, # guest-cid pro= perty must be greater than 2 + {'device':'virtio-9p-ccw', 'expected':True}, # 9pfs device c= ouldn't find fsdev with the id =3D NULL + {'device':'virtio-9p-device', 'expected':True}, # 9pfs device c= ouldn't find fsdev with the id =3D NULL + {'device':'virtio-9p-pci', 'expected':True}, # 9pfs device c= ouldn't find fsdev with the id =3D NULL + {'device':'virtio-blk-ccw', 'expected':True}, # drive propert= y not set + {'device':'virtio-blk-device', 'expected':True}, # drive propert= y not set + {'device':'virtio-blk-device', 'expected':True}, # drive propert= y not set + {'device':'virtio-blk-pci', 'expected':True}, # drive propert= y not set + {'device':'virtio-crypto-ccw', 'expected':True}, # 'cryptodev' p= arameter expects a valid object + {'device':'virtio-crypto-device', 'expected':True}, # 'cryptodev' p= arameter expects a valid object + {'device':'virtio-crypto-pci', 'expected':True}, # 'cryptodev' p= arameter expects a valid object + {'device':'virtio-input-host-device', 'expected':True}, # evdev proper= ty is required + {'device':'virtio-input-host-pci', 'expected':True}, # evdev propert= y is required + {'device':'xen-pvdevice', 'expected':True}, # Device ID inv= alid, it must always be supplied + {'device':'vhost-vsock-ccw', 'expected':True}, # guest-cid pro= perty must be greater than 2 + {'device':'ALTR.timer', 'expected':True}, # "clock-freque= ncy" property must be provided + {'device':'zpci', 'expected':True}, # target must b= e defined + {'device':'pnv-(occ|icp|lpc)', 'expected':True}, # required link= 'xics' not found: Property '.xics' not found + {'device':'powernv-cpu-.*', 'expected':True}, # pnv_core_real= ize: required link 'xics' not found: Property '.xics' not found + + # ioapic devices are already created by pc and will fail: + {'machine':'q35|pc.*', 'device':'kvm-ioapic', 'expected':True}, # Only= 1 ioapics allowed + {'machine':'q35|pc.*', 'device':'ioapic', 'expected':True}, # Only= 1 ioapics allowed + + # KVM-specific devices shouldn't be tried without accel=3Dkvm: + {'accel':'(?!kvm).*', 'device':'kvmclock', 'expected':True}, + {'accel':'(?!kvm).*', 'device':'kvm-pci-assign', 'expected':True}, + + # xen-specific machines and devices: + {'accel':'(?!xen).*', 'machine':'xen.*', 'expected':True}, + {'accel':'(?!xen).*', 'device':'xen-.*', 'expected':True}, + + # this fails on some machine-types, but not all, so they don't have ex= pected=3DTrue: + {'device':'vmgenid'}, # vmgenid requires DMA write support in fw_cfg, = which this machine type does not provide + + # Silence INFO messages for errors that are common on multiple + # devices/machines: + {'log':r"No '[\w-]+' bus found for device '[\w-]+'"}, + {'log':r"images* must be given with the 'pflash' parameter"}, + {'log':r"(Guest|ROM|Flash|Kernel) image must be specified"}, + {'log':r"[cC]ould not load [\w ]+ (BIOS|bios) '[\w-]+\.bin'"}, + {'log':r"Couldn't find rom image '[\w-]+\.bin'"}, + {'log':r"speed mismatch trying to attach usb device"}, + {'log':r"Can't create a second ISA bus"}, + {'log':r"duplicate fw_cfg file name"}, + # sysbus-related error messages: most machines reject most dynamic sys= bus devices: + {'log':r"Option '-device [\w.,-]+' cannot be handled by this machine"}, + {'log':r"Device [\w.,-]+ is not supported by this machine yet"}, + {'log':r"Device [\w.,-]+ can not be dynamically instantiated"}, + {'log':r"Platform Bus: Can not fit MMIO region of size "}, + # other more specific errors we will ignore: + {'device':'allwinner-a10', 'log':"Unsupported NIC model:"}, + {'device':'.*-spapr-cpu-core', 'log':r"CPU core type should be"}, + {'log':r"MSI(-X)? is not supported by interrupt controller"}, + {'log':r"pxb-pcie? devices cannot reside on a PCIe? bus"}, + {'log':r"Ignoring smp_cpus value"}, + {'log':r"sd_init failed: Drive 'sd0' is already in use because it has = been automatically connected to another device"}, + {'log':r"This CPU requires a smaller page size than the system is usin= g"}, + {'log':r"MSI-X support is mandatory in the S390 architecture"}, + {'log':r"rom check and register reset failed"}, + {'log':r"Unable to initialize GIC, CPUState for CPU#0 not valid"}, + {'log':r"Multiple VT220 operator consoles are not supported"}, + {'log':r"core 0 already populated"}, + {'log':r"could not find stage1 bootloader"}, + + # other exitcode=3D1 failures not listed above will just generate INFO= messages: + {'exitcode':1, 'loglevel':logging.INFO}, + + # KNOWN CRASHES: + # Known crashes will generate error messages, but won't be fatal. + # Those entries must be removed once we fix the crashes. + {'exitcode':-6, 'log':r"Device 'serial0' is in use", 'loglevel':loggin= g.ERROR}, + {'exitcode':-6, 'log':r"spapr_rtas_register: Assertion .*rtas_table\[t= oken\]\.name.* failed", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"qemu_net_client_setup: Assertion `!peer->peer'= failed", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r'RAMBlock "[\w.-]+" already registered', 'logle= vel':logging.ERROR}, + {'exitcode':-6, 'log':r"find_ram_offset: Assertion `size !=3D 0' faile= d.", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"puv3_load_kernel: Assertion `kernel_filename != =3D NULL' failed", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"add_cpreg_to_hashtable: code should not be rea= ched", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"qemu_alloc_display: Assertion `surface->image = !=3D NULL' failed", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"Unexpected error in error_set_from_qdev_prop_e= rror", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"Object .* is not an instance of type spapr-mac= hine", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"Object .* is not an instance of type generic-p= c-machine", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"Object .* is not an instance of type e500-ccsr= ", 'loglevel':logging.ERROR}, + {'exitcode':-6, 'log':r"vmstate_register_with_alias_id: Assertion `!se= ->compat || se->instance_id =3D=3D 0' failed", 'loglevel':logging.ERROR}, + {'exitcode':-11, 'device':'stm32f205-soc', 'loglevel':logging.ERROR, '= expected':True}, + {'exitcode':-11, 'device':'xlnx,zynqmp', 'loglevel':logging.ERROR, 'ex= pected':True}, + {'exitcode':-11, 'device':'mips-cps', 'loglevel':logging.ERROR, 'expec= ted':True}, + {'exitcode':-11, 'device':'gus', 'loglevel':logging.ERROR, 'expected':= True}, + {'exitcode':-11, 'device':'a9mpcore_priv', 'loglevel':logging.ERROR, '= expected':True}, + {'exitcode':-11, 'device':'a15mpcore_priv', 'loglevel':logging.ERROR, = 'expected':True}, + {'exitcode':-11, 'device':'isa-serial', 'loglevel':logging.ERROR, 'exp= ected':True}, + {'exitcode':-11, 'device':'sb16', 'loglevel':logging.ERROR, 'expected'= :True}, + {'exitcode':-11, 'device':'cs4231a', 'loglevel':logging.ERROR, 'expect= ed':True}, + {'exitcode':-11, 'device':'arm-gicv3', 'loglevel':logging.ERROR, 'expe= cted':True}, + {'exitcode':-11, 'machine':'isapc', 'device':'.*-iommu', 'loglevel':lo= gging.ERROR, 'expected':True}, + + # everything else (including SIGABRT and SIGSEGV) will be a fatal erro= r: + {'exitcode':None, 'fatal':True, 'loglevel':logging.FATAL}, +] + + +def whitelistTestCaseMatch(wl, t): + """Check if a test case specification can match a whitelist entry + + This only checks if a whitelist entry is a candidate match + for a given test case, it won't check if the test case + results/output match the entry. See whitelistResultMatch(). + """ + return (('machine' not in wl or + 'machine' not in t or + re.match(wl['machine'] + '$', t['machine'])) and + ('accel' not in wl or + 'accel' not in t or + re.match(wl['accel'] + '$', t['accel'])) and + ('device' not in wl or + 'device' not in t or + re.match(wl['device'] + '$', t['device']))) + + +def whitelistCandidates(t): + """Generate the list of candidates that can match a test case""" + for i, wl in enumerate(ERROR_WHITELIST): + if whitelistTestCaseMatch(wl, t): + yield (i, wl) + + +def findExpectedResult(t): + """Check if there's an expected=3DTrue whitelist entry for a test case + + Returns (i, wl) tuple, where i is the index in + ERROR_WHITELIST and wl is the whitelist entry itself. + """ + for i, wl in whitelistCandidates(t): + if wl.get('expected'): + return (i, wl) + + +def whitelistResultMatch(wl, r): + """Check if test case results/output match a whitelist entry + + It is valid to call this function only if + whitelistTestCaseMatch() is True for the entry (e.g. on + entries returned by whitelistCandidates()) + """ + assert whitelistTestCaseMatch(wl, r['testcase']) + return ((wl.get('exitcode', 1) is None or + r['exitcode'] =3D=3D wl.get('exitcode', 1)) and + ('log' not in wl or + re.search(wl['log'], r['log'], re.MULTILINE))) + + +def checkResultWhitelist(r): + """Look up whitelist entry for a given test case result + + Returns (i, wl) tuple, where i is the index in + ERROR_WHITELIST and wl is the whitelist entry itself. + """ + for i, wl in whitelistCandidates(r['testcase']): + if whitelistResultMatch(wl, r): + return i, wl + + raise Exception("this should never happen") + + +def qemuOptsEscape(s): + """Escape option value QemuOpts""" + return s.replace(",", ",,") + + +def formatTestCase(t): + """Format test case info as "key=3Dvalue key=3Dvalue" for prettier log= ging output""" + return ' '.join('%s=3D%s' % (k, v) for k, v in t.items()) + + +def qomListTypeNames(vm, **kwargs): + """Run qom-list-types QMP command, return type names""" + types =3D vm.command('qom-list-types', **kwargs) + return [t['name'] for t in types] + + +def infoQDM(vm): + """Parse 'info qdm' output""" + args =3D {'command-line': 'info qdm'} + devhelp =3D vm.command('human-monitor-command', **args) + for l in devhelp.split('\n'): + l =3D l.strip() + if l =3D=3D '' or l.endswith(':'): + continue + d =3D {'name': re.search(r'name "([^"]+)"', l).group(1), + 'no-user': (re.search(', no-user', l) is not None)} + yield d + + +class QemuBinaryInfo(object): + def __init__(self, binary, devtype): + if devtype is None: + devtype =3D 'device' + + self.binary =3D binary + self._machine_info =3D {} + + dbg("devtype: %r", devtype) + args =3D ['-S', '-machine', 'none,accel=3Dkvm:tcg'] + dbg("querying info for QEMU binary: %s", binary) + vm =3D QEMUMachine(binary=3Dbinary, args=3Dargs) + vm.launch() + try: + self.alldevs =3D set(qomListTypeNames(vm, implements=3Ddevtype= , abstract=3DFalse)) + # there's no way to query DeviceClass::user_creatable using QM= P, + # so use 'info qdm': + self.no_user_devs =3D set([d['name'] for d in infoQDM(vm, ) if= d['no-user']]) + self.machines =3D list(m['name'] for m in vm.command('query-ma= chines')) + self.user_devs =3D self.alldevs.difference(self.no_user_devs) + self.kvm_available =3D vm.command('query-kvm')['enabled'] + finally: + vm.shutdown() + + def machineInfo(self, machine): + """Query for information on a specific machine-type + + Results are cached internally, in case the same machine- + type is queried multiple times. + """ + if machine in self._machine_info: + return self._machine_info[machine] + + mi =3D {} + args =3D ['-S', '-machine', '%s' % (machine)] + dbg("querying machine info for binary=3D%s machine=3D%s", self.bin= ary, machine) + vm =3D QEMUMachine(binary=3Dself.binary, args=3Dargs) + try: + vm.launch() + mi['runnable'] =3D True + except KeyboardInterrupt: + raise + except: + dbg("exception trying to run binary=3D%s machine=3D%s", self.b= inary, machine, exc_info=3Dsys.exc_info()) + dbg("log: %r", vm.get_log()) + mi['runnable'] =3D False + + vm.shutdown() + self._machine_info[machine] =3D mi + return mi + + +BINARY_INFO =3D {} + + +def getBinaryInfo(args, binary): + if binary not in BINARY_INFO: + BINARY_INFO[binary] =3D QemuBinaryInfo(binary, args.devtype) + return BINARY_INFO[binary] + + +def checkOneCase(args, testcase): + """Check one specific case + + Returns a dictionary containing failure information on error, + or None on success + """ + binary =3D testcase['binary'] + accel =3D testcase['accel'] + machine =3D testcase['machine'] + device =3D testcase['device'] + + dbg("will test: %r", testcase) + + args =3D ['-S', '-machine', '%s,accel=3D%s' % (machine, accel), + '-device', qemuOptsEscape(device)] + cmdline =3D ' '.join([binary] + args) + dbg("will launch QEMU: %s", cmdline) + vm =3D QEMUMachine(binary=3Dbinary, args=3Dargs) + + exc_traceback =3D None + try: + vm.launch() + except KeyboardInterrupt: + raise + except: + exc_traceback =3D traceback.format_exc() + dbg("Exception while running test case") + finally: + vm.shutdown() + ec =3D vm.exitcode() + log =3D vm.get_log() + + if exc_traceback is not None or ec !=3D 0: + return {'exc_traceback':exc_traceback, + 'exitcode':ec, + 'log':log, + 'testcase':testcase, + 'cmdline':cmdline} + + +def binariesToTest(args, testcase): + if args.qemu: + r =3D args.qemu + else: + r =3D glob.glob('./*-softmmu/qemu-system-*') + return r + + +def accelsToTest(args, testcase): + if getBinaryInfo(args, testcase['binary']).kvm_available: + yield 'kvm' + yield 'tcg' + + +def machinesToTest(args, testcase): + return getBinaryInfo(args, testcase['binary']).machines + + +def devicesToTest(args, testcase): + return getBinaryInfo(args, testcase['binary']).user_devs + + +TESTCASE_VARIABLES =3D [ + ('binary', binariesToTest), + ('accel', accelsToTest), + ('machine', machinesToTest), + ('device', devicesToTest), +] + + +def genCases1(args, testcases, var, fn): + """Generate new testcases for one variable + + If an existing item already has a variable set, don't + generate new items and just return it directly. This + allows the "-t" command-line option to be used to choose + a specific test case. + """ + for testcase in testcases: + if var in testcase: + yield testcase.copy() + else: + for i in fn(args, testcase): + t =3D testcase.copy() + t[var] =3D i + yield t + + +def genCases(args, testcase): + """Generate test cases for all variables + """ + cases =3D [testcase.copy()] + for var, fn in TESTCASE_VARIABLES: + dbg("var: %r, fn: %r", var, fn) + cases =3D genCases1(args, cases, var, fn) + return cases + + +def casesToTest(args, testcase): + cases =3D genCases(args, testcase) + if args.random: + cases =3D list(cases) + cases =3D random.sample(cases, min(args.random, len(cases))) + if args.debug: + cases =3D list(cases) + dbg("%d test cases to test", len(cases)) + if args.shuffle: + cases =3D list(cases) + random.shuffle(cases) + return cases + + +def logFailure(f, level): + t =3D f['testcase'] + logger.log(level, "failed: %s", formatTestCase(t)) + logger.log(level, "cmdline: %s", f['cmdline']) + for l in f['log'].strip().split('\n'): + logger.log(level, "log: %s", l) + logger.log(level, "exit code: %r", f['exitcode']) + if f['exc_traceback']: + logger.log(level, "exception:") + for l in f['exc_traceback'].split('\n'): + logger.log(level, " %s", l.rstrip('\n')) + + +def main(): + parser =3D argparse.ArgumentParser(description=3D"QEMU -device crash t= est") + parser.add_argument('-t', metavar=3D'KEY=3DVALUE', nargs=3D'*', + help=3D"Limit test cases to KEY=3DVALUE", + action=3D'append', dest=3D'testcases', default=3D[= ]) + parser.add_argument('-d', '--debug', action=3D'store_true', + help=3D'debug output') + parser.add_argument('-v', '--verbose', action=3D'store_true', default= =3DTrue, + help=3D'verbose output') + parser.add_argument('-q', '--quiet', dest=3D'verbose', action=3D'store= _false', + help=3D'non-verbose output') + parser.add_argument('-r', '--random', type=3Dint, metavar=3D'COUNT', + help=3D'run a random sample of COUNT test cases', + default=3D0) + parser.add_argument('--shuffle', action=3D'store_true', + help=3D'Run test cases in random order') + parser.add_argument('--dry-run', action=3D'store_true', + help=3D"Don't run any tests, just generate list") + parser.add_argument('-D', '--devtype', metavar=3D'TYPE', + help=3D"Test only device types that implement TYPE= ") + parser.add_argument('-Q', '--quick', action=3D'store_true', default=3D= True, + help=3D"Quick mode: skip test cases that are expec= ted to fail") + parser.add_argument('-F', '--full', action=3D'store_false', dest=3D'qu= ick', + help=3D"Full mode: test cases that are expected to= fail") + parser.add_argument('--strict', action=3D'store_true', dest=3D'strict', + help=3D"Treat all warnings as fatal") + parser.add_argument('qemu', nargs=3D'*', metavar=3D'QEMU', + help=3D'QEMU binary to run') + args =3D parser.parse_args() + + if args.debug: + lvl =3D logging.DEBUG + elif args.verbose: + lvl =3D logging.INFO + else: + lvl =3D logging.WARN + logging.basicConfig(stream=3Dsys.stdout, level=3Dlvl, format=3D'%(leve= lname)s: %(message)s') + + fatal_failures =3D [] + wl_stats =3D {} + skipped =3D 0 + total =3D 0 + + tc =3D {} + dbg("testcases: %r", args.testcases) + if args.testcases: + for t in chain(*args.testcases): + for kv in t.split(): + k, v =3D kv.split('=3D', 1) + tc[k] =3D v + + if len(binariesToTest(args, tc)) =3D=3D 0: + print >>sys.stderr, "No QEMU binary found" + parser.print_usage(sys.stderr) + return 1 + + for t in casesToTest(args, tc): + logger.info("running test case: %s", formatTestCase(t)) + total +=3D 1 + + expected_match =3D findExpectedResult(t) + if (args.quick and + (expected_match or + not getBinaryInfo(args, t['binary']).machineInfo(t['machi= ne'])['runnable'])): + dbg("skipped: %s", formatTestCase(t)) + skipped +=3D 1 + continue + + if args.dry_run: + continue + + try: + f =3D checkOneCase(args, t) + except KeyboardInterrupt: + break + + if f: + i, wl =3D checkResultWhitelist(f) + dbg("testcase: %r, whitelist match: %r", t, wl) + wl_stats.setdefault(i, []).append(f) + level =3D wl.get('loglevel', logging.DEBUG) + logFailure(f, level) + if wl.get('fatal') or (args.strict and level >=3D logging.WARN= ): + fatal_failures.append(f) + else: + dbg("success: %s", formatTestCase(t)) + if expected_match: + logger.warn("Didn't fail as expected: %s", formatTestCase(= t)) + + logger.info("Total: %d test cases", total) + if skipped: + logger.info("Skipped %d test cases", skipped) + + if args.debug: + stats =3D sorted([(len(wl_stats.get(i, [])), wl) for i, wl in enum= erate(ERROR_WHITELIST)]) + for count, wl in stats: + dbg("whitelist entry stats: %d: %r", count, wl) + + if fatal_failures: + for f in fatal_failures: + t =3D f['testcase'] + logger.error("Fatal failure: %s", formatTestCase(t)) + logger.error("Fatal failures on some machine/device combinations") + return 1 + +if __name__ =3D=3D '__main__': + sys.exit(main()) --=20 2.11.0.259.g40922b1