Skip to content

Commit 2be1833

Browse files
authored
Merge pull request kubernetes#128997 from srivastav-abhishek/fix-search-disk-test
Replaced util.NewIOHandler() with fakeIOHandler to make UT pass on different host envs
2 parents bd55d18 + 41f805b commit 2be1833

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//go:build linux
2+
// +build linux
3+
4+
/*
5+
Copyright 2024 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package fc
21+
22+
import (
23+
"testing"
24+
25+
"k8s.io/kubernetes/pkg/volume/util"
26+
)
27+
28+
func TestSearchDiskMultipathDevice(t *testing.T) {
29+
tests := []struct {
30+
name string
31+
wwns []string
32+
lun string
33+
expectError bool
34+
}{
35+
{
36+
name: "Non PCI disk 0",
37+
wwns: []string{"500507681021a537"},
38+
lun: "0",
39+
},
40+
{
41+
name: "Non PCI disk 1",
42+
wwns: []string{"500507681022a554"},
43+
lun: "2",
44+
},
45+
}
46+
for _, test := range tests {
47+
t.Run(test.name, func(t *testing.T) {
48+
fakeMounter := fcDiskMounter{
49+
fcDisk: &fcDisk{
50+
wwns: test.wwns,
51+
lun: test.lun,
52+
io: &fakeIOHandler{},
53+
},
54+
deviceUtil: util.NewDeviceHandler(&fakeIOHandler{}),
55+
}
56+
devicePath, err := searchDisk(fakeMounter)
57+
if test.expectError && err == nil {
58+
t.Errorf("expected error but got none")
59+
}
60+
if !test.expectError && err != nil {
61+
t.Errorf("got unexpected error: %s", err)
62+
}
63+
// if no disk matches input wwn and lun, exit
64+
if devicePath == "" && !test.expectError {
65+
t.Errorf("no fc disk found")
66+
}
67+
if devicePath != "/dev/dm-1" {
68+
t.Errorf("multipath device not found dm-1 expected got [%s]", devicePath)
69+
}
70+
})
71+
}
72+
}

pkg/volume/fc/fc_util_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package fc
1818

1919
import (
20+
"errors"
2021
"os"
2122
"reflect"
2223
"testing"
@@ -75,7 +76,13 @@ func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
7576
f6 := &fakeFileInfo{
7677
name: "fc-0x5005076810213b32-lun-25",
7778
}
78-
return []os.FileInfo{f4, f5, f6, f1, f2, f3}, nil
79+
f7 := &fakeFileInfo{
80+
name: "fc-0x500507681021a537-lun-0",
81+
}
82+
f8 := &fakeFileInfo{
83+
name: "fc-0x500507681022a554-lun-2",
84+
}
85+
return []os.FileInfo{f4, f5, f6, f1, f2, f3, f7, f8}, nil
7986
case "/sys/block/":
8087
f := &fakeFileInfo{
8188
name: "dm-1",
@@ -91,7 +98,15 @@ func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
9198
}
9299

93100
func (handler *fakeIOHandler) Lstat(name string) (os.FileInfo, error) {
94-
return nil, nil
101+
links := map[string]string{
102+
"/sys/block/dm-1/slaves/sde": "sde",
103+
"/sys/block/dm-1/slaves/sdf": "sdf",
104+
"/sys/block/dm-1/slaves/sdg": "sdg",
105+
}
106+
if dev, ok := links[name]; ok {
107+
return &fakeFileInfo{name: dev}, nil
108+
}
109+
return nil, errors.New("device not found for mock")
95110
}
96111

97112
func (handler *fakeIOHandler) EvalSymlinks(path string) (string, error) {
@@ -108,6 +123,14 @@ func (handler *fakeIOHandler) EvalSymlinks(path string) (string, error) {
108123
return "/dev/sdx", nil
109124
case "/dev/disk/by-id/scsi-3600508b400105e210000900000490000":
110125
return "/dev/sdd", nil
126+
case "/dev/disk/by-path/fc-0x500507681021a537-lun-0":
127+
return "/dev/sde", nil
128+
case "/dev/disk/by-path/fc-0x500507681022a554-lun-2":
129+
return "/dev/sdf", nil
130+
case "/dev/sde":
131+
return "/dev/sde", nil
132+
case "/dev/sdf":
133+
return "/dev/sdf", nil
111134
}
112135
return "", nil
113136
}
@@ -116,6 +139,10 @@ func (handler *fakeIOHandler) WriteFile(filename string, data []byte, perm os.Fi
116139
return nil
117140
}
118141

142+
func (handler *fakeIOHandler) ReadFile(filename string) ([]byte, error) {
143+
return nil, nil
144+
}
145+
119146
func TestSearchDisk(t *testing.T) {
120147
tests := []struct {
121148
name string
@@ -164,7 +191,7 @@ func TestSearchDisk(t *testing.T) {
164191
lun: test.lun,
165192
io: &fakeIOHandler{},
166193
},
167-
deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
194+
deviceUtil: util.NewDeviceHandler(&fakeIOHandler{}),
168195
}
169196
devicePath, err := searchDisk(fakeMounter)
170197
if test.expectError && err == nil {

0 commit comments

Comments
 (0)