@@ -247,3 +247,51 @@ func TestGetRegionFromZones(t *testing.T) {
247
247
248
248
}
249
249
}
250
+
251
+ func TestKeyToVolumeID (t * testing.T ) {
252
+ testName := "test-name"
253
+ testZone := "test-zone"
254
+ testProject := "test-project"
255
+ testRegion := "test-region"
256
+
257
+ testCases := []struct {
258
+ name string
259
+ key * meta.Key
260
+ expID string
261
+ expErr bool
262
+ }{
263
+ {
264
+ name : "normal zonal" ,
265
+ key : meta .ZonalKey (testName , testZone ),
266
+ expID : fmt .Sprintf (volIDZoneFmt , testProject , testZone , testName ),
267
+ },
268
+ {
269
+ name : "normal regional" ,
270
+ key : meta .RegionalKey (testName , testRegion ),
271
+ expID : fmt .Sprintf (volIDRegionFmt , testProject , testRegion , testName ),
272
+ },
273
+ {
274
+ name : "malformed / unsupported global" ,
275
+ key : meta .GlobalKey (testName ),
276
+ expErr : true ,
277
+ },
278
+ }
279
+ for _ , tc := range testCases {
280
+ t .Logf ("test case: %s" , tc .name )
281
+ gotID , err := KeyToVolumeID (tc .key , testProject )
282
+ if err == nil && tc .expErr {
283
+ t .Errorf ("Expected error but got none" )
284
+ }
285
+ if err != nil {
286
+ if ! tc .expErr {
287
+ t .Errorf ("Did not expect error but got: %v" , err )
288
+ }
289
+ continue
290
+ }
291
+
292
+ if ! reflect .DeepEqual (gotID , tc .expID ) {
293
+ t .Errorf ("Got ID %v, but expected %v, from volume key %v" , gotID , tc .expID , tc .key )
294
+ }
295
+ }
296
+
297
+ }
0 commit comments