Merge pull request #114764 from claudiubelu/unittests-7

unittests: Fixes unit tests for Windows (part 7)
This commit is contained in:
Kubernetes Prow Robot 2023-05-08 11:52:51 -07:00 committed by GitHub
commit 76552fac38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 51 deletions

View File

@ -136,30 +136,40 @@ func getPersistentPlugin(t *testing.T) (string, volume.PersistentVolumePlugin) {
} }
func getDeviceMountablePluginWithBlockPath(t *testing.T, isBlockDevice bool) (string, volume.DeviceMountableVolumePlugin) { func getDeviceMountablePluginWithBlockPath(t *testing.T, isBlockDevice bool) (string, volume.DeviceMountableVolumePlugin) {
tmpDir, err := utiltesting.MkTmpdir("localVolumeTest") var (
if err != nil { source string
t.Fatalf("can't make a temp dir: %v", err) err error
)
if isBlockDevice && runtime.GOOS == "windows" {
// On Windows, block devices are referenced by the disk number, which is validated by the mounter,
source = "0"
} else {
source, err = utiltesting.MkTmpdir("localVolumeTest")
if err != nil {
t.Fatalf("can't make a temp dir: %v", err)
}
} }
plugMgr := volume.VolumePluginMgr{} plugMgr := volume.VolumePluginMgr{}
var pathToFSType map[string]hostutil.FileType var pathToFSType map[string]hostutil.FileType
if isBlockDevice { if isBlockDevice {
pathToFSType = map[string]hostutil.FileType{ pathToFSType = map[string]hostutil.FileType{
tmpDir: hostutil.FileTypeBlockDev, source: hostutil.FileTypeBlockDev,
} }
} }
plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeKubeletVolumeHostWithMounterFSType(t, tmpDir, nil, nil, pathToFSType)) plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeKubeletVolumeHostWithMounterFSType(t, source, nil, nil, pathToFSType))
plug, err := plugMgr.FindDeviceMountablePluginByName(localVolumePluginName) plug, err := plugMgr.FindDeviceMountablePluginByName(localVolumePluginName)
if err != nil { if err != nil {
os.RemoveAll(tmpDir) os.RemoveAll(source)
t.Fatalf("Can't find the plugin by name") t.Fatalf("Can't find the plugin by name")
} }
if plug.GetPluginName() != localVolumePluginName { if plug.GetPluginName() != localVolumePluginName {
t.Errorf("Wrong name: %s", plug.GetPluginName()) t.Errorf("Wrong name: %s", plug.GetPluginName())
} }
return tmpDir, plug return source, plug
} }
func getTestVolume(readOnly bool, path string, isBlock bool, mountOptions []string) *volume.Spec { func getTestVolume(readOnly bool, path string, isBlock bool, mountOptions []string) *volume.Spec {
@ -244,11 +254,6 @@ func TestInvalidLocalPath(t *testing.T) {
} }
func TestBlockDeviceGlobalPathAndMountDevice(t *testing.T) { func TestBlockDeviceGlobalPathAndMountDevice(t *testing.T) {
// Skip tests that fail on Windows, as discussed during the SIG Testing meeting from January 10, 2023
if runtime.GOOS == "windows" {
t.Skip("Skipping test that fails on Windows")
}
// Block device global mount path testing // Block device global mount path testing
tmpBlockDir, plug := getDeviceMountablePluginWithBlockPath(t, true) tmpBlockDir, plug := getDeviceMountablePluginWithBlockPath(t, true)
defer os.RemoveAll(tmpBlockDir) defer os.RemoveAll(tmpBlockDir)

View File

@ -359,11 +359,6 @@ type testcase struct {
} }
func TestPlugin(t *testing.T) { func TestPlugin(t *testing.T) {
// Skip tests that fail on Windows, as discussed during the SIG Testing meeting from January 10, 2023
if runtime.GOOS == "windows" {
t.Skip("Skipping test that fails on Windows")
}
tmpDir, err := utiltesting.MkTmpdir("rbd_test") tmpDir, err := utiltesting.MkTmpdir("rbd_test")
if err != nil { if err != nil {
t.Fatalf("error creating temp dir: %v", err) t.Fatalf("error creating temp dir: %v", err)
@ -374,10 +369,10 @@ func TestPlugin(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
expectedDevicePath := "/dev/rbd1" expectedDevicePath := "/dev/rbd0"
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// Windows expects Disk Numbers. // Windows expects Disk Numbers.
expectedDevicePath = "1" expectedDevicePath = "0"
} }
podUID := uuid.NewUUID() podUID := uuid.NewUUID()

View File

@ -26,9 +26,10 @@ import (
func (fake *fakeDiskManager) AttachDisk(b rbdMounter) (string, error) { func (fake *fakeDiskManager) AttachDisk(b rbdMounter) (string, error) {
fake.mutex.Lock() fake.mutex.Lock()
defer fake.mutex.Unlock() defer fake.mutex.Unlock()
fake.rbdMapIndex++
devicePath := fmt.Sprintf("/dev/rbd%d", fake.rbdMapIndex) devicePath := fmt.Sprintf("/dev/rbd%d", fake.rbdMapIndex)
fake.rbdDevices[devicePath] = true fake.rbdDevices[devicePath] = true
// Increment rbdMapIndex afterwards, so we can start from rbd0.
fake.rbdMapIndex++
return devicePath, nil return devicePath, nil
} }

View File

@ -20,42 +20,29 @@ limitations under the License.
package rbd package rbd
import ( import (
"fmt"
"os/exec"
"strconv" "strconv"
"strings"
"k8s.io/mount-utils"
) )
func (fake *fakeDiskManager) AttachDisk(b rbdMounter) (string, error) { func (fake *fakeDiskManager) AttachDisk(b rbdMounter) (string, error) {
fake.mutex.Lock() fake.mutex.Lock()
defer fake.mutex.Unlock() defer fake.mutex.Unlock()
fake.rbdMapIndex++
// Windows expects Disk Numbers. // Windows expects Disk Numbers. We start with rbdMapIndex 0, referring to the first Disk.
volIds, err := listVolumesOnDisk(strconv.Itoa(fake.rbdMapIndex)) volIds, err := mount.ListVolumesOnDisk(strconv.Itoa(fake.rbdMapIndex))
if err != nil { if err != nil {
return "", err return "", err
} }
fake.rbdDevices[volIds[0]] = true fake.rbdDevices[volIds[0]] = true
devicePath := strconv.Itoa(fake.rbdMapIndex) devicePath := strconv.Itoa(fake.rbdMapIndex)
fake.rbdMapIndex++
return devicePath, nil return devicePath, nil
} }
// listVolumesOnDisk - returns back list of volumes(volumeIDs) in the disk (requested in diskID) on Windows.
func listVolumesOnDisk(diskID string) (volumeIDs []string, err error) {
cmd := fmt.Sprintf("(Get-Disk -DeviceId %s | Get-Partition | Get-Volume).UniqueId", diskID)
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
if err != nil {
return []string{}, fmt.Errorf("error list volumes on disk. cmd: %s, output: %s, error: %v", cmd, string(output), err)
}
volumeIds := strings.Split(strings.TrimSpace(string(output)), "\r\n")
return volumeIds, nil
}
func getLoggedSource(devicePath string) (string, error) { func getLoggedSource(devicePath string) (string, error) {
// Windows mounter is mounting based on the Disk's Unique ID. // Windows mounter is mounting based on the Disk's Unique ID.
volIds, err := listVolumesOnDisk(devicePath) volIds, err := mount.ListVolumesOnDisk(devicePath)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -40,6 +40,10 @@ func verifyDevicePath(path string) (string, error) {
klog.V(4).Infof("Found vSphere disk attached with disk number %v", path) klog.V(4).Infof("Found vSphere disk attached with disk number %v", path)
return path, nil return path, nil
} }
// NOTE: If a powershell command that would return an array (e.g.: Get-Disk) would return an array of
// one element, powershell will in fact return that object directly, and **not an array containing
// that elemenent, which means piping it to ConvertTo-Json would not result in array as expected below.
// The following syntax forces it to always be an array.
cmd := exec.Command("powershell", "/c", "Get-Disk | Select Number, SerialNumber | ConvertTo-JSON") cmd := exec.Command("powershell", "/c", "Get-Disk | Select Number, SerialNumber | ConvertTo-JSON")
output, err := cmd.Output() output, err := cmd.Output()
if err != nil { if err != nil {

View File

@ -299,26 +299,20 @@ func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target
} }
klog.V(4).Infof("diskMount: Disk successfully formatted, disk: %q, fstype: %q", source, fstype) klog.V(4).Infof("diskMount: Disk successfully formatted, disk: %q, fstype: %q", source, fstype)
volumeIds, err := listVolumesOnDisk(source) volumeIds, err := ListVolumesOnDisk(source)
if err != nil { if err != nil {
return err return err
} }
driverPath := volumeIds[0] driverPath := volumeIds[0]
target = NormalizeWindowsPath(target) return mounter.MountSensitive(driverPath, target, fstype, options, sensitiveOptions)
output, err := mounter.Exec.Command("cmd", "/c", "mklink", "/D", target, driverPath).CombinedOutput()
if err != nil {
klog.Errorf("mklink(%s, %s) failed: %v, output: %q", target, driverPath, err, string(output))
return err
}
klog.V(2).Infof("formatAndMount disk(%s) fstype(%s) on(%s) with output(%s) successfully", driverPath, fstype, target, string(output))
return nil
} }
// ListVolumesOnDisk - returns back list of volumes(volumeIDs) in the disk (requested in diskID). // ListVolumesOnDisk - returns back list of volumes(volumeIDs) in the disk (requested in diskID).
func listVolumesOnDisk(diskID string) (volumeIDs []string, err error) { func ListVolumesOnDisk(diskID string) (volumeIDs []string, err error) {
cmd := fmt.Sprintf("(Get-Disk -DeviceId %s | Get-Partition | Get-Volume).UniqueId", diskID) // If a Disk has multiple volumes, Get-Volume may not return items in the same order.
cmd := fmt.Sprintf("(Get-Disk -DeviceId %s | Get-Partition | Get-Volume | Sort-Object -Property UniqueId).UniqueId", diskID)
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput() output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
klog.V(4).Infof("listVolumesOnDisk id from %s: %s", diskID, string(output)) klog.V(4).Infof("ListVolumesOnDisk id from %s: %s", diskID, string(output))
if err != nil { if err != nil {
return []string{}, fmt.Errorf("error list volumes on disk. cmd: %s, output: %s, error: %v", cmd, string(output), err) return []string{}, fmt.Errorf("error list volumes on disk. cmd: %s, output: %s, error: %v", cmd, string(output), err)
} }