Skip to content

Commit f293474

Browse files
authored
Merge pull request #344 from hantaowang/fix-key-to-volume-id-bug
fix a bug where keys are not properly translated to volume id
2 parents b434c09 + 0d77cc4 commit f293474

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

pkg/common/utils.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ func VolumeIDToKey(id string) (*meta.Key, error) {
7676
func KeyToVolumeID(volKey *meta.Key, project string) (string, error) {
7777
switch volKey.Type() {
7878
case meta.Zonal:
79-
return fmt.Sprintf(volIDZonalFmt, project, volKey.Zone, volKey.Zone), nil
79+
return fmt.Sprintf(volIDZonalFmt, project, volKey.Zone, volKey.Name), nil
8080
case meta.Regional:
81-
return fmt.Sprintf(volIDZonalFmt, project, volKey.Region, volKey.Zone), nil
81+
return fmt.Sprintf(volIDRegionalFmt, project, volKey.Region, volKey.Name), nil
8282
default:
83-
return "", fmt.Errorf("volume key %v neither zonal nor regional", volKey.Name)
83+
return "", fmt.Errorf("volume key %v neither zonal nor regional", volKey.String())
8484
}
8585
}
8686

pkg/common/utils_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,51 @@ func TestGetRegionFromZones(t *testing.T) {
247247

248248
}
249249
}
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

Comments
 (0)