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:
		
							
								
								
									
										72
									
								
								pkg/volume/fc/fc_util_linux_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								pkg/volume/fc/fc_util_linux_test.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -17,6 +17,7 @@ limitations under the License.
 | 
			
		||||
package fc
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"os"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"testing"
 | 
			
		||||
@@ -75,7 +76,13 @@ func (handler *fakeIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
 | 
			
		||||
		f6 := &fakeFileInfo{
 | 
			
		||||
			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/":
 | 
			
		||||
		f := &fakeFileInfo{
 | 
			
		||||
			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) {
 | 
			
		||||
	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) {
 | 
			
		||||
@@ -108,6 +123,14 @@ func (handler *fakeIOHandler) EvalSymlinks(path string) (string, error) {
 | 
			
		||||
		return "/dev/sdx", nil
 | 
			
		||||
	case "/dev/disk/by-id/scsi-3600508b400105e210000900000490000":
 | 
			
		||||
		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
 | 
			
		||||
}
 | 
			
		||||
@@ -116,6 +139,10 @@ func (handler *fakeIOHandler) WriteFile(filename string, data []byte, perm os.Fi
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (handler *fakeIOHandler) ReadFile(filename string) ([]byte, error) {
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSearchDisk(t *testing.T) {
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name        string
 | 
			
		||||
@@ -164,7 +191,7 @@ func TestSearchDisk(t *testing.T) {
 | 
			
		||||
					lun:  test.lun,
 | 
			
		||||
					io:   &fakeIOHandler{},
 | 
			
		||||
				},
 | 
			
		||||
				deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
 | 
			
		||||
				deviceUtil: util.NewDeviceHandler(&fakeIOHandler{}),
 | 
			
		||||
			}
 | 
			
		||||
			devicePath, err := searchDisk(fakeMounter)
 | 
			
		||||
			if test.expectError && err == nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user