diff --git a/cmd/csi-sanity/sanity_test.go b/cmd/csi-sanity/sanity_test.go index bf4c3f63..30960b18 100644 --- a/cmd/csi-sanity/sanity_test.go +++ b/cmd/csi-sanity/sanity_test.go @@ -50,6 +50,7 @@ func init() { flag.Int64Var(&config.TestVolumeSize, prefix+"testvolumesize", sanity.DefTestVolumeSize, "Base volume size used for provisioned volumes") flag.Int64Var(&config.TestVolumeExpandSize, prefix+"testvolumeexpandsize", 0, "Target size for expanded volumes") flag.StringVar(&config.TestVolumeParametersFile, prefix+"testvolumeparameters", "", "YAML file of volume parameters for provisioned volumes") + flag.StringVar(&config.TestSnapshotParametersFile, prefix+"testsnapshotparameters", "", "YAML file of snapshot parameters for provisioned snapshots") flag.BoolVar(&config.TestNodeVolumeAttachLimit, prefix+"testnodevolumeattachlimit", false, "Test node volume attach limit") flag.StringVar(&config.JUnitFile, prefix+"junitfile", "", "JUnit XML output file where test results will be written") flag.Parse() diff --git a/pkg/sanity/controller.go b/pkg/sanity/controller.go index 084bc660..01d8603d 100644 --- a/pkg/sanity/controller.go +++ b/pkg/sanity/controller.go @@ -732,7 +732,7 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity By("creating a snapshot") snapName := UniqueString("sanity-controller-snap-from-vol") - snapReq := MakeCreateSnapshotReq(sc, snapName, volume1.GetVolume().GetVolumeId(), nil) + snapReq := MakeCreateSnapshotReq(sc, snapName, volume1.GetVolume().GetVolumeId()) snap, err := c.CreateSnapshot(context.Background(), snapReq) Expect(err).NotTo(HaveOccurred()) Expect(snap).NotTo(BeNil()) @@ -1650,7 +1650,7 @@ var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *SanityConte Expect(err).NotTo(HaveOccurred()) By("creating a snapshot") - snapshotReq := MakeCreateSnapshotReq(sc, "listSnapshots-snapshot-1", volume.GetVolume().GetVolumeId(), nil) + snapshotReq := MakeCreateSnapshotReq(sc, "listSnapshots-snapshot-1", volume.GetVolume().GetVolumeId()) snapshot, err := c.CreateSnapshot(context.Background(), snapshotReq) Expect(err).NotTo(HaveOccurred()) @@ -1692,7 +1692,7 @@ var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *SanityConte Expect(err).NotTo(HaveOccurred()) By("creating a snapshot") - snapshotReq := MakeCreateSnapshotReq(sc, "listSnapshots-snapshot-2", volume.GetVolume().GetVolumeId(), nil) + snapshotReq := MakeCreateSnapshotReq(sc, "listSnapshots-snapshot-2", volume.GetVolume().GetVolumeId()) snapshot, err := c.CreateSnapshot(context.Background(), snapshotReq) Expect(err).NotTo(HaveOccurred()) @@ -1743,7 +1743,7 @@ var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *SanityConte Expect(err).NotTo(HaveOccurred()) By("creating a snapshot") - snapReq := MakeCreateSnapshotReq(sc, "listSnapshots-snapshot-3", volume.GetVolume().GetVolumeId(), nil) + snapReq := MakeCreateSnapshotReq(sc, "listSnapshots-snapshot-3", volume.GetVolume().GetVolumeId()) snapshot, err := c.CreateSnapshot(context.Background(), snapReq) Expect(err).NotTo(HaveOccurred()) Expect(snapshot).NotTo(BeNil()) @@ -1811,7 +1811,7 @@ var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *SanityConte Expect(volume).NotTo(BeNil()) createVols = append(createVols, volume.GetVolume()) - snapReq := MakeCreateSnapshotReq(sc, "snapshot"+strconv.Itoa(i), volume.GetVolume().GetVolumeId(), nil) + snapReq := MakeCreateSnapshotReq(sc, "snapshot"+strconv.Itoa(i), volume.GetVolume().GetVolumeId()) snapshot, err := c.CreateSnapshot(context.Background(), snapReq) Expect(err).NotTo(HaveOccurred()) Expect(snapshot).NotTo(BeNil()) @@ -1915,7 +1915,7 @@ var _ = DescribeSanity("DeleteSnapshot [Controller Server]", func(sc *SanityCont // Create Snapshot First By("creating a snapshot") - snapshotReq := MakeCreateSnapshotReq(sc, "DeleteSnapshot-snapshot-1", volume.GetVolume().GetVolumeId(), nil) + snapshotReq := MakeCreateSnapshotReq(sc, "DeleteSnapshot-snapshot-1", volume.GetVolume().GetVolumeId()) snapshot, err := c.CreateSnapshot(context.Background(), snapshotReq) Expect(err).NotTo(HaveOccurred()) Expect(snapshot).NotTo(BeNil()) @@ -1988,7 +1988,7 @@ var _ = DescribeSanity("CreateSnapshot [Controller Server]", func(sc *SanityCont Expect(err).NotTo(HaveOccurred()) By("creating a snapshot") - snapReq1 := MakeCreateSnapshotReq(sc, "CreateSnapshot-snapshot-1", volume.GetVolume().GetVolumeId(), nil) + snapReq1 := MakeCreateSnapshotReq(sc, "CreateSnapshot-snapshot-1", volume.GetVolume().GetVolumeId()) snap1, err := c.CreateSnapshot(context.Background(), snapReq1) Expect(err).NotTo(HaveOccurred()) Expect(snap1).NotTo(BeNil()) @@ -2017,7 +2017,7 @@ var _ = DescribeSanity("CreateSnapshot [Controller Server]", func(sc *SanityCont Expect(err).ToNot(HaveOccurred()) By("creating a snapshot with the created volume source id") - req1 := MakeCreateSnapshotReq(sc, "CreateSnapshot-snapshot-2", volume.GetVolume().GetVolumeId(), nil) + req1 := MakeCreateSnapshotReq(sc, "CreateSnapshot-snapshot-2", volume.GetVolume().GetVolumeId()) snap1, err := c.CreateSnapshot(context.Background(), req1) Expect(err).NotTo(HaveOccurred()) Expect(snap1).NotTo(BeNil()) @@ -2027,7 +2027,7 @@ var _ = DescribeSanity("CreateSnapshot [Controller Server]", func(sc *SanityCont Expect(err).ToNot(HaveOccurred()) By("creating a snapshot with the same name but different volume source id") - req2 := MakeCreateSnapshotReq(sc, "CreateSnapshot-snapshot-2", volume2.GetVolume().GetVolumeId(), nil) + req2 := MakeCreateSnapshotReq(sc, "CreateSnapshot-snapshot-2", volume2.GetVolume().GetVolumeId()) _, err = c.CreateSnapshot(context.Background(), req2) Expect(err).To(HaveOccurred()) serverError, ok := status.FromError(err) @@ -2059,7 +2059,7 @@ var _ = DescribeSanity("CreateSnapshot [Controller Server]", func(sc *SanityCont name := string(nameBytes) By("creating a snapshot") - snapReq1 := MakeCreateSnapshotReq(sc, name, volume.GetVolume().GetVolumeId(), nil) + snapReq1 := MakeCreateSnapshotReq(sc, name, volume.GetVolume().GetVolumeId()) snap1, err := c.CreateSnapshot(context.Background(), snapReq1) Expect(err).NotTo(HaveOccurred()) Expect(snap1).NotTo(BeNil()) @@ -2217,11 +2217,11 @@ func MakeCreateVolumeReq(sc *SanityContext, name string) *csi.CreateVolumeReques return req } -func MakeCreateSnapshotReq(sc *SanityContext, name, sourceVolumeId string, parameters map[string]string) *csi.CreateSnapshotRequest { +func MakeCreateSnapshotReq(sc *SanityContext, name, sourceVolumeId string) *csi.CreateSnapshotRequest { req := &csi.CreateSnapshotRequest{ Name: name, SourceVolumeId: sourceVolumeId, - Parameters: parameters, + Parameters: sc.Config.TestSnapshotParameters, } if sc.Secrets != nil { diff --git a/pkg/sanity/sanity.go b/pkg/sanity/sanity.go index 27c983ff..060e2891 100644 --- a/pkg/sanity/sanity.go +++ b/pkg/sanity/sanity.go @@ -76,6 +76,10 @@ type Config struct { JUnitFile string + // TestSnapshotParametersFile for setting CreateVolumeRequest.Parameters. + TestSnapshotParametersFile string + TestSnapshotParameters map[string]string + // Callback functions to customize the creation of target and staging // directories. Returns the new paths for mount and staging. // If not defined, directories are created in the default way at TargetPath @@ -163,17 +167,10 @@ func newContext(reqConfig *Config) *SanityContext { // Test will test the CSI driver at the specified address by // setting up a Ginkgo suite and running it. func Test(t *testing.T, reqConfig *Config) { - path := reqConfig.TestVolumeParametersFile - if len(path) != 0 { - yamlFile, err := ioutil.ReadFile(path) - if err != nil { - panic(fmt.Sprintf("failed to read file %q: %v", path, err)) - } - err = yaml.Unmarshal(yamlFile, &reqConfig.TestVolumeParameters) - if err != nil { - panic(fmt.Sprintf("error unmarshaling yaml: %v", err)) - } - } + // Get StorageClass parameters from TestVolumeParametersFile + loadFromFile(reqConfig.TestVolumeParametersFile, &reqConfig.TestVolumeParameters) + // Get VolumeSnapshotClass parameters from TestSnapshotParametersFile + loadFromFile(reqConfig.TestSnapshotParametersFile, &reqConfig.TestSnapshotParameters) sc := newContext(reqConfig) registerTestsInGinkgo(sc) @@ -357,6 +354,20 @@ func loadSecrets(path string) (*CSISecrets, error) { return &creds, nil } +// loadFromFile reads struct from given file path. +func loadFromFile(from string, to interface{}) { + if len(from) != 0 { + yamlFile, err := ioutil.ReadFile(from) + if err != nil { + panic(fmt.Sprintf("failed to read file %q: %v", from, err)) + } + err = yaml.Unmarshal(yamlFile, to) + if err != nil { + panic(fmt.Sprintf("error unmarshaling yaml: %v", err)) + } + } +} + var uniqueSuffix = "-" + PseudoUUID() // PseudoUUID returns a unique string generated from random