'Test snapshot creation with file-based disk.'

def _test_volume_snapshot_create(self, quiesce=True, can_quiesce=True, quiesce_required=False):

    self.flags(instance_name_template='instance-%s')
    self.mox.StubOutWithMock(self.drvr._host, 'get_domain')
    self.mox.StubOutWithMock(self.drvr, '_volume_api')
    if quiesce_required:
        self.inst['system_metadata']['image_os_require_quiesce'] = True
    instance = objects.Instance(**self.inst)
    new_file = 'new-file'
    domain = FakeVirtDomain(fake_xml=self.dom_xml)
    self.mox.StubOutWithMock(domain, 'XMLDesc')
    self.mox.StubOutWithMock(domain, 'snapshotCreateXML')
    domain.XMLDesc(flags=0).AndReturn(self.dom_xml)
    snap_xml_src = '<domainsnapshot>\n        <disks>\n                <disk    name="disk1_file"    snapshot="external"    type="file">\n                        <source    file="new-file"/>\n                </disk>\n                <disk    name="vdb"    snapshot="no"/>\n        </disks>\n</domainsnapshot>\n'
    fakelibvirt.VIR_DOMAIN_SNAPSHOT_CREATE_REUSE_EXT = 32
    fakelibvirt.VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE = 64
    snap_flags = ((fakelibvirt.VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY | fakelibvirt.VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA) | fakelibvirt.VIR_DOMAIN_SNAPSHOT_CREATE_REUSE_EXT)
    snap_flags_q = (snap_flags | fakelibvirt.VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE)
    can_quiesce_mock = mock.Mock()
    if can_quiesce:
        can_quiesce_mock.return_value = None
        if quiesce:
            domain.snapshotCreateXML(snap_xml_src, flags=snap_flags_q)
        else:
            domain.snapshotCreateXML(snap_xml_src, flags=snap_flags_q).AndRaise(fakelibvirt.libvirtError('quiescing    failed,    no    qemu-ga'))
            if (not quiesce_required):
                domain.snapshotCreateXML(snap_xml_src, flags=snap_flags)
    else:
        can_quiesce_mock.side_effect = exception.QemuGuestAgentNotEnabled
        if (not quiesce_required):
            domain.snapshotCreateXML(snap_xml_src, flags=snap_flags)
    self.drvr._can_quiesce = can_quiesce_mock
    self.mox.ReplayAll()
    guest = libvirt_guest.Guest(domain)
    if (quiesce_required and ((not quiesce) or (not can_quiesce))):
        if (not quiesce):
            expected_error = fakelibvirt.libvirtError
        else:
            expected_error = exception.QemuGuestAgentNotEnabled
        self.assertRaises(expected_error, self.drvr._volume_snapshot_create, self.c, instance, guest, self.volume_uuid, new_file)
    else:
        self.drvr._volume_snapshot_create(self.c, instance, guest, self.volume_uuid, new_file)
    can_quiesce_mock.assert_called_once_with(instance, mock.ANY)
    self.mox.VerifyAll()
