Skip to content

Commit

Permalink
test(storage/virtio-blk): add client delete virtio-blk tests
Browse files Browse the repository at this point in the history
Signed-off-by: Artsiom Koltun <[email protected]>
  • Loading branch information
artek-koltun authored and sandersms committed Jan 16, 2024
1 parent df0b7d4 commit 43c6fae
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions storage/virtio_blk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/wrapperspb"
)

Expand Down Expand Up @@ -107,3 +108,74 @@ func TestCreateVirtioBlk(t *testing.T) {
})
}
}

func TestDeleteVirtioBlk(t *testing.T) {
testControllerName := "virtioBlk0Name"
testRequest := &pb.DeleteVirtioBlkRequest{
Name: testControllerName,
AllowMissing: true,
}
tests := map[string]struct {
giveClientErr error
giveConnectorErr error
wantErr error
wantRequest *pb.DeleteVirtioBlkRequest
wantConnClosed bool
}{
"successful call": {
giveConnectorErr: nil,
giveClientErr: nil,
wantErr: nil,
wantRequest: proto.Clone(testRequest).(*pb.DeleteVirtioBlkRequest),
wantConnClosed: true,
},
"client err": {
giveConnectorErr: nil,
giveClientErr: errors.New("Some client error"),
wantErr: errors.New("Some client error"),
wantRequest: proto.Clone(testRequest).(*pb.DeleteVirtioBlkRequest),
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.NewFrontendVirtioBlkServiceClient(t)
if tt.wantRequest != nil {
mockClient.EXPECT().DeleteVirtioBlk(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,
pb.NewFrontendNvmeServiceClient,
func(grpc.ClientConnInterface) pb.FrontendVirtioBlkServiceClient {
return mockClient
},
)

err := c.DeleteVirtioBlk(ctx, testControllerName, true)

require.Equal(t, tt.wantErr, err)
require.Equal(t, tt.wantConnClosed, connClosed)
})
}
}

0 comments on commit 43c6fae

Please sign in to comment.