Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for RecursiveUnmount when a submount fails to unmount. #107

Merged
merged 1 commit into from Apr 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions mount/mount_unix_test.go
Expand Up @@ -4,12 +4,14 @@
package mount

import (
"errors"
"os"
"path"
"strings"
"testing"

"github.com/moby/sys/mountinfo"
"golang.org/x/sys/unix"
)

func TestMountOptionsParsing(t *testing.T) {
Expand Down Expand Up @@ -230,3 +232,45 @@ func TestRecursiveUnmountTooGreedy(t *testing.T) {
t.Fatal("expected dir-other to be mounted, but it's not")
}
}

func TestRecursiveUnmount_SubMountFailsToUnmount(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("root required")
}

var (
tmp = t.TempDir()
parent = tmp + "/sub1"
child = tmp + "/sub1/sub2"
grandChild = tmp + "/sub1/sub2/sub3"
)

err := os.MkdirAll(grandChild, 0o700)
if err != nil {
t.Fatal(err)
}

// Create a set of mounts that should result in RecursiveUnmount failure,
// caused by the fact that the grandchild mount is shadowed by the child mount,
// and the child mount is shadowed by the parent mount. So. these two mounts
// are listed in mountinfo, but since they are unreachable, unmount will fail.
toMount := []string{grandChild, child, parent}
for _, dir := range toMount {
dir := dir
if err := Mount("tmpfs", dir, "tmpfs", ""); err != nil {
t.Fatal(err)
}
defer Unmount(dir) //nolint:errcheck
}

manugupt1 marked this conversation as resolved.
Show resolved Hide resolved
// unmount shadowed mounts
shadowedMounts := []string{child, grandChild}
for _, shadowedMount := range shadowedMounts {
t.Run(shadowedMount, func(t *testing.T) {
err := RecursiveUnmount(shadowedMount)
if !errors.Is(err, unix.ENOENT) {
t.Fatalf("expected submount(shadowed) %s to return unix.ENOENT, got %v", shadowedMount, err)
}
})
}
}