2015-03-27 01:04:23 +03:00
# encoding=utf-8
#
2013-10-28 00:59:46 +04:00
# Copyright (C) 2013 Red Hat, Inc.
2013-03-18 01:06:52 +04:00
#
# 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
2013-10-28 00:59:47 +04:00
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
2013-03-18 01:06:52 +04:00
#
# 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.
import os . path
import unittest
2013-09-23 02:13:24 +04:00
from virtinst import NodeDevice
2013-03-18 01:06:52 +04:00
from virtinst import VirtualHostDevice
2013-04-11 03:48:07 +04:00
from tests import utils
2013-03-18 01:06:52 +04:00
conn = utils . open_testdriver ( )
unknown_xml = """
< device >
< name > foodevice < / name >
< parent > computer < / parent >
< capability type = ' frobtype ' >
< hardware >
< vendor > LENOVO < / vendor >
< / hardware >
< / capability >
< / device >
"""
2015-03-27 01:04:23 +03:00
# Requires XML_SANITIZE to parse correctly, see bug 1184131
funky_chars_xml = """
< device >
< name > computer < / name >
< capability type = ' system ' >
< hardware >
< vendor > LENOVOá < / vendor >
< version > ThinkPad T61 < / version >
< serial > L3B2616 < / serial >
< uuid > 97e80381 - 494 f - 11 cb - 8e0 e - cbc168f7d753 < / uuid >
< / hardware >
< firmware >
< vendor > LENOVO < / vendor >
< version > 7 LET51WW ( 1.21 ) < / version >
< release_date > 08 / 22 / 2007 < / release_date >
< / firmware >
< / capability >
< / device >
"""
2013-04-13 22:34:52 +04:00
2013-03-18 01:06:52 +04:00
class TestNodeDev ( unittest . TestCase ) :
def _nodeDevFromName ( self , devname ) :
node = conn . nodeDeviceLookupByName ( devname )
xml = node . XMLDesc ( 0 )
2013-09-23 02:13:24 +04:00
return NodeDevice . parse ( conn , xml )
2013-03-18 01:06:52 +04:00
def _testCompare ( self , devname , vals , devxml = None ) :
if devxml :
2013-09-23 02:13:24 +04:00
dev = NodeDevice . parse ( conn , devxml )
2013-03-18 01:06:52 +04:00
else :
dev = self . _nodeDevFromName ( devname )
for attr in vals . keys ( ) :
2013-09-23 02:13:24 +04:00
expect = vals [ attr ]
actual = getattr ( dev , attr )
if expect != actual :
raise AssertionError ( " devname= %s attribute= %s did not match: \n "
" expect= %s \n actual= %s " % ( devname , attr , expect , actual ) )
2013-03-18 01:06:52 +04:00
self . assertEqual ( vals [ attr ] , getattr ( dev , attr ) )
2015-07-23 14:15:26 +03:00
def _testNode2DeviceCompare ( self , nodename , devfile , nodedev = None ) :
2013-03-18 01:06:52 +04:00
devfile = os . path . join ( " tests/nodedev-xml/devxml " , devfile )
if not nodedev :
nodedev = self . _nodeDevFromName ( nodename )
2013-09-28 02:06:39 +04:00
dev = VirtualHostDevice ( conn )
2015-07-23 14:15:26 +03:00
dev . set_from_nodedev ( nodedev )
2013-03-18 01:06:52 +04:00
utils . diff_compare ( dev . get_xml_config ( ) + " \n " , devfile )
def testSystemDevice ( self ) :
devname = " computer "
vals = { " hw_vendor " : " LENOVO " , " hw_version " : " ThinkPad T61 " ,
" hw_serial " : " L3B2616 " ,
" hw_uuid " : " 97e80381-494f-11cb-8e0e-cbc168f7d753 " ,
" fw_vendor " : " LENOVO " , " fw_version " : " 7LET51WW (1.21 ) " ,
" fw_date " : " 08/22/2007 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_SYSTEM ,
2013-03-18 01:06:52 +04:00
" name " : " computer " , " parent " : None }
self . _testCompare ( devname , vals )
2015-03-27 01:04:23 +03:00
def testFunkyChars ( self ) :
vals = { " hw_vendor " : " LENOVO " , " hw_version " : " ThinkPad T61 " ,
" hw_serial " : " L3B2616 " ,
" hw_uuid " : " 97e80381-494f-11cb-8e0e-cbc168f7d753 " ,
" fw_vendor " : " LENOVO " , " fw_version " : " 7LET51WW (1.21 ) " ,
" fw_date " : " 08/22/2007 " ,
" device_type " : NodeDevice . CAPABILITY_TYPE_SYSTEM ,
" name " : " computer " , " parent " : None }
self . _testCompare ( None , vals , funky_chars_xml )
2013-03-18 01:06:52 +04:00
def testNetDevice1 ( self ) :
devname = " net_00_1c_25_10_b1_e4 "
vals = { " name " : " net_00_1c_25_10_b1_e4 " , " parent " : " pci_8086_1049 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_NET ,
2013-03-18 01:06:52 +04:00
" interface " : " eth0 " , " address " : " 00:1c:25:10:b1:e4 " ,
" capability_type " : " 80203 " }
self . _testCompare ( devname , vals )
def testNetDevice2 ( self ) :
devname = " net_00_1c_bf_04_29_a4 "
vals = { " name " : " net_00_1c_bf_04_29_a4 " , " parent " : " pci_8086_4227 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_NET ,
2013-03-18 01:06:52 +04:00
" interface " : " wlan0 " , " address " : " 00:1c:bf:04:29:a4 " ,
" capability_type " : " 80211 " }
self . _testCompare ( devname , vals )
def testPCIDevice1 ( self ) :
devname = " pci_1180_592 "
vals = { " name " : " pci_1180_592 " , " parent " : " pci_8086_2448 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_PCI ,
2013-03-18 01:06:52 +04:00
" domain " : " 0 " , " bus " : " 21 " , " slot " : " 0 " , " function " : " 4 " ,
" product_id " : " 0x0592 " , " vendor_id " : " 0x1180 " ,
" product_name " : " R5C592 Memory Stick Bus Host Adapter " ,
" vendor_name " : " Ricoh Co Ltd " }
self . _testCompare ( devname , vals )
def testPCIDevice2 ( self ) :
devname = " pci_8086_1049 "
vals = { " name " : " pci_8086_1049 " , " parent " : " computer " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_PCI ,
2013-03-18 01:06:52 +04:00
" domain " : " 0 " , " bus " : " 0 " , " slot " : " 25 " , " function " : " 0 " ,
" product_id " : " 0x1049 " , " vendor_id " : " 0x8086 " ,
" product_name " : " 82566MM Gigabit Network Connection " ,
" vendor_name " : " Intel Corporation " }
self . _testCompare ( devname , vals )
def testUSBDevDevice1 ( self ) :
devname = " usb_device_781_5151_2004453082054CA1BEEE "
vals = { " name " : " usb_device_781_5151_2004453082054CA1BEEE " ,
" parent " : " usb_device_1d6b_2_0000_00_1a_7 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_USBDEV ,
2013-03-18 01:06:52 +04:00
" bus " : " 1 " , " device " : " 4 " , " product_id " : ' 0x5151 ' ,
" vendor_id " : ' 0x0781 ' ,
" vendor_name " : " SanDisk Corp. " ,
" product_name " : " Cruzer Micro 256/512MB Flash Drive " }
self . _testCompare ( devname , vals )
def testUSBDevDevice2 ( self ) :
devname = " usb_device_483_2016_noserial "
vals = { " name " : " usb_device_483_2016_noserial " ,
" parent " : " usb_device_1d6b_1_0000_00_1a_0 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_USBDEV ,
2013-03-18 01:06:52 +04:00
" bus " : " 3 " , " device " : " 2 " , " product_id " : ' 0x2016 ' ,
" vendor_id " : ' 0x0483 ' ,
" vendor_name " : " SGS Thomson Microelectronics " ,
" product_name " : " Fingerprint Reader " }
self . _testCompare ( devname , vals )
def testStorageDevice1 ( self ) :
devname = " storage_serial_SATA_WDC_WD1600AAJS__WD_WCAP95119685 "
vals = { " name " : " storage_serial_SATA_WDC_WD1600AAJS__WD_WCAP95119685 " ,
" parent " : " pci_8086_27c0_scsi_host_scsi_device_lun0 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_STORAGE ,
2013-03-18 01:06:52 +04:00
" block " : " /dev/sda " , " bus " : " scsi " , " drive_type " : " disk " ,
" model " : " WDC WD1600AAJS-2 " , " vendor " : " ATA " ,
" size " : 160041885696 , " removable " : False ,
2013-09-23 02:13:24 +04:00
" hotpluggable " : False , " media_available " : None ,
" media_size " : None , " media_label " : None }
2013-03-18 01:06:52 +04:00
self . _testCompare ( devname , vals )
def testStorageDevice2 ( self ) :
devname = " storage_serial_SanDisk_Cruzer_Micro_2004453082054CA1BEEE_0_0 "
vals = { " name " : " storage_serial_SanDisk_Cruzer_Micro_2004453082054CA1BEEE_0_0 " ,
" parent " : " usb_device_781_5151_2004453082054CA1BEEE_if0_scsi_host_0_scsi_device_lun0 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_STORAGE ,
2013-03-18 01:06:52 +04:00
" block " : " /dev/sdb " , " bus " : " usb " , " drive_type " : " disk " ,
2013-09-23 02:13:24 +04:00
" model " : " Cruzer Micro " , " vendor " : " SanDisk " , " size " : None ,
2013-03-18 01:06:52 +04:00
" removable " : True , " hotpluggable " : True ,
" media_available " : True , " media_size " : 12345678 }
self . _testCompare ( devname , vals )
def testUSBBus ( self ) :
devname = " usb_device_1d6b_1_0000_00_1d_1_if0 "
vals = { " name " : " usb_device_1d6b_1_0000_00_1d_1_if0 " ,
" parent " : " usb_device_1d6b_1_0000_00_1d_1 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_USBBUS ,
2013-03-18 01:06:52 +04:00
" number " : " 0 " , " classval " : " 9 " , " subclass " : " 0 " ,
" protocol " : " 0 " }
self . _testCompare ( devname , vals )
def testSCSIBus ( self ) :
devname = " pci_8086_2829_scsi_host_1 "
vals = { " name " : " pci_8086_2829_scsi_host_1 " ,
" parent " : " pci_8086_2829 " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_SCSIBUS ,
2013-03-18 01:06:52 +04:00
" host " : " 2 " }
self . _testCompare ( devname , vals )
def testNPIV ( self ) :
devname = " pci_10df_fe00_0_scsi_host "
vals = { " name " : " pci_10df_fe00_0_scsi_host " ,
2013-09-23 02:13:24 +04:00
" device_type " : NodeDevice . CAPABILITY_TYPE_SCSIBUS ,
2013-03-18 01:06:52 +04:00
" host " : " 4 " , " fc_host " : True , " vport_ops " : True ,
" wwnn " : " 20000000c9848141 " , " wwpn " : " 10000000c9848141 " }
self . _testCompare ( devname , vals )
def testSCSIDevice ( self ) :
devname = " pci_8086_2829_scsi_host_scsi_device_lun0 "
vals = { " name " : " pci_8086_2829_scsi_host_scsi_device_lun0 " ,
" parent " : " pci_8086_2829_scsi_host " ,
" host " : " 0 " , " bus " : " 0 " , " target " : " 0 " , " lun " : " 0 " ,
" type " : " disk " }
self . _testCompare ( devname , vals )
def testUnknownDevice ( self ) :
vals = { " name " : " foodevice " , " parent " : " computer " ,
" device_type " : " frobtype " }
self . _testCompare ( None , vals , devxml = unknown_xml )
# NodeDevice 2 Device XML tests
def testNodeDev2USB1 ( self ) :
nodename = " usb_device_781_5151_2004453082054CA1BEEE "
devfile = " usbdev1.xml "
self . _testNode2DeviceCompare ( nodename , devfile )
def testNodeDev2USB2 ( self ) :
2015-07-23 14:15:26 +03:00
nodename = " usb_device_1d6b_2_0000_00_1d_7 "
2013-03-18 01:06:52 +04:00
devfile = " usbdev2.xml "
nodedev = self . _nodeDevFromName ( nodename )
2015-07-23 14:15:26 +03:00
self . _testNode2DeviceCompare ( nodename , devfile , nodedev = nodedev )
2013-03-18 01:06:52 +04:00
def testNodeDev2PCI ( self ) :
nodename = " pci_1180_592 "
devfile = " pcidev.xml "
self . _testNode2DeviceCompare ( nodename , devfile )
2013-10-10 17:41:49 +04:00
def testPCIParse ( self ) :
nodename = " pci_1180_476 "
obj = self . _nodeDevFromName ( nodename )
self . assertEqual ( obj . iommu_group , 3 )
2013-03-18 01:06:52 +04:00
def testNodeDevFail ( self ) :
nodename = " usb_device_1d6b_1_0000_00_1d_1_if0 "
devfile = " "
# This should exist, since usbbus is not a valid device to
# pass to a guest.
self . assertRaises ( ValueError ,
self . _testNode2DeviceCompare , nodename , devfile )
if __name__ == " __main__ " :
unittest . main ( )