fix(agent): promote existing entry when root key is already registered

When the root drive is also listed in EXTRA_FILESYSTEMS, its key is
taken before addPartitionRootFs runs. Returning false there sent the
agent to addLastResortRootFs, which picks the most active device and
could register a different drive as root, overwriting that drive's
extra entry. addPartitionRootFs has already resolved the root device,
so promote the existing entry to root instead.
This commit is contained in:
henrygd
2026-09-25 13:24:11 -04:00
parent 3fb97b800c
commit 3dfe062ee4
2 changed files with 21 additions and 15 deletions

View File

@@ -1128,11 +1128,11 @@ func TestAddPartitionRootFsWindowsDrive(t *testing.T) {
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.
// before the root fallback runs. The existing entry must be promoted to root
// rather than falling back to the most active device, which here is D:.
agent := &Agent{fsStats: map[string]*system.FsStats{
"C:": {Mountpoint: `C:\`},
"C:": {Mountpoint: `C:\`, Name: "System"},
"D:": {Mountpoint: `D:\`},
}}
discovery := diskDiscovery{
agent: agent,
@@ -1141,16 +1141,16 @@ func TestAddPartitionRootFsKeyAlreadyRegistered(t *testing.T) {
isWindows: true,
diskIoCounters: map[string]disk.IOCountersStat{
"C:": {Name: "C:", ReadBytes: 10},
"D:": {Name: "D:"},
"D:": {Name: "D:", ReadBytes: 100},
},
},
}
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, ok)
assert.Len(t, agent.fsStats, 2)
assert.True(t, agent.fsStats["C:"].Root)
assert.Equal(t, `C:\`, agent.fsStats["C:"].Mountpoint)
assert.Equal(t, "System", agent.fsStats["C:"].Name)
assert.False(t, agent.fsStats["D:"].Root)
}