@@ -22,9 +22,11 @@ import (
22
22
"testing"
23
23
"time"
24
24
25
+ "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
25
26
"github.com/golang/protobuf/ptypes"
26
27
27
28
"context"
29
+
28
30
compute "google.golang.org/api/compute/v1"
29
31
"google.golang.org/grpc/codes"
30
32
"google.golang.org/grpc/status"
@@ -588,34 +590,6 @@ func TestCreateVolumeArguments(t *testing.T) {
588
590
},
589
591
},
590
592
},
591
- {
592
- name : "success with data source of snapshot type" ,
593
- req : & csi.CreateVolumeRequest {
594
- Name : "test-name" ,
595
- CapacityRange : stdCapRange ,
596
- VolumeCapabilities : stdVolCaps ,
597
- VolumeContentSource : & csi.VolumeContentSource {
598
- Type : & csi.VolumeContentSource_Snapshot {
599
- Snapshot : & csi.VolumeContentSource_SnapshotSource {
600
- SnapshotId : "snapshot-source" ,
601
- },
602
- },
603
- },
604
- },
605
- expVol : & csi.Volume {
606
- CapacityBytes : common .GbToBytes (20 ),
607
- VolumeId : testVolumeId ,
608
- VolumeContext : nil ,
609
- AccessibleTopology : stdTopology ,
610
- ContentSource : & csi.VolumeContentSource {
611
- Type : & csi.VolumeContentSource_Snapshot {
612
- Snapshot : & csi.VolumeContentSource_SnapshotSource {
613
- SnapshotId : "snapshot-source" ,
614
- },
615
- },
616
- },
617
- },
618
- },
619
593
{
620
594
name : "success with block volume capability" ,
621
595
req : & csi.CreateVolumeRequest {
@@ -690,6 +664,8 @@ func TestCreateVolumeArguments(t *testing.T) {
690
664
// Setup new driver each time so no interference
691
665
gceDriver := initGCEDriver (t , nil )
692
666
667
+ //gceDriver.cs.CloudProvider.CreateSnapshot(context.Background, )
668
+
693
669
// Start Test
694
670
resp , err := gceDriver .cs .CreateVolume (context .Background (), tc .req )
695
671
//check response
@@ -728,6 +704,77 @@ func TestCreateVolumeArguments(t *testing.T) {
728
704
}
729
705
}
730
706
707
+ func TestCreateVolumeWithVolumeSource (t * testing.T ) {
708
+ // Define test cases
709
+ testCases := []struct {
710
+ name string
711
+ volKey * meta.Key
712
+ snapshotOnCloud bool
713
+ expErrCode codes.Code
714
+ }{
715
+ {
716
+ name : "success with data source of snapshot type" ,
717
+ volKey : meta .ZonalKey ("my-disk" , zone ),
718
+ snapshotOnCloud : true ,
719
+ },
720
+ {
721
+ name : "fail with data source of snapshot type that doesn't exist" ,
722
+ volKey : meta .ZonalKey ("my-disk" , zone ),
723
+ snapshotOnCloud : false ,
724
+ expErrCode : codes .NotFound ,
725
+ },
726
+ }
727
+
728
+ // Run test cases
729
+ for _ , tc := range testCases {
730
+ t .Logf ("test case: %s" , tc .name )
731
+ // Setup new driver each time so no interference
732
+ gceDriver := initGCEDriver (t , nil )
733
+
734
+ //gceDriver.cs.CloudProvider.CreateSnapshot(context.Background, )
735
+
736
+ // Start Test
737
+ req := & csi.CreateVolumeRequest {
738
+ Name : "test-name" ,
739
+ CapacityRange : stdCapRange ,
740
+ VolumeCapabilities : stdVolCaps ,
741
+ VolumeContentSource : & csi.VolumeContentSource {
742
+ Type : & csi.VolumeContentSource_Snapshot {
743
+ Snapshot : & csi.VolumeContentSource_SnapshotSource {
744
+ SnapshotId : testSnapshotId ,
745
+ },
746
+ },
747
+ },
748
+ }
749
+
750
+ if tc .snapshotOnCloud {
751
+ gceDriver .cs .CloudProvider .CreateSnapshot (context .Background (), tc .volKey , name )
752
+ }
753
+ resp , err := gceDriver .cs .CreateVolume (context .Background (), req )
754
+ //check response
755
+ if err != nil {
756
+ serverError , ok := status .FromError (err )
757
+ if ! ok {
758
+ t .Fatalf ("Could not get error status code from err: %v" , serverError )
759
+ }
760
+ if serverError .Code () != tc .expErrCode {
761
+ t .Fatalf ("Expected error code: %v, got: %v. err : %v" , tc .expErrCode , serverError .Code (), err )
762
+ }
763
+ continue
764
+ }
765
+ if tc .expErrCode != codes .OK {
766
+ t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
767
+ }
768
+
769
+ // Make sure response has snapshot
770
+ vol := resp .GetVolume ()
771
+ if vol .ContentSource == nil || vol .ContentSource .Type == nil || vol .ContentSource .GetSnapshot () == nil || vol .ContentSource .GetSnapshot ().SnapshotId == "" {
772
+ t .Fatalf ("Expected volume content source to have snapshot ID, got none" )
773
+ }
774
+
775
+ }
776
+ }
777
+
731
778
func TestCreateVolumeRandomRequisiteTopology (t * testing.T ) {
732
779
req := & csi.CreateVolumeRequest {
733
780
Name : "test-name" ,
0 commit comments