Skip to content

Commit

Permalink
Add mount info test cases
Browse files Browse the repository at this point in the history
The new tests verify that mountinfo data is correctly parsed and the
proper mount path can be retrieved.

Signed-off-by: Vasiliy Ulyanov <[email protected]>
  • Loading branch information
vasiliy-ul committed Dec 22, 2020
1 parent 9e052b2 commit 5c93e12
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/virt-handler/isolation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_test(
"isolation_suite_test.go",
"isolation_test.go",
],
data = glob(["testdata/**"]),
embed = [":go_default_library"],
deps = [
"//pkg/virt-handler/cmd-client:go_default_library",
Expand Down
7 changes: 6 additions & 1 deletion pkg/virt-handler/isolation/isolation.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ type MountInfo struct {
MountPoint string
}

// The unit test suite overwrites this function
var mountInfoFunc = func(pid int) string {
return fmt.Sprintf("/proc/%d/mountinfo", pid)
}

type socketBasedIsolationDetector struct {
socketDir string
controller []string
Expand Down Expand Up @@ -259,7 +264,7 @@ func (r *realIsolationResult) MountNamespace() string {
}

func (r *realIsolationResult) mountInfo() string {
return fmt.Sprintf("/proc/%d/mountinfo", r.pid)
return mountInfoFunc(r.pid)
}

// MountInfoRoot returns information about the root entry in /proc/mountinfo
Expand Down
41 changes: 41 additions & 0 deletions pkg/virt-handler/isolation/isolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import (
cmdclient "kubevirt.io/kubevirt/pkg/virt-handler/cmd-client"
)

var (
mountInfoTestData = map[string]string{
"overlay": "/proc/1/root/var/lib/docker/overlay2/f15d9ce07df72e80d809aa99ab4a171f2f3636f65f0653e75db8ca0befd8ae02/merged",
"devicemapper": "/proc/1/root/var/lib/docker/devicemapper/mnt/d0990551ba8254871a449b2ff0d9063061ae96a2c195d7a850b62f030eae1710/rootfs",
//TODO Enable when btrfs support is fixed
//"btrfs": "/proc/1/root/var/lib/containers/storage/btrfs/subvolumes/e9a94e2cde75c54834378d4835d4eda6bebb56b02068b9254780de6f9344ad0e"
}
)

var _ = Describe("Isolation", func() {

Context("With an existing socket", func() {
Expand Down Expand Up @@ -99,6 +108,38 @@ var _ = Describe("Isolation", func() {
Expect(result.NetNamespace()).To(Equal(fmt.Sprintf("/proc/%d/ns/net", os.Getpid())))
})

It("Should detect the root mount info of the test suite", func() {
result, err := NewSocketBasedIsolationDetector(tmpDir).Whitelist([]string{"devices"}).Detect(vm)
Expect(err).ToNot(HaveOccurred())
mountInfo, err := result.MountInfoRoot()
Expect(err).ToNot(HaveOccurred())
Expect(mountInfo.MountPoint).To(Equal("/"))
})

It("Should detect the full path of the mount on a node", func() {
// Restore the overwritten function
defer func(f func(int) string) { mountInfoFunc = f }(mountInfoFunc)

for testCase, want := range mountInfoTestData {
mountInfoFunc = func(pid int) string {
Expect(pid).To(SatisfyAny(Equal(1), Equal(os.Getpid())))
base := filepath.Join("testdata", "mountinfo")
if pid == 1 {
return filepath.Join(base, fmt.Sprintf("%s_host", testCase))
}
return filepath.Join(base, fmt.Sprintf("%s_launcher", testCase))
}

result, err := NewSocketBasedIsolationDetector(tmpDir).Whitelist([]string{"devices"}).Detect(vm)
Expect(err).ToNot(HaveOccurred())
mountInfo, err := result.MountInfoRoot()
Expect(err).ToNot(HaveOccurred())
fullPath, err := NodeIsolationResult().FullPath(mountInfo)
Expect(err).ToNot(HaveOccurred())
Expect(fullPath).To(Equal(want))
}
})

AfterEach(func() {
socket.Close()
os.RemoveAll(tmpDir)
Expand Down

0 comments on commit 5c93e12

Please sign in to comment.