util: fix bug in health checker

This commit fixes a bug in health checker that
caused shared checker to get keyed with volumeID+volumepath
instead of just volumeID and the other way around
for non-shared checkers.

Signed-off-by: Rakshith R <rar@redhat.com>
This commit is contained in:
Rakshith R 2025-04-14 16:28:21 +05:30 committed by mergify[bot]
parent 98a2256e4d
commit 86f2ad9e0d
2 changed files with 56 additions and 3 deletions

View File

@ -137,7 +137,8 @@ func (hcm *healthCheckManager) startStatChecker(volumeID, path string, shared bo
// are key'd by theit volumeID+path.
func (hcm *healthCheckManager) startChecker(cc ConditionChecker, volumeID, path string, shared bool) error {
key := volumeID
if shared {
if !shared {
// if it is non-shared checker, try to use the path as well.
key = fallbackKey(volumeID, path)
}

View File

@ -18,12 +18,15 @@ package healthchecker
import (
"testing"
"github.com/stretchr/testify/require"
)
const volumeID = "fake-volume-id"
func TestManager(t *testing.T) {
t.Parallel()
volumeID := "fake-volume-id"
volumePath := t.TempDir()
mgr := NewHealthCheckManager()
@ -52,7 +55,6 @@ func TestManager(t *testing.T) {
func TestSharedChecker(t *testing.T) {
t.Parallel()
volumeID := "fake-volume-id"
volumePath := t.TempDir()
mgr := NewHealthCheckManager()
@ -83,3 +85,53 @@ func TestSharedChecker(t *testing.T) {
t.Log("stop the checker")
mgr.StopSharedChecker(volumeID)
}
func TestTwoNonSharedChecker(t *testing.T) {
t.Parallel()
// create two different paths for same volumeID
// to test if the checkers are independent
firstVolumePath := t.TempDir()
secondVolumePath := t.TempDir()
mgr := NewHealthCheckManager()
t.Log("start the first checker")
err := mgr.StartChecker(volumeID, firstVolumePath, StatCheckerType)
if err != nil {
t.Fatalf("ConditionChecker could not get started: %v", err)
}
t.Log("check health for first path, should be healthy")
healthy, msg := mgr.IsHealthy(volumeID, firstVolumePath)
if !healthy || err != nil {
t.Errorf("volume is unhealthy: %s", msg)
}
t.Log("check health for second path, should error out since checker is not started")
_, msg = mgr.IsHealthy(volumeID, secondVolumePath)
require.ErrorContains(t, msg, "no ConditionChecker for volume-id")
t.Log("start the second checker")
err = mgr.StartChecker(volumeID, secondVolumePath, StatCheckerType)
if err != nil {
t.Fatalf("ConditionChecker could not get started: %v", err)
}
t.Log("check health, should be healthy")
healthy, msg = mgr.IsHealthy(volumeID, secondVolumePath)
if !healthy || err != nil {
t.Errorf("volume is unhealthy: %s", msg)
}
t.Log("stop the first checker")
mgr.StopChecker(volumeID, firstVolumePath)
t.Log("check health of second path, should still be healthy")
healthy, msg = mgr.IsHealthy(volumeID, secondVolumePath)
if !healthy || err != nil {
t.Errorf("volume is unhealthy: %s", msg)
}
t.Log("stop the second checker")
mgr.StopChecker(volumeID, secondVolumePath)
}