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