test(kubernetes): pin the mount-utils behaviour the CSI node relies on

The upstream kubevirt CSI node service is built here against the
mount-utils this module replaces it with, not the release upstream
tests against. Pin what it relies on: mount point detection, cleanup
of a published target, and the blkid, mkfs and fsck calls of
SafeFormatAndMount.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-16 01:07:38 +03:00
1 parent 2a04bfd0db
commit ea0ae7ec1d
2 files changed
+200

No files matched your search

@@ -0,0 +1,124 @@
//go:build linux
package main
import (
"errors"
"slices"
"testing"
"k8s.io/mount-utils"
"k8s.io/utils/exec"
testingexec "k8s.io/utils/exec/testing"
)
type cmdResult struct {
out string
err error
}
type execCall struct {
cmd string
args []string
}
// scriptedExec answers each command with the next output and error, and
// records what was run.
func scriptedExec(calls *[]execCall, results ...cmdResult) *testingexec.FakeExec {
fe := &testingexec.FakeExec{}
for _, r := range results {
fe.CommandScript = append(fe.CommandScript, func(cmd string, args ...string) exec.Cmd {
*calls = append(*calls, execCall{cmd, args})
return &testingexec.FakeCmd{CombinedOutputScript: []testingexec.FakeAction{
func() ([]byte, []byte, error) { return []byte(r.out), nil, r.err },
}}
})
}
return fe
}
var blkidArgs = []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/vdb"}
func TestMountUtilsSafeFormatAndMountCommands(t *testing.T) {
unformatted := &testingexec.FakeExitError{Status: 2}
for _, tc := range []struct {
name string
fstype string
options []string
results []cmdResult
want []execCall
wantErr bool
wantMount bool
}{
{
name: "unformatted ext4",
fstype: "ext4",
results: []cmdResult{{"", unformatted}, {"", nil}},
want: []execCall{{"blkid", blkidArgs}, {"mkfs.ext4", []string{"-F", "-m0", "/dev/vdb"}}},
wantMount: true,
},
{
name: "unformatted defaults to ext4",
results: []cmdResult{{"", unformatted}, {"", nil}},
want: []execCall{{"blkid", blkidArgs}, {"mkfs.ext4", []string{"-F", "-m0", "/dev/vdb"}}},
wantMount: true,
},
{
name: "unformatted xfs",
fstype: "xfs",
results: []cmdResult{{"", unformatted}, {"", nil}},
want: []execCall{{"blkid", blkidArgs}, {"mkfs.xfs", []string{"-f", "/dev/vdb"}}},
wantMount: true,
},
{
name: "formatted ext4 is checked, not formatted",
fstype: "ext4",
results: []cmdResult{{"DEVNAME=/dev/vdb\nTYPE=ext4\n", nil}, {"", nil}},
want: []execCall{{"blkid", blkidArgs}, {"fsck", []string{"-a", "/dev/vdb"}}},
wantMount: true,
},
{
name: "unformatted read-only is refused",
fstype: "ext4",
options: []string{"ro"},
results: []cmdResult{{"", unformatted}},
want: []execCall{{"blkid", blkidArgs}},
wantErr: true,
},
{
name: "mkfs failure is reported",
fstype: "ext4",
results: []cmdResult{{"", unformatted}, {"boom", errors.New("exit 1")}},
want: []execCall{{"blkid", blkidArgs}, {"mkfs.ext4", []string{"-F", "-m0", "/dev/vdb"}}},
wantErr: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
var calls []execCall
fm := mount.NewFakeMounter(nil)
m := &mount.SafeFormatAndMount{Interface: fm, Exec: scriptedExec(&calls, tc.results...)}
err := m.FormatAndMount("/dev/vdb", "/target", tc.fstype, tc.options)
if (err != nil) != tc.wantErr {
t.Fatalf("FormatAndMount error = %v, wantErr %v", err, tc.wantErr)
}
if !slices.EqualFunc(calls, tc.want, func(a, b execCall) bool { return a.cmd == b.cmd && slices.Equal(a.args, b.args) }) {
t.Errorf("commands = %v, want %v", calls, tc.want)
}
mps, _ := fm.List()
if !tc.wantMount {
if len(mps) != 0 {
t.Errorf("mounted %v, want nothing", mps)
}
return
}
wantType := tc.fstype
if wantType == "" {
wantType = "ext4"
}
if len(mps) != 1 || mps[0].Device != "/dev/vdb" || mps[0].Path != "/target" || mps[0].Type != wantType || !slices.Contains(mps[0].Opts, "defaults") {
t.Errorf("mount points = %+v, want /dev/vdb on /target type %s with defaults", mps, wantType)
}
})
}
}
@@ -0,0 +1,76 @@
package main
import (
"os"
"path/filepath"
"testing"
"k8s.io/mount-utils"
)
// The upstream node service is compiled against the mount-utils this module
// pins, not the one upstream tests with. These pin the parts of it upstream
// relies on when it decides whether to mount and how to clean up.
func TestMountUtilsFakeMounterMountPointDetection(t *testing.T) {
dir := resolvedTempDir(t)
mounted := filepath.Join(dir, "mounted")
plain := filepath.Join(dir, "plain")
for _, d := range []string{mounted, plain} {
if err := os.Mkdir(d, 0o755); err != nil {
t.Fatal(err)
}
}
m := mount.NewFakeMounter([]mount.MountPoint{{Device: "/dev/vdb", Path: mounted, Type: "ext4"}})
if notMnt, err := m.IsLikelyNotMountPoint(mounted); err != nil || notMnt {
t.Errorf("IsLikelyNotMountPoint(mounted) = %v, %v; want false, nil", notMnt, err)
}
if notMnt, err := m.IsLikelyNotMountPoint(plain); err != nil || !notMnt {
t.Errorf("IsLikelyNotMountPoint(plain) = %v, %v; want true, nil", notMnt, err)
}
if _, err := m.IsLikelyNotMountPoint(filepath.Join(dir, "missing")); !os.IsNotExist(err) {
t.Errorf("IsLikelyNotMountPoint(missing) error = %v, want not-exist", err)
}
if err := m.Unmount(mounted); err != nil {
t.Fatalf("Unmount: %v", err)
}
if notMnt, err := m.IsLikelyNotMountPoint(mounted); err != nil || !notMnt {
t.Errorf("after Unmount: IsLikelyNotMountPoint = %v, %v; want true, nil", notMnt, err)
}
}
// NodeUnpublishVolume upstream calls CleanupMountPoint: it unmounts a mounted
// target and removes the directory, and tolerates a target that is already gone.
func TestMountUtilsCleanupMountPoint(t *testing.T) {
dir := resolvedTempDir(t)
target := filepath.Join(dir, "target")
if err := os.Mkdir(target, 0o755); err != nil {
t.Fatal(err)
}
m := mount.NewFakeMounter([]mount.MountPoint{{Device: "/dev/vdb", Path: target, Type: "ext4"}})
if err := mount.CleanupMountPoint(target, m, false); err != nil {
t.Fatalf("CleanupMountPoint: %v", err)
}
if _, err := os.Stat(target); !os.IsNotExist(err) {
t.Errorf("target still present after cleanup: %v", err)
}
if mps, _ := m.List(); len(mps) != 0 {
t.Errorf("mount points after cleanup = %v, want none", mps)
}
if err := mount.CleanupMountPoint(target, m, false); err != nil {
t.Errorf("CleanupMountPoint on a missing target: %v", err)
}
}
// FakeMounter resolves symlinks before matching a path, and the temp dir is
// behind one on some systems.
func resolvedTempDir(t *testing.T) string {
t.Helper()
dir, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
return dir
}