diff --git a/storage/backend/nvme_path_test.go b/storage/backend/nvme_path_test.go index 729f4e4..ae776c3 100644 --- a/storage/backend/nvme_path_test.go +++ b/storage/backend/nvme_path_test.go @@ -17,6 +17,7 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/emptypb" ) func TestCreateNvmeTCPPath(t *testing.T) { @@ -153,3 +154,73 @@ func TestCreateNvmeTCPPath(t *testing.T) { }) } } + +func TestDeleteNvmePath(t *testing.T) { + testPathName := "path0" + testRequest := &pb.DeleteNvmePathRequest{ + Name: testPathName, + AllowMissing: true, + } + tests := map[string]struct { + giveClientErr error + giveConnectorErr error + wantErr error + wantRequest *pb.DeleteNvmePathRequest + wantConnClosed bool + }{ + "successful call": { + giveConnectorErr: nil, + giveClientErr: nil, + wantErr: nil, + wantRequest: proto.Clone(testRequest).(*pb.DeleteNvmePathRequest), + wantConnClosed: true, + }, + "client err": { + giveConnectorErr: nil, + giveClientErr: errors.New("Some client error"), + wantErr: errors.New("Some client error"), + wantRequest: proto.Clone(testRequest).(*pb.DeleteNvmePathRequest), + wantConnClosed: true, + }, + "connector err": { + giveConnectorErr: errors.New("Some conn error"), + giveClientErr: nil, + wantErr: errors.New("Some conn error"), + wantRequest: nil, + wantConnClosed: false, + }, + } + + for testName, tt := range tests { + t.Run(testName, func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + mockClient := mocks.NewNvmeRemoteControllerServiceClient(t) + if tt.wantRequest != nil { + mockClient.EXPECT().DeleteNvmePath(ctx, tt.wantRequest). + Return(&emptypb.Empty{}, tt.giveClientErr) + } + + connClosed := false + mockConn := mocks.NewConnector(t) + mockConn.EXPECT().NewConn().Return( + &grpc.ClientConn{}, + func() { connClosed = true }, + tt.giveConnectorErr, + ) + + c, _ := NewWithArgs( + mockConn, + func(grpc.ClientConnInterface) pb.NvmeRemoteControllerServiceClient { + return mockClient + }, + ) + + err := c.DeleteNvmePath(ctx, testPathName, true) + + require.Equal(t, tt.wantErr, err) + require.Equal(t, tt.wantConnClosed, connClosed) + }) + } +}