Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the implementation for IsBlockDevice method in CSI Driver for Windows #4224

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions ecs-agent/daemonimages/csidriver/util/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,56 @@ limitations under the License.

package util

import "errors"
import (
"errors"
"fmt"
"strings"
"unsafe"

// IsBlockDevice checks if the given path is a block device
"golang.org/x/sys/windows"
)

// Define functions from Windows API
var (
// We will define them here so that they are loaded by default for Windows
// instead of loading them on each invocation of this method.
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetVolumeNameForVolumeMountPointW = kernel32.NewProc("GetVolumeNameForVolumeMountPointW")

// funcGetVolumeNameForVolumeMountPointW is the GetVolumeNameForVolumeMountPointW Win32 API.
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumenameforvolumemountpointw
funcGetVolumeNameForVolumeMountPointW func(a ...uintptr) (r1 uintptr, r2 uintptr, lastErr error) = procGetVolumeNameForVolumeMountPointW.Call
)

// IsBlockDevice checks if the given path is a block device on Windows.
func IsBlockDevice(fullPath string) (bool, error) {
// TODO
return false, errors.New("not supported")
// Ensure the fullPath ends with a backslash.
// This is as per the API documentation- https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumenameforvolumemountpointw#parameters
if !strings.HasSuffix(fullPath, "\\") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: From the doc, it seems like only one \ is needed (for example, "Y:\MountX"). Any reason why we're appending 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second \ is for escaping purposes. The string when read in Go would have escaped \ and therefore, we need to cater to the same.

fullPath += "\\"
}

var volumeName [windows.MAX_PATH + 1]uint16
mountPointUTF16, err := windows.UTF16PtrFromString(fullPath)
if err != nil {
return false, fmt.Errorf("failed to convert path <%q> to UTF-16 pointer: %w", fullPath, err)
}

ret, _, err := funcGetVolumeNameForVolumeMountPointW(
uintptr(unsafe.Pointer(mountPointUTF16)),
uintptr(unsafe.Pointer(&volumeName[0])),
windows.MAX_PATH+1,
)
// If the return value is zero, then it means the call failed.
// We will unwrap the error and return the same.
if ret == 0 {
var errno windows.Errno
ok := errors.As(err, &errno)
if !ok {
return false, fmt.Errorf("unknown error occurred while checking block device: %v", err)
}
return false, fmt.Errorf("error checking block device at %q: %s", fullPath, errno.Error())
}

return true, nil
}
49 changes: 49 additions & 0 deletions ecs-agent/daemonimages/csidriver/util/utils_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/sys/windows"
)

func TestParseEndpoint(t *testing.T) {
Expand Down Expand Up @@ -65,3 +66,51 @@ func TestParseEndpoint(t *testing.T) {
}

}

// TestIsBlockDevice tests the IsBlockDevice method for positive and negative cases.
func TestIsBlockDevice(t *testing.T) {
testCases := []struct {
name string
mockGetVolumeNameF func(a ...uintptr) (r1 uintptr, r2 uintptr, lastErr error)
path string
expectedResult bool
expectedError error
}{
{
name: "Valid block device",
mockGetVolumeNameF: func(a ...uintptr) (r1 uintptr, r2 uintptr, lastErr error) {
return 1, 0, nil
},
path: "C:\\mount",
expectedResult: true,
expectedError: nil,
},
{
name: "Invalid block device",
mockGetVolumeNameF: func(a ...uintptr) (r1 uintptr, r2 uintptr, lastErr error) {
return 0, 0, windows.Errno(1)
},
path: "C:\\invalid\\mount",
expectedResult: false,
expectedError: windows.Errno(1),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
oldGetVolumeNameF := funcGetVolumeNameForVolumeMountPointW
defer func() {
funcGetVolumeNameForVolumeMountPointW = oldGetVolumeNameF
}()

// Set the dummy Win32 API method and invoke the IsBlockDevice method.
funcGetVolumeNameForVolumeMountPointW = tc.mockGetVolumeNameF
isBlockDevice, err := IsBlockDevice(tc.path)

assert.Equal(t, tc.expectedResult, isBlockDevice,
"expected isBlockDevice to be %t, got %t", tc.expectedResult, isBlockDevice)
assert.ErrorAs(t, err, tc.expectedError,
"expected error %v, got %v", tc.expectedError, err)
})
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading