From f44e0d6630b027cec452485792926cce5a99d147 Mon Sep 17 00:00:00 2001 From: Utkarsh Dubey Date: Thu, 25 Jan 2024 11:06:01 +0530 Subject: [PATCH] Adds unit test --- pkg/common/common_test.go | 11 ++++ pkg/controller/controller_test.go | 86 ++++++++++++++++++++--------- pkg/controller/publisher_test.go | 2 + pkg/node/node_test.go | 90 +++++++++++++++++++++++++------ 4 files changed, 150 insertions(+), 39 deletions(-) diff --git a/pkg/common/common_test.go b/pkg/common/common_test.go index 99b90b2c..080d5423 100644 --- a/pkg/common/common_test.go +++ b/pkg/common/common_test.go @@ -118,6 +118,17 @@ func TestGetISCSITargetsInfoFromStorage(t *testing.T) { }) } +func TestGetNVMETCPTargetsInfoFromStorage(t *testing.T) { + t.Run("api error", func(t *testing.T) { + e := errors.New("some error") + clientMock := new(gopowerstoremock.Client) + clientMock.On("GetCluster", context.Background()).Return(gopowerstore.Cluster{}, e) + clientMock.On("GetStorageNVMETCPTargetAddresses", context.Background()).Return([]gopowerstore.IPPoolAddress{}, e) + _, err := common.GetNVMETCPTargetsInfoFromStorage(clientMock, "A1") + assert.EqualError(t, err, e.Error()) + }) +} + func TestGetFCTargetsInfoFromStorage(t *testing.T) { t.Run("api error", func(t *testing.T) { e := errors.New("some error") diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index fd5cc383..aa6f3129 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -1880,7 +1880,13 @@ var _ = ginkgo.Describe("CSIControllerService", func() { IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, }, }, nil) - + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{ + { + Address: "192.168.1.1", + IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, + }, + }, nil) clientMock.On("GetFCPorts", mock.Anything). Return([]gopowerstore.FcPort{ { @@ -1902,13 +1908,15 @@ var _ = ginkgo.Describe("CSIControllerService", func() { gomega.Expect(err).To(gomega.BeNil()) gomega.Expect(res).To(gomega.Equal(&csi.ControllerPublishVolumeResponse{ PublishContext: map[string]string{ - "PORTAL0": "192.168.1.1:3260", - "TARGET0": "iqn", - "NVMEFCPORTAL0": "nn-0x58ccf090c9200c22:pn-0x58ccf091492b0c22", - "NVMEFCTARGET0": "nqn", - "DEVICE_WWN": "68ccf098003ceb5e4577a20be6d11bf9", - "LUN_ADDRESS": "1", - "FCWWPN0": "58ccf09348a003a3", + "PORTAL0": "192.168.1.1:3260", + "TARGET0": "iqn", + "NVMEFCPORTAL0": "nn-0x58ccf090c9200c22:pn-0x58ccf091492b0c22", + "NVMEFCTARGET0": "nqn", + "DEVICE_WWN": "68ccf098003ceb5e4577a20be6d11bf9", + "LUN_ADDRESS": "1", + "FCWWPN0": "58ccf09348a003a3", + "NVMETCPTARGET0": "nqn", + "NVMETCPPORTAL0": "192.168.1.1:4420", }, })) }) @@ -2073,7 +2081,13 @@ var _ = ginkgo.Describe("CSIControllerService", func() { IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, }, }, nil) - + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{ + { + Address: "192.168.1.1", + IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, + }, + }, nil) clientMock.On("GetFCPorts", mock.Anything). Return([]gopowerstore.FcPort{ { @@ -2092,10 +2106,12 @@ var _ = ginkgo.Describe("CSIControllerService", func() { gomega.Expect(err).To(gomega.BeNil()) gomega.Expect(res).To(gomega.Equal(&csi.ControllerPublishVolumeResponse{ PublishContext: map[string]string{ - "PORTAL0": "192.168.1.1:3260", - "TARGET0": "iqn", - "DEVICE_WWN": "68ccf098003ceb5e4577a20be6d11bf9", - "LUN_ADDRESS": "1", + "PORTAL0": "192.168.1.1:3260", + "TARGET0": "iqn", + "DEVICE_WWN": "68ccf098003ceb5e4577a20be6d11bf9", + "LUN_ADDRESS": "1", + "NVMETCPPORTAL0": "192.168.1.1:4420", + "NVMETCPTARGET0": "", }, })) }) @@ -2183,7 +2199,13 @@ var _ = ginkgo.Describe("CSIControllerService", func() { IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, }, }, nil) - + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{ + { + Address: "192.168.1.1", + IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, + }, + }, nil) clientMock.On("GetFCPorts", mock.Anything). Return([]gopowerstore.FcPort{ { @@ -2202,10 +2224,12 @@ var _ = ginkgo.Describe("CSIControllerService", func() { gomega.Expect(err).To(gomega.BeNil()) gomega.Expect(res).To(gomega.Equal(&csi.ControllerPublishVolumeResponse{ PublishContext: map[string]string{ - "PORTAL0": "192.168.1.1:3260", - "TARGET0": "iqn", - "DEVICE_WWN": "68ccf098003ceb5e4577a20be6d11bf9", - "LUN_ADDRESS": "1", + "PORTAL0": "192.168.1.1:3260", + "TARGET0": "iqn", + "DEVICE_WWN": "68ccf098003ceb5e4577a20be6d11bf9", + "LUN_ADDRESS": "1", + "NVMETCPPORTAL0": "192.168.1.1:4420", + "NVMETCPTARGET0": "", }, })) }) @@ -2229,7 +2253,13 @@ var _ = ginkgo.Describe("CSIControllerService", func() { IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, }, }, nil) - + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{ + { + Address: "192.168.1.1", + IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, + }, + }, nil) clientMock.On("GetFCPorts", mock.Anything). Return([]gopowerstore.FcPort{ { @@ -2276,7 +2306,13 @@ var _ = ginkgo.Describe("CSIControllerService", func() { IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, }, }, nil) - + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{ + { + Address: "192.168.1.1", + IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, + }, + }, nil) clientMock.On("GetFCPorts", mock.Anything). Return([]gopowerstore.FcPort{ { @@ -2295,10 +2331,12 @@ var _ = ginkgo.Describe("CSIControllerService", func() { gomega.Expect(err).To(gomega.BeNil()) gomega.Expect(res).To(gomega.Equal(&csi.ControllerPublishVolumeResponse{ PublishContext: map[string]string{ - "PORTAL0": "192.168.1.1:3260", - "TARGET0": "iqn", - "DEVICE_WWN": "68ccf098003ceb5e4577a20be6d11bf9", - "LUN_ADDRESS": "2", + "PORTAL0": "192.168.1.1:3260", + "TARGET0": "iqn", + "DEVICE_WWN": "68ccf098003ceb5e4577a20be6d11bf9", + "LUN_ADDRESS": "2", + "NVMETCPPORTAL0": "192.168.1.1:4420", + "NVMETCPTARGET0": "", }, })) }) diff --git a/pkg/controller/publisher_test.go b/pkg/controller/publisher_test.go index f3cbc414..b4937c77 100644 --- a/pkg/controller/publisher_test.go +++ b/pkg/controller/publisher_test.go @@ -129,6 +129,8 @@ func TestVolumePublisher_Publish(t *testing.T) { Return([]gopowerstore.HostVolumeMapping{}, nil).Once() clientMock.On("GetStorageISCSITargetAddresses", mock.Anything). Return([]gopowerstore.IPPoolAddress{}, e) + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{}, e) clientMock.On("GetFCPorts", mock.Anything). Return([]gopowerstore.FcPort{}, nil) clientMock.On("GetCluster", mock.Anything). diff --git a/pkg/node/node_test.go b/pkg/node/node_test.go index 30e53629..db1587a0 100644 --- a/pkg/node/node_test.go +++ b/pkg/node/node_test.go @@ -21,6 +21,7 @@ package node import ( "context" "errors" + "github.com/onsi/ginkgo/reporters" "net" "net/http" "os" @@ -43,7 +44,6 @@ import ( "github.com/dell/gopowerstore/api" gopowerstoremock "github.com/dell/gopowerstore/mocks" ginkgo "github.com/onsi/ginkgo" - "github.com/onsi/ginkgo/reporters" gomega "github.com/onsi/gomega" "github.com/stretchr/testify/mock" ) @@ -675,8 +675,14 @@ var _ = ginkgo.Describe("CSINodeService", func() { arrays := getTestArrays() clientMock.On("GetStorageISCSITargetAddresses", mock.Anything). Return([]gopowerstore.IPPoolAddress{}, nil) + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{}, nil) + clientMock.On("GetCluster", mock.Anything). + Return(gopowerstore.Cluster{ + Name: validClusterName, + NVMeNQN: validNVMEInitiators[0], + }, nil) err := nodeSvc.nodeProbe(context.Background(), arrays["gid1"]) - gomega.Expect(err.Error()).To(gomega.ContainSubstring("no active iscsi sessions")) }) }) @@ -696,6 +702,13 @@ var _ = ginkgo.Describe("CSINodeService", func() { nodeSvc.useNVME = true clientMock.On("GetStorageISCSITargetAddresses", mock.Anything). Return([]gopowerstore.IPPoolAddress{}, nil) + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{}, nil) + clientMock.On("GetCluster", mock.Anything). + Return(gopowerstore.Cluster{ + Name: validClusterName, + NVMeNQN: validNVMEInitiators[0], + }, nil) err := nodeSvc.nodeProbe(context.Background(), arrays["gid1"]) nodeSvc.useNVME = false gomega.Expect(err.Error()).To(gomega.ContainSubstring("no active nvme sessions")) @@ -719,6 +732,13 @@ var _ = ginkgo.Describe("CSINodeService", func() { }, nil) clientMock.On("GetStorageISCSITargetAddresses", mock.Anything). Return([]gopowerstore.IPPoolAddress{}, nil) + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{}, nil) + clientMock.On("GetCluster", mock.Anything). + Return(gopowerstore.Cluster{ + Name: validClusterName, + NVMeNQN: validNVMEInitiators[0], + }, nil) nodeSvc.useNFS = true arrays := getTestArrays() err := nodeSvc.nodeProbe(context.Background(), arrays["gid1"]) @@ -744,6 +764,13 @@ var _ = ginkgo.Describe("CSINodeService", func() { }, nil) clientMock.On("GetStorageISCSITargetAddresses", mock.Anything). Return([]gopowerstore.IPPoolAddress{}, nil) + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). + Return([]gopowerstore.IPPoolAddress{}, nil) + clientMock.On("GetCluster", mock.Anything). + Return(gopowerstore.Cluster{ + Name: validClusterName, + NVMeNQN: validNVMEInitiators[0], + }, nil) nodeSvc.useNFS = true nodeSvc.useNVME = true arrays := getTestArrays() @@ -885,8 +912,8 @@ var _ = ginkgo.Describe("CSINodeService", func() { }, nil) arrays := getTestArrays() - nodeSvc.startNodeToArrayConnectivityCheck(context.Background()) nodeSvc.iscsiTargets["unique"] = []string{"iqn.2015-10.com.dell:dellemc-foobar-123-a-7ceb34a0"} + nodeSvc.startNodeToArrayConnectivityCheck(context.Background()) err := nodeSvc.nodeProbe(context.Background(), arrays["gid1"]) gomega.Expect(err).To(gomega.BeNil()) @@ -1251,7 +1278,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil).Times(4) fsMock.On("ParseProcMounts", context.Background(), mock.Anything). Return(mountInfo, nil).Twice() - fsMock.On("MkFileIDempotent", filepath.Join(nodeStagePrivateDir, validBaseVolumeID)). + fsMock.On("MkFileIdempotent", filepath.Join(nodeStagePrivateDir, validBaseVolumeID)). Return(true, nil).Once() fsMock.On("GetUtil").Return(utilMock) @@ -1407,7 +1434,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { }).Return(gobrick.Device{}, nil) fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil).Times(2) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) - fsMock.On("MkFileIDempotent", filepath.Join(nodeStagePrivateDir, validBaseVolumeID)).Return(true, nil) + fsMock.On("MkFileIdempotent", filepath.Join(nodeStagePrivateDir, validBaseVolumeID)).Return(true, nil) fsMock.On("GetUtil").Return(utilMock) utilMock.On("BindMount", mock.Anything, "/dev", filepath.Join(nodeStagePrivateDir, validBaseVolumeID)).Return(e) @@ -1862,7 +1889,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil).Times(2) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) - fsMock.On("MkFileIDempotent", validTargetPath).Return(true, nil) + fsMock.On("MkFileIdempotent", validTargetPath).Return(true, nil) utilMock.On("BindMount", mock.Anything, stagingPath, validTargetPath).Return(nil) res, err := nodeSvc.NodePublishVolume(context.Background(), &csi.NodePublishVolumeRequest{ @@ -1883,7 +1910,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil).Times(2) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) - fsMock.On("MkFileIDempotent", validTargetPath).Return(true, nil) + fsMock.On("MkFileIdempotent", validTargetPath).Return(true, nil) utilMock.On("BindMount", mock.Anything, stagingPath, validTargetPath, "ro").Return(nil) _, err := nodeSvc.NodePublishVolume(context.Background(), &csi.NodePublishVolumeRequest{ @@ -1903,7 +1930,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil).Times(2) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) - fsMock.On("MkFileIDempotent", validTargetPath).Return(false, errors.New("failed")) + fsMock.On("MkFileIdempotent", validTargetPath).Return(false, errors.New("failed")) utilMock.On("BindMount", mock.Anything, stagingPath, validTargetPath).Return(nil) _, err := nodeSvc.NodePublishVolume(context.Background(), &csi.NodePublishVolumeRequest{ @@ -1923,7 +1950,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil).Times(2) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) - fsMock.On("MkFileIDempotent", validTargetPath).Return(true, nil) + fsMock.On("MkFileIdempotent", validTargetPath).Return(true, nil) utilMock.On("BindMount", mock.Anything, stagingPath, validTargetPath).Return(errors.New("failed to bind")) _, err := nodeSvc.NodePublishVolume(context.Background(), &csi.NodePublishVolumeRequest{ @@ -2641,7 +2668,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { utilMock.On("BindMount", mock.Anything, "/dev", mock.Anything).Return(nil) fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) - fsMock.On("MkFileIDempotent", mock.Anything).Return(true, nil) + fsMock.On("MkFileIdempotent", mock.Anything).Return(true, nil) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) fsMock.On("MkdirAll", mock.Anything, mock.Anything).Return(nil) utilMock.On("GetDiskFormat", mock.Anything, mock.Anything).Return("", nil) @@ -2770,7 +2797,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { utilMock.On("BindMount", mock.Anything, "/dev", mock.Anything).Return(nil) fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil).Times(2) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) - fsMock.On("MkFileIDempotent", mock.Anything).Return(true, errors.New("error")) + fsMock.On("MkFileIdempotent", mock.Anything).Return(true, errors.New("error")) fsMock.On("GetUtil").Return(utilMock) mountInfo := []gofsutil.Info{ @@ -2966,7 +2993,7 @@ var _ = ginkgo.Describe("CSINodeService", func() { utilMock.On("BindMount", mock.Anything, "/dev", mock.Anything).Return(nil) fsMock.On("ReadFile", "/proc/self/mountinfo").Return([]byte{}, nil) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) - fsMock.On("MkFileIDempotent", mock.Anything).Return(true, nil) + fsMock.On("MkFileIdempotent", mock.Anything).Return(true, nil) fsMock.On("ParseProcMounts", context.Background(), mock.Anything).Return([]gofsutil.Info{}, nil) fsMock.On("MkdirAll", mock.Anything, mock.Anything).Return(nil) utilMock.On("GetDiskFormat", mock.Anything, mock.Anything).Return("", nil) @@ -3721,13 +3748,24 @@ var _ = ginkgo.Describe("CSINodeService", func() { ginkgo.It("should return NVMeTCP topology segments", func() { nodeSvc.useNVME = true nodeSvc.useFC = false - clientMock.On("GetStorageISCSITargetAddresses", mock.Anything). + clientMock.On("GetStorageISCSITargetAddresses", mock.Anything).Return([]gopowerstore.IPPoolAddress{ + { + Address: "192.168.1.1", + IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, + }, + }, nil) + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). Return([]gopowerstore.IPPoolAddress{ { Address: "192.168.1.1", IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, }, }, nil) + clientMock.On("GetCluster", mock.Anything). + Return(gopowerstore.Cluster{ + Name: validClusterName, + NVMeNQN: validNVMEInitiators[0], + }, nil) conn, _ := net.Dial("udp", "127.0.0.1:80") fsMock.On("NetDial", mock.Anything).Return( conn, @@ -3756,13 +3794,24 @@ var _ = ginkgo.Describe("CSINodeService", func() { gonvme.GONVMEMock.InduceDiscoveryError = true nodeSvc.useNVME = true nodeSvc.useFC = false - clientMock.On("GetStorageISCSITargetAddresses", mock.Anything). + clientMock.On("GetStorageISCSITargetAddresses", mock.Anything).Return([]gopowerstore.IPPoolAddress{ + { + Address: "192.168.1.1", + IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, + }, + }, nil) + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). Return([]gopowerstore.IPPoolAddress{ { Address: "192.168.1.1", IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, }, }, nil) + clientMock.On("GetCluster", mock.Anything). + Return(gopowerstore.Cluster{ + Name: validClusterName, + NVMeNQN: validNVMEInitiators[0], + }, nil) conn, _ := net.Dial("udp", "127.0.0.1:80") fsMock.On("NetDial", mock.Anything).Return( conn, @@ -3791,8 +3840,19 @@ var _ = ginkgo.Describe("CSINodeService", func() { nodeSvc.useNVME = true nodeSvc.useFC = false e := "internalerror" - clientMock.On("GetStorageISCSITargetAddresses", mock.Anything). + clientMock.On("GetStorageISCSITargetAddresses", mock.Anything).Return([]gopowerstore.IPPoolAddress{ + { + Address: "192.168.1.1", + IPPort: gopowerstore.IPPortInstance{TargetIqn: "iqn"}, + }, + }, nil) + clientMock.On("GetStorageNVMETCPTargetAddresses", mock.Anything). Return([]gopowerstore.IPPoolAddress{}, errors.New(e)) + clientMock.On("GetCluster", mock.Anything). + Return(gopowerstore.Cluster{ + Name: validClusterName, + NVMeNQN: validNVMEInitiators[0], + }, nil) conn, _ := net.Dial("udp", "127.0.0.1:80") fsMock.On("NetDial", mock.Anything).Return( conn,