|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + csi "github.com/container-storage-interface/spec/lib/go/csi" |
| 13 | + mount "k8s.io/mount-utils" |
| 14 | +) |
| 15 | + |
| 16 | +// A path that does not exist, so the fake mounter's symlink resolution keeps |
| 17 | +// it as is and matches it against its mount table. |
| 18 | +const tempMountPath = "/nonexistent/nfs-init-pvc-1-123" |
| 19 | + |
| 20 | +// forceFakeMounter adds mount.MounterForceUnmounter to the fake, as the real |
| 21 | +// Linux mounter implements it. |
| 22 | +type forceFakeMounter struct { |
| 23 | + *mount.FakeMounter |
| 24 | + forceErr error |
| 25 | + forcedTargets []string |
| 26 | + forcedTimeout time.Duration |
| 27 | +} |
| 28 | + |
| 29 | +func (f *forceFakeMounter) UnmountWithForce(target string, timeout time.Duration) error { |
| 30 | + f.forcedTargets = append(f.forcedTargets, target) |
| 31 | + f.forcedTimeout = timeout |
| 32 | + return f.forceErr |
| 33 | +} |
| 34 | + |
| 35 | +func mountedFake(unmountErr error) *mount.FakeMounter { |
| 36 | + m := mount.NewFakeMounter([]mount.MountPoint{{Path: tempMountPath, Type: "nfs"}}) |
| 37 | + if unmountErr != nil { |
| 38 | + m.UnmountFunc = func(string) error { return unmountErr } |
| 39 | + } |
| 40 | + return m |
| 41 | +} |
| 42 | + |
| 43 | +func TestUnmountTempMountUsesForceUnmounter(t *testing.T) { |
| 44 | + m := &forceFakeMounter{FakeMounter: mountedFake(errors.New("plain Unmount called"))} |
| 45 | + |
| 46 | + if err := unmountTempMount(m, tempMountPath); err != nil { |
| 47 | + t.Fatalf("unmountTempMount: %v", err) |
| 48 | + } |
| 49 | + if len(m.forcedTargets) != 1 || m.forcedTargets[0] != tempMountPath { |
| 50 | + t.Fatalf("UnmountWithForce targets = %v, want [%s]", m.forcedTargets, tempMountPath) |
| 51 | + } |
| 52 | + if m.forcedTimeout <= 0 || m.forcedTimeout >= kubeletCSIOperationTimeout { |
| 53 | + t.Errorf("UnmountWithForce timeout = %v, want within (0, %v)", m.forcedTimeout, kubeletCSIOperationTimeout) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// main wires mount.New(""); without this the force path above is never taken |
| 58 | +// in production and the fallback is silent. |
| 59 | +func TestLinuxMounterIsForceUnmounter(t *testing.T) { |
| 60 | + if runtime.GOOS != "linux" { |
| 61 | + t.Skip("mount.New returns the unsupported mounter off Linux") |
| 62 | + } |
| 63 | + if _, ok := mount.New("").(mount.MounterForceUnmounter); !ok { |
| 64 | + t.Fatal("mount.New(\"\") does not implement mount.MounterForceUnmounter") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// blockingForceMounter stands for an umount -f that never returns: |
| 69 | +// mount-utils runs the forced retry with no deadline of its own. |
| 70 | +type blockingForceMounter struct { |
| 71 | + *mount.FakeMounter |
| 72 | + release chan struct{} |
| 73 | +} |
| 74 | + |
| 75 | +func (b *blockingForceMounter) UnmountWithForce(string, time.Duration) error { |
| 76 | + <-b.release |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func TestUnmountTempMountGivesUpOnAStalledForceRetry(t *testing.T) { |
| 81 | + m := &blockingForceMounter{FakeMounter: mountedFake(nil), release: make(chan struct{})} |
| 82 | + defer close(m.release) |
| 83 | + |
| 84 | + done := make(chan error, 1) |
| 85 | + go func() { done <- unmountWithin(m, tempMountPath, 50*time.Millisecond) }() |
| 86 | + select { |
| 87 | + case err := <-done: |
| 88 | + if err == nil { |
| 89 | + t.Fatal("unmountWithin reported success for an unmount still running") |
| 90 | + } |
| 91 | + case <-time.After(5 * time.Second): |
| 92 | + t.Fatal("unmountWithin did not return within its budget while the forced retry stalled") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestUnmountTempMountReportsForceFailure(t *testing.T) { |
| 97 | + m := &forceFakeMounter{FakeMounter: mountedFake(nil), forceErr: errors.New("device busy")} |
| 98 | + |
| 99 | + if err := unmountTempMount(m, tempMountPath); err == nil { |
| 100 | + t.Fatal("unmountTempMount succeeded although UnmountWithForce failed") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestUnmountTempMountFallsBackToPlainUnmount(t *testing.T) { |
| 105 | + m := mountedFake(nil) |
| 106 | + if err := unmountTempMount(m, tempMountPath); err != nil { |
| 107 | + t.Fatalf("unmountTempMount: %v", err) |
| 108 | + } |
| 109 | + if mps, _ := m.List(); len(mps) != 0 { |
| 110 | + t.Errorf("mount points after unmount = %v, want none", mps) |
| 111 | + } |
| 112 | + |
| 113 | + if err := unmountTempMount(mountedFake(errors.New("device busy")), tempMountPath); err == nil { |
| 114 | + t.Error("unmountTempMount succeeded although Unmount failed") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestNodePublishVolumeNFSUnmountsTempMountWithForce(t *testing.T) { |
| 119 | + tmpRoot := t.TempDir() |
| 120 | + t.Setenv("TMPDIR", tmpRoot) |
| 121 | + |
| 122 | + m := &forceFakeMounter{FakeMounter: mount.NewFakeMounter(nil)} |
| 123 | + w := &WrappedNodeService{mounter: m} |
| 124 | + req := &csi.NodePublishVolumeRequest{ |
| 125 | + VolumeId: "pvc-1", |
| 126 | + TargetPath: filepath.Join(t.TempDir(), "target"), |
| 127 | + PublishContext: map[string]string{nfsExportKey: "nfs://192.0.2.10:2049/export"}, |
| 128 | + } |
| 129 | + if _, err := w.NodePublishVolume(context.Background(), req); err != nil { |
| 130 | + t.Fatalf("NodePublishVolume: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + if len(m.forcedTargets) != 1 || !strings.HasPrefix(filepath.Base(m.forcedTargets[0]), "nfs-init-pvc-1-") { |
| 134 | + t.Fatalf("UnmountWithForce targets = %v, want the one nfs-init-pvc-1-* temp mount", m.forcedTargets) |
| 135 | + } |
| 136 | +} |
0 commit comments