From d8e1e6dd0ed5da0c0fd8a951214c9f1cdb52ec00 Mon Sep 17 00:00:00 2001 From: Joachim Bartosik Date: Thu, 2 May 2024 10:31:51 +0200 Subject: [PATCH 1/2] Test enum formatting --- cluster-autoscaler/utils/drain/drain_test.go | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/cluster-autoscaler/utils/drain/drain_test.go b/cluster-autoscaler/utils/drain/drain_test.go index 95176fdf55d0..02cd6aa0eb5e 100644 --- a/cluster-autoscaler/utils/drain/drain_test.go +++ b/cluster-autoscaler/utils/drain/drain_test.go @@ -17,6 +17,7 @@ limitations under the License. package drain import ( + "fmt" "testing" "time" @@ -128,3 +129,57 @@ func TestIsPodLongTerminating(t *testing.T) { }) } } + +func TestBlockingPodReasonFormatting(t *testing.T) { + for _, tc := range []struct { + bpr BlockingPodReason + want string + }{ + { + bpr: NoReason, + want: "0", + }, + { + bpr: ControllerNotFound, + want: "1", + }, + { + bpr: MinReplicasReached, + want: "2", + }, + { + bpr: NotReplicated, + want: "3", + }, + { + bpr: LocalStorageRequested, + want: "4", + }, + { + bpr: NotSafeToEvictAnnotation, + want: "5", + }, + { + bpr: UnmovableKubeSystemPod, + want: "6", + }, + { + bpr: NotEnoughPdb, + want: "7", + }, + { + bpr: UnexpectedError, + want: "8", + }, + { + bpr: BlockingPodReason(9), + want: "9", + }, + } { + t.Run(tc.want, func(t *testing.T) { + if got := fmt.Sprintf("%v", tc.bpr); got != tc.want { + t.Errorf("got: %s, want %s", got, tc.want) + } + }) + } +} From 4e42fe78b6730a4511abb5bb8756c8e52b3ea26e Mon Sep 17 00:00:00 2001 From: Joachim Bartosik Date: Tue, 30 Apr 2024 11:12:23 +0200 Subject: [PATCH 2/2] Format BlockingPodReason in human readbale way with %v Without this the enum is output as its int value, which is not readable --- cluster-autoscaler/utils/drain/drain.go | 26 ++++++++++++++++++++ cluster-autoscaler/utils/drain/drain_test.go | 22 ++++++++--------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/cluster-autoscaler/utils/drain/drain.go b/cluster-autoscaler/utils/drain/drain.go index 81eef9ad858a..00f66e673fb5 100644 --- a/cluster-autoscaler/utils/drain/drain.go +++ b/cluster-autoscaler/utils/drain/drain.go @@ -17,6 +17,7 @@ limitations under the License. package drain import ( + "fmt" "strings" "time" @@ -68,6 +69,31 @@ const ( UnexpectedError ) +func (e BlockingPodReason) String() string { + switch e { + case NoReason: + return "NoReason" + case ControllerNotFound: + return "ControllerNotFound" + case MinReplicasReached: + return "MinReplicasReached" + case NotReplicated: + return "NotReplicated" + case LocalStorageRequested: + return "LocalStorageRequested" + case NotSafeToEvictAnnotation: + return "NotSafeToEvictAnnotation" + case UnmovableKubeSystemPod: + return "UnmovableKubeSystemPod" + case NotEnoughPdb: + return "NotEnoughPdb" + case UnexpectedError: + return "UnexpectedError" + default: + return fmt.Sprintf("unrecognized reason: %d", int(e)) + } +} + // ControllerRef returns the OwnerReference to pod's controller. func ControllerRef(pod *apiv1.Pod) *metav1.OwnerReference { return metav1.GetControllerOf(pod) diff --git a/cluster-autoscaler/utils/drain/drain_test.go b/cluster-autoscaler/utils/drain/drain_test.go index 02cd6aa0eb5e..ba65b3a8f38c 100644 --- a/cluster-autoscaler/utils/drain/drain_test.go +++ b/cluster-autoscaler/utils/drain/drain_test.go @@ -137,43 +137,43 @@ func TestBlockingPodReasonFormatting(t *testing.T) { }{ { bpr: NoReason, - want: "0", + want: "NoReason", }, { bpr: ControllerNotFound, - want: "1", + want: "ControllerNotFound", }, { - bpr: MinReplicasReached, - want: "2", + bpr: ControllerNotFound, + want: "ControllerNotFound", }, { bpr: NotReplicated, - want: "3", + want: "NotReplicated", }, { bpr: LocalStorageRequested, - want: "4", + want: "LocalStorageRequested", }, { bpr: NotSafeToEvictAnnotation, - want: "5", + want: "NotSafeToEvictAnnotation", }, { bpr: UnmovableKubeSystemPod, - want: "6", + want: "UnmovableKubeSystemPod", }, { bpr: NotEnoughPdb, - want: "7", + want: "NotEnoughPdb", }, { bpr: UnexpectedError, - want: "8", + want: "UnexpectedError", }, { bpr: BlockingPodReason(9), - want: "9", + want: "unrecognized reason: 9", }, } { t.Run(tc.want, func(t *testing.T) {