Skip to content

Commit

Permalink
Add test for RecursiveUnmount when a submount fails to unmount.
Browse files Browse the repository at this point in the history
Uses shadow mounts to get ENOENT for deeper level mounts.

#45

Signed-off-by: Manu Gupta <manugupt1@gmail.com>
  • Loading branch information
manugupt1 committed Mar 8, 2022
1 parent b8d8fab commit 616f9ba
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions mount/mount_unix_test.go
Expand Up @@ -4,12 +4,16 @@
package mount

import (
"errors"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"

"golang.org/x/sys/unix"

"github.com/moby/sys/mountinfo"
)

Expand Down Expand Up @@ -251,3 +255,43 @@ 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")
}

tmp := t.TempDir()

dir := "sub1/sub2/sub3"
err := os.MkdirAll(path.Join(tmp, dir), 0o700)
if err != nil {
t.Fatal(err)
}

grandchild := path.Join(tmp, dir)
child := filepath.Dir(grandchild)
parent := filepath.Dir(child)

// order is necessary to create shadow mounts.
toMount := []string{grandchild, child, parent}
for _, dir := range toMount {
dir := dir
if err := Mount("tmpfs", dir, "tmpfs", ""); err != nil {
t.Fatal(err)
}
//nolint:errcheck
defer Unmount(dir)
}

// unmount shadowed mounts
shadowedMounts := []string{child, grandchild}
for _, shadowedMount := range shadowedMounts {
// Unmount dir, make sure dir-other is still mounted.
if err := RecursiveUnmount(shadowedMount); err == nil {
t.Fatal("error cannot be nil")
} else if err != nil && !errors.Is(err, unix.ENOENT) {
t.Fatalf("expected error to be unix.ENOENT for shadow mount")
}
}
}

0 comments on commit 616f9ba

Please sign in to comment.