def setUp(self):
        self.dummy_virsh = FakeVirshFactory()
        # make a tmp_dir to store informations.
        LibvirtXMLTestBase.__doms_dir__ = os.path.join(data_dir.get_tmp_dir(),
                                                       'domains')
        if not os.path.isdir(LibvirtXMLTestBase.__doms_dir__):
            os.makedirs(LibvirtXMLTestBase.__doms_dir__)

        # Normally not kosher to call super_set, but required here for testing
        self.dummy_virsh.__super_set__('capabilities', self._capabilities)
        self.dummy_virsh.__super_set__('dumpxml', self._dumpxml)
        self.dummy_virsh.__super_set__('domuuid', self._domuuid)
        self.dummy_virsh.__super_set__('define', self._define)
        self.dummy_virsh.__super_set__('nodedev_dumpxml', self._nodedev_dumpxml)
class LibvirtXMLTestBase(unittest.TestCase):

    # Override instance methods needed for testing

    # domain_xml
    # usage:
    #    xml = __domain_xml__ % (name, uuid)
    __domain_xml__ = ('<domain type="kvm">'
                      '    <name>%s</name>'
                      '    <uuid>%s</uuid>'
                      '    <cputune>'
                      '      <vcpupin vcpu="0" cpuset="1-4,^2"/>'
                      '      <vcpupin vcpu="1" cpuset="0,1"/>'
                      '      <vcpupin vcpu="2" cpuset="2,3"/>'
                      '      <vcpupin vcpu="3" cpuset="0,4"/>'
                      '      <emulatorpin cpuset="1-3"/>'
                      '      <shares>2048</shares>'
                      '      <period>1000000</period>'
                      '      <quota>-1</quota>'
                      '      <emulator_period>1000000</emulator_period>'
                      '      <emulator_quota>-1</emulator_quota>'
                      '    </cputune>'
                      '    <devices>'  # Tests below depend on device order

                      '       <serial type="pty">'
                      '           <target port="0"/>'
                      '       </serial>'
                      '       <serial type="pty">'
                      '           <target port="1"/>'
                      '           <source path="/dev/null"/>'
                      '       </serial>'
                      '       <serial type="tcp">'
                      '         <source mode="connect" host="1.2.3.4"\
                                                        service="2445"/>'
                      '         <protocol type="raw"/>'
                      '         <target port="2"/>'
                      '       </serial>'
                      '       <serial type="udp">'
                      '         <source mode="bind" host="1.2.3.4"\
                                                        service="2445"/>'
                      '         <source mode="connect" host="4.3.2.1"\
                                                        service="5442"/>'
                      '         <target port="3"/>'
                      '       </serial>'

                      '       <channel type="foo1">'
                      '         <source mode="foo2" path="foo3" />'
                      '         <target name="foo4" type="foo5" />'
                      '       </channel>'
                      '       <channel type="bar1">'
                      '         <source mode="bar2" path="bar3" />'
                      '         <target name="bar4" type="bar5" />'
                      '       </channel>'

                      '       <graphics type="vnc" port="-1" autoport="yes"/>'

                      '       <disk type="file" device="disk">'
                      '         <driver name="qemu" type="qcow2"/>'
                      '         <source file="/foo/bar/baz.qcow2"/>'
                      '         <target dev="vda" bus="virtio"/>'
                      '         <address type="pci" domain="0x0000"'
                      '               bus="0x00" slot="0x04" function="0x0"/>'
                      '       </disk>'

                      '    </devices>'

                      '    <seclabel type="sec_type" model="sec_model">'
                      '       <label>sec_label</label>'
                      '       <baselabel>sec_baselabel</baselabel>'
                      '       <imagelabel>sec_imagelabel</imagelabel>'
                      '    </seclabel>'

                      '</domain>')

    __doms_dir__ = None

    @staticmethod
    def _capabilities(option='', **dargs):
        # Compacted to save space
        return CAPABILITIES

    @staticmethod
    def _domuuid(name, **dargs):
        return "ddb0cf86-5ba8-4f83-480a-d96f54339219"

    @staticmethod
    def _define(file_path, **dargs):
        vmxml = xml_utils.XMLTreeFile(file_path)
        dom_name = vmxml.find('name').text
        xml_path = os.path.join(LibvirtXMLTestBase.__doms_dir__,
                                '%s.xml' % dom_name)
        shutil.copy(file_path, xml_path)

    @staticmethod
    def _dumpxml(name, to_file="", **dargs):
        """
        Get a xml from name.
        """
        if not name:
            cmd = "virsh dumpxml %s" % name
            stdout = "error: command 'dumpxml' requires <domain> option"
            stderr = stdout
            exit_status = 1
            result = utils.CmdResult(cmd, stdout, stderr, exit_status)
            raise error.CmdError(cmd, result,
                                 "Virsh Command returned non-zero exit status")

        file_path = os.path.join(LibvirtXMLTestBase.__doms_dir__,
                                 '%s.xml' % name)
        if os.path.exists(file_path):
            xml_file = open(file_path, 'r')
            domain_xml = xml_file.read()
        else:
            xml_file = open(file_path, 'w')
            domain_xml = LibvirtXMLTestBase.__domain_xml__ % (name,
                                                              LibvirtXMLTestBase._domuuid(None))
            xml_file.write(domain_xml)
        xml_file.close()

        cmd = "virsh dumpxml %s" % name
        stdout = domain_xml
        stderr = ""
        exit_status = 0
        return utils.CmdResult(cmd, stdout, stderr, exit_status)

    @staticmethod
    def _nodedev_dumpxml(name, options="", to_file=None, **dargs):
        # Must mirror virsh.nodedev_dumpxml() API but can't test this option
        if options != "":
            raise ValueError('Dummy virsh for testing does not support options'
                             ' parameter')
        if to_file is not None:
            raise ValueError('Dummy virsh for testing does not support to_file'
                             ' parameter')
        if name is not 'pci_0000_00_00_0':
            raise ValueError('Dummy virsh for testing only support '
                             ' device name pci_0000_00_00_0')
        xml = ("<device>"
               "<name>pci_0000_00_00_0</name>"
               "<path>/sys/devices/pci0000:00/0000:00:00.0</path>"
               "<parent>computer</parent>"
               "<capability type='pci'>"
               "<domain>0</domain>"
               "<bus>0</bus>"
               "<slot>0</slot>"
               "<function>0</function>"
               "<product id='0x25c0'>5000X Chipset Memory Controller Hub</product>"
               "<vendor id='0x8086'>Intel Corporation</vendor>"
               "</capability>"
               "</device>")
        return utils.CmdResult('virsh nodedev-dumpxml pci_0000_00_00_0',
                               xml, '', 0)

    def setUp(self):
        self.dummy_virsh = FakeVirshFactory()
        # make a tmp_dir to store informations.
        LibvirtXMLTestBase.__doms_dir__ = os.path.join(data_dir.get_tmp_dir(),
                                                       'domains')
        if not os.path.isdir(LibvirtXMLTestBase.__doms_dir__):
            os.makedirs(LibvirtXMLTestBase.__doms_dir__)

        # Normally not kosher to call super_set, but required here for testing
        self.dummy_virsh.__super_set__('capabilities', self._capabilities)
        self.dummy_virsh.__super_set__('dumpxml', self._dumpxml)
        self.dummy_virsh.__super_set__('domuuid', self._domuuid)
        self.dummy_virsh.__super_set__('define', self._define)
        self.dummy_virsh.__super_set__('nodedev_dumpxml', self._nodedev_dumpxml)

    def tearDown(self):
        librarian.DEVICE_TYPES = list(ORIGINAL_DEVICE_TYPES)
        if os.path.isdir(self.__doms_dir__):
            shutil.rmtree(self.__doms_dir__)