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