From febd9f176e60b6a79517019227534777b5b2cf1c Mon Sep 17 00:00:00 2001 From: immanuwell Date: Sun, 31 May 2026 21:32:48 +0400 Subject: [PATCH] fix: return defaultValue on conversion failure in ConvertStringToTypeOrDefault Signed-off-by: Immanuel Tikhonov Signed-off-by: immanuwell --- pkg/utils/cmd.go | 8 ++++---- pkg/utils/common_test.go | 30 ++++++++++++++++++++++++++++++ pkg/utils/kubernetes/runtime.go | 2 +- pkg/utils/longhorn/longhorn.go | 2 +- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/pkg/utils/cmd.go b/pkg/utils/cmd.go index f51b15309..9c42560fb 100644 --- a/pkg/utils/cmd.go +++ b/pkg/utils/cmd.go @@ -105,17 +105,17 @@ func ConvertStringToTypeOrDefault[T any](str string, defaultValue T) T { intValue, err := strconv.Atoi(str) if err != nil { logrus.WithError(err).Warn("Failed to convert string to integer") - } else { - value = reflect.ValueOf(intValue).Interface().(T) + return defaultValue } + value = reflect.ValueOf(intValue).Interface().(T) case reflect.Bool: boolValue, err := strconv.ParseBool(str) if err != nil { logrus.WithError(err).Warn("Failed to convert string to boolean") - } else { - value = reflect.ValueOf(boolValue).Interface().(T) + return defaultValue } + value = reflect.ValueOf(boolValue).Interface().(T) default: logrus.WithField("type", reflect.TypeOf(defaultValue)).Warn("Unsupported default value type") diff --git a/pkg/utils/common_test.go b/pkg/utils/common_test.go index 3082a4405..fb72bac13 100644 --- a/pkg/utils/common_test.go +++ b/pkg/utils/common_test.go @@ -2,6 +2,36 @@ package utils import "testing" +func TestConvertStringToTypeOrDefault(t *testing.T) { + for testName, testCase := range map[string]struct { + input string + defaultValue any + expected any + }{ + "empty string returns default int": {"", 2048, 2048}, + "valid int string": {"4096", 2048, 4096}, + "invalid int returns default": {"bad", 2048, 2048}, + "empty string returns default bool": {"", true, true}, + "valid bool string true": {"true", false, true}, + "valid bool string false": {"false", true, false}, + "invalid bool returns default": {"bad", true, true}, + "invalid bool returns default (false)": {"bad", false, false}, + } { + t.Run(testName, func(t *testing.T) { + var got any + switch d := testCase.defaultValue.(type) { + case int: + got = ConvertStringToTypeOrDefault(testCase.input, d) + case bool: + got = ConvertStringToTypeOrDefault(testCase.input, d) + } + if got != testCase.expected { + t.Errorf("expected %v, got %v", testCase.expected, got) + } + }) + } +} + func TestBuildImageName(t *testing.T) { for testName, testCase := range map[string]struct { expected, image, globalRegistry string diff --git a/pkg/utils/kubernetes/runtime.go b/pkg/utils/kubernetes/runtime.go index 791ea80ab..6e53f0982 100644 --- a/pkg/utils/kubernetes/runtime.go +++ b/pkg/utils/kubernetes/runtime.go @@ -160,7 +160,7 @@ func NewWorkload(kubeClient *kubeclient.Clientset, obj interface{}, kind, labelS "kind": kind, "namespace": namespace, "name": name, - "lable-selectors": labelSelector, + "label-selectors": labelSelector, }) return &Workload{ logger: logger, diff --git a/pkg/utils/longhorn/longhorn.go b/pkg/utils/longhorn/longhorn.go index 495b15697..b49173529 100644 --- a/pkg/utils/longhorn/longhorn.go +++ b/pkg/utils/longhorn/longhorn.go @@ -54,7 +54,7 @@ func GetDataDirectory(logger *logrus.Entry, hostDirectory, inputDataDirectory st return "", err } } else { - return "", errors.Wrapf(err, "directory %s does not exist", dataDir) + return "", errors.Wrapf(err, "failed to stat directory %s", dataDir) } } }