Skip to content

Commit 82af2a2

Browse files
authored
Merge pull request #307 from ritazh/fixes-library-development
Cherry-pick Ensuring all paths in powershell are passes and env vars on library-development
2 parents 4fdf927 + e0ea2f2 commit 82af2a2

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

.github/workflows/windows.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ jobs:
1717
uses: actions/checkout@v2
1818
- name: Run Windows Integration Tests
1919
run: |
20+
# required for running Volume and Disk tests
21+
Install-WindowsFeature -name Hyper-V-PowerShell
22+
2023
$env:CSI_PROXY_GH_ACTIONS="TRUE"
21-
go test -v -race ./integrationtests/...
24+
go test --timeout 20m -v -race ./integrationtests/...
2225
unit_tests:
2326
strategy:
2427
matrix:

integrationtests/disk_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func TestDisk(t *testing.T) {
7878
})
7979

8080
t.Run("Get/SetDiskState", func(t *testing.T) {
81-
skipTestOnCondition(t, isRunningOnGhActions())
8281

8382
client, err := disk.New(diskapi.New())
8483
require.Nil(t, err)
@@ -143,7 +142,6 @@ func TestDisk(t *testing.T) {
143142
})
144143

145144
t.Run("PartitionDisk", func(t *testing.T) {
146-
skipTestOnCondition(t, isRunningOnGhActions())
147145

148146
var err error
149147
client, err := disk.New(diskapi.New())

integrationtests/volume_test.go

-5
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ func TestVolume(t *testing.T) {
2121
negativeVolumeTests(t)
2222
})
2323

24-
// TODO: These tests will fail on Github Actions because Hyper-V is disabled
25-
// see https://github.com/actions/virtual-environments/pull/2525
26-
2724
// these tests should be considered frozen from the API point of view
2825
volumeClient, err := volume.New(volumeapi.New())
2926
require.Nil(t, err)
@@ -32,12 +29,10 @@ func TestVolume(t *testing.T) {
3229
require.Nil(t, err)
3330

3431
t.Run("MountVolume", func(t *testing.T) {
35-
skipTestOnCondition(t, isRunningOnGhActions())
3632
mountVolumeTests(diskClient, volumeClient, t)
3733
})
3834

3935
t.Run("GetClosestVolumeFromTargetPath", func(t *testing.T) {
40-
skipTestOnCondition(t, isRunningOnGhActions())
4136
getClosestVolumeFromTargetPathTests(diskClient, volumeClient, t)
4237
})
4338
}

pkg/volume/hostapi/hostapi.go

+48-26
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ func New() HostAPI {
6262
}
6363

6464
func getVolumeSize(volumeID string) (int64, error) {
65-
cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Get-partition).Size", volumeID)
66-
out, err := utils.RunPowershellCmd(cmd)
65+
cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Get-partition).Size`
66+
cmdEnv := fmt.Sprintf("volumeID=%s", volumeID)
67+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
6768

6869
if err != nil || len(out) == 0 {
6970
return -1, fmt.Errorf("error getting size of the partition from mount. cmd %s, output: %s, error: %v", cmd, string(out), err)
@@ -98,8 +99,9 @@ func (volumeAPI) ListVolumesOnDisk(diskNumber uint32, partitionNumber uint32) (v
9899

99100
// FormatVolume - Formats a volume with the NTFS format.
100101
func (volumeAPI) FormatVolume(volumeID string) (err error) {
101-
cmd := fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Format-Volume -FileSystem ntfs -Confirm:$false", volumeID)
102-
out, err := utils.RunPowershellCmd(cmd)
102+
cmd := `Get-Volume -UniqueId "$Env:volumeID" | Format-Volume -FileSystem ntfs -Confirm:$false`
103+
cmdEnv := fmt.Sprintf("volumeID=%s", volumeID)
104+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
103105

104106
if err != nil {
105107
return fmt.Errorf("error formatting volume. cmd: %s, output: %s, error: %v", cmd, string(out), err)
@@ -115,8 +117,10 @@ func (volumeAPI) WriteVolumeCache(volumeID string) (err error) {
115117

116118
// IsVolumeFormatted - Check if the volume is formatted with the pre specified filesystem(typically ntfs).
117119
func (volumeAPI) IsVolumeFormatted(volumeID string) (bool, error) {
118-
cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" -ErrorAction Stop).FileSystemType", volumeID)
119-
out, err := utils.RunPowershellCmd(cmd)
120+
cmd := `(Get-Volume -UniqueId "$Env:volumeID" -ErrorAction Stop).FileSystemType`
121+
cmdEnv := fmt.Sprintf("volumeID=%s", volumeID)
122+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
123+
120124
if err != nil {
121125
return false, fmt.Errorf("error checking if volume is formatted. cmd: %s, output: %s, error: %v", cmd, string(out), err)
122126
}
@@ -129,8 +133,12 @@ func (volumeAPI) IsVolumeFormatted(volumeID string) (bool, error) {
129133

130134
// MountVolume - mounts a volume to a path. This is done using the Add-PartitionAccessPath for presenting the volume via a path.
131135
func (volumeAPI) MountVolume(volumeID, path string) error {
132-
cmd := fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-Partition | Add-PartitionAccessPath -AccessPath %s", volumeID, path)
133-
out, err := utils.RunPowershellCmd(cmd)
136+
cmd := `Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Add-PartitionAccessPath -AccessPath $Env:mountpath`
137+
cmdEnv := []string{}
138+
cmdEnv = append(cmdEnv, fmt.Sprintf("volumeID=%s", volumeID))
139+
cmdEnv = append(cmdEnv, fmt.Sprintf("mountpath=%s", path))
140+
out, err := utils.RunPowershellCmd(cmd, cmdEnv...)
141+
134142
if err != nil {
135143
return fmt.Errorf("error mount volume to path. cmd: %s, output: %s, error: %v", cmd, string(out), err)
136144
}
@@ -143,8 +151,13 @@ func (volumeAPI) UnmountVolume(volumeID, path string) error {
143151
if err := writeCache(volumeID); err != nil {
144152
return err
145153
}
146-
cmd := fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-Partition | Remove-PartitionAccessPath -AccessPath %s", volumeID, path)
147-
out, err := utils.RunPowershellCmd(cmd)
154+
155+
cmd := `Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Remove-PartitionAccessPath -AccessPath $Env:mountpath`
156+
cmdEnv := []string{}
157+
cmdEnv = append(cmdEnv, fmt.Sprintf("volumeID=%s", volumeID))
158+
cmdEnv = append(cmdEnv, fmt.Sprintf("mountpath=%s", path))
159+
out, err := utils.RunPowershellCmd(cmd, cmdEnv...)
160+
148161
if err != nil {
149162
return fmt.Errorf("error getting driver letter to mount volume. cmd: %s, output: %s,error: %v", cmd, string(out), err)
150163
}
@@ -160,8 +173,9 @@ func (volumeAPI) ResizeVolume(volumeID string, size int64) error {
160173
var finalSize int64
161174
var outString string
162175
if size == 0 {
163-
cmd = fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json", volumeID)
164-
out, err = utils.RunPowershellCmd(cmd)
176+
cmd = `Get-Volume -UniqueId "$Env:volumeID" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json`
177+
cmdEnv := fmt.Sprintf("volumeID=%s", volumeID)
178+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
165179

166180
if err != nil || len(out) == 0 {
167181
return fmt.Errorf("error getting sizemin,sizemax from mount. cmd: %s, output: %s, error: %v", cmd, string(out), err)
@@ -192,8 +206,10 @@ func (volumeAPI) ResizeVolume(volumeID string, size int64) error {
192206
return nil
193207
}
194208

195-
cmd = fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-Partition | Resize-Partition -Size %d", volumeID, finalSize)
196-
out, err = utils.RunPowershellCmd(cmd)
209+
cmd = fmt.Sprintf(`Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Resize-Partition -Size %d`, finalSize)
210+
cmdEnv := []string{}
211+
cmdEnv = append(cmdEnv, fmt.Sprintf("volumeID=%s", volumeID))
212+
out, err = utils.RunPowershellCmd(cmd, cmdEnv...)
197213
if err != nil {
198214
return fmt.Errorf("error resizing volume. cmd: %s, output: %s size:%v, finalSize %v, error: %v", cmd, string(out), size, finalSize, err)
199215
}
@@ -203,8 +219,9 @@ func (volumeAPI) ResizeVolume(volumeID string, size int64) error {
203219
// GetVolumeStats - retrieves the volume stats for a given volume
204220
func (volumeAPI) GetVolumeStats(volumeID string) (int64, int64, error) {
205221
// get the size and sizeRemaining for the volume
206-
cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Select SizeRemaining,Size) | ConvertTo-Json", volumeID)
207-
out, err := utils.RunPowershellCmd(cmd)
222+
cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Select SizeRemaining,Size) | ConvertTo-Json`
223+
cmdEnv := fmt.Sprintf("volumeID=%s", volumeID)
224+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
208225

209226
if err != nil {
210227
return -1, -1, fmt.Errorf("error getting capacity and used size of volume. cmd: %s, output: %s, error: %v", cmd, string(out), err)
@@ -227,8 +244,9 @@ func (volumeAPI) GetVolumeStats(volumeID string) (int64, int64, error) {
227244
// GetDiskNumberFromVolumeID - gets the disk number where the volume is.
228245
func (volumeAPI) GetDiskNumberFromVolumeID(volumeID string) (uint32, error) {
229246
// get the size and sizeRemaining for the volume
230-
cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Get-Partition).DiskNumber", volumeID)
231-
out, err := utils.RunPowershellCmd(cmd)
247+
cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Get-Partition).DiskNumber`
248+
cmdEnv := fmt.Sprintf("volumeID=%s", volumeID)
249+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
232250

233251
if err != nil || len(out) == 0 {
234252
return 0, fmt.Errorf("error getting disk number. cmd: %s, output: %s, error: %v", cmd, string(out), err)
@@ -261,8 +279,9 @@ func (volumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
261279
}
262280

263281
func getTarget(mount string) (string, error) {
264-
cmd := fmt.Sprintf("(Get-Item -Path %s).Target", mount)
265-
out, err := utils.RunPowershellCmd(cmd)
282+
cmd := `(Get-Item -Path $Env:mountpath).Target`
283+
cmdEnv := fmt.Sprintf("mountpath=%s", mount)
284+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
266285
if err != nil || len(out) == 0 {
267286
return "", fmt.Errorf("error getting volume from mount. cmd: %s, output: %s, error: %v", cmd, string(out), err)
268287
}
@@ -352,8 +371,9 @@ func ensureVolumePrefix(volume string) string {
352371

353372
// dereferenceSymlink dereferences the symlink `path` and returns the stdout.
354373
func dereferenceSymlink(path string) (string, error) {
355-
cmd := fmt.Sprintf(`(Get-Item -Path %s).Target`, path)
356-
out, err := utils.RunPowershellCmd(cmd)
374+
cmd := `(Get-Item -Path $Env:linkpath).Target`
375+
cmdEnv := fmt.Sprintf("linkpath=%s", path)
376+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
357377
if err != nil {
358378
return "", err
359379
}
@@ -368,8 +388,9 @@ func getVolumeForDriveLetter(path string) (string, error) {
368388
return "", fmt.Errorf("The path=%s is not a valid DriverLetter", path)
369389
}
370390

371-
cmd := fmt.Sprintf(`(Get-Partition -DriveLetter %s | Get-Volume).UniqueId`, path)
372-
out, err := utils.RunPowershellCmd(cmd)
391+
cmd := `(Get-Partition -DriveLetter $Env:drivepath | Get-Volume).UniqueId`
392+
cmdEnv := fmt.Sprintf("drivepath=%s", path)
393+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
373394
if err != nil {
374395
return "", err
375396
}
@@ -379,8 +400,9 @@ func getVolumeForDriveLetter(path string) (string, error) {
379400
}
380401

381402
func writeCache(volumeID string) error {
382-
cmd := fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Write-Volumecache", volumeID)
383-
out, err := utils.RunPowershellCmd(cmd)
403+
cmd := `Get-Volume -UniqueId "$Env:volumeID" | Write-Volumecache`
404+
cmdEnv := fmt.Sprintf("volumeID=%s", volumeID)
405+
out, err := utils.RunPowershellCmd(cmd, cmdEnv)
384406
if err != nil {
385407
return fmt.Errorf("error writing volume cache. cmd: %s, output: %s, error: %v", cmd, string(out), err)
386408
}

0 commit comments

Comments
 (0)