github.com/GoogleCloudPlatform/testgrid@v0.0.174/util/gcs/real_gcs_test.go (about) 1 /* 2 Copyright 2022 The TestGrid 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 gcs 18 19 import ( 20 "errors" 21 "net/http" 22 "testing" 23 24 "cloud.google.com/go/storage" 25 "github.com/google/go-cmp/cmp" 26 "google.golang.org/api/googleapi" 27 ) 28 29 func TestWrapGoogleAPIError(t *testing.T) { 30 create := func(code int) error { 31 var e googleapi.Error 32 e.Code = code 33 return &e 34 } 35 cases := []struct { 36 name string 37 err error 38 want string 39 is error 40 }{ 41 { 42 name: "nil", 43 }, 44 { 45 name: "basic", 46 err: errors.New("hi"), 47 want: errors.New("hi").Error(), 48 }, 49 { 50 name: "random code", 51 err: create(http.StatusInternalServerError), 52 want: create(http.StatusInternalServerError).Error(), 53 }, 54 { 55 name: "404", 56 err: create(http.StatusNotFound), 57 want: create(http.StatusNotFound).Error(), 58 is: storage.ErrObjectNotExist, 59 }, 60 } 61 62 for _, tc := range cases { 63 t.Run(tc.name, func(t *testing.T) { 64 gotErr := wrapGoogleAPIError(tc.err) 65 var got string 66 if gotErr != nil { 67 got = gotErr.Error() 68 } 69 if diff := cmp.Diff(tc.want, got); diff != "" { 70 t.Errorf("wrapGoogleAPIError() got unexpected diff (-want +got):\n%s", diff) 71 } 72 if tc.is != nil && !errors.Is(gotErr, tc.is) { 73 t.Errorf("errors.Is(%v, %v) returned false", gotErr, tc.is) 74 } 75 }) 76 } 77 }