Skip to content

Commit 2fddf72

Browse files
committed
Correct wrong check of resulting image sparsiness
The first issue is that we look at content size since 2168, which is not beneficial in the sparseness check as opposed to disk usage. The second issue is that the check we have in tests for sparsiness is not honest because of the "equal to" part. The virtual size has to be strictly greater than the content (as the content is displayed by tools that understand sparseness). The third issue is that we almost always resize which results in significantly larger virtual size. What we really care about is comparing against the original virtual size of the image. Signed-off-by: Alex Kalenyuk <[email protected]>
1 parent cd56b6c commit 2fddf72

File tree

6 files changed

+29
-58
lines changed

6 files changed

+29
-58
lines changed

tests/cloner_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ var _ = Describe("all clone tests", func() {
18991899
Expect(err).ToNot(HaveOccurred())
19001900

19011901
By("Verifying the image is sparse")
1902-
Expect(f.VerifySparse(f.Namespace, utils.PersistentVolumeClaimFromDataVolume(targetDv), utils.DefaultImagePath)).To(BeTrue())
1902+
Expect(f.VerifySparse(f.Namespace, utils.PersistentVolumeClaimFromDataVolume(targetDv), utils.DefaultImagePath, utils.UploadFileSize)).To(BeTrue())
19031903
By("Deleting verifier pod")
19041904
err = utils.DeleteVerifierPod(f.K8sClient, f.Namespace.Name)
19051905
Expect(err).ToNot(HaveOccurred())

tests/framework/pvc.go

+4-34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"strconv"
87
"strings"
98
"time"
109

@@ -284,24 +283,15 @@ func (f *Framework) VerifyBlankDisk(namespace *k8sv1.Namespace, pvc *k8sv1.Persi
284283
}
285284

286285
// VerifySparse checks a disk image being sparse after creation/resize.
287-
func (f *Framework) VerifySparse(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim, imagePath string) (bool, error) {
286+
func (f *Framework) VerifySparse(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim, imagePath string, originalVirtualSize int64) (bool, error) {
288287
var info image.ImgInfo
289-
var imageContentSize int64
290288
err := f.GetImageInfo(namespace, pvc, imagePath, &info)
291289
if err != nil {
292290
return false, err
293291
}
294-
// qemu-img info gives us ActualSize but that is size on disk
295-
// which isn't important to us in this comparison; we compare content size
296-
err = f.GetImageContentSize(namespace, pvc, imagePath, &imageContentSize)
297-
if err != nil {
298-
return false, err
299-
}
300-
if info.ActualSize-imageContentSize >= units.MiB {
301-
return false, fmt.Errorf("Diff between content size %d and size on disk %d is significant, something's not right", imageContentSize, info.ActualSize)
302-
}
303-
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: VerifySparse comparison: Virtual: %d vs Content: %d\n", info.VirtualSize, imageContentSize)
304-
return info.VirtualSize >= imageContentSize, nil
292+
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: VerifySparse comparison: OriginalVirtual: %d vs SizeOnDisk: %d\n", originalVirtualSize, info.ActualSize)
293+
// The size on disk of a sparse image is significantly lower than the image's virtual size
294+
return originalVirtualSize-info.ActualSize >= units.MB, nil
305295
}
306296

307297
// VerifyFSOverhead checks whether virtual size is smaller than actual size. That means FS Overhead has been accounted for.
@@ -548,26 +538,6 @@ func (f *Framework) GetImageInfo(namespace *k8sv1.Namespace, pvc *k8sv1.Persiste
548538
return err
549539
}
550540

551-
// GetImageContentSize returns the content size (as opposed to size on disk) of an image
552-
func (f *Framework) GetImageContentSize(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim, imagePath string, imageSize *int64) error {
553-
cmd := fmt.Sprintf("du -s --apparent-size -B 1 %s | cut -f 1", imagePath)
554-
555-
_, err := f.verifyInPod(namespace, pvc, cmd, func(output, stderr string) (bool, error) {
556-
fmt.Fprintf(ginkgo.GinkgoWriter, "CMD (%s) output %s\n", cmd, output)
557-
558-
size, err := strconv.ParseInt(output, 10, 64)
559-
if err != nil {
560-
klog.Errorf("Invalid image content size:\n%s\n", output)
561-
return false, err
562-
}
563-
*imageSize = size
564-
565-
return true, nil
566-
})
567-
568-
return err
569-
}
570-
571541
func (f *Framework) startVerifierPod(namespace *k8sv1.Namespace, pvc *k8sv1.PersistentVolumeClaim) (*k8sv1.Pod, error) {
572542
var executorPod *k8sv1.Pod
573543
var err error

tests/import_test.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
. "github.com/onsi/ginkgo/v2"
1818
. "github.com/onsi/gomega"
1919

20+
"github.com/docker/go-units"
2021
"github.com/google/uuid"
2122

2223
v1 "k8s.io/api/core/v1"
@@ -126,7 +127,7 @@ var _ = Describe("[rfe_id:1115][crit:high][vendor:[email protected]][level:compo
126127
By("Verify the image contents")
127128
Expect(f.VerifyBlankDisk(f.Namespace, pvc)).To(BeTrue())
128129
By("Verifying the image is sparse")
129-
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath)).To(BeTrue())
130+
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath, utils.UploadFileSize)).To(BeTrue())
130131
By("Verifying permissions are 660")
131132
Expect(f.VerifyPermissions(f.Namespace, pvc)).To(BeTrue(), "Permissions on disk image are not 660")
132133
if utils.DefaultStorageCSIRespectsFsGroup {
@@ -1626,7 +1627,7 @@ var _ = Describe("Import populator", func() {
16261627
tests.DisableWebhookPvcRendering(f.CrClient)
16271628
})
16281629

1629-
DescribeTable("should import fileSystem PVC", func(expectedMD5 string, volumeImportSourceFunc func(cdiv1.DataVolumeContentType, bool) error, preallocation, webhookRendering bool) {
1630+
DescribeTable("should import fileSystem PVC", func(expectedMD5 string, originalVirtualSize int, volumeImportSourceFunc func(cdiv1.DataVolumeContentType, bool) error, preallocation, webhookRendering bool) {
16301631
pvc = importPopulationPVCDefinition()
16311632

16321633
if webhookRendering {
@@ -1660,7 +1661,7 @@ var _ = Describe("Import populator", func() {
16601661
Expect(ok).To(BeTrue())
16611662
} else {
16621663
By("Verifying the image is sparse")
1663-
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath)).To(BeTrue())
1664+
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath, int64(originalVirtualSize))).To(BeTrue())
16641665
}
16651666

16661667
if utils.DefaultStorageCSIRespectsFsGroup {
@@ -1685,17 +1686,17 @@ var _ = Describe("Import populator", func() {
16851686
return err != nil && k8serrors.IsNotFound(err)
16861687
}, timeout, pollingInterval).Should(BeTrue())
16871688
},
1688-
Entry("[test_id:11001]with HTTP image and preallocation", utils.TinyCoreMD5, createHTTPImportPopulatorCR, true, false),
1689-
Entry("[test_id:11002]with HTTP image without preallocation", utils.TinyCoreMD5, createHTTPImportPopulatorCR, false, false),
1690-
Entry("[rfe_id:10985][crit:high][test_id:11003]with HTTP image and preallocation, with incomplete PVC webhook rendering", Serial, utils.TinyCoreMD5, createHTTPImportPopulatorCR, true, true),
1691-
Entry("[test_id:11004]with Registry image and preallocation", utils.TinyCoreMD5, createRegistryImportPopulatorCR, true, false),
1692-
Entry("[test_id:11005]with Registry image without preallocation", utils.TinyCoreMD5, createRegistryImportPopulatorCR, false, false),
1693-
Entry("[test_id:11006]with ImageIO image with preallocation", Serial, utils.ImageioMD5, createImageIOImportPopulatorCR, true, false),
1694-
Entry("[test_id:11007]with ImageIO image without preallocation", Serial, utils.ImageioMD5, createImageIOImportPopulatorCR, false, false),
1695-
Entry("[test_id:11008]with VDDK image with preallocation", utils.VcenterMD5, createVDDKImportPopulatorCR, true, false),
1696-
Entry("[test_id:11009]with VDDK image without preallocation", utils.VcenterMD5, createVDDKImportPopulatorCR, false, false),
1697-
Entry("[test_id:11010]with Blank image with preallocation", utils.BlankMD5, createBlankImportPopulatorCR, true, false),
1698-
Entry("[test_id:11011]with Blank image without preallocation", utils.BlankMD5, createBlankImportPopulatorCR, false, false),
1689+
Entry("[test_id:11001]with HTTP image and preallocation", utils.TinyCoreMD5, utils.UploadFileSize, createHTTPImportPopulatorCR, true, false),
1690+
Entry("[test_id:11002]with HTTP image without preallocation", utils.TinyCoreMD5, utils.UploadFileSize, createHTTPImportPopulatorCR, false, false),
1691+
Entry("[rfe_id:10985][crit:high][test_id:11003]with HTTP image and preallocation, with incomplete PVC webhook rendering", Serial, utils.TinyCoreMD5, utils.UploadFileSize, createHTTPImportPopulatorCR, true, true),
1692+
Entry("[test_id:11004]with Registry image and preallocation", utils.TinyCoreMD5, utils.UploadFileSize, createRegistryImportPopulatorCR, true, false),
1693+
Entry("[test_id:11005]with Registry image without preallocation", utils.TinyCoreMD5, utils.UploadFileSize, createRegistryImportPopulatorCR, false, false),
1694+
Entry("[test_id:11006]with ImageIO image with preallocation", Serial, utils.ImageioMD5, utils.CirrosRawFileSize, createImageIOImportPopulatorCR, true, false),
1695+
Entry("[test_id:11007]with ImageIO image without preallocation", Serial, utils.ImageioMD5, utils.CirrosRawFileSize, createImageIOImportPopulatorCR, false, false),
1696+
Entry("[test_id:11008]with VDDK image with preallocation", utils.VcenterMD5, utils.CirrosRawFileSize, createVDDKImportPopulatorCR, true, false),
1697+
Entry("[test_id:11009]with VDDK image without preallocation", utils.VcenterMD5, utils.CirrosRawFileSize, createVDDKImportPopulatorCR, false, false),
1698+
Entry("[test_id:11010]with Blank image with preallocation", utils.BlankMD5, units.MiB, createBlankImportPopulatorCR, true, false),
1699+
Entry("[test_id:11011]with Blank image without preallocation", utils.BlankMD5, units.MiB, createBlankImportPopulatorCR, false, false),
16991700
)
17001701

17011702
DescribeTable("should import Block PVC", func(expectedMD5 string, volumeImportSourceFunc func(cdiv1.DataVolumeContentType, bool) error) {
@@ -1723,7 +1724,7 @@ var _ = Describe("Import populator", func() {
17231724
Expect(err).ToNot(HaveOccurred())
17241725
Expect(md5).To(Equal(expectedMD5))
17251726
By("Verifying the image is sparse")
1726-
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultPvcMountPath)).To(BeTrue())
1727+
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultPvcMountPath, utils.UploadFileSize)).To(BeTrue())
17271728

17281729
By("Verify 100.0% annotation")
17291730
progress, ok, err := utils.WaitForPVCAnnotation(f.K8sClient, f.Namespace.Name, pvc, controller.AnnPopulatorProgress)
@@ -1799,8 +1800,6 @@ var _ = Describe("Import populator", func() {
17991800
md5, err := f.GetMD5(f.Namespace, pvc, utils.DefaultImagePath, utils.MD5PrefixSize)
18001801
Expect(err).ToNot(HaveOccurred())
18011802
Expect(md5).To(Equal(utils.TinyCoreMD5))
1802-
By("Verifying the image is sparse")
1803-
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath)).To(BeTrue())
18041803
sourceMD5 := md5
18051804

18061805
By("Retaining PV")

tests/transport_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ var _ = Describe("Transport Tests", func() {
123123
}
124124
}
125125
By("Verifying the image is sparse")
126-
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath)).To(BeTrue())
126+
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath, utils.UploadFileSize)).To(BeTrue())
127127
} else {
128128
By("Verify PVC status annotation says failed")
129129
found, err := utils.WaitPVCPodStatusRunning(f.K8sClient, pvc)

tests/upload_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:[email protected]][level:compon
195195
Expect(err).ToNot(HaveOccurred())
196196
Expect(same).To(BeTrue())
197197
By("Verifying the image is sparse")
198-
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath)).To(BeTrue())
198+
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath, utils.UploadFileSize)).To(BeTrue())
199199
if utils.DefaultStorageCSIRespectsFsGroup {
200200
// CSI storage class, it should respect fsGroup
201201
By("Checking that disk image group is qemu")
@@ -411,7 +411,7 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:[email protected]][level:compon
411411
Expect(err).ToNot(HaveOccurred())
412412
Expect(same).To(BeTrue())
413413
By("Verifying the image is sparse")
414-
Expect(f.VerifySparse(f.Namespace, archivePVC, pathInPvc)).To(BeTrue())
414+
Expect(f.VerifySparse(f.Namespace, archivePVC, pathInPvc, utils.UploadFileSize)).To(BeTrue())
415415
}
416416
} else {
417417
checkFailureNoValidToken(archivePVC)
@@ -525,7 +525,7 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:[email protected]][level:compon
525525
Expect(err).ToNot(HaveOccurred())
526526
Expect(same).To(BeTrue())
527527
By("Verifying the image is sparse")
528-
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath)).To(BeTrue())
528+
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath, utils.UploadFileSize)).To(BeTrue())
529529
if utils.DefaultStorageCSIRespectsFsGroup {
530530
// CSI storage class, it should respect fsGroup
531531
By("Checking that disk image group is qemu")
@@ -599,7 +599,7 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:[email protected]][level:compon
599599
Expect(err).ToNot(HaveOccurred())
600600
Expect(same).To(BeTrue())
601601
By("Verifying the image is sparse")
602-
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath)).To(BeTrue())
602+
Expect(f.VerifySparse(f.Namespace, pvc, utils.DefaultImagePath, utils.UploadFileSize)).To(BeTrue())
603603
if utils.DefaultStorageCSIRespectsFsGroup {
604604
// CSI storage class, it should respect fsGroup
605605
By("Checking that disk image group is qemu")
@@ -730,7 +730,7 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:[email protected]][level:compon
730730
Expect(err).ToNot(HaveOccurred())
731731
Expect(same).To(BeTrue())
732732
By("Verifying the image is sparse")
733-
Expect(f.VerifySparse(f.Namespace, pvc, pathInPvc)).To(BeTrue())
733+
Expect(f.VerifySparse(f.Namespace, pvc, pathInPvc, utils.UploadFileSize)).To(BeTrue())
734734
}
735735
} else {
736736
checkFailureNoValidToken(pvcPrime)

tests/utils/upload.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const (
4141

4242
// UploadFileSize is the size of UploadFile
4343
UploadFileSize = 18874368
44+
// CirrosRawFileSize is the size of cirros.raw
45+
CirrosRawFileSize = 46137344
4446

4547
// UploadFileMD5 is the expected MD5 of the uploaded file
4648
UploadFileMD5 = "2a7a52285c846314d1dbd79e9818270d"

0 commit comments

Comments
 (0)