sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/errors_test.go (about) 1 /* 2 Copyright 2019 The Kubernetes 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 azure 18 19 import ( 20 "context" 21 "net/http" 22 "testing" 23 "time" 24 25 "github.com/Azure/azure-sdk-for-go/sdk/azcore" 26 "github.com/pkg/errors" 27 ) 28 29 func TestIsContextDeadlineExceededOrCanceled(t *testing.T) { 30 tests := []struct { 31 name string 32 want bool 33 err error 34 }{ 35 { 36 name: "Context deadline exceeded error", 37 err: func() error { 38 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-7*time.Hour)) 39 defer cancel() 40 return ctx.Err() 41 }(), 42 want: true, 43 }, 44 { 45 name: "Context canceled exceeded error", 46 err: func() error { 47 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Hour)) 48 cancel() 49 return ctx.Err() 50 }(), 51 want: true, 52 }, 53 { 54 name: "Nil error", 55 err: nil, 56 want: false, 57 }, 58 { 59 name: "Error other than context deadline exceeded or canceled error", 60 err: errors.New("dummy error"), 61 want: false, 62 }, 63 } 64 for _, tt := range tests { 65 t.Run(tt.name, func(t *testing.T) { 66 if got := IsContextDeadlineExceededOrCanceledError(tt.err); got != tt.want { 67 t.Errorf("IsContextDeadlineExceededOrCanceled() = %v, want %v", got, tt.want) 68 } 69 }) 70 } 71 } 72 73 func TestResourceNotFound(t *testing.T) { 74 tests := []struct { 75 name string 76 err error 77 success bool 78 }{ 79 { 80 name: "Not Found response error", 81 err: &azcore.ResponseError{StatusCode: http.StatusNotFound}, 82 success: true, 83 }, 84 { 85 name: "Conflict response error", 86 err: &azcore.ResponseError{StatusCode: http.StatusConflict}, 87 success: false, 88 }, 89 { 90 name: "Not Found generic error", 91 err: errors.New("404: Not Found"), 92 success: false, 93 }, 94 } 95 for _, tc := range tests { 96 tc := tc 97 t.Run(tc.name, func(t *testing.T) { 98 t.Parallel() 99 if got := ResourceNotFound(tc.err); got != tc.success { 100 t.Errorf("ResourceNotFound() = %v, want %v", got, tc.success) 101 } 102 }) 103 } 104 }