Skip to content

Commit

Permalink
Return error when fetching the snapshot fails (#233)
Browse files Browse the repository at this point in the history
We previously continued processing when fetching the snapshot failed for
errors other than 404.
  • Loading branch information
Timo Reimann authored Dec 12, 2019
1 parent e0620ae commit dbcb92c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Use WARN log level for non-critical failures to get an action
[[GH-241]](https://github.com/digitalocean/csi-digitalocean/pull/241)
* Return error when fetching the snapshot fails
[[GH-233]](https://github.com/digitalocean/csi-digitalocean/pull/233)
* Reject requests for block access type
[[GH-225]](https://github.com/digitalocean/csi-digitalocean/pull/225)
* Assume detached state on 404 during ControllerUnpublishVolume
Expand Down
1 change: 1 addition & 0 deletions driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, status.Errorf(codes.NotFound, "snapshot %q not found", snapshotID)
}
return nil, err
}

ll.WithField("snapshot_id", snapshotID).Info("using snapshot as volume source")
Expand Down
68 changes: 68 additions & 0 deletions driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -255,6 +256,73 @@ func TestControllerExpandVolume(t *testing.T) {
}
}

func TestCreateVolume(t *testing.T) {
tests := []struct {
name string
listVolumesErr error
getSnapshotErr error
}{
{
name: "listing volumes failing",
listVolumesErr: errors.New("failed to list volumes"),
},
{
name: "fetching snapshot failing",
getSnapshotErr: errors.New("failed to get snapshot"),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
d := &Driver{
storage: &fakeStorageDriver{
listVolumesErr: test.listVolumesErr,
},
snapshots: &fakeSnapshotsDriver{
getSnapshotErr: test.getSnapshotErr,
},
log: logrus.New().WithField("test_enabed", true),
}

_, err := d.CreateVolume(context.Background(), &csi.CreateVolumeRequest{
Name: "name",
VolumeCapabilities: []*csi.VolumeCapability{
&csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{},
},
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
},
},
VolumeContentSource: &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{
SnapshotId: "snapshotId",
},
},
},
})

var wantErr error
switch {
case test.listVolumesErr != nil:
wantErr = test.listVolumesErr
case test.getSnapshotErr != nil:
wantErr = test.getSnapshotErr
}

if wantErr == nil && err != nil {
t.Errorf("got error %q, want none", err)
}
if wantErr != nil && !strings.Contains(err.Error(), wantErr.Error()) {
t.Errorf("want error %q to include %q", err, wantErr)
}
})
}
}

type fakeStorageAction struct {
*fakeStorageActionsDriver
storageGetValsFunc func(invocation int) (*godo.Action, *godo.Response, error)
Expand Down
16 changes: 13 additions & 3 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,16 @@ func (f *fakeAccountDriver) Get(context.Context) (*godo.Account, *godo.Response,
}

type fakeStorageDriver struct {
volumes map[string]*godo.Volume
snapshots map[string]*godo.Snapshot
volumes map[string]*godo.Volume
snapshots map[string]*godo.Snapshot
listVolumesErr error
}

func (f *fakeStorageDriver) ListVolumes(ctx context.Context, param *godo.ListVolumeParams) ([]godo.Volume, *godo.Response, error) {
if f.listVolumesErr != nil {
return nil, nil, f.listVolumesErr
}

var volumes []godo.Volume

for _, vol := range f.volumes {
Expand Down Expand Up @@ -313,7 +318,8 @@ func (f *fakeDropletsDriver) Neighbors(context.Context, int) ([]godo.Droplet, *g
}

type fakeSnapshotsDriver struct {
snapshots map[string]*godo.Snapshot
snapshots map[string]*godo.Snapshot
getSnapshotErr error
}

func (f *fakeSnapshotsDriver) List(context.Context, *godo.ListOptions) ([]godo.Snapshot, *godo.Response, error) {
Expand All @@ -334,6 +340,10 @@ func (f *fakeSnapshotsDriver) ListDroplet(context.Context, *godo.ListOptions) ([
}

func (f *fakeSnapshotsDriver) Get(ctx context.Context, id string) (*godo.Snapshot, *godo.Response, error) {
if f.getSnapshotErr != nil {
return nil, nil, f.getSnapshotErr
}

resp := godoResponse()
snap, ok := f.snapshots[id]
if !ok {
Expand Down

0 comments on commit dbcb92c

Please sign in to comment.