Skip to content

Commit

Permalink
🌱Improve else case
Browse files Browse the repository at this point in the history
  • Loading branch information
yrs147 committed Aug 14, 2024
1 parent f185a46 commit 21fd624
Showing 1 changed file with 49 additions and 39 deletions.
88 changes: 49 additions & 39 deletions pkg/services/hcloud/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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
}

0 comments on commit 21fd624

Please sign in to comment.