diff --git a/cli/resources/logs_helper_test.go b/cli/resources/logs_helper_test.go index ebdeca8..3981f92 100644 --- a/cli/resources/logs_helper_test.go +++ b/cli/resources/logs_helper_test.go @@ -23,7 +23,6 @@ import ( "testing" "github.com/agiledragon/gomonkey/v2" - coreV1 "k8s.io/api/core/v1" ) diff --git a/connector/connector_utils_test.go b/connector/connector_utils_test.go index f59a737..f803896 100644 --- a/connector/connector_utils_test.go +++ b/connector/connector_utils_test.go @@ -67,15 +67,6 @@ func TestGetDevice(t *testing.T) { } func TestGetDeviceLink(t *testing.T) { - const ( - stubTgtLunGUID = "test123456" - - normalCmdOutput = "test output" - nofileCmdOutput = "No such file or directory" - emptyCmdOutput = "" - otherErrorCmdOutput = "other result" - ) - var stubCtx = context.TODO() type args struct { @@ -93,11 +84,14 @@ func TestGetDeviceLink(t *testing.T) { want string wantErr bool }{ - // TODO: Add test cases. - {"Normal", args{stubCtx, stubTgtLunGUID}, outputs{normalCmdOutput, nil}, "test output", false}, - {"EmptyCmdResult", args{stubCtx, stubTgtLunGUID}, outputs{emptyCmdOutput, errors.New("test")}, "", false}, - {"CmdResultIsFileOrDirectoryNoExist", args{stubCtx, stubTgtLunGUID}, outputs{nofileCmdOutput, errors.New("test")}, "", false}, - {"CmdResultIsOtherError", args{stubCtx, stubTgtLunGUID}, outputs{otherErrorCmdOutput, errors.New("test")}, "", true}, + {"Normal", args{stubCtx, "test123456"}, + outputs{"test output", nil}, "test output", false}, + {"EmptyCmdResult", args{stubCtx, "test123456"}, + outputs{"", errors.New("test")}, "", false}, + {"CmdResultIsFileOrDirectoryNoExist", args{stubCtx, "test123456"}, + outputs{"No such file or directory", errors.New("test")}, "", false}, + {"CmdResultIsOtherError", args{stubCtx, "test123456"}, + outputs{"other result", errors.New("test")}, "", true}, } stub := utils.ExecShellCmd @@ -364,34 +358,6 @@ func TestXfsResize(t *testing.T) { } func TestGetVirtualDevice(t *testing.T) { - type args struct { - ctx context.Context - LunWWN string - } - type outputs struct { - output []string - cmdOutput string - err error - cmdErr error - } - tests := []struct { - name string - args args - mockOutputs outputs - wantDeviceName string - wantDeviceKind int - wantErr bool - }{ - {"NormalUltrapath*", args{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, outputs{[]string{"ultrapathh"}, "", nil, nil}, "ultrapathh", UseUltraPathNVMe, false}, - {"NormalDm-*", args{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, outputs{[]string{"dm-2"}, "lrwxrwxrwx. 1 root root 7 Mar 14 10:26 mpatha -> ../dm-2", nil, nil}, "dm-2", UseDMMultipath, false}, - {"NormalPhysicalSd*", args{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, outputs{[]string{"sdd"}, "", nil, nil}, "sdd", NotUseMultipath, false}, - {"NormalPhysicalNVMe*", args{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, outputs{[]string{"nvme1n1"}, "", nil, nil}, "nvme1n1", NotUseMultipath, false}, - {"ErrorMultiUltrapath*", args{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, outputs{[]string{"ultrapathh", "ultrapathi"}, "", nil, nil}, "", 0, true}, - {"ErrorPartitionUltrapath*", args{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, outputs{[]string{"ultrapathh", "ultrapathh2"}, "", nil, nil}, "ultrapathh", UseUltraPathNVMe, false}, - {"ErrorPartitionDm-*", args{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, outputs{[]string{"dm-2"}, "lrwxrwxrwx. 1 root root 7 Mar 14 10:26 mpatha2 -> ../dm-2", nil, nil}, "", 0, false}, - {"ErrorPartitionNvme*", args{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, outputs{[]string{"nvme1n1", "nvme1n1p1"}, "", nil, nil}, "nvme1n1", 0, false}, - } - stub := GetDevicesByGUID stub2 := utils.ExecShellCmd defer func() { @@ -399,6 +365,7 @@ func TestGetVirtualDevice(t *testing.T) { utils.ExecShellCmd = stub2 }() + tests := getVirtualDeviceTest() for _, tt := range tests { GetDevicesByGUID = func(_ context.Context, tgtLunGUID string) ([]string, error) { return tt.mockOutputs.output, tt.mockOutputs.err @@ -417,6 +384,70 @@ func TestGetVirtualDevice(t *testing.T) { } } +type VirtualDeviceArgs struct { + ctx context.Context + LunWWN string +} +type VirtualDeviceOutputs struct { + output []string + cmdOutput string + err error + cmdErr error +} + +func getVirtualDeviceTest() []struct { + name string + args VirtualDeviceArgs + mockOutputs VirtualDeviceOutputs + wantDeviceName string + wantDeviceKind int + wantErr bool +} { + return []struct { + name string + args VirtualDeviceArgs + mockOutputs VirtualDeviceOutputs + wantDeviceName string + wantDeviceKind int + wantErr bool + }{ + {"NormalUltrapath*", + VirtualDeviceArgs{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, + VirtualDeviceOutputs{[]string{"ultrapathh"}, + "", nil, nil}, "ultrapathh", UseUltraPathNVMe, false}, + {"NormalDm-*", + VirtualDeviceArgs{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, + VirtualDeviceOutputs{[]string{"dm-2"}, + "lrwxrwxrwx. 1 root root 7 Mar 14 10:26 mpatha -> ../dm-2", nil, nil}, + "dm-2", UseDMMultipath, false}, + {"NormalPhysicalSd*", + VirtualDeviceArgs{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, + VirtualDeviceOutputs{[]string{"sdd"}, + "", nil, nil}, "sdd", NotUseMultipath, false}, + {"NormalPhysicalNVMe*", + VirtualDeviceArgs{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, + VirtualDeviceOutputs{[]string{"nvme1n1"}, + "", nil, nil}, "nvme1n1", NotUseMultipath, false}, + {"ErrorMultiUltrapath*", + VirtualDeviceArgs{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, + VirtualDeviceOutputs{[]string{"ultrapathh", "ultrapathi"}, + "", nil, nil}, "", 0, true}, + {"ErrorPartitionUltrapath*", + VirtualDeviceArgs{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, + VirtualDeviceOutputs{[]string{"ultrapathh", "ultrapathh2"}, + "", nil, nil}, "ultrapathh", UseUltraPathNVMe, false}, + {"ErrorPartitionDm-*", + VirtualDeviceArgs{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, + VirtualDeviceOutputs{[]string{"dm-2"}, + "lrwxrwxrwx. 1 root root 7 Mar 14 10:26 mpatha2 -> ../dm-2", nil, nil}, + "", 0, false}, + {"ErrorPartitionNvme*", + VirtualDeviceArgs{context.TODO(), "7100e98b8e19b76d00e4069a00000003"}, + VirtualDeviceOutputs{[]string{"nvme1n1", "nvme1n1p1"}, + "", nil, nil}, "nvme1n1", 0, false}, + } +} + func TestWatchDMDevice(t *testing.T) { var cases = []struct { name string @@ -428,36 +459,12 @@ func TestWatchDMDevice(t *testing.T) { pathCompleteTime time.Duration err error }{ - { - "Normal", - "6582575100bc510f12345678000103e8", - "dm-0", - 3, - []string{"sdb", "sdc", "sdd"}, - 100 * time.Millisecond, - 100 * time.Millisecond, - nil, - }, - { - "PathIncomplete", - "6582575100bc510f12345678000103e8", - "dm-0", - 3, - []string{"sdb", "sdc"}, - 100 * time.Millisecond, - 100 * time.Millisecond, - errors.New(VolumePathIncomplete), - }, - { - "Timeout", - "6582575100bc510f12345678000103e8", - "dm-0", - 3, - []string{"sdb", "sdc", "sdd"}, - 100 * time.Millisecond, - 10000 * time.Millisecond, - errors.New(VolumeNotFound), - }, + {"Normal", "6582575100bc510f12345678000103e8", "dm-0", 3, []string{"sdb", "sdc", "sdd"}, + 100 * time.Millisecond, 100 * time.Millisecond, nil}, + {"PathIncomplete", "6582575100bc510f12345678000103e8", "dm-0", 3, []string{"sdb", "sdc"}, + 100 * time.Millisecond, 100 * time.Millisecond, errors.New(VolumePathIncomplete)}, + {"Timeout", "6582575100bc510f12345678000103e8", "dm-0", 3, []string{"sdb", "sdc", "sdd"}, + 100 * time.Millisecond, 10000 * time.Millisecond, errors.New(VolumeNotFound)}, } stubs := gostub.New() @@ -521,7 +528,8 @@ func TestGetFsTypeByDevPath(t *testing.T) { t.Run(tt.name, func(t *testing.T) { fsType, err := GetFsTypeByDevPath(tt.args.ctx, tt.args.devPath) if (err != nil) != tt.wantErr || fsType != tt.want { - t.Errorf("Test GetFsTypeByDevPath() error = %v, wantErr: [%v]; fsType: [%s], want: [%s]", err, tt.wantErr, fsType, tt.want) + t.Errorf("Test GetFsTypeByDevPath() error = %v, wantErr: [%v]; fsType: [%s], want: [%s]", + err, tt.wantErr, fsType, tt.want) } }) } @@ -571,38 +579,15 @@ func TestGetDeviceFromMountFile(t *testing.T) { mountMap map[string]string wantErr bool }{ - { - name: "test_device_not_exist", - targetPath: "/mnt/test", - checkDevRef: true, - want: "", - mountMap: map[string]string{}, - wantErr: true, - }, - { - name: "test_device_exist_and_ref_one_path", - targetPath: "/mnt/test1", - checkDevRef: true, - want: "/dev/sda", - mountMap: map[string]string{"/mnt/test1": "/dev/sda", "/mnt/test2": "/dev/sdb"}, - wantErr: false, - }, - { - name: "test_device_exist_and_ref_multiple_path", - targetPath: "/mnt/test1", - checkDevRef: true, - want: "", - mountMap: map[string]string{"/mnt/test1": "/dev/sda", "/mnt/test2": "/dev/sda"}, - wantErr: true, - }, - { - name: "test_device_exist_and_ref_multiple_path_and_no_check", - targetPath: "/mnt/test1", - checkDevRef: false, - want: "/dev/sda", - mountMap: map[string]string{"/mnt/test1": "/dev/sda", "/mnt/test2": "/dev/sda"}, - wantErr: false, - }, + {name: "test_device_not_exist", targetPath: "/mnt/test", + checkDevRef: true, want: "", mountMap: map[string]string{}, wantErr: true}, + {name: "test_device_exist_and_ref_one_path", targetPath: "/mnt/test1", checkDevRef: true, want: "/dev/sda", + mountMap: map[string]string{"/mnt/test1": "/dev/sda", "/mnt/test2": "/dev/sdb"}, wantErr: false}, + {name: "test_device_exist_and_ref_multiple_path", targetPath: "/mnt/test1", checkDevRef: true, want: "", + mountMap: map[string]string{"/mnt/test1": "/dev/sda", "/mnt/test2": "/dev/sda"}, wantErr: true}, + {name: "test_device_exist_and_ref_multiple_path_and_no_check", targetPath: "/mnt/test1", checkDevRef: false, + want: "/dev/sda", mountMap: map[string]string{"/mnt/test1": "/dev/sda", "/mnt/test2": "/dev/sda"}, + wantErr: false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/connector/connector_utils_ultrapath_test.go b/connector/connector_utils_ultrapath_test.go index 5580177..e5323b8 100644 --- a/connector/connector_utils_ultrapath_test.go +++ b/connector/connector_utils_ultrapath_test.go @@ -27,37 +27,22 @@ import ( "github.com/Huawei/eSDK_K8S_Plugin/v4/utils" ) +type RunUpCommandArgs struct { + ctx context.Context + upType string + format string + args []interface{} +} + func TestRunUpCommand(t *testing.T) { const stubFormat = "show vlun | grep -w %s" - var stubCtx = context.TODO() - var stubArgs = []interface{}{"test-targetLunWWN", "test-devName"} - - type args struct { - ctx context.Context - upType string - format string - args []interface{} - } - var ultraPathCommandArgs = args{ - stubCtx, - UltraPathCommand, - stubFormat, - stubArgs[:1], - } - var UltraPathNVMeCommandArgs = args{ - stubCtx, - UltraPathNVMeCommand, - stubFormat, - stubArgs[1:], - } - var noneUpTypeArgs = args{ - stubCtx, - "", - stubFormat, - stubArgs, - } + var ultraPathCommandArgs = RunUpCommandArgs{ctx: stubCtx, + upType: UltraPathCommand, format: stubFormat, args: stubArgs[:1]} + var UltraPathNVMeCommandArgs = RunUpCommandArgs{ctx: stubCtx, + upType: UltraPathNVMeCommand, format: stubFormat, args: stubArgs[1:]} + var noneUpTypeArgs = RunUpCommandArgs{ctx: stubCtx, upType: "", format: stubFormat, args: stubArgs} type outputs struct { output string @@ -67,7 +52,7 @@ func TestRunUpCommand(t *testing.T) { tests := []struct { name string - args args + args RunUpCommandArgs outputs outputs want string wantErr bool @@ -78,9 +63,7 @@ func TestRunUpCommand(t *testing.T) { } stub := utils.ExecShellCmd - defer func() { - utils.ExecShellCmd = stub - }() + defer func() { utils.ExecShellCmd = stub }() for _, tt := range tests { utils.ExecShellCmd = func(_ context.Context, format string, args ...interface{}) (string, error) { return tt.outputs.output, tt.outputs.err diff --git a/connector/local/local_test.go b/connector/local/local_test.go index 3aed9a5..4cc516e 100644 --- a/connector/local/local_test.go +++ b/connector/local/local_test.go @@ -49,32 +49,11 @@ func TestConnectVolume(t *testing.T) { want string wantErr bool }{ - { - name: "NoTgtLunWWN", - args: args{ - ctx: ctx, - conn: map[string]interface{}{}}, - want: "", - wantErr: true, - }, - { - name: "devPathNoExist", - args: args{ - ctx: ctx, - conn: map[string]interface{}{"tgtLunWWN": "test"}, - }, - want: "", - wantErr: false, - }, - { - name: "Normal", - args: args{ - ctx: ctx, - conn: map[string]interface{}{"tgtLunWWN": "tgtLunWWN"}, - }, - want: "/dev/disk/by-id/wwn-0xtgtLunWWN", - wantErr: false, - }, + {name: "NoTgtLunWWN", args: args{ctx: ctx, conn: map[string]interface{}{}}, want: "", wantErr: true}, + {name: "devPathNoExist", args: args{ctx: ctx, conn: map[string]interface{}{"tgtLunWWN": "test"}}, + want: "", wantErr: false}, + {name: "Normal", args: args{ctx: ctx, conn: map[string]interface{}{ + "tgtLunWWN": "tgtLunWWN"}}, want: "/dev/disk/by-id/wwn-0xtgtLunWWN", wantErr: false}, } var interval = waitDevOnlineTimeInterval @@ -116,30 +95,9 @@ func TestDisConnectVolume(t *testing.T) { args args wantErr bool }{ - { - name: "EmptyTgtLunWWN", - args: args{ - ctx: ctx, - tgtLunWWN: "", - }, - wantErr: false, - }, - { - name: "DeviceNotExist", - args: args{ - ctx: ctx, - tgtLunWWN: "test", - }, - wantErr: false, - }, - { - name: "Normal", - args: args{ - ctx: ctx, - tgtLunWWN: "tgtLunWWN", - }, - wantErr: true, - }, + {name: "EmptyTgtLunWWN", args: args{ctx: ctx, tgtLunWWN: ""}, wantErr: false}, + {name: "DeviceNotExist", args: args{ctx: ctx, tgtLunWWN: "test"}, wantErr: false}, + {name: "Normal", args: args{ctx: ctx, tgtLunWWN: "tgtLunWWN"}, wantErr: true}, } stubs := gostub.Stub(&connector.DisconnectVolumeTimeOut, time.Millisecond) diff --git a/connector/nfs/nfs_test.go b/connector/nfs/nfs_test.go index 191961c..4a6fe12 100644 --- a/connector/nfs/nfs_test.go +++ b/connector/nfs/nfs_test.go @@ -48,7 +48,6 @@ func testExecShellCmd(_ context.Context, format string, args ...interface{}) (st } func TestConnectVolume(t *testing.T) { - var ctx = context.TODO() if err := os.MkdirAll("test-sourcePath", 0750); err != nil { t.Fatal("can not create a source path") @@ -56,78 +55,20 @@ func TestConnectVolume(t *testing.T) { defer utils.RemoveDir("test-sourcePath", "test-sourcePath") defer utils.RemoveDir("test-targetPath", "test-targetPath") - var blockConnMap = map[string]interface{}{ - "srcType": "block", - "sourcePath": "test-sourcePath", - "targetPath": "test-targetPath", - "fsType": "", - "mountFlags": "", - } - var existFsTypeIsEmptyMap = map[string]interface{}{ - "srcType": "block", - "sourcePath": "sourcePath", - "targetPath": "test-targetPath", - "fsType": "", - "mountFlags": "", - } - var fsConnMap = map[string]interface{}{ - "srcType": "fs", - "sourcePath": "test-sourcePath", - "targetPath": "test", - "fsType": "", - "mountFlags": "test-flag", - } - var otherSrcTypeMap = map[string]interface{}{ - "srcType": "test", - } - var emptySrcTypeMap = map[string]interface{}{ - "srcType": "", - } - var emptySourcePathMap = map[string]interface{}{ - "srcType": "block", - "sourcePath": "", - } - var emptyTargetPathMap = map[string]interface{}{ - "srcType": "fs", - "sourcePath": "testSourcePath", - "targetPath": "", - } - - type args struct { - ctx context.Context - conn map[string]interface{} - } - tests := []struct { - name string - args args - want string - wantErr bool - }{ - {"EmptySrcType", args{ctx, emptySrcTypeMap}, "", true}, - {"EmptySourcePath", args{ctx, emptySourcePathMap}, "", true}, - {"EmptyTargetPath", args{ctx, emptyTargetPathMap}, "", true}, - - {"SrcTypeIsOther", args{ctx, otherSrcTypeMap}, "", true}, - {"SrcTypeIsFS", args{ctx, fsConnMap}, "", false}, - - {"SrcTypeIsBlock", args{ctx, blockConnMap}, "", false}, - {"ExistFsTypeIsEmpty", args{ctx, existFsTypeIsEmptyMap}, "", true}, - } - stubs := gostub.StubFunc(&connector.ReadDevice, []byte{}, nil) - defer stubs.Reset() - stubs.StubFunc(&utils.PathExist, true, nil) stubs.StubFunc(&connector.ResizeMountPath, nil) stubs.StubFunc(&connector.IsInFormatting, false, nil) stubs.StubFunc(&connector.GetDeviceSize, int64(halfTiSizeBytes), nil) stubs.Stub(&utils.ExecShellCmd, testExecShellCmd) + defer stubs.Reset() readFile := gomonkey.ApplyFunc(ioutil.ReadFile, func(filename string) ([]byte, error) { return []byte("test test\n"), nil }) defer readFile.Reset() + tests := getConnectVolumeTests() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { nfs := &Connector{} @@ -143,6 +84,45 @@ func TestConnectVolume(t *testing.T) { } } +type args struct { + ctx context.Context + conn map[string]interface{} +} + +func getConnectVolumeTests() []struct { + name string + args args + want string + wantErr bool +} { + var ctx = context.TODO() + var blockConnMap = map[string]any{"srcType": "block", "sourcePath": "test-sourcePath", + "targetPath": "test-targetPath", "fsType": "", "mountFlags": ""} + var existFsTypeIsEmptyMap = map[string]any{"srcType": "block", "sourcePath": "sourcePath", + "targetPath": "test-targetPath", "fsType": "", "mountFlags": ""} + var fsConnMap = map[string]any{"srcType": "fs", "sourcePath": "test-sourcePath", + "targetPath": "test", "fsType": "", "mountFlags": "test-flag"} + var otherSrcTypeMap = map[string]any{"srcType": "test"} + var emptySrcTypeMap = map[string]any{"srcType": ""} + var emptySourcePathMap = map[string]any{"srcType": "block", "sourcePath": ""} + var emptyTargetPathMap = map[string]any{"srcType": "fs", "sourcePath": "testSourcePath", "targetPath": ""} + + return []struct { + name string + args args + want string + wantErr bool + }{ + {"EmptySrcType", args{ctx, emptySrcTypeMap}, "", true}, + {"EmptySourcePath", args{ctx, emptySourcePathMap}, "", true}, + {"EmptyTargetPath", args{ctx, emptyTargetPathMap}, "", true}, + {"SrcTypeIsOther", args{ctx, otherSrcTypeMap}, "", true}, + {"SrcTypeIsFS", args{ctx, fsConnMap}, "", false}, + {"SrcTypeIsBlock", args{ctx, blockConnMap}, "", false}, + {"ExistFsTypeIsEmpty", args{ctx, existFsTypeIsEmptyMap}, "", true}, + } +} + func TestDisConnectVolume(t *testing.T) { var ctx = context.TODO() diff --git a/connector/nvme/nvme_test.go b/connector/nvme/nvme_test.go index c5ea2c1..70af2b2 100644 --- a/connector/nvme/nvme_test.go +++ b/connector/nvme/nvme_test.go @@ -37,50 +37,26 @@ const ( logName = "nvmeTest.log" ) +type args struct { + ctx context.Context + conn map[string]any +} + func TestConnectVolume(t *testing.T) { var ctx = context.TODO() - var mutex = sync.Mutex{} + var GetSubSysInfoOutput = map[string]any{ + "Subsystems": []any{map[string]any{"Paths": []any{map[string]any{ + "Transport": "fc", "State": "live", "Name": "channelName", "Address": "address"}}}}} + var normalConnMap = map[string]any{"tgtLunGuid": "LunGUID", "volumeUseMultiPath": true, + "multiPathType": "UseUltraPath", + "portWWNList": []PortWWNPair{{"address", "address"}}} + var noTgtLunGuidConnMap = map[string]any{} + var noVolumeUseMultiPathConnMap = map[string]any{"tgtLunGuid": "LunGUID"} + var noMultiPathTypeConnMap = map[string]any{"tgtLunGuid": "LunGUID", "volumeUseMultiPath": true} + var noPortWWNListConnMap = map[string]any{"tgtLunGuid": "LunGUID", "volumeUseMultiPath": true, + "multiPathType": "UseUltraPath"} - var GetSubSysInfoOutput = map[string]interface{}{ - "Subsystems": []interface{}{ - map[string]interface{}{ - "Paths": []interface{}{ - map[string]interface{}{ - "Transport": "fc", - "State": "live", - "Name": "channelName", - "Address": "address", - }, - }, - }, - }, - } - - var normalConnMap = map[string]interface{}{ - "tgtLunGuid": "LunGUID", - "volumeUseMultiPath": true, - "multiPathType": "UseUltraPath", - "portWWNList": []PortWWNPair{{"address", "address"}}, - } - var noTgtLunGuidConnMap = map[string]interface{}{} - var noVolumeUseMultiPathConnMap = map[string]interface{}{ - "tgtLunGuid": "LunGUID", - } - var noMultiPathTypeConnMap = map[string]interface{}{ - "tgtLunGuid": "LunGUID", - "volumeUseMultiPath": true, - } - var noPortWWNListConnMap = map[string]interface{}{ - "tgtLunGuid": "LunGUID", - "volumeUseMultiPath": true, - "multiPathType": "UseUltraPath", - } - - type args struct { - ctx context.Context - conn map[string]interface{} - } tests := []struct { name string mutex sync.Mutex @@ -94,7 +70,6 @@ func TestConnectVolume(t *testing.T) { {"NoMultiPathType", mutex, args{ctx, noMultiPathTypeConnMap}, "", true}, {"NoPortWWNList", mutex, args{ctx, noPortWWNListConnMap}, "", true}, } - stubs := gostub.StubFunc(&connector.GetSubSysInfo, GetSubSysInfoOutput, nil) defer stubs.Reset() stubs.StubFunc(&connector.DoScanNVMeDevice, nil) @@ -103,9 +78,7 @@ func TestConnectVolume(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - fc := &FCNVMe{ - mutex: tt.mutex, - } + fc := &FCNVMe{mutex: tt.mutex} got, err := fc.ConnectVolume(tt.args.ctx, tt.args.conn) if (err != nil) != tt.wantErr { t.Errorf("ConnectVolume() error = %v, wantErr %v", err, tt.wantErr) diff --git a/csi/backend/backend_test.go b/csi/backend/backend_test.go index dd2f78b..8af4db1 100644 --- a/csi/backend/backend_test.go +++ b/csi/backend/backend_test.go @@ -768,42 +768,24 @@ func TestFilterByStorageQuota(t *testing.T) { expect int64 expectErr bool }{ - {"NormalSoftQuota", - `{"spaceQuota": "softQuota", "gracePeriod": 100}`, - []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, - 1, - false, - }, - {"NormalHardQuota", - `{"spaceQuota": "hardQuota"}`, - []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, - 1, - false, - }, - {"NegativePeriod", - `{"spaceQuota": "hardQuota", "gracePeriod": -1}`, - []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, - 0, - true, - }, - {"ExceedsTheMaximumPeriod", - `{"spaceQuota": "hardQuota", "gracePeriod": 4294967295}`, - []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, - 0, - true, - }, - {"WrongType", - `{"spaceQuota": "WrongType"`, - []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, - 0, - true, - }, - {"HardWithPeriod", - `{"spaceQuota": "hardQuota", "gracePeriod": 10}`, - []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, - 0, - true, - }, + {name: "NormalSoftQuota", storageQuota: `{"spaceQuota": "softQuota", "gracePeriod": 100}`, + candidatePools: []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, + expect: 1, expectErr: false}, + {name: "NormalHardQuota", storageQuota: `{"spaceQuota": "hardQuota"}`, + candidatePools: []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, + expect: 1, expectErr: false}, + {name: "NegativePeriod", storageQuota: `{"spaceQuota": "hardQuota", "gracePeriod": -1}`, + candidatePools: []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, + expect: 0, expectErr: true}, + {name: "ExceedsTheMaximumPeriod", storageQuota: `{"spaceQuota": "hardQuota", "gracePeriod": 4294967295}`, + candidatePools: []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, + expect: 0, expectErr: true}, + {name: "WrongType", storageQuota: `{"spaceQuota": "WrongType"`, + candidatePools: []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, + expect: 0, expectErr: true}, + {name: "HardWithPeriod", storageQuota: `{"spaceQuota": "hardQuota", "gracePeriod": 10}`, + candidatePools: []*model.StoragePool{{Capabilities: map[string]bool{"SupportQuota": true}}}, + expect: 0, expectErr: true}, } for _, tt := range tests { diff --git a/csi/manage/manager_helper_test.go b/csi/manage/manager_helper_test.go index 4794151..ff48046 100644 --- a/csi/manage/manager_helper_test.go +++ b/csi/manage/manager_helper_test.go @@ -418,17 +418,18 @@ func TestNewManagerAndProtocolNotExist(t *testing.T) { // newManagerTest is a helper function called from multiple test cases func newManagerTest(t *testing.T, testCase testCaseStructForNewManager) { - getBackendConfig := gomonkey.ApplyFunc(GetBackendConfig, func(ctx context.Context, backendName string) (*BackendConfig, error) { - if backendName != "test_backend_name" { - return nil, errors.New("not found backend") - } - - var portals []string - if testCase.protocol == "nfs" { - portals = []string{"127.0.0.1"} - } - return &BackendConfig{protocol: testCase.protocol, portals: portals, metroPortals: []string{}}, nil - }) + getBackendConfig := gomonkey.ApplyFunc(GetBackendConfig, + func(ctx context.Context, backendName string) (*BackendConfig, error) { + if backendName != "test_backend_name" { + return nil, errors.New("not found backend") + } + + var portals []string + if testCase.protocol == "nfs" { + portals = []string{"127.0.0.1"} + } + return &BackendConfig{protocol: testCase.protocol, portals: portals, metroPortals: []string{}}, nil + }) defer getBackendConfig.Reset() got, err := NewManager(context.Background(), testCase.backendName) diff --git a/csi/manage/san_manager_test.go b/csi/manage/san_manager_test.go index d8e8285..6ca0fd7 100644 --- a/csi/manage/san_manager_test.go +++ b/csi/manage/san_manager_test.go @@ -44,40 +44,21 @@ func TestSanManagerStageFileSystemVolume(t *testing.T) { manager *SanManager connectVolumeFunc func(patch *gomonkey.Patches, conn connector.VolumeConnector) wantErr bool - }{ - { - name: "TestSanManagerStageIscsiFileSystemVolume", - manager: &SanManager{ - protocol: "iscsi", - Conn: connector.GetConnector(context.Background(), connector.ISCSIDriver), - }, - connectVolumeFunc: mockConnectIscsiVolume, - }, - { - name: "TestSanManagerStageFcFileSystemVolume", - manager: &SanManager{ - protocol: "fc", - Conn: connector.GetConnector(context.Background(), connector.FCDriver), - }, - connectVolumeFunc: mockConnectFcVolume, - }, - { - name: "TestSanManagerStageRoceFileSystemVolume", - manager: &SanManager{ - protocol: "roce", - Conn: connector.GetConnector(context.Background(), connector.RoCEDriver), - }, - connectVolumeFunc: mockConnectRoceVolume, - }, - { - name: "TestSanManagerStageFcNvmeFileSystemVolume", - manager: &SanManager{ - protocol: "fc-nvme", - Conn: connector.GetConnector(context.Background(), connector.FCNVMeDriver), - }, - connectVolumeFunc: mockConnectFcNvmeVolume, - }, - } + }{{name: "TestSanManagerStageIscsiFileSystemVolume", manager: &SanManager{protocol: "iscsi", + Conn: connector.GetConnector(context.Background(), connector.ISCSIDriver)}, + connectVolumeFunc: mockConnectIscsiVolume}, + {name: "TestSanManagerStageFcFileSystemVolume", manager: &SanManager{protocol: "fc", + Conn: connector.GetConnector(context.Background(), connector.FCDriver)}, + connectVolumeFunc: mockConnectFcVolume}, + {name: "TestSanManagerStageRoceFileSystemVolume", manager: &SanManager{ + protocol: "roce", + Conn: connector.GetConnector(context.Background(), connector.RoCEDriver)}, + connectVolumeFunc: mockConnectRoceVolume}, + {name: "TestSanManagerStageFcNvmeFileSystemVolume", manager: &SanManager{ + protocol: "fc-nvme", + Conn: connector.GetConnector(context.Background(), connector.FCNVMeDriver)}, + connectVolumeFunc: mockConnectFcNvmeVolume}} + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { patches := gomonkey.NewPatches() diff --git a/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.5.0 User Guide 02.pdf b/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.5.0 User Guide 02.pdf deleted file mode 100644 index 7d88e57..0000000 Binary files a/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.5.0 User Guide 02.pdf and /dev/null differ diff --git "a/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.5.0 \347\224\250\346\210\267\346\214\207\345\215\227 02.pdf" "b/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.5.0 \347\224\250\346\210\267\346\214\207\345\215\227 02.pdf" deleted file mode 100644 index 1a5e646..0000000 Binary files "a/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.5.0 \347\224\250\346\210\267\346\214\207\345\215\227 02.pdf" and /dev/null differ diff --git a/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.6.0 User Guide 01.pdf b/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.6.0 User Guide 01.pdf new file mode 100644 index 0000000..1f505a7 Binary files /dev/null and b/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.6.0 User Guide 01.pdf differ diff --git "a/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.6.0 \347\224\250\346\210\267\346\214\207\345\215\227 01.pdf" "b/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.6.0 \347\224\250\346\210\267\346\214\207\345\215\227 01.pdf" new file mode 100644 index 0000000..5d557e2 Binary files /dev/null and "b/docs/eSDK Huawei Storage Kubernetes CSI Plugins V4.6.0 \347\224\250\346\210\267\346\214\207\345\215\227 01.pdf" differ diff --git a/pkg/storage-backend/controller/claim_delete_test.go b/pkg/storage-backend/controller/claim_delete_test.go index 8636d91..e9594ff 100644 --- a/pkg/storage-backend/controller/claim_delete_test.go +++ b/pkg/storage-backend/controller/claim_delete_test.go @@ -38,9 +38,10 @@ func TestDeleteStorageBackendClaim(t *testing.T) { } func TestProcessWithDeletionTimeStamp(t *testing.T) { - removePatch := gomonkey.ApplyFunc(utils.NeedRemoveClaimBoundFinalizers, func(storageBackend *xuanwuv1.StorageBackendClaim) bool { - return false - }) + removePatch := gomonkey.ApplyFunc(utils.NeedRemoveClaimBoundFinalizers, + func(storageBackend *xuanwuv1.StorageBackendClaim) bool { + return false + }) defer removePatch.Reset() fakeClaim := newClaim(xuanwuv1.StorageBackendClaimSpec{}) diff --git a/pkg/storage-backend/controller/content_delete_test.go b/pkg/storage-backend/controller/content_delete_test.go index 473fa43..1bc2ce3 100644 --- a/pkg/storage-backend/controller/content_delete_test.go +++ b/pkg/storage-backend/controller/content_delete_test.go @@ -29,11 +29,10 @@ import ( coreV1 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/tools/record" - "github.com/Huawei/eSDK_K8S_Plugin/v4/utils/log" - xuanwuv1 "github.com/Huawei/eSDK_K8S_Plugin/v4/client/apis/xuanwu/v1" "github.com/Huawei/eSDK_K8S_Plugin/v4/pkg/client/clientset/versioned/fake" backendInformers "github.com/Huawei/eSDK_K8S_Plugin/v4/pkg/client/informers/externalversions" + "github.com/Huawei/eSDK_K8S_Plugin/v4/utils/log" ) const ( diff --git a/proto/proto_test.go b/proto/proto_test.go index b74b7dd..b9287e5 100644 --- a/proto/proto_test.go +++ b/proto/proto_test.go @@ -40,25 +40,27 @@ func TestGetISCSIInitiator(t *testing.T) { wantErr error }{ { - "Normal scenario", - "iqn.1994-05.com.redhat:98d87323a952", - nil, - "iqn.1994-05.com.redhat:98d87323a952", - nil, + name: "Normal scenario", + output: "iqn.1994-05.com.redhat:98d87323a952", + err: nil, + wantIQN: "iqn.1994-05.com.redhat:98d87323a952", + wantErr: nil, }, { - "If the initiatorname.iscsi file does not exist", - "awk: cmd. line:1: fatal: cannot open file `/etc/iscsi/initiatorname.iscsi' for reading (No such file or directory)", - errors.New("status 2"), - "", - errors.New("no ISCSI initiator exist"), + name: "If the initiatorname.iscsi file does not exist", + output: "awk: cmd. line:1: fatal: cannot open file" + + " `/etc/iscsi/initiatorname.iscsi' for reading (No such file or directory)", + err: errors.New("status 2"), + wantIQN: "", + wantErr: errors.New("no ISCSI initiator exist"), }, { - "Execution Error", - "fork/exec awk 'BEGIN{FS=\"=\";ORS=\"\"}/^InitiatorName=/{print $2}' /etc/iscsi/initiatorname.iscs: no such file or directory", - errors.New("status 2"), - "", - errors.New("status 2"), + name: "Execution Error", + output: "fork/exec awk 'BEGIN{FS=\"=\";ORS=\"\"}/^InitiatorName=/{print $2}'" + + " /etc/iscsi/initiatorname.iscs: no such file or directory", + err: errors.New("status 2"), + wantIQN: "", + wantErr: errors.New("status 2"), }, } diff --git a/storage/oceanstorage/oceanstor/volume/creator/parameter.go b/storage/oceanstorage/oceanstor/volume/creator/parameter.go index 5b58281..bbc96a2 100644 --- a/storage/oceanstorage/oceanstor/volume/creator/parameter.go +++ b/storage/oceanstorage/oceanstor/volume/creator/parameter.go @@ -297,19 +297,19 @@ func (p *Parameter) SnapshotReservePer() (int, bool) { // AccessKrb5 gets the AccessKrb5 value of the params map. func (p *Parameter) AccessKrb5() int { - val := utils.GetValueOrFallback(p.params, AccessKrb5Key, AccessKrb("")) + val := AccessKrb(utils.GetValueOrFallback(p.params, AccessKrb5Key, "")) return val.Int() } // AccessKrb5i gets the AccessKrb5i value of the params map. func (p *Parameter) AccessKrb5i() int { - val := utils.GetValueOrFallback(p.params, AccessKrb5iKey, AccessKrb("")) + val := AccessKrb(utils.GetValueOrFallback(p.params, AccessKrb5iKey, "")) return val.Int() } // AccessKrb5p gets the AccessKrb5p value of the params map. func (p *Parameter) AccessKrb5p() int { - val := utils.GetValueOrFallback(p.params, AccessKrb5pKey, AccessKrb("")) + val := AccessKrb(utils.GetValueOrFallback(p.params, AccessKrb5pKey, "")) return val.Int() } diff --git a/utils/utils_test.go b/utils/utils_test.go index 323d5f4..d4d3bbc 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -23,17 +23,15 @@ import ( "reflect" "testing" - "github.com/stretchr/testify/require" - - "github.com/Huawei/eSDK_K8S_Plugin/v4/pkg/constants" - "github.com/agiledragon/gomonkey/v2" "github.com/prashantv/gostub" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" "github.com/Huawei/eSDK_K8S_Plugin/v4/csi/app" cfg "github.com/Huawei/eSDK_K8S_Plugin/v4/csi/app/config" + "github.com/Huawei/eSDK_K8S_Plugin/v4/pkg/constants" "github.com/Huawei/eSDK_K8S_Plugin/v4/utils/k8sutils" "github.com/Huawei/eSDK_K8S_Plugin/v4/utils/log" )