@@ -17,11 +17,17 @@ limitations under the License.
17
17
package common
18
18
19
19
import (
20
+ "context"
21
+ "errors"
20
22
"fmt"
23
+ "net/http"
21
24
"reflect"
22
25
"testing"
23
26
24
27
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
28
+ "google.golang.org/api/googleapi"
29
+ "google.golang.org/grpc/codes"
30
+ "google.golang.org/grpc/status"
25
31
)
26
32
27
33
const (
@@ -847,3 +853,166 @@ func TestConvertMiBStringToInt64(t *testing.T) {
847
853
})
848
854
}
849
855
}
856
+ << << << < HEAD
857
+ == == == =
858
+
859
+ func TestParseMachineType (t * testing.T ) {
860
+ tests := []struct {
861
+ desc string
862
+ inputMachineTypeUrl string
863
+ expectedMachineType string
864
+ expectError bool
865
+ }{
866
+ {
867
+ desc : "full URL machine type" ,
868
+ inputMachineTypeUrl : "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4" ,
869
+ expectedMachineType : "c3-highcpu-4" ,
870
+ },
871
+ {
872
+ desc : "partial URL machine type" ,
873
+ inputMachineTypeUrl : "zones/us-central1-c/machineTypes/n2-standard-4" ,
874
+ expectedMachineType : "n2-standard-4" ,
875
+ },
876
+ {
877
+ desc : "custom partial URL machine type" ,
878
+ inputMachineTypeUrl : "zones/us-central1-c/machineTypes/e2-custom-2-4096" ,
879
+ expectedMachineType : "e2-custom-2-4096" ,
880
+ },
881
+ {
882
+ desc : "incorrect URL" ,
883
+ inputMachineTypeUrl : "https://www.googleapis.com/compute/v1/projects/psch-gke-dev/zones/us-central1-c" ,
884
+ expectError : true ,
885
+ },
886
+ {
887
+ desc : "incorrect partial URL" ,
888
+ inputMachineTypeUrl : "zones/us-central1-c/machineTypes/" ,
889
+ expectError : true ,
890
+ },
891
+ {
892
+ desc : "missing zone" ,
893
+ inputMachineTypeUrl : "zones//machineTypes/n2-standard-4" ,
894
+ expectError : true ,
895
+ },
896
+ }
897
+ for _ , tc := range tests {
898
+ t .Run (tc .desc , func (t * testing.T ) {
899
+ actualMachineFamily , err := ParseMachineType (tc .inputMachineTypeUrl )
900
+ if err != nil && ! tc .expectError {
901
+ t .Errorf ("Got error %v parsing machine type %s; expect no error" , err , tc .inputMachineTypeUrl )
902
+ }
903
+ if err == nil && tc .expectError {
904
+ t .Errorf ("Got no error parsing machine type %s; expect an error" , tc .inputMachineTypeUrl )
905
+ }
906
+ if err == nil && actualMachineFamily != tc .expectedMachineType {
907
+ t .Errorf ("Got %s parsing machine type; expect %s" , actualMachineFamily , tc .expectedMachineType )
908
+ }
909
+ })
910
+ }
911
+ }
912
+
913
+ func TestCodeForError (t * testing.T ) {
914
+ internalErrorCode := codes .Internal
915
+ userErrorCode := codes .InvalidArgument
916
+ testCases := []struct {
917
+ name string
918
+ inputErr error
919
+ expCode * codes.Code
920
+ }{
921
+ {
922
+ name : "Not googleapi.Error" ,
923
+ inputErr : errors .New ("I am not a googleapi.Error" ),
924
+ expCode : & internalErrorCode ,
925
+ },
926
+ {
927
+ name : "User error" ,
928
+ inputErr : & googleapi.Error {Code : http .StatusBadRequest , Message : "User error with bad request" },
929
+ expCode : & userErrorCode ,
930
+ },
931
+ {
932
+ name : "googleapi.Error but not a user error" ,
933
+ inputErr : & googleapi.Error {Code : http .StatusInternalServerError , Message : "Internal error" },
934
+ expCode : & internalErrorCode ,
935
+ },
936
+ {
937
+ name : "context canceled error" ,
938
+ inputErr : context .Canceled ,
939
+ expCode : errCodePtr (codes .Canceled ),
940
+ },
941
+ {
942
+ name : "context deadline exceeded error" ,
943
+ inputErr : context .DeadlineExceeded ,
944
+ expCode : errCodePtr (codes .DeadlineExceeded ),
945
+ },
946
+ {
947
+ name : "status error with Aborted error code" ,
948
+ inputErr : status .Error (codes .Aborted , "aborted error" ),
949
+ expCode : errCodePtr (codes .Aborted ),
950
+ },
951
+ {
952
+ name : "nil error" ,
953
+ inputErr : nil ,
954
+ expCode : nil ,
955
+ },
956
+ }
957
+
958
+ for _ , tc := range testCases {
959
+ t .Logf ("Running test: %v" , tc .name )
960
+ errCode := CodeForError (tc .inputErr )
961
+ if (tc .expCode == nil ) != (errCode == nil ) {
962
+ t .Errorf ("test %v failed: got %v, expected %v" , tc .name , errCode , tc .expCode )
963
+ }
964
+ if tc .expCode != nil && * errCode != * tc .expCode {
965
+ t .Errorf ("test %v failed: got %v, expected %v" , tc .name , errCode , tc .expCode )
966
+ }
967
+ }
968
+ }
969
+
970
+ func TestIsContextError (t * testing.T ) {
971
+ cases := []struct {
972
+ name string
973
+ err error
974
+ expectedErrCode * codes.Code
975
+ }{
976
+ {
977
+ name : "deadline exceeded error" ,
978
+ err : context .DeadlineExceeded ,
979
+ expectedErrCode : errCodePtr (codes .DeadlineExceeded ),
980
+ },
981
+ {
982
+ name : "contains 'context deadline exceeded'" ,
983
+ err : fmt .Errorf ("got error: %w" , context .DeadlineExceeded ),
984
+ expectedErrCode : errCodePtr (codes .DeadlineExceeded ),
985
+ },
986
+ {
987
+ name : "context canceled error" ,
988
+ err : context .Canceled ,
989
+ expectedErrCode : errCodePtr (codes .Canceled ),
990
+ },
991
+ {
992
+ name : "contains 'context canceled'" ,
993
+ err : fmt .Errorf ("got error: %w" , context .Canceled ),
994
+ expectedErrCode : errCodePtr (codes .Canceled ),
995
+ },
996
+ {
997
+ name : "does not contain 'context canceled' or 'context deadline exceeded'" ,
998
+ err : fmt .Errorf ("unknown error" ),
999
+ expectedErrCode : nil ,
1000
+ },
1001
+ {
1002
+ name : "nil error" ,
1003
+ err : nil ,
1004
+ expectedErrCode : nil ,
1005
+ },
1006
+ }
1007
+
1008
+ for _ , test := range cases {
1009
+ errCode := isContextError (test .err )
1010
+ if (test .expectedErrCode == nil ) != (errCode == nil ) {
1011
+ t .Errorf ("test %v failed: got %v, expected %v" , test .name , errCode , test .expectedErrCode )
1012
+ }
1013
+ if test .expectedErrCode != nil && * errCode != * test .expectedErrCode {
1014
+ t .Errorf ("test %v failed: got %v, expected %v" , test .name , errCode , test .expectedErrCode )
1015
+ }
1016
+ }
1017
+ }
1018
+ >> >> >> > Adding new metric pdcsi_operation_errors to fetch error count
0 commit comments