@@ -3,6 +3,10 @@ package integrationtests
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "os"
7
+ "os/exec"
8
+ "path/filepath"
9
+ "strings"
6
10
"testing"
7
11
8
12
diskv1 "github.com/kubernetes-csi/csi-proxy/client/api/disk/v1"
@@ -11,23 +15,10 @@ import (
11
15
v2alpha1client "github.com/kubernetes-csi/csi-proxy/client/groups/volume/v2alpha1"
12
16
)
13
17
14
- func v2alpha1VolumeTests (t * testing.T ) {
15
- var volumeClient * v2alpha1client.Client
16
- var diskClient * diskv1client.Client
17
- var err error
18
-
19
- if volumeClient , err = v2alpha1client .NewClient (); err != nil {
20
- t .Fatalf ("Client new error: %v" , err )
21
- }
22
- defer volumeClient .Close ()
23
-
24
- if diskClient , err = diskv1client .NewClient (); err != nil {
25
- t .Fatalf ("DiskClient new error: %v" , err )
26
- }
27
- defer diskClient .Close ()
28
-
18
+ // volumeInit initializes a volume, it creates a VHD, initializes it,
19
+ // creates a partition with the max size and formats the volume corresponding to that partition
20
+ func volumeInit (volumeClient * v2alpha1client.Client , t * testing.T ) (* VirtualHardDisk , string , func ()) {
29
21
vhd , vhdCleanup := diskInit (t )
30
- defer vhdCleanup ()
31
22
32
23
listRequest := & v2alpha1.ListVolumesOnDiskRequest {
33
24
DiskNumber : vhd .DiskNumber ,
@@ -42,6 +33,7 @@ func v2alpha1VolumeTests(t *testing.T) {
42
33
t .Fatalf ("Number of volumes not equal to 1: %d" , volumeIDsLen )
43
34
}
44
35
volumeID := listResponse .VolumeIds [0 ]
36
+ t .Logf ("VolumeId %v" , volumeID )
45
37
46
38
isVolumeFormattedRequest := & v2alpha1.IsVolumeFormattedRequest {
47
39
VolumeId : volumeID ,
@@ -69,8 +61,146 @@ func v2alpha1VolumeTests(t *testing.T) {
69
61
if ! isVolumeFormattedResponse .Formatted {
70
62
t .Fatal ("Volume should be formatted. Unexpected !!" )
71
63
}
64
+ return vhd , volumeID , vhdCleanup
65
+ }
66
+
67
+ func v2alpha1GetClosestVolumeFromTargetPathTests (diskClient * diskv1client.Client , volumeClient * v2alpha1client.Client , t * testing.T ) {
68
+ t .Run ("DriveLetterVolume" , func (t * testing.T ) {
69
+ vhd , _ , vhdCleanup := volumeInit (volumeClient , t )
70
+ defer vhdCleanup ()
71
+
72
+ // vhd.Mount dir exists, because there are no volumes above it should return the C:\ volume
73
+ var request * v2alpha1.GetClosestVolumeIDFromTargetPathRequest
74
+ var response * v2alpha1.GetClosestVolumeIDFromTargetPathResponse
75
+ request = & v2alpha1.GetClosestVolumeIDFromTargetPathRequest {
76
+ TargetPath : vhd .Mount ,
77
+ }
78
+ response , err := volumeClient .GetClosestVolumeIDFromTargetPath (context .TODO (), request )
79
+ if err != nil {
80
+ t .Fatalf ("GetClosestVolumeIDFromTargetPath request error, err=%v" , err )
81
+ }
82
+
83
+ // the C drive volume
84
+ cmd := exec .Command ("powershell" , "/c" , `(Get-Partition -DriveLetter C | Get-Volume).UniqueId` )
85
+ targetb , err := cmd .Output ()
86
+ if err != nil {
87
+ t .Fatalf ("Failed to get the C: drive volume" )
88
+ }
89
+ cDriveVolume := strings .TrimSpace (string (targetb ))
90
+
91
+ if response .VolumeId != cDriveVolume {
92
+ t .Fatalf ("The volume from GetClosestVolumeIDFromTargetPath doesn't match the C: drive volume" )
93
+ }
94
+ })
95
+ t .Run ("AncestorVolumeFromNestedDirectory" , func (t * testing.T ) {
96
+ var err error
97
+ vhd , volumeID , vhdCleanup := volumeInit (volumeClient , t )
98
+ defer vhdCleanup ()
99
+
100
+ // Mount the volume
101
+ mountVolumeRequest := & v2alpha1.MountVolumeRequest {
102
+ VolumeId : volumeID ,
103
+ TargetPath : vhd .Mount ,
104
+ }
105
+ _ , err = volumeClient .MountVolume (context .TODO (), mountVolumeRequest )
106
+ if err != nil {
107
+ t .Fatalf ("Volume id %s mount to path %s failed. Error: %v" , volumeID , vhd .Mount , err )
108
+ }
109
+
110
+ // Unmount the volume
111
+ defer func () {
112
+ unmountVolumeRequest := & v2alpha1.UnmountVolumeRequest {
113
+ VolumeId : volumeID ,
114
+ TargetPath : vhd .Mount ,
115
+ }
116
+ _ , err = volumeClient .UnmountVolume (context .TODO (), unmountVolumeRequest )
117
+ if err != nil {
118
+ t .Fatalf ("Volume id %s mount to path %s failed. Error: %v" , volumeID , vhd .Mount , err )
119
+ }
120
+ }()
121
+
122
+ nestedDirectory := filepath .Join (vhd .Mount , "foo/bar" )
123
+ err = os .MkdirAll (nestedDirectory , os .ModeDir )
124
+ if err != nil {
125
+ t .Fatalf ("Failed to create directory=%s" , nestedDirectory )
126
+ }
127
+
128
+ // the volume returned should be the VHD volume
129
+ var request * v2alpha1.GetClosestVolumeIDFromTargetPathRequest
130
+ var response * v2alpha1.GetClosestVolumeIDFromTargetPathResponse
131
+ request = & v2alpha1.GetClosestVolumeIDFromTargetPathRequest {
132
+ TargetPath : nestedDirectory ,
133
+ }
134
+ response , err = volumeClient .GetClosestVolumeIDFromTargetPath (context .TODO (), request )
135
+ if err != nil {
136
+ t .Fatalf ("GetClosestVolumeIDFromTargetPath request error, err=%v" , err )
137
+ }
138
+
139
+ if response .VolumeId != volumeID {
140
+ t .Fatalf ("The volume from GetClosestVolumeIDFromTargetPath doesn't match the VHD volume=%s" , volumeID )
141
+ }
142
+ })
143
+
144
+ t .Run ("SymlinkToVolume" , func (t * testing.T ) {
145
+ var err error
146
+ vhd , volumeID , vhdCleanup := volumeInit (volumeClient , t )
147
+ defer vhdCleanup ()
148
+
149
+ // Mount the volume
150
+ mountVolumeRequest := & v2alpha1.MountVolumeRequest {
151
+ VolumeId : volumeID ,
152
+ TargetPath : vhd .Mount ,
153
+ }
154
+ _ , err = volumeClient .MountVolume (context .TODO (), mountVolumeRequest )
155
+ if err != nil {
156
+ t .Fatalf ("Volume id %s mount to path %s failed. Error: %v" , volumeID , vhd .Mount , err )
157
+ }
158
+
159
+ // Unmount the volume
160
+ defer func () {
161
+ unmountVolumeRequest := & v2alpha1.UnmountVolumeRequest {
162
+ VolumeId : volumeID ,
163
+ TargetPath : vhd .Mount ,
164
+ }
165
+ _ , err = volumeClient .UnmountVolume (context .TODO (), unmountVolumeRequest )
166
+ if err != nil {
167
+ t .Fatalf ("Volume id %s mount to path %s failed. Error: %v" , volumeID , vhd .Mount , err )
168
+ }
169
+ }()
170
+
171
+ testPluginPath , _ := getTestPluginPath ()
172
+ err = os .MkdirAll (testPluginPath , os .ModeDir )
173
+ if err != nil {
174
+ t .Fatalf ("Failed to create directory=%s" , testPluginPath )
175
+ }
176
+
177
+ sourceSymlink := filepath .Join (testPluginPath , "source" )
178
+ err = os .Symlink (vhd .Mount , sourceSymlink )
179
+ if err != nil {
180
+ t .Fatalf ("Failed to create the symlink=%s" , sourceSymlink )
181
+ }
182
+
183
+ // the volume returned should be the VHD volume
184
+ var request * v2alpha1.GetClosestVolumeIDFromTargetPathRequest
185
+ var response * v2alpha1.GetClosestVolumeIDFromTargetPathResponse
186
+ request = & v2alpha1.GetClosestVolumeIDFromTargetPathRequest {
187
+ TargetPath : sourceSymlink ,
188
+ }
189
+ response , err = volumeClient .GetClosestVolumeIDFromTargetPath (context .TODO (), request )
190
+ if err != nil {
191
+ t .Fatalf ("GetClosestVolumeIDFromTargetPath request error, err=%v" , err )
192
+ }
193
+
194
+ if response .VolumeId != volumeID {
195
+ t .Fatalf ("The volume from GetClosestVolumeIDFromTargetPath doesn't match the VHD volume=%s" , volumeID )
196
+ }
197
+ })
198
+ }
199
+
200
+ func v2alpha1MountVolumeTests (diskClient * diskv1client.Client , volumeClient * v2alpha1client.Client , t * testing.T ) {
201
+ vhd , volumeID , vhdCleanup := volumeInit (volumeClient , t )
202
+ defer vhdCleanup ()
72
203
73
- t .Logf ("VolumeId %v" , volumeID )
74
204
volumeStatsRequest := & v2alpha1.GetVolumeStatsRequest {
75
205
VolumeId : volumeID ,
76
206
}
@@ -167,3 +297,26 @@ func v2alpha1VolumeTests(t *testing.T) {
167
297
t .Fatalf ("Volume id %s mount to path %s failed. Error: %v" , volumeID , vhd .Mount , err )
168
298
}
169
299
}
300
+
301
+ func v2alpha1VolumeTests (t * testing.T ) {
302
+ var volumeClient * v2alpha1client.Client
303
+ var diskClient * diskv1client.Client
304
+ var err error
305
+
306
+ if volumeClient , err = v2alpha1client .NewClient (); err != nil {
307
+ t .Fatalf ("Client new error: %v" , err )
308
+ }
309
+ defer volumeClient .Close ()
310
+
311
+ if diskClient , err = diskv1client .NewClient (); err != nil {
312
+ t .Fatalf ("DiskClient new error: %v" , err )
313
+ }
314
+ defer diskClient .Close ()
315
+
316
+ t .Run ("MountVolume" , func (t * testing.T ) {
317
+ v2alpha1MountVolumeTests (diskClient , volumeClient , t )
318
+ })
319
+ t .Run ("GetClosestVolumeFromTargetPath" , func (t * testing.T ) {
320
+ v2alpha1GetClosestVolumeFromTargetPathTests (diskClient , volumeClient , t )
321
+ })
322
+ }
0 commit comments