Skip to content

Commit fda8ad3

Browse files
authored
Merge pull request #212 from davidz627/feature/volIDGenerator
Add ID Generator interface for generating valid IDs for Volume and Node
2 parents c094d20 + 43c8a31 commit fda8ad3

25 files changed

+1046
-17
lines changed

Gopkg.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hack/_embedded/embedded_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var _ = Describe("MyCSIDriver", func() {
3030
StagingPath: os.TempDir() + "/csi-staging",
3131
Address: "/tmp/e2e-csi-sanity.sock",
3232
TestNodeVolumeAttachLimit: true,
33+
IDGen: &sanity.DefaultIDGenerator{},
3334
}
3435

3536
BeforeEach(func() {})

pkg/sanity/controller.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity
825825
volReq.VolumeContentSource = &csi.VolumeContentSource{
826826
Type: &csi.VolumeContentSource_Volume{
827827
Volume: &csi.VolumeContentSource_VolumeSource{
828-
VolumeId: "non-existing-volume-id",
828+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
829829
},
830830
},
831831
}
@@ -864,7 +864,7 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity
864864
_, err := c.DeleteVolume(
865865
context.Background(),
866866
&csi.DeleteVolumeRequest{
867-
VolumeId: "reallyfakevolumeid",
867+
VolumeId: sc.Config.IDGen.GenerateInvalidVolumeID(),
868868
Secrets: sc.Secrets.DeleteVolumeSecret,
869869
},
870870
)
@@ -1061,7 +1061,7 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity
10611061
_, err := c.ValidateVolumeCapabilities(
10621062
context.Background(),
10631063
&csi.ValidateVolumeCapabilitiesRequest{
1064-
VolumeId: "some-vol-id",
1064+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
10651065
VolumeCapabilities: []*csi.VolumeCapability{
10661066
{
10671067
AccessType: &csi.VolumeCapability_Mount{
@@ -1110,7 +1110,7 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity
11101110
_, err := c.ControllerPublishVolume(
11111111
context.Background(),
11121112
&csi.ControllerPublishVolumeRequest{
1113-
VolumeId: "id",
1113+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
11141114
Secrets: sc.Secrets.ControllerPublishVolumeSecret,
11151115
},
11161116
)
@@ -1126,8 +1126,8 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity
11261126
_, err := c.ControllerPublishVolume(
11271127
context.Background(),
11281128
&csi.ControllerPublishVolumeRequest{
1129-
VolumeId: "id",
1130-
NodeId: "fakenode",
1129+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
1130+
NodeId: sc.Config.IDGen.GenerateUniqueValidNodeID(),
11311131
Secrets: sc.Secrets.ControllerPublishVolumeSecret,
11321132
},
11331133
)
@@ -1287,8 +1287,8 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity
12871287
conpubvol, err := c.ControllerPublishVolume(
12881288
context.Background(),
12891289
&csi.ControllerPublishVolumeRequest{
1290-
VolumeId: "some-vol-id",
1291-
NodeId: "some-node-id",
1290+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
1291+
NodeId: sc.Config.IDGen.GenerateUniqueValidNodeID(),
12921292
VolumeCapability: &csi.VolumeCapability{
12931293
AccessType: &csi.VolumeCapability_Mount{
12941294
Mount: &csi.VolumeCapability_MountVolume{},
@@ -1346,7 +1346,7 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity
13461346
context.Background(),
13471347
&csi.ControllerPublishVolumeRequest{
13481348
VolumeId: vol.GetVolume().GetVolumeId(),
1349-
NodeId: "some-fake-node-id",
1349+
NodeId: sc.Config.IDGen.GenerateUniqueValidNodeID(),
13501350
VolumeCapability: &csi.VolumeCapability{
13511351
AccessType: &csi.VolumeCapability_Mount{
13521352
Mount: &csi.VolumeCapability_MountVolume{},
@@ -1711,7 +1711,7 @@ var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *SanityConte
17111711

17121712
snapshots, err := c.ListSnapshots(
17131713
context.Background(),
1714-
&csi.ListSnapshotsRequest{SourceVolumeId: "none-exist-volume-id"})
1714+
&csi.ListSnapshotsRequest{SourceVolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID()})
17151715
Expect(err).NotTo(HaveOccurred())
17161716
Expect(snapshots).NotTo(BeNil())
17171717
Expect(snapshots.GetEntries()).To(BeEmpty())

pkg/sanity/node.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
187187
_, err := c.NodePublishVolume(
188188
context.Background(),
189189
&csi.NodePublishVolumeRequest{
190-
VolumeId: "id",
190+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
191191
Secrets: sc.Secrets.NodePublishVolumeSecret,
192192
},
193193
)
@@ -202,7 +202,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
202202
_, err := c.NodePublishVolume(
203203
context.Background(),
204204
&csi.NodePublishVolumeRequest{
205-
VolumeId: "id",
205+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
206206
TargetPath: sc.TargetPath + "/target",
207207
Secrets: sc.Secrets.NodePublishVolumeSecret,
208208
},
@@ -233,7 +233,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
233233
_, err := c.NodeUnpublishVolume(
234234
context.Background(),
235235
&csi.NodeUnpublishVolumeRequest{
236-
VolumeId: "id",
236+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
237237
})
238238
Expect(err).To(HaveOccurred())
239239

@@ -286,7 +286,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
286286
_, err := c.NodeStageVolume(
287287
context.Background(),
288288
&csi.NodeStageVolumeRequest{
289-
VolumeId: "id",
289+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
290290
VolumeCapability: &csi.VolumeCapability{
291291
AccessType: &csi.VolumeCapability_Mount{
292292
Mount: &csi.VolumeCapability_MountVolume{},
@@ -395,7 +395,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
395395
_, err := c.NodeUnstageVolume(
396396
context.Background(),
397397
&csi.NodeUnstageVolumeRequest{
398-
VolumeId: "id",
398+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
399399
})
400400
Expect(err).To(HaveOccurred())
401401

@@ -430,7 +430,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
430430
_, err := c.NodeGetVolumeStats(
431431
context.Background(),
432432
&csi.NodeGetVolumeStatsRequest{
433-
VolumeId: "id",
433+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
434434
},
435435
)
436436
Expect(err).To(HaveOccurred())
@@ -444,7 +444,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
444444
_, err := c.NodeGetVolumeStats(
445445
context.Background(),
446446
&csi.NodeGetVolumeStatsRequest{
447-
VolumeId: "id",
447+
VolumeId: sc.Config.IDGen.GenerateUniqueValidVolumeID(),
448448
VolumePath: "some/path",
449449
},
450450
)

pkg/sanity/sanity.go

+9
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ type Config struct {
120120
RemoveStagingPathCmd string
121121
// Timeout for the executed commands for path removal.
122122
RemovePathCmdTimeout int
123+
124+
// IDGen is an optional interface for callers to provide a generator for
125+
// valid Volume and Node IDs. Defaults to DefaultIDGenerator which generates
126+
// generic string IDs
127+
IDGen IDGenerator
123128
}
124129

125130
// SanityContext holds the variables that each test can depend on. It
@@ -153,6 +158,10 @@ func Test(t *testing.T, reqConfig *Config) {
153158
}
154159
}
155160

161+
if reqConfig.IDGen == nil {
162+
reqConfig.IDGen = &DefaultIDGenerator{}
163+
}
164+
156165
sc := &SanityContext{
157166
Config: reqConfig,
158167
}

pkg/sanity/util.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2019 Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package sanity
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/google/uuid"
23+
)
24+
25+
// IDGenerator generates valid and invalid Volume and Node IDs to be used in
26+
// tests
27+
type IDGenerator interface {
28+
// GenerateUniqueValidVolumeID must generate a unique Volume ID that the CSI
29+
// Driver considers in valid form
30+
GenerateUniqueValidVolumeID() string
31+
32+
// GenerateInvalidVolumeID must output a Volume ID that the CSI Driver MAY
33+
// consider invalid. Some drivers may not have requirements on IDs in which
34+
// case this method should output any non-empty ID
35+
GenerateInvalidVolumeID() string
36+
37+
// GenerateUniqueValidNodeID must generate a unique Node ID that the CSI
38+
// Driver considers in valid form
39+
GenerateUniqueValidNodeID() string
40+
41+
// GenerateInvalidNodeID must output a Node ID that the CSI Driver MAY
42+
// consider invalid. Some drivers may not have requirements on IDs in which
43+
// case this method should output any non-empty ID
44+
GenerateInvalidNodeID() string
45+
}
46+
47+
var _ IDGenerator = &DefaultIDGenerator{}
48+
49+
type DefaultIDGenerator struct {
50+
}
51+
52+
func (d DefaultIDGenerator) GenerateUniqueValidVolumeID() string {
53+
return fmt.Sprintf("fake-vol-id-%s", uuid.New().String()[:10])
54+
}
55+
56+
func (d DefaultIDGenerator) GenerateInvalidVolumeID() string {
57+
return "fake-vol-id"
58+
}
59+
60+
func (d DefaultIDGenerator) GenerateUniqueValidNodeID() string {
61+
return fmt.Sprintf("fake-node-id-%s", uuid.New().String()[:10])
62+
}
63+
64+
func (d DefaultIDGenerator) GenerateInvalidNodeID() string {
65+
return "fake-node-id"
66+
}

vendor/github.com/google/uuid/.travis.yml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/CONTRIBUTING.md

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/CONTRIBUTORS

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/LICENSE

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/README.md

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)