From e1db875efeba113b068c470b92e24173054f0e6f Mon Sep 17 00:00:00 2001 From: faizanahmad055 Date: Tue, 6 Jan 2026 22:20:20 +0100 Subject: [PATCH] Fix failing tests Signed-off-by: faizanahmad055 --- internal/pkg/crypto/sha.go | 6 ++---- internal/pkg/crypto/sha_test.go | 13 +++++++++++++ internal/pkg/handler/upgrade.go | 6 +++--- internal/pkg/handler/upgrade_test.go | 6 +++--- internal/pkg/testutil/kube.go | 6 +++++- internal/pkg/util/util.go | 2 +- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/internal/pkg/crypto/sha.go b/internal/pkg/crypto/sha.go index 9235425..f9ae235 100644 --- a/internal/pkg/crypto/sha.go +++ b/internal/pkg/crypto/sha.go @@ -6,11 +6,9 @@ import ( ) // GenerateSHA generates SHA from string +// Always returns a hash value, even for empty strings, to ensure consistent behavior +// and avoid issues with string matching operations (e.g., strings.Contains(str, "") always returns true) func GenerateSHA(data string) string { - if data == "" { - return "" - } - hash := sha512.Sum512_256([]byte(data)) return hex.EncodeToString(hash[:]) } diff --git a/internal/pkg/crypto/sha_test.go b/internal/pkg/crypto/sha_test.go index 60d5af6..761f8d0 100644 --- a/internal/pkg/crypto/sha_test.go +++ b/internal/pkg/crypto/sha_test.go @@ -13,3 +13,16 @@ func TestGenerateSHA(t *testing.T) { t.Errorf("Failed to generate SHA") } } + +// TestGenerateSHAEmptyString verifies that empty string generates a valid hash +// This ensures consistent behavior and avoids issues with string matching operations +func TestGenerateSHAEmptyString(t *testing.T) { + result := GenerateSHA("") + expected := "c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a" + if result != expected { + t.Errorf("Failed to generate SHA for empty string. Expected: %s, Got: %s", expected, result) + } + if len(result) != 64 { + t.Errorf("SHA hash should be 64 characters long, got %d", len(result)) + } +} diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index b10bfbc..6d63d5c 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -539,8 +539,8 @@ func updatePodAnnotations(upgradeFuncs callbacks.RollingUpgradeFuncs, item runti } func secretProviderClassAnnotationReloaded(oldAnnotations map[string]string, newConfig common.Config) bool { - annotaion := oldAnnotations[getReloaderAnnotationKey()] - return strings.Contains(annotaion, newConfig.ResourceName) && strings.Contains(annotaion, newConfig.SHAValue) + annotation := oldAnnotations[getReloaderAnnotationKey()] + return strings.Contains(annotation, newConfig.ResourceName) && strings.Contains(annotation, newConfig.SHAValue) } func getReloaderAnnotationKey() string { @@ -645,7 +645,7 @@ func secretProviderClassEnvReloaded(containers []v1.Container, envVar string, sh } func populateAnnotationsFromSecretProviderClass(clients kube.Clients, config *common.Config) { - obj, err := clients.CSIClient.SecretsstoreV1().SecretProviderClasses(config.Namespace).Get(context.TODO(), config.ResourceName, metav1.GetOptions{}) + obj, err := clients.CSIClient.SecretsstoreV1().SecretProviderClasses(config.Namespace).Get(context.Background(), config.ResourceName, metav1.GetOptions{}) annotations := make(map[string]string) if err != nil { if apierrors.IsNotFound(err) { diff --git a/internal/pkg/handler/upgrade_test.go b/internal/pkg/handler/upgrade_test.go index a334db0..e905ee0 100644 --- a/internal/pkg/handler/upgrade_test.go +++ b/internal/pkg/handler/upgrade_test.go @@ -3327,7 +3327,7 @@ func TestRollingUpgradeForStatefulSetWithSecretProviderClassUsingArs(t *testing. err := PerformAction(clients, config, statefulSetFuncs, collectors, nil, invokeReloadStrategy) time.Sleep(5 * time.Second) if err != nil { - t.Errorf("Rolling upgrade failed for StatefulSet with SecretProviderClass") + t.Errorf("Rolling upgrade failed for StatefulSet with SecretProviderClass: %v", err) } logrus.Infof("Verifying statefulSet update") @@ -3337,11 +3337,11 @@ func TestRollingUpgradeForStatefulSetWithSecretProviderClassUsingArs(t *testing. } if promtestutil.ToFloat64(collectors.Reloaded.With(labelSucceeded)) != 1 { - t.Errorf("Counter was not increased") + t.Errorf("Counter was not increased, expected 1 but got %f", promtestutil.ToFloat64(collectors.Reloaded.With(labelSucceeded))) } if promtestutil.ToFloat64(collectors.ReloadedByNamespace.With(prometheus.Labels{"success": "true", "namespace": arsNamespace})) != 1 { - t.Errorf("Counter by namespace was not increased") + t.Errorf("Counter by namespace was not increased, expected 1 but got %f", promtestutil.ToFloat64(collectors.ReloadedByNamespace.With(prometheus.Labels{"success": "true", "namespace": arsNamespace}))) } testRollingUpgradeInvokeDeleteStrategyArs(t, clients, config, statefulSetFuncs, collectors, envVarPostfix) diff --git a/internal/pkg/testutil/kube.go b/internal/pkg/testutil/kube.go index 4901d9a..a778eb1 100644 --- a/internal/pkg/testutil/kube.go +++ b/internal/pkg/testutil/kube.go @@ -1327,7 +1327,11 @@ func VerifyResourceAnnotationUpdate(clients kube.Clients, config common.Config, } func GetSHAfromEmptyData() string { - return crypto.GenerateSHA("") + // Use a special marker that represents "deleted" or "empty" state + // This ensures we have a distinct, deterministic hash for the delete strategy + // Note: We could use GenerateSHA("") which now returns a hash, but using a marker + // makes the intent clearer and avoids potential confusion with actual empty data + return crypto.GenerateSHA("__RELOADER_EMPTY_DELETE_MARKER__") } // GetRollout provides rollout for testing diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index 047d068..476cdb9 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -106,7 +106,7 @@ func ConfigureReloaderFlags(cmd *cobra.Command) { cmd.PersistentFlags().BoolVar(&options.SyncAfterRestart, "sync-after-restart", false, "Sync add events after reloader restarts") cmd.PersistentFlags().BoolVar(&options.EnablePProf, "enable-pprof", false, "Enable pprof for profiling") cmd.PersistentFlags().StringVar(&options.PProfAddr, "pprof-addr", ":6060", "Address to start pprof server on. Default is :6060") - cmd.PersistentFlags().BoolVar(&options.EnableCSIIntegration, "enable-csi-integration", false, "Enables CSI integration. Default is :true") + cmd.PersistentFlags().BoolVar(&options.EnableCSIIntegration, "enable-csi-integration", false, "Enables CSI integration. Default is :false") } func GetIgnoredResourcesList() (List, error) {