Skip to content

Commit 4a75c7e

Browse files
committed
Fix fcpath
1 parent ee297b6 commit 4a75c7e

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

pkg/volume/fc/fc_util.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
"regexp"
2425
"strconv"
2526
"strings"
2627

@@ -60,12 +61,13 @@ func (handler *osIOHandler) WriteFile(filename string, data []byte, perm os.File
6061

6162
// given a wwn and lun, find the device and associated devicemapper parent
6263
func findDisk(wwn, lun string, io ioHandler, deviceUtil volumeutil.DeviceUtil) (string, string) {
63-
fcPath := "-fc-0x" + wwn + "-lun-" + lun
64+
fcPathExp := "^(pci-.*-fc|fc)-0x" + wwn + "-lun-" + lun
65+
r := regexp.MustCompile(fcPathExp)
6466
devPath := byPath
6567
if dirs, err := io.ReadDir(devPath); err == nil {
6668
for _, f := range dirs {
6769
name := f.Name()
68-
if strings.Contains(name, fcPath) {
70+
if r.MatchString(name) {
6971
if disk, err1 := io.EvalSymlinks(devPath + name); err1 == nil {
7072
dm := deviceUtil.FindMultipathDeviceForDevice(disk)
7173
klog.Infof("fc: find disk: %v, dm: %v", disk, dm)

pkg/volume/fc/fc_util_test.go

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ type fakeIOHandler struct{}
5757
func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
5858
switch dirname {
5959
case "/dev/disk/by-path/":
60-
f := &fakeFileInfo{
60+
f1 := &fakeFileInfo{
6161
name: "pci-0000:41:00.0-fc-0x500a0981891b8dc5-lun-0",
6262
}
63-
return []os.FileInfo{f}, nil
63+
f2 := &fakeFileInfo{
64+
name: "fc-0x5005076810213b32-lun-2",
65+
}
66+
f3 := &fakeFileInfo{
67+
name: "abc-0000:41:00.0-fc-0x5005076810213404-lun-0",
68+
}
69+
return []os.FileInfo{f1, f2, f3}, nil
6470
case "/sys/block/":
6571
f := &fakeFileInfo{
6672
name: "dm-1",
@@ -88,18 +94,58 @@ func (handler *fakeIOHandler) WriteFile(filename string, data []byte, perm os.Fi
8894
}
8995

9096
func TestSearchDisk(t *testing.T) {
91-
fakeMounter := fcDiskMounter{
92-
fcDisk: &fcDisk{
97+
tests := []struct {
98+
name string
99+
wwns []string
100+
lun string
101+
expectError bool
102+
}{
103+
{
104+
name: "PCI disk",
93105
wwns: []string{"500a0981891b8dc5"},
94106
lun: "0",
95-
io: &fakeIOHandler{},
96107
},
97-
deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
108+
{
109+
name: "Non PCI disk",
110+
wwns: []string{"5005076810213b32"},
111+
lun: "2",
112+
},
113+
{
114+
name: "Invalid Storage Controller",
115+
wwns: []string{"5005076810213404"},
116+
lun: "0",
117+
expectError: true,
118+
},
119+
{
120+
name: "Non existing disk",
121+
wwns: []string{"500507681fffffff"},
122+
lun: "0",
123+
expectError: true,
124+
},
98125
}
99-
devicePath, error := searchDisk(fakeMounter)
100-
// if no disk matches input wwn and lun, exit
101-
if devicePath == "" || error != nil {
102-
t.Errorf("no fc disk found")
126+
127+
for _, test := range tests {
128+
t.Run(test.name, func(t *testing.T) {
129+
fakeMounter := fcDiskMounter{
130+
fcDisk: &fcDisk{
131+
wwns: test.wwns,
132+
lun: test.lun,
133+
io: &fakeIOHandler{},
134+
},
135+
deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
136+
}
137+
devicePath, err := searchDisk(fakeMounter)
138+
if test.expectError && err == nil {
139+
t.Errorf("expected error but got none")
140+
}
141+
if !test.expectError && err != nil {
142+
t.Errorf("got unexpected error: %s", err)
143+
}
144+
// if no disk matches input wwn and lun, exit
145+
if devicePath == "" && !test.expectError {
146+
t.Errorf("no fc disk found")
147+
}
148+
})
103149
}
104150
}
105151

0 commit comments

Comments
 (0)