Add a unit test that we can avoid discard=unmap
@@ -326,6 +326,78 @@ var _ = Describe("Manager", func() {
Expect(err).To(BeNil())
})
+ It("should not add discard=unmap if a disk is preallocated", func() {
+ // Make sure that we always free the domain after use
+ mockDomain.EXPECT().Free()
+ vmi := newVMI(testNamespace, testVmName)
+ vmi.Spec.Domain.Devices.Disks = []v1.Disk{
+ {
+ Name: "permvolume1",
+ DiskDevice: v1.DiskDevice{
+ Disk: &v1.DiskTarget{
+ Bus: "virtio",
+ },
+ },
+ Cache: "none",
+ },
+ }
+ vmi.Spec.Volumes = []v1.Volume{
+ {
+ Name: "permvolume1",
+ VolumeSource: v1.VolumeSource{
+ DataVolume: &v1.DataVolumeSource{
+ Name: "dv1",
+ },
+ },
+ },
+ }
+ vmi.Status.VolumeStatus = []v1.VolumeStatus{
+ {
+ Name: "permvolume1",
+ Phase: v1.VolumeReady,
+ },
+ }
+ mockConn.EXPECT().LookupDomainByName(testDomainName).Return(mockDomain, libvirt.Error{Code: libvirt.ERR_NO_DOMAIN})
+ domainSpec := expectIsolationDetectionForVMI(vmi)
+ domainSpec.Devices.Disks = []api.Disk{
+ {
+ Device: "disk",
+ Type: "file",
+ Source: api.DiskSource{
+ File: "/var/run/kubevirt-private/vmi-disks/permvolume1/disk.img",
+ },
+ Target: api.DiskTarget{
+ Bus: "virtio",
+ Device: "vda",
+ },
+ Driver: &api.DiskDriver{
+ Cache: "none",
+ Name: "qemu",
+ Type: "raw",
+ ErrorPolicy: "stop",
+ },
+ Alias: api.NewUserDefinedAlias("permvolume1"),
+ },
+ }
+ xmlDomain, err := xml.MarshalIndent(domainSpec, "", "\t")
+ Expect(err).ToNot(HaveOccurred())
+ mockConn.EXPECT().DomainDefineXML(gomock.Any()).DoAndReturn(func(xml string) (cli.VirDomain, error) {
+ By(fmt.Sprintf("%s\n", xml))
+ Expect(strings.Contains(xml, "discard=\"unmap\"")).To(BeFalse())
+ return mockDomain, nil
+ })
+ mockDomain.EXPECT().GetState().Return(libvirt.DOMAIN_SHUTDOWN, 1, nil)
+ mockDomain.EXPECT().Create().Return(nil)
+ mockDomain.EXPECT().GetXMLDesc(libvirt.DomainXMLFlags(0)).MaxTimes(2).Return(string(xmlDomain), nil)
+ mockDomain.EXPECT().Free()
+ manager, _ := NewLibvirtDomainManager(mockConn, "fake", nil, 0, nil, "/usr/share/OVMF")
+ newspec, err := manager.SyncVMI(vmi, true, &cmdv1.VirtualMachineOptions{
+ VirtualMachineSMBios: &cmdv1.SMBios{},
+ PreallocatedVolumes: []string{"permvolume1"},
+ })
+ Expect(err).To(BeNil())
+ Expect(newspec).ToNot(BeNil())
+ })
It("should hotplug a disk if a volume was hotplugged", func() {
// Make sure that we always free the domain after use
mockDomain.EXPECT().Free()
