|
| 1 | +/* |
| 2 | +Copyright The ORC Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package errors |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/gophercloud/gophercloud/v2" |
| 25 | +) |
| 26 | + |
| 27 | +func newHTTPError(statusCode int, body string) error { |
| 28 | + return gophercloud.ErrUnexpectedResponseCode{ |
| 29 | + Actual: statusCode, |
| 30 | + Body: []byte(body), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestIsRetryable(t *testing.T) { |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + err error |
| 38 | + want bool |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "nil error is not retryable", |
| 42 | + err: nil, |
| 43 | + want: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "non-HTTP error is not retryable", |
| 47 | + err: fmt.Errorf("some client-side validation error"), |
| 48 | + want: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "409 Conflict is not retryable", |
| 52 | + err: newHTTPError(http.StatusConflict, `{"NeutronError": {"type": "IpAddressInUse"}}`), |
| 53 | + want: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "409 Conflict with Neutron OverQuota is retryable", |
| 57 | + err: newHTTPError(http.StatusConflict, `{"NeutronError": {"type": "OverQuota", "message": "Quota exceeded for resources: port."}}`), |
| 58 | + want: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "501 Not Implemented is not retryable", |
| 62 | + err: newHTTPError(http.StatusNotImplemented, ""), |
| 63 | + want: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "400 Bad Request is retryable", |
| 67 | + err: newHTTPError(http.StatusBadRequest, `{"NeutronError": {"type": "BadRequest"}}`), |
| 68 | + want: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "403 Forbidden is retryable", |
| 72 | + err: newHTTPError(http.StatusForbidden, ""), |
| 73 | + want: true, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "500 Internal Server Error is retryable", |
| 77 | + err: newHTTPError(http.StatusInternalServerError, ""), |
| 78 | + want: true, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "503 Service Unavailable is retryable", |
| 82 | + err: newHTTPError(http.StatusServiceUnavailable, ""), |
| 83 | + want: true, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "wrapped non-HTTP error is not retryable", |
| 87 | + err: fmt.Errorf("wrapping: %w", fmt.Errorf("banned key")), |
| 88 | + want: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "wrapped 409 is not retryable", |
| 92 | + err: fmt.Errorf("wrapping: %w", newHTTPError(http.StatusConflict, "")), |
| 93 | + want: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "wrapped 409 with OverQuota is retryable", |
| 97 | + err: fmt.Errorf("wrapping: %w", newHTTPError(http.StatusConflict, `{"NeutronError": {"type": "OverQuota"}}`)), |
| 98 | + want: true, |
| 99 | + }, |
| 100 | + } |
| 101 | + for _, tt := range tests { |
| 102 | + t.Run(tt.name, func(t *testing.T) { |
| 103 | + if got := IsRetryable(tt.err); got != tt.want { |
| 104 | + t.Errorf("IsRetryable() = %v, want %v", got, tt.want) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments