Check if ListSnapshots is supported during GetSnapshotStatus

This commit is contained in:
Grant Griffiths
2019-07-10 16:36:31 -07:00
parent c0658cb68d
commit ea5d2d6089
2 changed files with 86 additions and 31 deletions

View File

@@ -101,13 +101,36 @@ func (s *snapshot) DeleteSnapshot(ctx context.Context, snapshotID string, snapsh
return nil return nil
} }
func (s *snapshot) isListSnapshotsSupported(ctx context.Context) (bool, error) {
client := csi.NewControllerClient(s.conn)
capRsp, err := client.ControllerGetCapabilities(ctx, &csi.ControllerGetCapabilitiesRequest{})
if err != nil {
return false, err
}
for _, cap := range capRsp.Capabilities {
if cap.GetRpc().GetType() == csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS {
return true, nil
}
}
return false, nil
}
func (s *snapshot) GetSnapshotStatus(ctx context.Context, snapshotID string) (bool, time.Time, int64, error) { func (s *snapshot) GetSnapshotStatus(ctx context.Context, snapshotID string) (bool, time.Time, int64, error) {
client := csi.NewControllerClient(s.conn) client := csi.NewControllerClient(s.conn)
// If the driver does not support ListSnapshots, assume the snapshot ID is valid.
listSnapshotsSupported, err := s.isListSnapshotsSupported(ctx)
if err != nil {
return false, time.Time{}, 0, fmt.Errorf("failed to check if ListSnapshots is supported: %s", err.Error())
}
if !listSnapshotsSupported {
return true, time.Time{}, 0, nil
}
req := csi.ListSnapshotsRequest{ req := csi.ListSnapshotsRequest{
SnapshotId: snapshotID, SnapshotId: snapshotID,
} }
rsp, err := client.ListSnapshots(ctx, &req) rsp, err := client.ListSnapshots(ctx, &req)
if err != nil { if err != nil {
return false, time.Time{}, 0, err return false, time.Time{}, 0, err

View File

@@ -370,41 +370,56 @@ func TestGetSnapshotStatus(t *testing.T) {
} }
tests := []struct { tests := []struct {
name string name string
snapshotID string snapshotID string
input *csi.ListSnapshotsRequest listSnapshotsSupported bool
output *csi.ListSnapshotsResponse input *csi.ListSnapshotsRequest
injectError codes.Code output *csi.ListSnapshotsResponse
expectError bool injectError codes.Code
expectReady bool expectError bool
expectCreateAt time.Time expectReady bool
expectSize int64 expectCreateAt time.Time
expectSize int64
}{ }{
{ {
name: "success", name: "success",
snapshotID: defaultID, snapshotID: defaultID,
input: defaultRequest, listSnapshotsSupported: true,
output: defaultResponse, input: defaultRequest,
expectError: false, output: defaultResponse,
expectReady: true, expectError: false,
expectCreateAt: createTime, expectReady: true,
expectSize: size, expectCreateAt: createTime,
expectSize: size,
}, },
{ {
name: "gRPC transient error", name: "ListSnapshots not supported",
snapshotID: defaultID, snapshotID: defaultID,
input: defaultRequest, listSnapshotsSupported: false,
output: nil, input: defaultRequest,
injectError: codes.DeadlineExceeded, output: defaultResponse,
expectError: true, expectError: false,
expectReady: true,
expectCreateAt: time.Time{},
expectSize: 0,
}, },
{ {
name: "gRPC final error", name: "gRPC transient error",
snapshotID: defaultID, snapshotID: defaultID,
input: defaultRequest, listSnapshotsSupported: true,
output: nil, input: defaultRequest,
injectError: codes.NotFound, output: nil,
expectError: true, injectError: codes.DeadlineExceeded,
expectError: true,
},
{
name: "gRPC final error",
snapshotID: defaultID,
listSnapshotsSupported: true,
input: defaultRequest,
output: nil,
injectError: codes.NotFound,
expectError: true,
}, },
} }
@@ -425,8 +440,25 @@ func TestGetSnapshotStatus(t *testing.T) {
} }
// Setup expectation // Setup expectation
listSnapshotsCap := &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
},
},
}
var controllerCapabilities []*csi.ControllerServiceCapability
if test.listSnapshotsSupported {
controllerCapabilities = append(controllerCapabilities, listSnapshotsCap)
}
if in != nil { if in != nil {
controllerServer.EXPECT().ListSnapshots(gomock.Any(), in).Return(out, injectedErr).Times(1) controllerServer.EXPECT().ControllerGetCapabilities(gomock.Any(), gomock.Any()).Return(&csi.ControllerGetCapabilitiesResponse{
Capabilities: controllerCapabilities,
}, nil).Times(1)
if test.listSnapshotsSupported {
controllerServer.EXPECT().ListSnapshots(gomock.Any(), in).Return(out, injectedErr).Times(1)
}
} }
s := NewSnapshotter(csiConn) s := NewSnapshotter(csiConn)