|
| 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 | +func TestUnmountTempMountReportsForceFailure(t *testing.T) { |
| 69 | + m := &forceFakeMounter{FakeMounter: mountedFake(nil), forceErr: errors.New("device busy")} |
| 70 | + |
| 71 | + if err := unmountTempMount(m, tempMountPath); err == nil { |
| 72 | + t.Fatal("unmountTempMount succeeded although UnmountWithForce failed") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestUnmountTempMountFallsBackToPlainUnmount(t *testing.T) { |
| 77 | + m := mountedFake(nil) |
| 78 | + if err := unmountTempMount(m, tempMountPath); err != nil { |
| 79 | + t.Fatalf("unmountTempMount: %v", err) |
| 80 | + } |
| 81 | + if mps, _ := m.List(); len(mps) != 0 { |
| 82 | + t.Errorf("mount points after unmount = %v, want none", mps) |
| 83 | + } |
| 84 | + |
| 85 | + if err := unmountTempMount(mountedFake(errors.New("device busy")), tempMountPath); err == nil { |
| 86 | + t.Error("unmountTempMount succeeded although Unmount failed") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestNodePublishVolumeNFSUnmountsTempMountWithForce(t *testing.T) { |
| 91 | + tmpRoot := t.TempDir() |
| 92 | + t.Setenv("TMPDIR", tmpRoot) |
| 93 | + |
| 94 | + m := &forceFakeMounter{FakeMounter: mount.NewFakeMounter(nil)} |
| 95 | + w := &WrappedNodeService{mounter: m} |
| 96 | + req := &csi.NodePublishVolumeRequest{ |
| 97 | + VolumeId: "pvc-1", |
| 98 | + TargetPath: filepath.Join(t.TempDir(), "target"), |
| 99 | + PublishContext: map[string]string{nfsExportKey: "nfs://192.0.2.10:2049/export"}, |
| 100 | + } |
| 101 | + if _, err := w.NodePublishVolume(context.Background(), req); err != nil { |
| 102 | + t.Fatalf("NodePublishVolume: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + if len(m.forcedTargets) != 1 || !strings.HasPrefix(filepath.Base(m.forcedTargets[0]), "nfs-init-pvc-1-") { |
| 106 | + t.Fatalf("UnmountWithForce targets = %v, want the one nfs-init-pvc-1-* temp mount", m.forcedTargets) |
| 107 | + } |
| 108 | +} |
0 commit comments