diff --git a/pkg/services/hcloud/server/server_test.go b/pkg/services/hcloud/server/server_test.go index 44141c70c..8f8d89faa 100644 --- a/pkg/services/hcloud/server/server_test.go +++ b/pkg/services/hcloud/server/server_test.go @@ -24,6 +24,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" @@ -343,12 +344,12 @@ var _ = Describe("Test ValidateLabels", func() { var _ = Describe("Test handleRateLimit", func() { type testCaseHandleRateLimit struct { - hm *infrav1.HCloudMachine - err error - functionName string - errMsg string - expectError error - conditionSet bool + hm *infrav1.HCloudMachine + err error + functionName string + errMsg string + expectError error + expectCondition bool } DescribeTable("Test handleRateLimit", @@ -359,21 +360,21 @@ var _ = Describe("Test handleRateLimit", func() { } else { Expect(err).To(BeNil()) } - if tc.conditionSet { - Expect(conditions.Has(tc.hm, infrav1.HetznerAPIReachableCondition)).To(BeTrue()) + if tc.expectCondition { + Expect(isPresentAndFalseWithReason(tc.hm, infrav1.HetznerAPIReachableCondition, infrav1.RateLimitExceededReason)).To(BeTrue()) } else { - Expect(conditions.Has(tc.hm, infrav1.HetznerAPIReachableCondition)).To(BeFalse()) + Expect(conditions.Get(tc.hm, infrav1.HetznerAPIReachableCondition)).To(BeNil()) } }, Entry("machine not ready, rate limit exceeded error", testCaseHandleRateLimit{ hm: &infrav1.HCloudMachine{ Status: infrav1.HCloudMachineStatus{Ready: false}, }, - err: hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}, - functionName: "TestFunction", - errMsg: "Test error message", - expectError: fmt.Errorf("Test error message: %w", hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}), - conditionSet: true, + err: hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}, + functionName: "TestFunction", + errMsg: "Test error message", + expectError: fmt.Errorf("Test error message: %w", hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}), + expectCondition: true, }), Entry("machine has deletion timestamp, rate limit exceeded error", testCaseHandleRateLimit{ hm: &infrav1.HCloudMachine{ @@ -382,11 +383,11 @@ var _ = Describe("Test handleRateLimit", func() { }, Status: infrav1.HCloudMachineStatus{Ready: true}, }, - err: hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}, - functionName: "TestFunction", - errMsg: "Test error message", - expectError: fmt.Errorf("Test error message: %w", hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}), - conditionSet: true, + err: hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}, + functionName: "TestFunction", + errMsg: "Test error message", + expectError: fmt.Errorf("Test error message: %w", hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}), + expectCondition: true, }), Entry("machine not ready, has deletion timestamp, rate limit exceeded error", testCaseHandleRateLimit{ hm: &infrav1.HCloudMachine{ @@ -395,41 +396,50 @@ var _ = Describe("Test handleRateLimit", func() { }, Status: infrav1.HCloudMachineStatus{Ready: false}, }, - err: hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}, - functionName: "TestFunction", - errMsg: "Test error message", - expectError: fmt.Errorf("Test error message: %w", hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}), - conditionSet: true, + err: hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}, + functionName: "TestFunction", + errMsg: "Test error message", + expectError: fmt.Errorf("Test error message: %w", hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}), + expectCondition: true, }), Entry("machine ready, rate limit exceeded error", testCaseHandleRateLimit{ hm: &infrav1.HCloudMachine{ Status: infrav1.HCloudMachineStatus{Ready: true}, }, - err: hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}, - functionName: "TestFunction", - errMsg: "Test error message", - expectError: nil, - conditionSet: false, + err: hcloud.Error{Code: hcloud.ErrorCodeRateLimitExceeded}, + functionName: "TestFunction", + errMsg: "Test error message", + expectError: nil, + expectCondition: false, }), Entry("machine ready, other error", testCaseHandleRateLimit{ hm: &infrav1.HCloudMachine{ Status: infrav1.HCloudMachineStatus{Ready: true}, }, - err: hcloud.Error{Code: hcloud.ErrorCodeResourceUnavailable}, - functionName: "TestFunction", - errMsg: "Test error message", - expectError: fmt.Errorf("Test error message: %w", hcloud.Error{Code: hcloud.ErrorCodeResourceUnavailable}), - conditionSet: false, + err: hcloud.Error{Code: hcloud.ErrorCodeResourceUnavailable}, + functionName: "TestFunction", + errMsg: "Test error message", + expectError: fmt.Errorf("Test error message: %w", hcloud.Error{Code: hcloud.ErrorCodeResourceUnavailable}), + expectCondition: false, }), Entry("machine not ready, other error", testCaseHandleRateLimit{ hm: &infrav1.HCloudMachine{ Status: infrav1.HCloudMachineStatus{Ready: false}, }, - err: hcloud.Error{Code: hcloud.ErrorCodeConflict}, - functionName: "TestFunction", - errMsg: "Test conflict error message", - expectError: fmt.Errorf("Test conflict error message: %w", hcloud.Error{Code: hcloud.ErrorCodeConflict}), - conditionSet: false, + err: hcloud.Error{Code: hcloud.ErrorCodeConflict}, + functionName: "TestFunction", + errMsg: "Test conflict error message", + expectError: fmt.Errorf("Test conflict error message: %w", hcloud.Error{Code: hcloud.ErrorCodeConflict}), + expectCondition: false, }), ) }) + +func isPresentAndFalseWithReason(getter conditions.Getter, condition clusterv1.ConditionType, reason string) bool { + if !conditions.Has(getter, condition) { + return false + } + objectCondition := conditions.Get(getter, condition) + return objectCondition.Status == corev1.ConditionFalse && + objectCondition.Reason == reason +}