@@ -30,6 +30,8 @@ import (
30
30
31
31
"google.golang.org/api/compute/v1"
32
32
"google.golang.org/api/googleapi"
33
+ "google.golang.org/grpc/codes"
34
+ "google.golang.org/grpc/status"
33
35
)
34
36
35
37
type mockTokenSource struct {}
@@ -101,6 +103,114 @@ func TestIsGCEError(t *testing.T) {
101
103
}
102
104
}
103
105
106
+ func TestErrorContainsRegex (t * testing.T ) {
107
+ testCases := []struct {
108
+ name string
109
+ inputErr error
110
+ regex string
111
+ expectedResult bool
112
+ }{
113
+ {
114
+ name : "regex found in error message" ,
115
+ inputErr : errors .New ("The resource 'my-resource' already exists" ),
116
+ regex : "The resource '[^']+' already exists" ,
117
+ expectedResult : true ,
118
+ },
119
+ {
120
+ name : "wrapped valid error message" ,
121
+ inputErr : status .Errorf (codes .AlreadyExists , "Snapshot already exists: %v" , errors .New ("The resource 'my-resource' already exists" )),
122
+ regex : "The resource '[^']+' already exists" ,
123
+ expectedResult : true ,
124
+ },
125
+ {
126
+ name : "regex not found in error message" ,
127
+ inputErr : errors .New ("The resource 'my-resource' does not exist" ),
128
+ regex : "The resource '[^']+' already exists" ,
129
+ expectedResult : false ,
130
+ },
131
+ {
132
+ name : "empty" ,
133
+ inputErr : errors .New (" " ),
134
+ regex : "The resource '[^']+' already exists" ,
135
+ expectedResult : false ,
136
+ },
137
+ {
138
+ name : "empty 2" ,
139
+ inputErr : errors .New ("" ),
140
+ regex : "The resource '[^']+' already exists" ,
141
+ expectedResult : false ,
142
+ },
143
+ {
144
+ name : "nil" ,
145
+ inputErr : nil ,
146
+ regex : "The resource '[^']+' already exists" ,
147
+ expectedResult : false ,
148
+ },
149
+ }
150
+
151
+ for _ , tc := range testCases {
152
+ t .Logf ("Running test: %v" , tc .name )
153
+ result := ErrorContainsRegex (tc .inputErr , tc .regex )
154
+ if tc .expectedResult != result {
155
+ t .Fatalf ("Got '%t', expected '%t'" , result , tc .expectedResult )
156
+ }
157
+ }
158
+ }
159
+
160
+ func TestErrorIsGCPViolationRegex (t * testing.T ) {
161
+ testCases := []struct {
162
+ name string
163
+ inputErr error
164
+ expectedResult bool
165
+ }{
166
+ {
167
+ name : "is gcp org violation error" ,
168
+ inputErr : errors .New ("Your api request violates constraint constraints/gcp.resourceLocations" ),
169
+ expectedResult : true ,
170
+ },
171
+ {
172
+ name : "is not gcp org violation error" ,
173
+ inputErr : errors .New ("Some incorrect error message" ),
174
+ expectedResult : false ,
175
+ },
176
+ }
177
+
178
+ for _ , tc := range testCases {
179
+ t .Logf ("Running test: %v" , tc .name )
180
+ result := IsGCPOrgViolationError (tc .inputErr )
181
+ if tc .expectedResult != result {
182
+ t .Fatalf ("Got '%t', expected '%t'" , result , tc .expectedResult )
183
+ }
184
+ }
185
+ }
186
+
187
+ func TestErrorIsSnapshotExistsError (t * testing.T ) {
188
+ testCases := []struct {
189
+ name string
190
+ inputErr error
191
+ expectedResult bool
192
+ }{
193
+ {
194
+ name : "is gcp org violation error" ,
195
+ inputErr : errors .New ("Your api request violates constraint constraints/gcp.resourceLocations" ),
196
+ expectedResult : true ,
197
+ },
198
+ {
199
+ name : "is not gcp org violation error" ,
200
+ inputErr : errors .New ("Some incorrect error message" ),
201
+ expectedResult : false ,
202
+ },
203
+ }
204
+
205
+ for _ , tc := range testCases {
206
+ t .Logf ("Running test: %v" , tc .name )
207
+ result := IsGCPOrgViolationError (tc .inputErr )
208
+ if tc .expectedResult != result {
209
+ t .Fatalf ("Got '%t', expected '%t'" , result , tc .expectedResult )
210
+ }
211
+ }
212
+ }
213
+
104
214
func TestGetComputeVersion (t * testing.T ) {
105
215
testCases := []struct {
106
216
name string
0 commit comments