Skip to content

Commit

Permalink
fix TestNodePublishVolume tests in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
boddumanohar committed Sep 26, 2020
1 parent 65f6394 commit b3e3ac9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,25 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v2
- name: Build Test
run: |
make smb-windows
- name: Run Windows Unit Tests
run: |
# start the CSI Proxy before running tests on windows
Start-Job -Name CSIProx -ScriptBlock {
Invoke-WebRequest https://kubernetesartifacts.azureedge.net/csi-proxy/v0.2.0/binaries/csi-proxy.tar.gz -OutFile csi-proxy.tar.gz;
tar -xvf csi-proxy.tar.gz
.\bin\csi-proxy.exe -v 9
};
# wait for the CSI Proxy to Come up
Start-Sleep -Seconds 30;
# Print all the named pipes to make sure that CSI-Proxy's named-pipes are created. ()
Write-Output "getting named pipes"
[System.IO.Directory]::GetFiles("\\.\\pipe\\")
# Start the tests
go test -v -race ./pkg/...
67 changes: 51 additions & 16 deletions pkg/smb/nodeserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"syscall"
"testing"

"github.com/kubernetes-csi/csi-driver-smb/pkg/mounter"
"github.com/kubernetes-csi/csi-driver-smb/test/utils/testutil"

"github.com/container-storage-interface/spec/lib/go/csi"
Expand All @@ -34,6 +37,15 @@ import (
"k8s.io/utils/mount"
)

// For CSI-Proxy(windows), the path has to be absolute and it should be within the context of value passed for
// csi-proxy argument: --kubelet-pod-path. In our tests we are using `C:\var\lib\kubelet\`
func getTestPath(path string) string {
if runtime.GOOS == "windows" {
return filepath.Join("C:\\var\\lib\\kubelet\\", path)
}
return "./" + path
}

func TestNodeStageVolume(t *testing.T) {
stdVolCap := csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Expand Down Expand Up @@ -198,18 +210,20 @@ func TestNodeExpandVolume(t *testing.T) {
}

func TestNodePublishVolume(t *testing.T) {
skipIfTestingOnWindows(t)
volumeCap := csi.VolumeCapability_AccessMode{Mode: csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER}
errorMountSource := "./error_mount_source"
alreadyMountedTarget := "./false_is_likely_exist_target"
smbFile := "./smb.go"
sourceTest := "./source_test"
targetTest := "./target_test"

errorMountSource := getTestPath("error_mount_source")
alreadyMountedTarget := getTestPath("false_is_likely_exist_target")
smbFile := getTestPath("smb.go")
sourceTest := getTestPath("source_test")
targetTest := getTestPath("target_test")

tests := []struct {
desc string
req csi.NodePublishVolumeRequest
expectedErr error
desc string
req csi.NodePublishVolumeRequest
expectedErr error
expectedErrUnix error
expectedErrWindows error
}{
{
desc: "[Error] Volume capabilities missing",
Expand All @@ -235,22 +249,26 @@ func TestNodePublishVolume(t *testing.T) {
expectedErr: status.Error(codes.InvalidArgument, "Staging target not provided"),
},
{
// mount behavior between Linux and Windows is a little different. Mount in Windows just creates a soft link at target pointing to source.
desc: "[Error] Not a directory",
req: csi.NodePublishVolumeRequest{VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
VolumeId: "vol_1",
TargetPath: smbFile,
StagingTargetPath: sourceTest,
Readonly: true},
expectedErr: status.Errorf(codes.Internal, "Could not mount target \"./smb.go\": mkdir ./smb.go: not a directory"),
expectedErrWindows: nil,
expectedErrUnix: status.Errorf(codes.Internal, "Could not mount target \"./smb.go\": mkdir ./smb.go: not a directory"),
},
{
// mount behavior between Linux and Windows is a little different. Mount in Windows just creates a soft link at target pointing to source.
desc: "[Error] Mount error mocked by Mount",
req: csi.NodePublishVolumeRequest{VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
VolumeId: "vol_1",
TargetPath: targetTest,
StagingTargetPath: errorMountSource,
Readonly: true},
expectedErr: status.Errorf(codes.Internal, "Could not mount \"./error_mount_source\" at \"./target_test\": fake Mount: source error"),
expectedErrWindows: nil,
expectedErrUnix: status.Errorf(codes.Internal, "Could not mount \"./error_mount_source\" at \"./target_test\": fake Mount: source error"),
},
{
desc: "[Success] Valid request read only",
Expand Down Expand Up @@ -284,15 +302,32 @@ func TestNodePublishVolume(t *testing.T) {
// Setup
_ = makeDir(alreadyMountedTarget)
d := NewFakeDriver()
fakeMounter := &fakeMounter{}
d.mounter = &mount.SafeFormatAndMount{
Interface: fakeMounter,

if runtime.GOOS == "windows" {
csiProxyMounter, err := mounter.NewSafeMounter()
assert.NoError(t, err)
d.mounter = &mount.SafeFormatAndMount{
Interface: csiProxyMounter.Interface,
}
} else {
fakeMounter := &fakeMounter{}
d.mounter = &mount.SafeFormatAndMount{
Interface: fakeMounter,
}
}

for _, test := range tests {
_, err := d.NodePublishVolume(context.Background(), &test.req)
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("test case: %s, Unexpected error: %v", test.desc, err)
expectedErr := test.expectedErr
if test.expectedErrWindows != test.expectedErrUnix {
if runtime.GOOS == "windows" {
expectedErr = test.expectedErrWindows
} else {
expectedErr = test.expectedErrUnix
}
}
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("test case: %s, Expected Error: %v \n Unexpected error: %v", test.desc, expectedErr, err)
}
}

Expand Down

0 comments on commit b3e3ac9

Please sign in to comment.