Skip to content

Commit

Permalink
add unit tests to improve code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: David Vossel <[email protected]>
  • Loading branch information
davidvossel committed Apr 13, 2020
1 parent 777d83d commit ae9c097
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 4 deletions.
30 changes: 30 additions & 0 deletions pkg/container-disk/container-disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var _ = Describe("ContainerDisk", func() {

It("by verifying host directory locations", func() {
vmi := v1.NewMinimalVMI("fake-vmi")
vmi.UID = "6789"
vmi.Status.ActivePods = map[types.UID]string{
"1234": "myhost",
}
Expand All @@ -107,6 +108,35 @@ var _ = Describe("ContainerDisk", func() {
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeTrue())
Expect(path).To(Equal(expectedPath))

// should be able to generate legacy socket path dir
legacySocket := GenerateLegacyVolumeMountDirOnHost(vmi)
Expect(legacySocket).To(Equal(filepath.Join(tmpDir, "6789")))

// should return error if disk target doesn't exist
targetPath, err := GenerateDiskTargetPathFromHostView(vmi, 1)
expectedPath = fmt.Sprintf("%s/1234/volumes/kubernetes.io~empty-dir/container-disks/disk_1.img", tmpDir)
Expect(err).ToNot(HaveOccurred())
Expect(targetPath).To(Equal(expectedPath))

})

It("by verifying launcher directory locations", func() {
vmi := v1.NewMinimalVMI("fake-vmi")
vmi.UID = "6789"

// This should fail if no file exists
path, err := GetDiskTargetPartFromLauncherView(1)
Expect(err).To(HaveOccurred())

expectedPath := fmt.Sprintf("%s/disk_1.img", tmpDir)
_, err = os.Create(expectedPath)
Expect(err).ToNot(HaveOccurred())

// this should pass once file exists
path, err = GetDiskTargetPartFromLauncherView(1)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal(expectedPath))
})

It("by verifying that resources are set if the VMI wants the guaranteed QOS class", func() {
Expand Down
21 changes: 21 additions & 0 deletions pkg/virt-handler/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ var _ = Describe("Domain informer", func() {
}

Context("with ghost record cache", func() {
It("Should be able to retrieve uid", func() {
err := AddGhostRecord("test1-namespace", "test1", "somefile1", "1234-1")
Expect(err).ToNot(HaveOccurred())

uid := LastKnownUIDFromGhostRecordCache("test1-namespace/test1")
Expect(string(uid)).To(Equal("1234-1"))

})

It("Should find ghost record by socket ", func() {
err := AddGhostRecord("test1-namespace", "test1", "somefile1", "1234-1")
Expect(err).ToNot(HaveOccurred())

record, exists := findGhostRecordBySocket("somefile1")
Expect(exists).To(BeTrue())
Expect(record.Name).To(Equal("test1"))

record, exists = findGhostRecordBySocket("does-not-exist")
Expect(exists).To(BeFalse())
})

It("Should initialize cache from disk", func() {
err := AddGhostRecord("test1-namespace", "test1", "somefile1", "1234-1")
Expect(err).ToNot(HaveOccurred())
Expand Down
27 changes: 27 additions & 0 deletions pkg/virt-handler/cmd-client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,19 @@ var _ = Describe("Virt remote commands", func() {
sock, err = FindSocketOnHost(vmi)
Expect(err).ToNot(HaveOccurred())
Expect(sock).To(Equal(filepath.Join(shareDir, "sockets", "1234_sock")))
})

It("Listing all sockets", func() {
// the new socket is already created in the Before function

// create a legacy socket to ensure we find both new and legacy sockets
f, err := os.Create(filepath.Join(socketsDir, "1234_sock"))
Expect(err).ToNot(HaveOccurred())
f.Close()

// listing all sockets should detect both the new and legacy sockets
sockets, err := ListAllSockets()
Expect(len(sockets)).To(Equal(2))
})

It("Detect unresponsive socket", func() {
Expand All @@ -123,5 +135,20 @@ var _ = Describe("Virt remote commands", func() {
unresponsive = IsSocketUnresponsive(sock)
Expect(unresponsive).To(BeTrue())
})

It("Determine legacy sockets vs new socket paths", func() {
legacy := IsLegacySocket("/some/path/something_sock")
Expect(legacy).To(BeTrue())

legacy = IsLegacySocket("/some/path/" + StandardLauncherSocketFileName)
Expect(legacy).To(BeFalse())

monEnabled := SocketMonitoringEnabled("/some/path/something_sock")
Expect(monEnabled).To(BeFalse())

monEnabled = SocketMonitoringEnabled("/some/path/" + StandardLauncherSocketFileName)
Expect(monEnabled).To(BeTrue())

})
})
})
2 changes: 1 addition & 1 deletion pkg/virt-handler/container-disk/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (m *Mounter) Unmount(vmi *v1.VirtualMachineInstance) error {
}
err = m.deleteMountTargetRecord(vmi)
if err != nil {
return nil
return err
}
}
return nil
Expand Down
13 changes: 13 additions & 0 deletions pkg/virt-handler/container-disk/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ package container_disk
import (
"io/ioutil"
"os"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

v1 "kubevirt.io/client-go/api/v1"
diskutils "kubevirt.io/kubevirt/pkg/ephemeral-disk-utils"
)

var _ = Describe("ContainerDisk", func() {
Expand Down Expand Up @@ -63,6 +65,12 @@ var _ = Describe("ContainerDisk", func() {
err = m.setMountTargetRecordEntry(vmi, "sometargetfile", "somesocketfile")
Expect(err).ToNot(HaveOccurred())

// verify the the file actually exists
recordFile := filepath.Join(tmpDir, string(vmi.UID))
exists, err := diskutils.FileExists(recordFile)
Expect(err).ToNot(HaveOccurred())
Expect(exists).To(BeTrue())

// verify we can read a result
record, err = m.getMountTargetRecord(vmi)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -87,6 +95,11 @@ var _ = Describe("ContainerDisk", func() {
err = m.deleteMountTargetRecord(vmi)
Expect(err).ToNot(HaveOccurred())

// verify the the file is actually removed
exists, err = diskutils.FileExists(recordFile)
Expect(err).ToNot(HaveOccurred())
Expect(exists).To(BeFalse())

// verify deleting results that don't exist won't fail
err = m.deleteMountTargetRecord(vmi)
Expect(err).ToNot(HaveOccurred())
Expand Down
6 changes: 3 additions & 3 deletions pkg/virt-handler/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ var _ = Describe("VirtualMachineInstance", func() {

It("should cleanup if vmi is finalized and domain does not exist", func() {
vmi := v1.NewMinimalVMI("testvmi")
vmi.UID = testUUID
vmi.UID = vmiTestUUID
vmi.Status.Phase = v1.Succeeded

mockWatchdog.CreateFile(vmi)
Expand Down Expand Up @@ -978,7 +978,7 @@ var _ = Describe("VirtualMachineInstance", func() {
}, 3)
It("update guest time after completed migration", func() {
vmi := v1.NewMinimalVMI("testvmi")
vmi.UID = testUUID
vmi.UID = vmiTestUUID
vmi.ObjectMeta.ResourceVersion = "1"
vmi.Status.Phase = v1.Running
vmi.Labels = make(map[string]string)
Expand All @@ -995,7 +995,7 @@ var _ = Describe("VirtualMachineInstance", func() {
}

mockWatchdog.CreateFile(vmi)
domain := api.NewMinimalDomainWithUUID("testvmi", testUUID)
domain := api.NewMinimalDomainWithUUID("testvmi", vmiTestUUID)
domain.Status.Status = api.Running

domain.Spec.Metadata.KubeVirt.Migration = &api.MigrationMetadata{
Expand Down
1 change: 1 addition & 0 deletions pkg/watchdog/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ go_test(
"//staging/src/kubevirt.io/client-go/precond:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
],
)
1 change: 1 addition & 0 deletions pkg/watchdog/watchdog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/types"

v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/precond"
)
Expand Down

0 comments on commit ae9c097

Please sign in to comment.