mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Fix fcpath
This commit is contained in:
parent
ee297b6f4d
commit
4a75c7ef1e
@ -21,6 +21,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -60,12 +61,13 @@ func (handler *osIOHandler) WriteFile(filename string, data []byte, perm os.File
|
|||||||
|
|
||||||
// given a wwn and lun, find the device and associated devicemapper parent
|
// given a wwn and lun, find the device and associated devicemapper parent
|
||||||
func findDisk(wwn, lun string, io ioHandler, deviceUtil volumeutil.DeviceUtil) (string, string) {
|
func findDisk(wwn, lun string, io ioHandler, deviceUtil volumeutil.DeviceUtil) (string, string) {
|
||||||
fcPath := "-fc-0x" + wwn + "-lun-" + lun
|
fcPathExp := "^(pci-.*-fc|fc)-0x" + wwn + "-lun-" + lun
|
||||||
|
r := regexp.MustCompile(fcPathExp)
|
||||||
devPath := byPath
|
devPath := byPath
|
||||||
if dirs, err := io.ReadDir(devPath); err == nil {
|
if dirs, err := io.ReadDir(devPath); err == nil {
|
||||||
for _, f := range dirs {
|
for _, f := range dirs {
|
||||||
name := f.Name()
|
name := f.Name()
|
||||||
if strings.Contains(name, fcPath) {
|
if r.MatchString(name) {
|
||||||
if disk, err1 := io.EvalSymlinks(devPath + name); err1 == nil {
|
if disk, err1 := io.EvalSymlinks(devPath + name); err1 == nil {
|
||||||
dm := deviceUtil.FindMultipathDeviceForDevice(disk)
|
dm := deviceUtil.FindMultipathDeviceForDevice(disk)
|
||||||
klog.Infof("fc: find disk: %v, dm: %v", disk, dm)
|
klog.Infof("fc: find disk: %v, dm: %v", disk, dm)
|
||||||
|
@ -57,10 +57,16 @@ type fakeIOHandler struct{}
|
|||||||
func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
|
func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
|
||||||
switch dirname {
|
switch dirname {
|
||||||
case "/dev/disk/by-path/":
|
case "/dev/disk/by-path/":
|
||||||
f := &fakeFileInfo{
|
f1 := &fakeFileInfo{
|
||||||
name: "pci-0000:41:00.0-fc-0x500a0981891b8dc5-lun-0",
|
name: "pci-0000:41:00.0-fc-0x500a0981891b8dc5-lun-0",
|
||||||
}
|
}
|
||||||
return []os.FileInfo{f}, nil
|
f2 := &fakeFileInfo{
|
||||||
|
name: "fc-0x5005076810213b32-lun-2",
|
||||||
|
}
|
||||||
|
f3 := &fakeFileInfo{
|
||||||
|
name: "abc-0000:41:00.0-fc-0x5005076810213404-lun-0",
|
||||||
|
}
|
||||||
|
return []os.FileInfo{f1, f2, f3}, nil
|
||||||
case "/sys/block/":
|
case "/sys/block/":
|
||||||
f := &fakeFileInfo{
|
f := &fakeFileInfo{
|
||||||
name: "dm-1",
|
name: "dm-1",
|
||||||
@ -88,18 +94,58 @@ func (handler *fakeIOHandler) WriteFile(filename string, data []byte, perm os.Fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchDisk(t *testing.T) {
|
func TestSearchDisk(t *testing.T) {
|
||||||
fakeMounter := fcDiskMounter{
|
tests := []struct {
|
||||||
fcDisk: &fcDisk{
|
name string
|
||||||
|
wwns []string
|
||||||
|
lun string
|
||||||
|
expectError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "PCI disk",
|
||||||
wwns: []string{"500a0981891b8dc5"},
|
wwns: []string{"500a0981891b8dc5"},
|
||||||
lun: "0",
|
lun: "0",
|
||||||
io: &fakeIOHandler{},
|
|
||||||
},
|
},
|
||||||
deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
|
{
|
||||||
|
name: "Non PCI disk",
|
||||||
|
wwns: []string{"5005076810213b32"},
|
||||||
|
lun: "2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid Storage Controller",
|
||||||
|
wwns: []string{"5005076810213404"},
|
||||||
|
lun: "0",
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non existing disk",
|
||||||
|
wwns: []string{"500507681fffffff"},
|
||||||
|
lun: "0",
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
devicePath, error := searchDisk(fakeMounter)
|
|
||||||
// if no disk matches input wwn and lun, exit
|
for _, test := range tests {
|
||||||
if devicePath == "" || error != nil {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
t.Errorf("no fc disk found")
|
fakeMounter := fcDiskMounter{
|
||||||
|
fcDisk: &fcDisk{
|
||||||
|
wwns: test.wwns,
|
||||||
|
lun: test.lun,
|
||||||
|
io: &fakeIOHandler{},
|
||||||
|
},
|
||||||
|
deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
|
||||||
|
}
|
||||||
|
devicePath, err := searchDisk(fakeMounter)
|
||||||
|
if test.expectError && err == nil {
|
||||||
|
t.Errorf("expected error but got none")
|
||||||
|
}
|
||||||
|
if !test.expectError && err != nil {
|
||||||
|
t.Errorf("got unexpected error: %s", err)
|
||||||
|
}
|
||||||
|
// if no disk matches input wwn and lun, exit
|
||||||
|
if devicePath == "" && !test.expectError {
|
||||||
|
t.Errorf("no fc disk found")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user