fix(agent): fall back to last-resort root when root key is taken

addPartitionRootFs reported success even when addFsStat skipped the
registration because the I/O key was already in use, e.g. when the root
drive is also listed in EXTRA_FILESYSTEMS. hasRoot was then set without
any root filesystem, and the last-resort fallback never ran. addFsStat
now reports whether it registered the filesystem and addPartitionRootFs
propagates that.

Also uppercase the drive letter in windowsVolumeName so "c:" and "C:"
normalize to the same key.
This commit is contained in:
henrygd
2026-09-25 13:14:35 -04:00
parent d708def38f
commit 3fb97b800c
2 changed files with 45 additions and 15 deletions

View File

@@ -1066,9 +1066,10 @@ func TestNormalizeDeviceName(t *testing.T) {
for _, spelling := range []string{"C:", `C:\`, "C:/", `C:\\`} {
assert.Equal(t, "C:", normalizeDeviceName(spelling), "spelling %q", spelling)
}
// Case is left to the caller, as it already is for Linux device names.
assert.Equal(t, "d:", normalizeDeviceName("d:"))
assert.Equal(t, "c:", normalizeDeviceName(" c: "))
// Drive letters are case-insensitive, so the letter is uppercased.
assert.Equal(t, "D:", normalizeDeviceName("d:"))
assert.Equal(t, "C:", normalizeDeviceName(" c: "))
assert.Equal(t, "C:", normalizeDeviceName(`c:\`))
// Non-volume inputs keep using filepath.Base.
assert.Equal(t, "sda1", normalizeDeviceName("/dev/sda1"))
@@ -1124,3 +1125,32 @@ func TestAddPartitionRootFsWindowsDrive(t *testing.T) {
assert.True(t, exists)
assert.True(t, stats.Root)
}
func TestAddPartitionRootFsKeyAlreadyRegistered(t *testing.T) {
// The root drive is also listed in EXTRA_FILESYSTEMS, so its key is taken
// before the root fallback runs. addPartitionRootFs must report failure so
// the caller still falls back to addLastResortRootFs instead of ending up
// with no root filesystem.
agent := &Agent{fsStats: map[string]*system.FsStats{
"C:": {Mountpoint: `C:\`},
}}
discovery := diskDiscovery{
agent: agent,
rootMountPoint: `C:\`,
ctx: fsRegistrationContext{
isWindows: true,
diskIoCounters: map[string]disk.IOCountersStat{
"C:": {Name: "C:", ReadBytes: 10},
"D:": {Name: "D:"},
},
},
}
ok := discovery.addPartitionRootFs("C:", `C:\`)
assert.False(t, ok)
assert.False(t, agent.fsStats["C:"].Root)
discovery.addLastResortRootFs()
assert.Len(t, agent.fsStats, 1)
assert.True(t, agent.fsStats["C:"].Root)
}