Skip to content

Commit

Permalink
Use real tmpdir path in qemu blank image unit tests (#2596)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Kalenyuk <akalenyu@redhat.com>
  • Loading branch information
akalenyu committed Feb 20, 2023
1 parent 3b1624c commit f37088f
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions pkg/image/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,22 +295,37 @@ var _ = Describe("quantity to qemu", func() {
})

var _ = Describe("Create blank image", func() {
var tmpDir, destPath string

BeforeEach(func() {
tmpDir, err := os.MkdirTemp(os.TempDir(), "qemutestdest")
Expect(err).NotTo(HaveOccurred())
By("tmpDir: " + tmpDir)
destPath = filepath.Join(tmpDir, "dest")
_, err = os.Create(destPath)
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
os.RemoveAll(tmpDir)
})

It("Should complete successfully if qemu-img resize succeeds", func() {
quantity, err := resource.ParseQuantity("10Gi")
Expect(err).NotTo(HaveOccurred())
size := convertQuantityToQemuSize(quantity)
replaceExecFunction(mockExecFunction("", "", nil, "create", "-f", "raw", "image", size), func() {
err = CreateBlankImage("image", quantity, false)
Expect(err.Error()).To(ContainSubstring("no such file or directory"))
replaceExecFunction(mockExecFunction("", "", nil, "create", "-f", "raw", destPath, size), func() {
err = CreateBlankImage(destPath, quantity, false)
Expect(err).ToNot(HaveOccurred())
})
})

It("Should fail if qemu-img resize fails", func() {
quantity, err := resource.ParseQuantity("10Gi")
Expect(err).NotTo(HaveOccurred())
size := convertQuantityToQemuSize(quantity)
replaceExecFunction(mockExecFunction("", "exit 1", nil, "create", "-f", "raw", "image", size), func() {
err = CreateBlankImage("image", quantity, false)
replaceExecFunction(mockExecFunction("", "exit 1", nil, "create", "-f", "raw", destPath, size), func() {
err = CreateBlankImage(destPath, quantity, false)
Expect(err).To(HaveOccurred())
Expect(strings.Contains(err.Error(), "could not create raw image with size ")).To(BeTrue())
})
Expand All @@ -320,19 +335,19 @@ var _ = Describe("Create blank image", func() {
quantity, err := resource.ParseQuantity("10Gi")
Expect(err).NotTo(HaveOccurred())
size := convertQuantityToQemuSize(quantity)
replaceExecFunction(mockExecFunctionStrict("", "", nil, "create", "-f", "raw", "image", size, "-o", "preallocation=falloc"), func() {
err = CreateBlankImage("image", quantity, true)
Expect(err.Error()).To(ContainSubstring("no such file or directory"))
replaceExecFunction(mockExecFunctionStrict("", "", nil, "create", "-f", "raw", destPath, size, "-o", "preallocation=falloc"), func() {
err = CreateBlankImage(destPath, quantity, true)
Expect(err).ToNot(HaveOccurred())
})
})

It("should not add preallocation if not requested", func() {
quantity, err := resource.ParseQuantity("10Gi")
Expect(err).NotTo(HaveOccurred())
size := convertQuantityToQemuSize(quantity)
replaceExecFunction(mockExecFunctionStrict("", "", nil, "create", "-f", "raw", "image", size), func() {
err = CreateBlankImage("image", quantity, false)
Expect(err.Error()).To(ContainSubstring("no such file or directory"))
replaceExecFunction(mockExecFunctionStrict("", "", nil, "create", "-f", "raw", destPath, size), func() {
err = CreateBlankImage(destPath, quantity, false)
Expect(err).ToNot(HaveOccurred())
})
})
})
Expand Down

0 comments on commit f37088f

Please sign in to comment.