diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 839812e6..57f014ca 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -17,8 +17,11 @@ jobs: uses: actions/checkout@v2 - name: Run Windows Integration Tests run: | + # required for running Volume and Disk tests + Install-WindowsFeature -name Hyper-V-PowerShell + $env:CSI_PROXY_GH_ACTIONS="TRUE" - go test -v -race ./integrationtests/... + go test --timeout 20m -v -race ./integrationtests/... unit_tests: strategy: matrix: diff --git a/integrationtests/disk_test.go b/integrationtests/disk_test.go index f93228f5..84f8edc3 100644 --- a/integrationtests/disk_test.go +++ b/integrationtests/disk_test.go @@ -78,7 +78,6 @@ func TestDisk(t *testing.T) { }) t.Run("Get/SetDiskState", func(t *testing.T) { - skipTestOnCondition(t, isRunningOnGhActions()) client, err := disk.New(diskapi.New()) require.Nil(t, err) @@ -143,7 +142,6 @@ func TestDisk(t *testing.T) { }) t.Run("PartitionDisk", func(t *testing.T) { - skipTestOnCondition(t, isRunningOnGhActions()) var err error client, err := disk.New(diskapi.New()) diff --git a/integrationtests/volume_test.go b/integrationtests/volume_test.go index a4af79e2..3ce3fab4 100644 --- a/integrationtests/volume_test.go +++ b/integrationtests/volume_test.go @@ -21,9 +21,6 @@ func TestVolume(t *testing.T) { negativeVolumeTests(t) }) - // TODO: These tests will fail on Github Actions because Hyper-V is disabled - // see https://github.com/actions/virtual-environments/pull/2525 - // these tests should be considered frozen from the API point of view volumeClient, err := volume.New(volumeapi.New()) require.Nil(t, err) @@ -32,12 +29,10 @@ func TestVolume(t *testing.T) { require.Nil(t, err) t.Run("MountVolume", func(t *testing.T) { - skipTestOnCondition(t, isRunningOnGhActions()) mountVolumeTests(diskClient, volumeClient, t) }) t.Run("GetClosestVolumeFromTargetPath", func(t *testing.T) { - skipTestOnCondition(t, isRunningOnGhActions()) getClosestVolumeFromTargetPathTests(diskClient, volumeClient, t) }) } diff --git a/pkg/volume/hostapi/hostapi.go b/pkg/volume/hostapi/hostapi.go index 1d9006c1..50de1a53 100644 --- a/pkg/volume/hostapi/hostapi.go +++ b/pkg/volume/hostapi/hostapi.go @@ -62,8 +62,9 @@ func New() HostAPI { } func getVolumeSize(volumeID string) (int64, error) { - cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Get-partition).Size", volumeID) - out, err := utils.RunPowershellCmd(cmd) + cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Get-partition).Size` + cmdEnv := fmt.Sprintf("volumeID=%s", volumeID) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil || len(out) == 0 { 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 // FormatVolume - Formats a volume with the NTFS format. func (volumeAPI) FormatVolume(volumeID string) (err error) { - cmd := fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Format-Volume -FileSystem ntfs -Confirm:$false", volumeID) - out, err := utils.RunPowershellCmd(cmd) + cmd := `Get-Volume -UniqueId "$Env:volumeID" | Format-Volume -FileSystem ntfs -Confirm:$false` + cmdEnv := fmt.Sprintf("volumeID=%s", volumeID) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil { 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) { // IsVolumeFormatted - Check if the volume is formatted with the pre specified filesystem(typically ntfs). func (volumeAPI) IsVolumeFormatted(volumeID string) (bool, error) { - cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" -ErrorAction Stop).FileSystemType", volumeID) - out, err := utils.RunPowershellCmd(cmd) + cmd := `(Get-Volume -UniqueId "$Env:volumeID" -ErrorAction Stop).FileSystemType` + cmdEnv := fmt.Sprintf("volumeID=%s", volumeID) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) + if err != nil { return false, fmt.Errorf("error checking if volume is formatted. cmd: %s, output: %s, error: %v", cmd, string(out), err) } @@ -129,8 +133,12 @@ func (volumeAPI) IsVolumeFormatted(volumeID string) (bool, error) { // MountVolume - mounts a volume to a path. This is done using the Add-PartitionAccessPath for presenting the volume via a path. func (volumeAPI) MountVolume(volumeID, path string) error { - cmd := fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-Partition | Add-PartitionAccessPath -AccessPath %s", volumeID, path) - out, err := utils.RunPowershellCmd(cmd) + cmd := `Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Add-PartitionAccessPath -AccessPath $Env:mountpath` + cmdEnv := []string{} + cmdEnv = append(cmdEnv, fmt.Sprintf("volumeID=%s", volumeID)) + cmdEnv = append(cmdEnv, fmt.Sprintf("mountpath=%s", path)) + out, err := utils.RunPowershellCmd(cmd, cmdEnv...) + if err != nil { return fmt.Errorf("error mount volume to path. cmd: %s, output: %s, error: %v", cmd, string(out), err) } @@ -143,8 +151,13 @@ func (volumeAPI) UnmountVolume(volumeID, path string) error { if err := writeCache(volumeID); err != nil { return err } - cmd := fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-Partition | Remove-PartitionAccessPath -AccessPath %s", volumeID, path) - out, err := utils.RunPowershellCmd(cmd) + + cmd := `Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Remove-PartitionAccessPath -AccessPath $Env:mountpath` + cmdEnv := []string{} + cmdEnv = append(cmdEnv, fmt.Sprintf("volumeID=%s", volumeID)) + cmdEnv = append(cmdEnv, fmt.Sprintf("mountpath=%s", path)) + out, err := utils.RunPowershellCmd(cmd, cmdEnv...) + if err != nil { return fmt.Errorf("error getting driver letter to mount volume. cmd: %s, output: %s,error: %v", cmd, string(out), err) } @@ -160,8 +173,9 @@ func (volumeAPI) ResizeVolume(volumeID string, size int64) error { var finalSize int64 var outString string if size == 0 { - cmd = fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json", volumeID) - out, err = utils.RunPowershellCmd(cmd) + cmd = `Get-Volume -UniqueId "$Env:volumeID" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json` + cmdEnv := fmt.Sprintf("volumeID=%s", volumeID) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil || len(out) == 0 { 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 { return nil } - cmd = fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-Partition | Resize-Partition -Size %d", volumeID, finalSize) - out, err = utils.RunPowershellCmd(cmd) + cmd = fmt.Sprintf(`Get-Volume -UniqueId "$Env:volumeID" | Get-Partition | Resize-Partition -Size %d`, finalSize) + cmdEnv := []string{} + cmdEnv = append(cmdEnv, fmt.Sprintf("volumeID=%s", volumeID)) + out, err = utils.RunPowershellCmd(cmd, cmdEnv...) if err != nil { return fmt.Errorf("error resizing volume. cmd: %s, output: %s size:%v, finalSize %v, error: %v", cmd, string(out), size, finalSize, err) } @@ -203,8 +219,9 @@ func (volumeAPI) ResizeVolume(volumeID string, size int64) error { // GetVolumeStats - retrieves the volume stats for a given volume func (volumeAPI) GetVolumeStats(volumeID string) (int64, int64, error) { // get the size and sizeRemaining for the volume - cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Select SizeRemaining,Size) | ConvertTo-Json", volumeID) - out, err := utils.RunPowershellCmd(cmd) + cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Select SizeRemaining,Size) | ConvertTo-Json` + cmdEnv := fmt.Sprintf("volumeID=%s", volumeID) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil { 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) { // GetDiskNumberFromVolumeID - gets the disk number where the volume is. func (volumeAPI) GetDiskNumberFromVolumeID(volumeID string) (uint32, error) { // get the size and sizeRemaining for the volume - cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Get-Partition).DiskNumber", volumeID) - out, err := utils.RunPowershellCmd(cmd) + cmd := `(Get-Volume -UniqueId "$Env:volumeID" | Get-Partition).DiskNumber` + cmdEnv := fmt.Sprintf("volumeID=%s", volumeID) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil || len(out) == 0 { 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) { } func getTarget(mount string) (string, error) { - cmd := fmt.Sprintf("(Get-Item -Path %s).Target", mount) - out, err := utils.RunPowershellCmd(cmd) + cmd := `(Get-Item -Path $Env:mountpath).Target` + cmdEnv := fmt.Sprintf("mountpath=%s", mount) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil || len(out) == 0 { return "", fmt.Errorf("error getting volume from mount. cmd: %s, output: %s, error: %v", cmd, string(out), err) } @@ -352,8 +371,9 @@ func ensureVolumePrefix(volume string) string { // dereferenceSymlink dereferences the symlink `path` and returns the stdout. func dereferenceSymlink(path string) (string, error) { - cmd := fmt.Sprintf(`(Get-Item -Path %s).Target`, path) - out, err := utils.RunPowershellCmd(cmd) + cmd := `(Get-Item -Path $Env:linkpath).Target` + cmdEnv := fmt.Sprintf("linkpath=%s", path) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil { return "", err } @@ -368,8 +388,9 @@ func getVolumeForDriveLetter(path string) (string, error) { return "", fmt.Errorf("The path=%s is not a valid DriverLetter", path) } - cmd := fmt.Sprintf(`(Get-Partition -DriveLetter %s | Get-Volume).UniqueId`, path) - out, err := utils.RunPowershellCmd(cmd) + cmd := `(Get-Partition -DriveLetter $Env:drivepath | Get-Volume).UniqueId` + cmdEnv := fmt.Sprintf("drivepath=%s", path) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil { return "", err } @@ -379,8 +400,9 @@ func getVolumeForDriveLetter(path string) (string, error) { } func writeCache(volumeID string) error { - cmd := fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Write-Volumecache", volumeID) - out, err := utils.RunPowershellCmd(cmd) + cmd := `Get-Volume -UniqueId "$Env:volumeID" | Write-Volumecache` + cmdEnv := fmt.Sprintf("volumeID=%s", volumeID) + out, err := utils.RunPowershellCmd(cmd, cmdEnv) if err != nil { return fmt.Errorf("error writing volume cache. cmd: %s, output: %s, error: %v", cmd, string(out), err) }