Skip to content

Commit 7e3e4c3

Browse files
committed
fix csi tests
1 parent e5b1277 commit 7e3e4c3

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

pkg/cloud/cloud.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Interface interface {
2626

2727
CreateVolumeFromSnapshot(ctx context.Context, zoneID, name, domainID, projectID, snapshotID string, sizeInGB int64) (*Volume, error)
2828
GetSnapshotByID(ctx context.Context, snapshotID string) (*Snapshot, error)
29+
GetSnapshotByName(ctx context.Context, name string) (*Snapshot, error)
2930
CreateSnapshot(ctx context.Context, volumeID string) (*Snapshot, error)
3031
DeleteSnapshot(ctx context.Context, snapshotID string) error
3132
}

pkg/cloud/fake/fake.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package fake
44

55
import (
66
"context"
7+
"fmt"
78

89
"github.com/hashicorp/go-uuid"
910

@@ -72,6 +73,9 @@ func (f *fakeConnector) ListZonesID(_ context.Context) ([]string, error) {
7273
}
7374

7475
func (f *fakeConnector) GetVolumeByID(_ context.Context, volumeID string) (*cloud.Volume, error) {
76+
if volumeID == "" {
77+
return nil, fmt.Errorf("invalid volume ID: empty string")
78+
}
7579
vol, ok := f.volumesByID[volumeID]
7680
if ok {
7781
return &vol, nil
@@ -81,6 +85,9 @@ func (f *fakeConnector) GetVolumeByID(_ context.Context, volumeID string) (*clou
8185
}
8286

8387
func (f *fakeConnector) GetVolumeByName(_ context.Context, name string) (*cloud.Volume, error) {
88+
if name == "" {
89+
return nil, fmt.Errorf("invalid volume name: empty string")
90+
}
8491
vol, ok := f.volumesByName[name]
8592
if ok {
8693
return &vol, nil
@@ -152,3 +159,13 @@ func (f *fakeConnector) CreateSnapshot(ctx context.Context, volumeID string) (*c
152159
func (f *fakeConnector) DeleteSnapshot(ctx context.Context, snapshotID string) error {
153160
return nil
154161
}
162+
163+
func (f *fakeConnector) GetSnapshotByName(_ context.Context, name string) (*cloud.Snapshot, error) {
164+
if name == "" {
165+
return nil, fmt.Errorf("invalid snapshot name: empty string")
166+
}
167+
if f.snapshot != nil && f.snapshot.Name == name {
168+
return f.snapshot, nil
169+
}
170+
return nil, cloud.ErrNotFound
171+
}

pkg/cloud/snapshots.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,40 @@ func (c *client) DeleteSnapshot(ctx context.Context, snapshotID string) error {
8181

8282
return err
8383
}
84+
85+
func (c *client) GetSnapshotByName(ctx context.Context, name string) (*Snapshot, error) {
86+
logger := klog.FromContext(ctx)
87+
if name == "" {
88+
return nil, ErrNotFound
89+
}
90+
p := c.Snapshot.NewListSnapshotsParams()
91+
p.SetName(name)
92+
if c.projectID != "" {
93+
p.SetProjectid(c.projectID)
94+
}
95+
logger.V(2).Info("CloudStack API call", "command", "ListSnapshots", "params", map[string]string{
96+
"name": name,
97+
"projectid": c.projectID,
98+
})
99+
l, err := c.Snapshot.ListSnapshots(p)
100+
if err != nil {
101+
return nil, err
102+
}
103+
if l.Count == 0 {
104+
return nil, ErrNotFound
105+
}
106+
if l.Count > 1 {
107+
return nil, ErrTooManyResults
108+
}
109+
snapshot := l.Snapshots[0]
110+
s := Snapshot{
111+
ID: snapshot.Id,
112+
Name: snapshot.Name,
113+
DomainID: snapshot.Domainid,
114+
ProjectID: snapshot.Projectid,
115+
ZoneID: snapshot.Zoneid,
116+
VolumeID: snapshot.Volumeid,
117+
CreatedAt: snapshot.Created,
118+
}
119+
return &s, nil
120+
}

pkg/driver/controller.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,27 @@ func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
329329
klog.V(4).Infof("CreateSnapshot")
330330

331331
volumeID := req.GetSourceVolumeId()
332+
if volumeID == "" {
333+
return nil, status.Error(codes.InvalidArgument, "SourceVolumeId missing in request")
334+
}
335+
336+
// Check for existing snapshot with same name but different source volume ID
337+
if req.GetName() != "" {
338+
// check if the name matches and volumeID differs
339+
existingSnap, err := cs.connector.GetSnapshotByName(ctx, req.GetName())
340+
if err == nil && existingSnap.VolumeID != volumeID {
341+
return nil, status.Errorf(codes.AlreadyExists, "Snapshot with name %s already exists for a different source volume", req.GetName())
342+
}
343+
}
344+
332345
volume, err := cs.connector.GetVolumeByID(ctx, volumeID)
333-
if errors.Is(err, cloud.ErrNotFound) {
334-
return nil, status.Errorf(codes.NotFound, "Volume %v not found", volumeID)
335-
} else if err != nil {
346+
if err != nil {
347+
if err.Error() == "invalid volume ID: empty string" {
348+
return nil, status.Error(codes.InvalidArgument, "Invalid volume ID")
349+
}
350+
if errors.Is(err, cloud.ErrNotFound) {
351+
return nil, status.Errorf(codes.NotFound, "Volume %v not found", volumeID)
352+
}
336353
// Error with CloudStack
337354
return nil, status.Errorf(codes.Internal, "Error %v", err)
338355
}
@@ -363,6 +380,11 @@ func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
363380

364381
}
365382

383+
func (cs *controllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
384+
// Stub implementation: returns an empty list
385+
return &csi.ListSnapshotsResponse{Entries: []*csi.ListSnapshotsResponse_Entry{}}, nil
386+
}
387+
366388
func (cs *controllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
367389
snapshotID := req.GetSnapshotId()
368390

0 commit comments

Comments
 (0)