Merge pull request #129744 from pjsharath28/automated-cherry-pick-of-#128997-upstream-release-1.31

Automated cherry pick of #128997: Replaced util.NewIOHandler() with fakeIOHandler to make UT pass on different host envs
This commit is contained in:
Kubernetes Prow Robot 2025-01-28 10:35:23 -08:00 committed by GitHub
commit 50c9c29e09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 102 additions and 3 deletions

View File

@ -0,0 +1,72 @@
//go:build linux
// +build linux
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fc
import (
"testing"
"k8s.io/kubernetes/pkg/volume/util"
)
func TestSearchDiskMultipathDevice(t *testing.T) {
tests := []struct {
name string
wwns []string
lun string
expectError bool
}{
{
name: "Non PCI disk 0",
wwns: []string{"500507681021a537"},
lun: "0",
},
{
name: "Non PCI disk 1",
wwns: []string{"500507681022a554"},
lun: "2",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fakeMounter := fcDiskMounter{
fcDisk: &fcDisk{
wwns: test.wwns,
lun: test.lun,
io: &fakeIOHandler{},
},
deviceUtil: util.NewDeviceHandler(&fakeIOHandler{}),
}
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")
}
if devicePath != "/dev/dm-1" {
t.Errorf("multipath device not found dm-1 expected got [%s]", devicePath)
}
})
}
}

View File

@ -17,6 +17,7 @@ limitations under the License.
package fc package fc
import ( import (
"errors"
"os" "os"
"reflect" "reflect"
"testing" "testing"
@ -75,7 +76,13 @@ func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
f6 := &fakeFileInfo{ f6 := &fakeFileInfo{
name: "fc-0x5005076810213b32-lun-25", name: "fc-0x5005076810213b32-lun-25",
} }
return []os.FileInfo{f4, f5, f6, f1, f2, f3}, nil f7 := &fakeFileInfo{
name: "fc-0x500507681021a537-lun-0",
}
f8 := &fakeFileInfo{
name: "fc-0x500507681022a554-lun-2",
}
return []os.FileInfo{f4, f5, f6, f1, f2, f3, f7, f8}, nil
case "/sys/block/": case "/sys/block/":
f := &fakeFileInfo{ f := &fakeFileInfo{
name: "dm-1", name: "dm-1",
@ -91,7 +98,15 @@ func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
} }
func (handler *fakeIOHandler) Lstat(name string) (os.FileInfo, error) { func (handler *fakeIOHandler) Lstat(name string) (os.FileInfo, error) {
return nil, nil links := map[string]string{
"/sys/block/dm-1/slaves/sde": "sde",
"/sys/block/dm-1/slaves/sdf": "sdf",
"/sys/block/dm-1/slaves/sdg": "sdg",
}
if dev, ok := links[name]; ok {
return &fakeFileInfo{name: dev}, nil
}
return nil, errors.New("device not found for mock")
} }
func (handler *fakeIOHandler) EvalSymlinks(path string) (string, error) { func (handler *fakeIOHandler) EvalSymlinks(path string) (string, error) {
@ -108,6 +123,14 @@ func (handler *fakeIOHandler) EvalSymlinks(path string) (string, error) {
return "/dev/sdx", nil return "/dev/sdx", nil
case "/dev/disk/by-id/scsi-3600508b400105e210000900000490000": case "/dev/disk/by-id/scsi-3600508b400105e210000900000490000":
return "/dev/sdd", nil return "/dev/sdd", nil
case "/dev/disk/by-path/fc-0x500507681021a537-lun-0":
return "/dev/sde", nil
case "/dev/disk/by-path/fc-0x500507681022a554-lun-2":
return "/dev/sdf", nil
case "/dev/sde":
return "/dev/sde", nil
case "/dev/sdf":
return "/dev/sdf", nil
} }
return "", nil return "", nil
} }
@ -116,6 +139,10 @@ func (handler *fakeIOHandler) WriteFile(filename string, data []byte, perm os.Fi
return nil return nil
} }
func (handler *fakeIOHandler) ReadFile(filename string) ([]byte, error) {
return nil, nil
}
func TestSearchDisk(t *testing.T) { func TestSearchDisk(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@ -164,7 +191,7 @@ func TestSearchDisk(t *testing.T) {
lun: test.lun, lun: test.lun,
io: &fakeIOHandler{}, io: &fakeIOHandler{},
}, },
deviceUtil: util.NewDeviceHandler(util.NewIOHandler()), deviceUtil: util.NewDeviceHandler(&fakeIOHandler{}),
} }
devicePath, err := searchDisk(fakeMounter) devicePath, err := searchDisk(fakeMounter)
if test.expectError && err == nil { if test.expectError && err == nil {