Add unit tests for direct IO checker
@@ -22,8 +22,10 @@ package converter
import (
"encoding/xml"
"fmt"
+ "io/ioutil"
"os"
"path"
+ "path/filepath"
"reflect"
"strconv"
"strings"
@@ -3329,6 +3331,65 @@ var _ = Describe("disk device naming", func() {
})
})
+var _ = Describe("direct IO checker", func() {
+ var directIOChecker DirectIOChecker
+ var tmpDir string
+ var existingFile string
+ var nonExistingFile string
+ var err error
+
+ BeforeEach(func() {
+ directIOChecker = NewDirectIOChecker()
+ tmpDir, err = os.MkdirTemp("", "direct-io-checker")
+ Expect(err).ToNot(HaveOccurred())
+ existingFile = filepath.Join(tmpDir, "disk.img")
+ err = ioutil.WriteFile(existingFile, []byte("test"), 0644)
+ Expect(err).ToNot(HaveOccurred())
+ nonExistingFile = filepath.Join(tmpDir, "non-existing-file")
+ })
+
+ AfterEach(func() {
+ err = os.RemoveAll(tmpDir)
+ Expect(err).ToNot(HaveOccurred())
+ })
+
+ It("should not fail when file/device exists", func() {
+ _, err = directIOChecker.CheckFile(existingFile)
+ Expect(err).ToNot(HaveOccurred())
+ _, err = directIOChecker.CheckBlockDevice(existingFile)
+ Expect(err).ToNot(HaveOccurred())
+ })
+
+ It("should not fail when file does not exist", func() {
+ _, err := directIOChecker.CheckFile(nonExistingFile)
+ Expect(err).ToNot(HaveOccurred())
+ _, err = os.Stat(nonExistingFile)
+ Expect(err).To(HaveOccurred())
+ Expect(os.IsNotExist(err)).To(BeTrue())
+ })
+
+ It("should fail when device does not exist", func() {
+ _, err := directIOChecker.CheckBlockDevice(nonExistingFile)
+ Expect(err).To(HaveOccurred())
+ _, err = os.Stat(nonExistingFile)
+ Expect(err).To(HaveOccurred())
+ Expect(os.IsNotExist(err)).To(BeTrue())
+ })
+
+ It("should fail when the path does not exist", func() {
+ nonExistingPath := "/non/existing/path/disk.img"
+ _, err = directIOChecker.CheckFile(nonExistingPath)
+ Expect(err).To(HaveOccurred())
+ Expect(os.IsNotExist(err)).To(BeTrue())
+ _, err = directIOChecker.CheckBlockDevice(nonExistingPath)
+ Expect(err).To(HaveOccurred())
+ Expect(os.IsNotExist(err)).To(BeTrue())
+ _, err = os.Stat(nonExistingPath)
+ Expect(err).To(HaveOccurred())
+ Expect(os.IsNotExist(err)).To(BeTrue())
+ })
+})
+
func diskToDiskXML(disk *v1.Disk) string {
devicePerBus := make(map[string]deviceNamer)
libvirtDisk := &api.Disk{}
