...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp/errors_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gcp_test
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp"
    22  	"google.golang.org/api/googleapi"
    23  )
    24  
    25  func TestIsNotFoundError(t *testing.T) {
    26  	testCases := []struct {
    27  		Name           string
    28  		Error          error
    29  		ExpectedResult bool
    30  	}{
    31  		{"GCP NotFound", &googleapi.Error{Code: 404}, true},
    32  		{"GCP NotAuthorized", &googleapi.Error{Code: 403}, false},
    33  		{"Generic", fmt.Errorf("my generic error"), false},
    34  	}
    35  	for _, tc := range testCases {
    36  		t.Run(tc.Name, func(t *testing.T) {
    37  			result := gcp.IsNotFoundError(tc.Error)
    38  			if result != tc.ExpectedResult {
    39  				t.Errorf("unexpected result for gcp.IsNotFoundError('%v'): got '%v', want '%v'", tc.Error, result, tc.ExpectedResult)
    40  			}
    41  		})
    42  	}
    43  }
    44  
    45  func TestIsNotAuthorizedError(t *testing.T) {
    46  	testCases := []struct {
    47  		Name           string
    48  		Error          error
    49  		ExpectedResult bool
    50  	}{
    51  		{"GCP NotAuthorized", &googleapi.Error{Code: 403}, true},
    52  		{"GCP NotFound", &googleapi.Error{Code: 404}, false},
    53  		{"Generic", fmt.Errorf("my generic error"), false},
    54  	}
    55  	for _, tc := range testCases {
    56  		t.Run(tc.Name, func(t *testing.T) {
    57  			result := gcp.IsNotAuthorizedError(tc.Error)
    58  			if result != tc.ExpectedResult {
    59  				t.Errorf("unexpected result for gcp.IsNotAuthorizedError('%v'): got '%v', want '%v'", tc.Error, result, tc.ExpectedResult)
    60  			}
    61  		})
    62  	}
    63  }
    64  

View as plain text