From 32abcf414ef93e36c60e8bff6e30fd73b7d92b59 Mon Sep 17 00:00:00 2001 From: Amim Knabben Date: Tue, 16 Apr 2024 19:01:20 -0300 Subject: [PATCH] Failing closed after maximum retry is achieved to avoid inifite recursion --- pkg/os/volume/api.go | 14 ++++++++++---- pkg/os/volume/api_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 pkg/os/volume/api_test.go diff --git a/pkg/os/volume/api.go b/pkg/os/volume/api.go index a3b7a550..70e8b5cb 100644 --- a/pkg/os/volume/api.go +++ b/pkg/os/volume/api.go @@ -268,7 +268,7 @@ func (VolumeAPI) GetDiskNumberFromVolumeID(volumeID string) (uint32, error) { // GetVolumeIDFromTargetPath - gets the volume ID given a mount point, the function is recursive until it find a volume or errors out func (VolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) { - volumeString, err := getTarget(mount) + volumeString, err := getTarget(mount, 0) if err != nil { 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) { return volumeString, nil } -func getTarget(mount string) (string, error) { +func getTarget(mount string, retry int) (string, error) { cmd := `(Get-Item -Path $Env:mountpath).Target` cmdEnv := fmt.Sprintf("mountpath=%s", mount) out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil || len(out) == 0 { return "", fmt.Errorf("error getting volume from mount. cmd: %s, output: %s, error: %v", cmd, string(out), err) } + if retry >= 256 { + return "", fmt.Errorf("maximum recursion reached, cmd: %s, output: %s, :retry %d", cmd, string(out), retry) + } + volumeString := strings.TrimSpace(string(out)) + klog.V(8).Infof("retry: %d, volumeString: %s", retry, volumeString) + if !strings.HasPrefix(volumeString, "Volume") { - return getTarget(volumeString) + return getTarget(volumeString, retry+1) } return ensureVolumePrefix(volumeString), nil } -// GetVolumeIDFromTargetPath returns the volume id of a given target path. +// GetClosestVolumeIDFromTargetPath returns the volume id of a given target path. func (VolumeAPI) GetClosestVolumeIDFromTargetPath(targetPath string) (string, error) { volumeString, err := findClosestVolume(targetPath) diff --git a/pkg/os/volume/api_test.go b/pkg/os/volume/api_test.go new file mode 100644 index 00000000..23b42778 --- /dev/null +++ b/pkg/os/volume/api_test.go @@ -0,0 +1,32 @@ +package volume + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetTarget(t *testing.T) { + tests := []struct { + mountpath string + expectedResult string + expectError bool + counter int + }{ + { + "c:\\", + "", + true, + 1, + }, + } + for _, test := range tests { + target, err := getTarget(test.mountpath, test.counter) + if test.expectError { + assert.NotNil(t, err, "Expect error during getTarget(%s)", test.mountpath) + } else { + assert.Nil(t, err, "Expect error is nil during getTarget(%s)", test.mountpath) + } + assert.Equal(t, target, test.expectedResult, "Expect result not equal with getTarget(%s) return: %q, expected: %s, error: %v", + test.mountpath, target, test.expectedResult, err) + } +}