|
| 1 | +/* |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package errors_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/aws/smithy-go" |
| 23 | + |
| 24 | + awserrors "github.com/aws/karpenter-provider-aws/pkg/errors" |
| 25 | +) |
| 26 | + |
| 27 | +type apiErr struct { |
| 28 | + code string |
| 29 | + message string |
| 30 | +} |
| 31 | + |
| 32 | +func (e *apiErr) Error() string { return fmt.Sprintf("%s: %s", e.code, e.message) } |
| 33 | +func (e *apiErr) ErrorCode() string { return e.code } |
| 34 | +func (e *apiErr) ErrorMessage() string { return e.message } |
| 35 | +func (e *apiErr) ErrorFault() smithy.ErrorFault { return smithy.FaultClient } |
| 36 | + |
| 37 | +func TestClassifyError(t *testing.T) { |
| 38 | + tests := []struct { |
| 39 | + name string |
| 40 | + err error |
| 41 | + wantReason string |
| 42 | + wantMessage string |
| 43 | + wantRetryable bool |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "nil error is retryable and returns empty classification", |
| 47 | + err: nil, |
| 48 | + wantReason: "", |
| 49 | + wantMessage: "", |
| 50 | + wantRetryable: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "non-AWS error falls through as retryable", |
| 54 | + err: errors.New("boom"), |
| 55 | + wantReason: "", |
| 56 | + wantMessage: "", |
| 57 | + wantRetryable: true, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "UnauthorizedOperation is terminal with Unauthorized reason", |
| 61 | + err: &apiErr{code: awserrors.UnauthorizedOperationErrorCode, message: "user is not authorized"}, |
| 62 | + wantReason: "Unauthorized", |
| 63 | + wantMessage: "user is not authorized", |
| 64 | + wantRetryable: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "AccessDenied is terminal with Unauthorized reason", |
| 68 | + err: &apiErr{code: awserrors.AccessDeniedErrorCode, message: "explicit deny"}, |
| 69 | + wantReason: "Unauthorized", |
| 70 | + wantMessage: "explicit deny", |
| 71 | + wantRetryable: false, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "AccessDeniedException (IAM shape) is terminal with Unauthorized reason", |
| 75 | + err: &apiErr{code: awserrors.AccessDeniedExceptionErrorCode, message: "iam explicit deny"}, |
| 76 | + wantReason: "Unauthorized", |
| 77 | + wantMessage: "iam explicit deny", |
| 78 | + wantRetryable: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "AuthFailure is terminal with Unauthorized reason", |
| 82 | + err: &apiErr{code: awserrors.AuthFailureErrorCode, message: "auth failure"}, |
| 83 | + wantReason: "Unauthorized", |
| 84 | + wantMessage: "auth failure", |
| 85 | + wantRetryable: false, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "LimitExceeded is terminal with LimitExceeded reason", |
| 89 | + err: &apiErr{code: awserrors.LimitExceededErrorCode, message: "instance profile limit reached"}, |
| 90 | + wantReason: "LimitExceeded", |
| 91 | + wantMessage: "instance profile limit reached", |
| 92 | + wantRetryable: false, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "RequestLimitExceeded (rate limiting) is retryable", |
| 96 | + err: &apiErr{code: awserrors.RateLimitingErrorCode, message: "slow down"}, |
| 97 | + wantReason: "", |
| 98 | + wantMessage: "", |
| 99 | + wantRetryable: true, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "InvalidParameterValue is retryable (transient/config error, not blanket-terminal)", |
| 103 | + err: &apiErr{code: awserrors.RunInstancesInvalidParameterValueCode, message: "invalid"}, |
| 104 | + wantReason: "", |
| 105 | + wantMessage: "", |
| 106 | + wantRetryable: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "wrapped terminal error is still classified as terminal", |
| 110 | + err: fmt.Errorf("listing subnets: %w", &apiErr{code: awserrors.AccessDeniedErrorCode, message: "wrapped"}), |
| 111 | + wantReason: "Unauthorized", |
| 112 | + wantMessage: "wrapped", |
| 113 | + wantRetryable: false, |
| 114 | + }, |
| 115 | + } |
| 116 | + for _, tc := range tests { |
| 117 | + t.Run(tc.name, func(t *testing.T) { |
| 118 | + reason, message, retryable := awserrors.ClassifyError(tc.err) |
| 119 | + if reason != tc.wantReason { |
| 120 | + t.Errorf("reason: got %q, want %q", reason, tc.wantReason) |
| 121 | + } |
| 122 | + if message != tc.wantMessage { |
| 123 | + t.Errorf("message: got %q, want %q", message, tc.wantMessage) |
| 124 | + } |
| 125 | + if retryable != tc.wantRetryable { |
| 126 | + t.Errorf("retryable: got %v, want %v", retryable, tc.wantRetryable) |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments