@@ -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 (
@@ -578,60 +584,6 @@ func TestSnapshotStorageLocations(t *testing.T) {
578
584
}
579
585
}
580
586
581
- func TestParseMachineType (t * testing.T ) {
582
- tests := []struct {
583
- desc string
584
- inputMachineTypeUrl string
585
- expectedMachineType string
586
- expectError bool
587
- }{
588
- {
589
- desc : "full URL machine type" ,
590
- inputMachineTypeUrl : "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4" ,
591
- expectedMachineType : "c3-highcpu-4" ,
592
- },
593
- {
594
- desc : "partial URL machine type" ,
595
- inputMachineTypeUrl : "zones/us-central1-c/machineTypes/n2-standard-4" ,
596
- expectedMachineType : "n2-standard-4" ,
597
- },
598
- {
599
- desc : "custom partial URL machine type" ,
600
- inputMachineTypeUrl : "zones/us-central1-c/machineTypes/e2-custom-2-4096" ,
601
- expectedMachineType : "e2-custom-2-4096" ,
602
- },
603
- {
604
- desc : "incorrect URL" ,
605
- inputMachineTypeUrl : "https://www.googleapis.com/compute/v1/projects/psch-gke-dev/zones/us-central1-c" ,
606
- expectError : true ,
607
- },
608
- {
609
- desc : "incorrect partial URL" ,
610
- inputMachineTypeUrl : "zones/us-central1-c/machineTypes/" ,
611
- expectError : true ,
612
- },
613
- {
614
- desc : "missing zone" ,
615
- inputMachineTypeUrl : "zones//machineTypes/n2-standard-4" ,
616
- expectError : true ,
617
- },
618
- }
619
- for _ , tc := range tests {
620
- t .Run (tc .desc , func (t * testing.T ) {
621
- actualMachineFamily , err := ParseMachineType (tc .inputMachineTypeUrl )
622
- if err != nil && ! tc .expectError {
623
- t .Errorf ("Got error %v parsing machine type %s; expect no error" , err , tc .inputMachineTypeUrl )
624
- }
625
- if err == nil && tc .expectError {
626
- t .Errorf ("Got no error parsing machine type %s; expect an error" , tc .inputMachineTypeUrl )
627
- }
628
- if err == nil && actualMachineFamily != tc .expectedMachineType {
629
- t .Errorf ("Got %s parsing machine type; expect %s" , actualMachineFamily , tc .expectedMachineType )
630
- }
631
- })
632
- }
633
- }
634
-
635
587
func TestConvertStringToInt64 (t * testing.T ) {
636
588
tests := []struct {
637
589
desc string
@@ -853,3 +805,163 @@ func TestConvertMiStringToInt64(t *testing.T) {
853
805
})
854
806
}
855
807
}
808
+
809
+ func TestParseMachineType (t * testing.T ) {
810
+ tests := []struct {
811
+ desc string
812
+ inputMachineTypeUrl string
813
+ expectedMachineType string
814
+ expectError bool
815
+ }{
816
+ {
817
+ desc : "full URL machine type" ,
818
+ inputMachineTypeUrl : "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/machineTypes/c3-highcpu-4" ,
819
+ expectedMachineType : "c3-highcpu-4" ,
820
+ },
821
+ {
822
+ desc : "partial URL machine type" ,
823
+ inputMachineTypeUrl : "zones/us-central1-c/machineTypes/n2-standard-4" ,
824
+ expectedMachineType : "n2-standard-4" ,
825
+ },
826
+ {
827
+ desc : "custom partial URL machine type" ,
828
+ inputMachineTypeUrl : "zones/us-central1-c/machineTypes/e2-custom-2-4096" ,
829
+ expectedMachineType : "e2-custom-2-4096" ,
830
+ },
831
+ {
832
+ desc : "incorrect URL" ,
833
+ inputMachineTypeUrl : "https://www.googleapis.com/compute/v1/projects/psch-gke-dev/zones/us-central1-c" ,
834
+ expectError : true ,
835
+ },
836
+ {
837
+ desc : "incorrect partial URL" ,
838
+ inputMachineTypeUrl : "zones/us-central1-c/machineTypes/" ,
839
+ expectError : true ,
840
+ },
841
+ {
842
+ desc : "missing zone" ,
843
+ inputMachineTypeUrl : "zones//machineTypes/n2-standard-4" ,
844
+ expectError : true ,
845
+ },
846
+ }
847
+ for _ , tc := range tests {
848
+ t .Run (tc .desc , func (t * testing.T ) {
849
+ actualMachineFamily , err := ParseMachineType (tc .inputMachineTypeUrl )
850
+ if err != nil && ! tc .expectError {
851
+ t .Errorf ("Got error %v parsing machine type %s; expect no error" , err , tc .inputMachineTypeUrl )
852
+ }
853
+ if err == nil && tc .expectError {
854
+ t .Errorf ("Got no error parsing machine type %s; expect an error" , tc .inputMachineTypeUrl )
855
+ }
856
+ if err == nil && actualMachineFamily != tc .expectedMachineType {
857
+ t .Errorf ("Got %s parsing machine type; expect %s" , actualMachineFamily , tc .expectedMachineType )
858
+ }
859
+ })
860
+ }
861
+ }
862
+
863
+ func TestCodeForError (t * testing.T ) {
864
+ internalErrorCode := codes .Internal
865
+ userErrorCode := codes .InvalidArgument
866
+ testCases := []struct {
867
+ name string
868
+ inputErr error
869
+ expCode * codes.Code
870
+ }{
871
+ {
872
+ name : "Not googleapi.Error" ,
873
+ inputErr : errors .New ("I am not a googleapi.Error" ),
874
+ expCode : & internalErrorCode ,
875
+ },
876
+ {
877
+ name : "User error" ,
878
+ inputErr : & googleapi.Error {Code : http .StatusBadRequest , Message : "User error with bad request" },
879
+ expCode : & userErrorCode ,
880
+ },
881
+ {
882
+ name : "googleapi.Error but not a user error" ,
883
+ inputErr : & googleapi.Error {Code : http .StatusInternalServerError , Message : "Internal error" },
884
+ expCode : & internalErrorCode ,
885
+ },
886
+ {
887
+ name : "context canceled error" ,
888
+ inputErr : context .Canceled ,
889
+ expCode : errCodePtr (codes .Canceled ),
890
+ },
891
+ {
892
+ name : "context deadline exceeded error" ,
893
+ inputErr : context .DeadlineExceeded ,
894
+ expCode : errCodePtr (codes .DeadlineExceeded ),
895
+ },
896
+ {
897
+ name : "status error with Aborted error code" ,
898
+ inputErr : status .Error (codes .Aborted , "aborted error" ),
899
+ expCode : errCodePtr (codes .Aborted ),
900
+ },
901
+ {
902
+ name : "nil error" ,
903
+ inputErr : nil ,
904
+ expCode : nil ,
905
+ },
906
+ }
907
+
908
+ for _ , tc := range testCases {
909
+ t .Logf ("Running test: %v" , tc .name )
910
+ errCode := CodeForError (tc .inputErr )
911
+ if (tc .expCode == nil ) != (errCode == nil ) {
912
+ t .Errorf ("test %v failed: got %v, expected %v" , tc .name , errCode , tc .expCode )
913
+ }
914
+ if tc .expCode != nil && * errCode != * tc .expCode {
915
+ t .Errorf ("test %v failed: got %v, expected %v" , tc .name , errCode , tc .expCode )
916
+ }
917
+ }
918
+ }
919
+
920
+ func TestIsContextError (t * testing.T ) {
921
+ cases := []struct {
922
+ name string
923
+ err error
924
+ expectedErrCode * codes.Code
925
+ }{
926
+ {
927
+ name : "deadline exceeded error" ,
928
+ err : context .DeadlineExceeded ,
929
+ expectedErrCode : errCodePtr (codes .DeadlineExceeded ),
930
+ },
931
+ {
932
+ name : "contains 'context deadline exceeded'" ,
933
+ err : fmt .Errorf ("got error: %w" , context .DeadlineExceeded ),
934
+ expectedErrCode : errCodePtr (codes .DeadlineExceeded ),
935
+ },
936
+ {
937
+ name : "context canceled error" ,
938
+ err : context .Canceled ,
939
+ expectedErrCode : errCodePtr (codes .Canceled ),
940
+ },
941
+ {
942
+ name : "contains 'context canceled'" ,
943
+ err : fmt .Errorf ("got error: %w" , context .Canceled ),
944
+ expectedErrCode : errCodePtr (codes .Canceled ),
945
+ },
946
+ {
947
+ name : "does not contain 'context canceled' or 'context deadline exceeded'" ,
948
+ err : fmt .Errorf ("unknown error" ),
949
+ expectedErrCode : nil ,
950
+ },
951
+ {
952
+ name : "nil error" ,
953
+ err : nil ,
954
+ expectedErrCode : nil ,
955
+ },
956
+ }
957
+
958
+ for _ , test := range cases {
959
+ errCode := isContextError (test .err )
960
+ if (test .expectedErrCode == nil ) != (errCode == nil ) {
961
+ t .Errorf ("test %v failed: got %v, expected %v" , test .name , errCode , test .expectedErrCode )
962
+ }
963
+ if test .expectedErrCode != nil && * errCode != * test .expectedErrCode {
964
+ t .Errorf ("test %v failed: got %v, expected %v" , test .name , errCode , test .expectedErrCode )
965
+ }
966
+ }
967
+ }
0 commit comments