Skip to content

Commit 79ade28

Browse files
committed
Update csi sanity caller to fix small errors, check for snapshot existence before creating volume
1 parent 5f28826 commit 79ade28

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pkg/gce-pd-csi-driver/controller.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/golang/protobuf/ptypes"
2525

2626
"context"
27+
2728
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
2829
csi "github.com/container-storage-interface/spec/lib/go/csi"
2930
compute "google.golang.org/api/compute/v1"
@@ -163,6 +164,14 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
163164
if content.GetSnapshot() != nil {
164165
// TODO(#161): Add support for Volume Source (cloning) introduced in CSI v1.0.0
165166
snapshotId = content.GetSnapshot().GetSnapshotId()
167+
168+
// Verify that snapshot exists
169+
sl, err := gceCS.getSnapshotById(ctx, snapshotId)
170+
if err != nil {
171+
return nil, status.Errorf(codes.Internal, "CreateVolume failed to get snapshot %s: %v", snapshotId, err)
172+
} else if len(sl.Entries) == 0 {
173+
return nil, status.Errorf(codes.NotFound, "CreateVolume source snapshot %s does not exist", snapshotId)
174+
}
166175
}
167176
}
168177

@@ -171,7 +180,7 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
171180
switch replicationType {
172181
case replicationTypeNone:
173182
if len(zones) != 1 {
174-
return nil, status.Errorf(codes.Internal, fmt.Sprintf("CreateVolume failed to get a single zone for creating zonal disk, instead got: %v", zones))
183+
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume failed to get a single zone for creating zonal disk, instead got: %v", zones))
175184
}
176185
disk, err = createSingleZoneDisk(ctx, gceCS.CloudProvider, name, zones, diskType, capacityRange, capBytes, snapshotId, diskEncryptionKmsKey)
177186
if err != nil {

test/sanity/sanity_test.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ limitations under the License.
1515
package sanitytest
1616

1717
import (
18+
"fmt"
19+
"os"
20+
"path"
1821
"testing"
1922

2023
sanity "github.com/kubernetes-csi/csi-test/pkg/sanity"
@@ -31,9 +34,10 @@ func TestSanity(t *testing.T) {
3134
project := "test-project"
3235
zone := "test-zone"
3336
vendorVersion := "test-version"
34-
endpoint := "unix:/tmp/csi.sock"
35-
mountPath := "/tmp/csi/mount"
36-
stagePath := "/tmp/csi/stage"
37+
tmpDir := "/tmp/csi"
38+
endpoint := fmt.Sprintf("unix:%s/csi.sock", tmpDir)
39+
mountPath := path.Join(tmpDir, "mount")
40+
stagePath := path.Join(tmpDir, "stage")
3741
// Set up driver and env
3842
gceDriver := driver.GetGCEDriver()
3943

@@ -57,6 +61,18 @@ func TestSanity(t *testing.T) {
5761
}
5862
cloudProvider.InsertInstance(instance, "test-location", "test-name")
5963

64+
err = os.MkdirAll(tmpDir, 0755)
65+
if err != nil {
66+
t.Fatalf("Failed to create sanity temp working dir %s: %v", tmpDir, err)
67+
}
68+
69+
defer func() {
70+
// Clean up tmp dir
71+
if err = os.RemoveAll(tmpDir); err != nil {
72+
t.Fatalf("Failed to clean up sanity temp working dir %s: %v", tmpDir, err)
73+
}
74+
}()
75+
6076
go func() {
6177
gceDriver.Run(endpoint)
6278
}()

0 commit comments

Comments
 (0)