@@ -18,6 +18,7 @@ import (
18
18
"testing"
19
19
20
20
computev1 "google.golang.org/api/compute/v1"
21
+ "google.golang.org/grpc/codes"
21
22
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
22
23
)
23
24
@@ -101,3 +102,75 @@ func TestValidateDiskParameters(t *testing.T) {
101
102
}
102
103
}
103
104
}
105
+
106
+ func TestCodeForGCEOpError (t * testing.T ) {
107
+ testCases := []struct {
108
+ name string
109
+ inputErr computev1.OperationErrorErrors
110
+ expCode codes.Code
111
+ }{
112
+ {
113
+ name : "RESOURCE_NOT_FOUND error" ,
114
+ inputErr : computev1.OperationErrorErrors {Code : "RESOURCE_NOT_FOUND" },
115
+ expCode : codes .NotFound ,
116
+ },
117
+ {
118
+ name : "RESOURCE_ALREADY_EXISTS error" ,
119
+ inputErr : computev1.OperationErrorErrors {Code : "RESOURCE_ALREADY_EXISTS" },
120
+ expCode : codes .AlreadyExists ,
121
+ },
122
+ {
123
+ name : "OPERATION_CANCELED_BY_USER error" ,
124
+ inputErr : computev1.OperationErrorErrors {Code : "OPERATION_CANCELED_BY_USER" },
125
+ expCode : codes .Aborted ,
126
+ },
127
+ {
128
+ name : "QUOTA_EXCEEDED error" ,
129
+ inputErr : computev1.OperationErrorErrors {Code : "QUOTA_EXCEEDED" },
130
+ expCode : codes .ResourceExhausted ,
131
+ },
132
+ {
133
+ name : "ZONE_RESOURCE_POOL_EXHAUSTED error" ,
134
+ inputErr : computev1.OperationErrorErrors {Code : "ZONE_RESOURCE_POOL_EXHAUSTED" },
135
+ expCode : codes .Unavailable ,
136
+ },
137
+ {
138
+ name : "ZONE_RESOURCE_POOL_EXHAUSTED_WITH_DETAILS error" ,
139
+ inputErr : computev1.OperationErrorErrors {Code : "ZONE_RESOURCE_POOL_EXHAUSTED_WITH_DETAILS" },
140
+ expCode : codes .Unavailable ,
141
+ },
142
+ {
143
+ name : "REGION_QUOTA_EXCEEDED error" ,
144
+ inputErr : computev1.OperationErrorErrors {Code : "REGION_QUOTA_EXCEEDED" },
145
+ expCode : codes .ResourceExhausted ,
146
+ },
147
+ {
148
+ name : "RATE_LIMIT_EXCEEDED error" ,
149
+ inputErr : computev1.OperationErrorErrors {Code : "RATE_LIMIT_EXCEEDED" },
150
+ expCode : codes .ResourceExhausted ,
151
+ },
152
+ {
153
+ name : "INVALID_USAGE error" ,
154
+ inputErr : computev1.OperationErrorErrors {Code : "INVALID_USAGE" },
155
+ expCode : codes .InvalidArgument ,
156
+ },
157
+ {
158
+ name : "RESOURCE_IN_USE_BY_ANOTHER_RESOURCE error" ,
159
+ inputErr : computev1.OperationErrorErrors {Code : "RESOURCE_IN_USE_BY_ANOTHER_RESOURCE" },
160
+ expCode : codes .InvalidArgument ,
161
+ },
162
+ {
163
+ name : "UNSUPPORTED_OPERATION error" ,
164
+ inputErr : computev1.OperationErrorErrors {Code : "UNSUPPORTED_OPERATION" },
165
+ expCode : codes .Internal ,
166
+ },
167
+ }
168
+
169
+ for _ , tc := range testCases {
170
+ t .Logf ("Running test: %v" , tc .name )
171
+ errCode := codeForGCEOpError (tc .inputErr )
172
+ if errCode != tc .expCode {
173
+ t .Errorf ("test %v failed: got %v, expected %v" , tc .name , errCode , tc .expCode )
174
+ }
175
+ }
176
+ }
0 commit comments