@@ -25,29 +25,25 @@ import (
2525 "syscall"
2626 "testing"
2727
28+ "github.com/kubernetes-csi/csi-driver-smb/test/utils/testutil"
29+
2830 "github.com/container-storage-interface/spec/lib/go/csi"
2931 "github.com/stretchr/testify/assert"
3032 "google.golang.org/grpc/codes"
3133 "google.golang.org/grpc/status"
3234 "k8s.io/utils/mount"
3335)
3436
35- const (
36- sourceTest = "./source_test"
37- targetTest = "./target_test"
38- )
39-
4037func TestNodeStageVolume (t * testing.T ) {
41- skipIfTestingOnWindows (t )
42-
4338 stdVolCap := csi.VolumeCapability {
4439 AccessType : & csi.VolumeCapability_Mount {
4540 Mount : & csi.VolumeCapability_MountVolume {},
4641 },
4742 }
4843
49- errorMountSensSource := "./error_mount_sens_source"
50- smbFile := "./smb.go"
44+ errorMountSensSource := testutil .GetWorkDirPath ("error_mount_sens_source" , t )
45+ smbFile := testutil .GetWorkDirPath ("smb.go" , t )
46+ sourceTest := testutil .GetWorkDirPath ("source_test" , t )
5147
5248 volContext := map [string ]string {
5349 sourceField : "test_source" ,
@@ -61,52 +57,72 @@ func TestNodeStageVolume(t *testing.T) {
6157 tests := []struct {
6258 desc string
6359 req csi.NodeStageVolumeRequest
64- expectedErr error
60+ expectedErr testutil. TestError
6561 }{
6662 {
67- desc : "[Error] Volume ID missing" ,
68- req : csi.NodeStageVolumeRequest {},
69- expectedErr : status .Error (codes .InvalidArgument , "Volume ID missing in request" ),
63+ desc : "[Error] Volume ID missing" ,
64+ req : csi.NodeStageVolumeRequest {},
65+ expectedErr : testutil.TestError {
66+ DefaultError : status .Error (codes .InvalidArgument , "Volume ID missing in request" ),
67+ },
7068 },
7169 {
72- desc : "[Error] Volume capabilities missing" ,
73- req : csi.NodeStageVolumeRequest {VolumeId : "vol_1" },
74- expectedErr : status .Error (codes .InvalidArgument , "Volume capability not provided" ),
70+ desc : "[Error] Volume capabilities missing" ,
71+ req : csi.NodeStageVolumeRequest {VolumeId : "vol_1" },
72+ expectedErr : testutil.TestError {
73+ DefaultError : status .Error (codes .InvalidArgument , "Volume capability not provided" ),
74+ },
7575 },
7676 {
77- desc : "[Error] Stage target path missing" ,
78- req : csi.NodeStageVolumeRequest {VolumeId : "vol_1" , VolumeCapability : & stdVolCap },
79- expectedErr : status .Error (codes .InvalidArgument , "Staging target not provided" ),
77+ desc : "[Error] Stage target path missing" ,
78+ req : csi.NodeStageVolumeRequest {VolumeId : "vol_1" , VolumeCapability : & stdVolCap },
79+ expectedErr : testutil.TestError {
80+ DefaultError : status .Error (codes .InvalidArgument , "Staging target not provided" ),
81+ },
8082 },
8183 {
8284 desc : "[Error] Source field is missing in context" ,
8385 req : csi.NodeStageVolumeRequest {VolumeId : "vol_1" , StagingTargetPath : sourceTest ,
8486 VolumeCapability : & stdVolCap },
85- expectedErr : status .Error (codes .InvalidArgument , "source field is missing, current context: map[]" ),
87+ expectedErr : testutil.TestError {
88+ DefaultError : status .Error (codes .InvalidArgument , "source field is missing, current context: map[]" ),
89+ },
8690 },
8791 {
8892 desc : "[Error] Not a Directory" ,
8993 req : csi.NodeStageVolumeRequest {VolumeId : "vol_1##" , StagingTargetPath : smbFile ,
9094 VolumeCapability : & stdVolCap ,
9195 VolumeContext : volContext ,
9296 Secrets : secrets },
93- expectedErr : status .Error (codes .Internal , "MkdirAll ./smb.go failed with error: mkdir ./smb.go: not a directory" ),
97+ expectedErr : testutil.TestError {
98+ DefaultError : status .Error (codes .Internal , fmt .Sprintf ("MkdirAll %s failed with error: mkdir %s: not a directory" , smbFile , smbFile )),
99+ WindowsError : status .Error (codes .Internal , fmt .Sprintf ("Could not mount target %s: mkdir %s: The system cannot find the path specified." , smbFile , smbFile )),
100+ },
94101 },
95102 {
96103 desc : "[Error] Failed SMB mount mocked by MountSensitive" ,
97104 req : csi.NodeStageVolumeRequest {VolumeId : "vol_1##" , StagingTargetPath : errorMountSensSource ,
98105 VolumeCapability : & stdVolCap ,
99106 VolumeContext : volContext ,
100107 Secrets : secrets },
101- expectedErr : status .Errorf (codes .Internal , "volume(vol_1##) mount \" test_source\" on \" ./error_mount_sens_source\" failed with fake MountSensitive: target error" ),
108+ expectedErr : testutil.TestError {
109+ DefaultError : status .Errorf (codes .Internal ,
110+ fmt .Sprintf ("volume(vol_1##) mount \" test_source\" on \" %s\" failed with fake MountSensitive: target error" ,
111+ errorMountSensSource )),
112+ // todo: Not a desired error. This will need a better fix
113+ WindowsError : fmt .Errorf ("prepare stage path failed for %s with error: could not cast to csi proxy class" , errorMountSensSource ),
114+ },
102115 },
103116 {
104117 desc : "[Success] Valid request" ,
105118 req : csi.NodeStageVolumeRequest {VolumeId : "vol_1##" , StagingTargetPath : sourceTest ,
106119 VolumeCapability : & stdVolCap ,
107120 VolumeContext : volContext ,
108121 Secrets : secrets },
109- expectedErr : nil ,
122+ expectedErr : testutil.TestError {
123+ // todo: Not a desired error. This will need a better fix
124+ WindowsError : fmt .Errorf ("prepare stage path failed for %s with error: could not cast to csi proxy class" , sourceTest ),
125+ },
110126 },
111127 }
112128
@@ -120,8 +136,8 @@ func TestNodeStageVolume(t *testing.T) {
120136 }
121137
122138 _ , err := d .NodeStageVolume (context .Background (), & test .req )
123- if ! reflect . DeepEqual ( err , test .expectedErr ) {
124- t .Errorf ("test case: %s, Unexpected error: %v" , test .desc , err )
139+ if ! testutil . AssertError ( & test .expectedErr , err ) {
140+ t .Errorf ("test case: %s, \n Unexpected error: %v\n Expected: %v " , test .desc , err , test . expectedErr . GetExpectedError () )
125141 }
126142 }
127143
@@ -187,6 +203,8 @@ func TestNodePublishVolume(t *testing.T) {
187203 errorMountSource := "./error_mount_source"
188204 alreadyMountedTarget := "./false_is_likely_exist_target"
189205 smbFile := "./smb.go"
206+ sourceTest := "./source_test"
207+ targetTest := "./target_test"
190208
191209 tests := []struct {
192210 desc string
@@ -289,6 +307,7 @@ func TestNodeUnpublishVolume(t *testing.T) {
289307 skipIfTestingOnWindows (t )
290308 errorTarget := "./error_is_likely_target"
291309 targetFile := "./abc.go"
310+ targetTest := "./target_test"
292311
293312 tests := []struct {
294313 desc string
@@ -341,6 +360,7 @@ func TestNodeUnstageVolume(t *testing.T) {
341360 skipIfTestingOnWindows (t )
342361 errorTarget := "./error_is_likely_target"
343362 targetFile := "./abc.go"
363+ targetTest := "./target_test"
344364
345365 tests := []struct {
346366 desc string
@@ -394,6 +414,7 @@ func TestEnsureMountPoint(t *testing.T) {
394414 alreadyExistTarget := "./false_is_likely_exist_target"
395415 falseTarget := "./false_is_likely_target"
396416 smbFile := "./smb.go"
417+ targetTest := "./target_test"
397418
398419 tests := []struct {
399420 desc string
@@ -450,6 +471,8 @@ func TestEnsureMountPoint(t *testing.T) {
450471}
451472
452473func TestMakeDir (t * testing.T ) {
474+ targetTest := "./target_test"
475+
453476 //Successfully create directory
454477 err := makeDir (targetTest )
455478 assert .NoError (t , err )
0 commit comments