Skip to content

Commit 67871ec

Browse files
authored
Merge pull request #336 from knabben/master
Failing closed after maximum retry is achieved to avoid inf recursion
2 parents be8bec7 + 32abcf4 commit 67871ec

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

pkg/os/volume/api.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (VolumeAPI) GetDiskNumberFromVolumeID(volumeID string) (uint32, error) {
268268

269269
// GetVolumeIDFromTargetPath - gets the volume ID given a mount point, the function is recursive until it find a volume or errors out
270270
func (VolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
271-
volumeString, err := getTarget(mount)
271+
volumeString, err := getTarget(mount, 0)
272272

273273
if err != nil {
274274
return "", fmt.Errorf("error getting the volume for the mount %s, internal error %v", mount, err)
@@ -277,22 +277,28 @@ func (VolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
277277
return volumeString, nil
278278
}
279279

280-
func getTarget(mount string) (string, error) {
280+
func getTarget(mount string, retry int) (string, error) {
281281
cmd := `(Get-Item -Path $Env:mountpath).Target`
282282
cmdEnv := fmt.Sprintf("mountpath=%s", mount)
283283
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
284284
if err != nil || len(out) == 0 {
285285
return "", fmt.Errorf("error getting volume from mount. cmd: %s, output: %s, error: %v", cmd, string(out), err)
286286
}
287+
if retry >= 256 {
288+
return "", fmt.Errorf("maximum recursion reached, cmd: %s, output: %s, :retry %d", cmd, string(out), retry)
289+
}
290+
287291
volumeString := strings.TrimSpace(string(out))
292+
klog.V(8).Infof("retry: %d, volumeString: %s", retry, volumeString)
293+
288294
if !strings.HasPrefix(volumeString, "Volume") {
289-
return getTarget(volumeString)
295+
return getTarget(volumeString, retry+1)
290296
}
291297

292298
return ensureVolumePrefix(volumeString), nil
293299
}
294300

295-
// GetVolumeIDFromTargetPath returns the volume id of a given target path.
301+
// GetClosestVolumeIDFromTargetPath returns the volume id of a given target path.
296302
func (VolumeAPI) GetClosestVolumeIDFromTargetPath(targetPath string) (string, error) {
297303
volumeString, err := findClosestVolume(targetPath)
298304

pkg/os/volume/api_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package volume
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestGetTarget(t *testing.T) {
9+
tests := []struct {
10+
mountpath string
11+
expectedResult string
12+
expectError bool
13+
counter int
14+
}{
15+
{
16+
"c:\\",
17+
"",
18+
true,
19+
1,
20+
},
21+
}
22+
for _, test := range tests {
23+
target, err := getTarget(test.mountpath, test.counter)
24+
if test.expectError {
25+
assert.NotNil(t, err, "Expect error during getTarget(%s)", test.mountpath)
26+
} else {
27+
assert.Nil(t, err, "Expect error is nil during getTarget(%s)", test.mountpath)
28+
}
29+
assert.Equal(t, target, test.expectedResult, "Expect result not equal with getTarget(%s) return: %q, expected: %s, error: %v",
30+
test.mountpath, target, test.expectedResult, err)
31+
}
32+
}

0 commit comments

Comments
 (0)