|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package gcecloudprovider |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "google.golang.org/api/googleapi" |
| 27 | +) |
| 28 | + |
| 29 | +func TestIsGCEError(t *testing.T) { |
| 30 | + testCases := []struct { |
| 31 | + name string |
| 32 | + inputErr error |
| 33 | + reason string |
| 34 | + expIsGCEError bool |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "Not googleapi.Error", |
| 38 | + inputErr: errors.New("I am not a googleapi.Error"), |
| 39 | + reason: "notFound", |
| 40 | + expIsGCEError: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "googleapi.Error not found error", |
| 44 | + inputErr: &googleapi.Error{ |
| 45 | + Code: http.StatusNotFound, |
| 46 | + Errors: []googleapi.ErrorItem{ |
| 47 | + { |
| 48 | + Reason: "notFound", |
| 49 | + }, |
| 50 | + }, |
| 51 | + Message: "Not found", |
| 52 | + }, |
| 53 | + reason: "notFound", |
| 54 | + expIsGCEError: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "wrapped googleapi.Error", |
| 58 | + inputErr: fmt.Errorf("encountered not found: %w", &googleapi.Error{ |
| 59 | + Code: http.StatusNotFound, |
| 60 | + Errors: []googleapi.ErrorItem{ |
| 61 | + { |
| 62 | + Reason: "notFound", |
| 63 | + }, |
| 64 | + }, |
| 65 | + Message: "Not found", |
| 66 | + }, |
| 67 | + ), |
| 68 | + reason: "notFound", |
| 69 | + expIsGCEError: true, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "nil error", |
| 73 | + inputErr: nil, |
| 74 | + reason: "notFound", |
| 75 | + expIsGCEError: false, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, tc := range testCases { |
| 80 | + t.Logf("Running test: %v", tc.name) |
| 81 | + isGCEError := IsGCEError(tc.inputErr, tc.reason) |
| 82 | + if tc.expIsGCEError != isGCEError { |
| 83 | + t.Fatalf("Got isGCEError '%t', expected '%t'", isGCEError, tc.expIsGCEError) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments