|
| 1 | +/* |
| 2 | + * Copyright (c) 2022. Nydus Developers. All rights reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +package mount |
| 8 | + |
| 9 | +import ( |
| 10 | + "syscall" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/pkg/errors" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestIsDisconnected(t *testing.T) { |
| 19 | + assert.True(t, isDisconnected(syscall.ENOTCONN)) |
| 20 | + assert.True(t, isDisconnected(syscall.ESTALE)) |
| 21 | + // Must see through pkg/errors wrapping (IsMountpoint wraps stat errors). |
| 22 | + assert.True(t, isDisconnected(errors.Wrapf(syscall.ENOTCONN, "stat target of %s", "/foo"))) |
| 23 | + assert.False(t, isDisconnected(syscall.EBUSY)) |
| 24 | + assert.False(t, isDisconnected(errors.New("some other error"))) |
| 25 | + assert.False(t, isDisconnected(nil)) |
| 26 | +} |
| 27 | + |
| 28 | +func TestUnmountWithFallback(t *testing.T) { |
| 29 | + origUnmount := syscallUnmount |
| 30 | + t.Cleanup(func() { syscallUnmount = origUnmount }) |
| 31 | + |
| 32 | + type call struct { |
| 33 | + flags int |
| 34 | + } |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + errByAttempt []error // error returned for the Nth syscallUnmount call |
| 39 | + wantErr bool |
| 40 | + wantCalls []call |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "plain umount succeeds", |
| 44 | + errByAttempt: []error{nil}, |
| 45 | + wantCalls: []call{{0}}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "EINVAL treated as already unmounted", |
| 49 | + errByAttempt: []error{syscall.EINVAL}, |
| 50 | + wantCalls: []call{{0}}, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "EBUSY falls back to force", |
| 54 | + errByAttempt: []error{syscall.EBUSY, nil}, |
| 55 | + wantCalls: []call{{0}, {umountForce}}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "force fails then lazy detach succeeds", |
| 59 | + errByAttempt: []error{syscall.EBUSY, syscall.EBUSY, nil}, |
| 60 | + wantCalls: []call{{0}, {umountForce}, {umountDetach}}, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "all attempts fail returns error", |
| 64 | + errByAttempt: []error{syscall.EBUSY, syscall.EBUSY, syscall.EBUSY}, |
| 65 | + wantErr: true, |
| 66 | + wantCalls: []call{{0}, {umountForce}, {umountDetach}}, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tc := range tests { |
| 71 | + t.Run(tc.name, func(t *testing.T) { |
| 72 | + var gotCalls []call |
| 73 | + idx := 0 |
| 74 | + syscallUnmount = func(_ string, flags int) error { |
| 75 | + gotCalls = append(gotCalls, call{flags}) |
| 76 | + err := tc.errByAttempt[idx] |
| 77 | + idx++ |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + err := unmountWithFallback("/mnt/target") |
| 82 | + if tc.wantErr { |
| 83 | + require.Error(t, err) |
| 84 | + } else { |
| 85 | + require.NoError(t, err) |
| 86 | + } |
| 87 | + assert.Equal(t, tc.wantCalls, gotCalls) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestUmount(t *testing.T) { |
| 93 | + origIsMountpoint := isMountpoint |
| 94 | + origUnmount := syscallUnmount |
| 95 | + t.Cleanup(func() { |
| 96 | + isMountpoint = origIsMountpoint |
| 97 | + syscallUnmount = origUnmount |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("disconnected mountpoint still gets unmounted", func(t *testing.T) { |
| 101 | + isMountpoint = func(string) (bool, error) { return false, syscall.ENOTCONN } |
| 102 | + unmounted := false |
| 103 | + syscallUnmount = func(string, int) error { unmounted = true; return nil } |
| 104 | + |
| 105 | + require.NoError(t, (&Mounter{}).Umount("/mnt/target")) |
| 106 | + assert.True(t, unmounted, "a disconnected mountpoint must still be unmounted") |
| 107 | + }) |
| 108 | + |
| 109 | + t.Run("not mounted returns nil without unmounting", func(t *testing.T) { |
| 110 | + isMountpoint = func(string) (bool, error) { return false, nil } |
| 111 | + called := false |
| 112 | + syscallUnmount = func(string, int) error { called = true; return nil } |
| 113 | + |
| 114 | + require.NoError(t, (&Mounter{}).Umount("/mnt/target")) |
| 115 | + assert.False(t, called, "must not attempt to unmount a non-mountpoint") |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("other IsMountpoint error is propagated", func(t *testing.T) { |
| 119 | + wantErr := errors.New("boom") |
| 120 | + isMountpoint = func(string) (bool, error) { return false, wantErr } |
| 121 | + syscallUnmount = func(string, int) error { return nil } |
| 122 | + |
| 123 | + require.ErrorIs(t, (&Mounter{}).Umount("/mnt/target"), wantErr) |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +func TestWaitUntilUnmountedIgnoresDisconnectedMountpoint(t *testing.T) { |
| 128 | + origIsMountpoint := isMountpoint |
| 129 | + t.Cleanup(func() { isMountpoint = origIsMountpoint }) |
| 130 | + |
| 131 | + calls := 0 |
| 132 | + isMountpoint = func(string) (bool, error) { |
| 133 | + calls++ |
| 134 | + return false, syscall.ENOTCONN |
| 135 | + } |
| 136 | + |
| 137 | + require.NoError(t, WaitUntilUnmounted("/mnt/target")) |
| 138 | + assert.Equal(t, 1, calls) |
| 139 | +} |
0 commit comments