Skip to content

Commit cc70cf6

Browse files
committed
Add Volume ID Generator interface for generating valid ID's
1 parent c094d20 commit cc70cf6

File tree

5 files changed

+76
-17
lines changed

5 files changed

+76
-17
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ type Config struct {
120120
RemoveStagingPathCmd string
121121
// Timeout for the executed commands for path removal.
122122
RemovePathCmdTimeout int
123+
124+
IDGen IDGenerator
123125
}
124126

125127
// SanityContext holds the variables that each test can depend on. It
@@ -153,6 +155,10 @@ func Test(t *testing.T, reqConfig *Config) {
153155
}
154156
}
155157

158+
if reqConfig.IDGen == nil {
159+
reqConfig.IDGen = &DefaultIDGenerator{}
160+
}
161+
156162
sc := &SanityContext{
157163
Config: reqConfig,
158164
}

pkg/sanity/util.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
type IDGenerator interface {
26+
GenerateUniqueValidVolumeID() string
27+
GenerateInvalidVolumeID() string
28+
29+
GenerateUniqueValidNodeID() string
30+
GenerateInvalidNodeID() string
31+
}
32+
33+
var _ IDGenerator = &DefaultIDGenerator{}
34+
35+
type DefaultIDGenerator struct {
36+
}
37+
38+
func (d DefaultIDGenerator) GenerateUniqueValidVolumeID() string {
39+
return fmt.Sprintf("fake-volid-%s", uuid.New().String()[:10])
40+
}
41+
42+
func (d DefaultIDGenerator) GenerateInvalidVolumeID() string {
43+
return "fake-volid"
44+
}
45+
46+
func (d DefaultIDGenerator) GenerateUniqueValidNodeID() string {
47+
return fmt.Sprintf("fake-nodeid-%s", uuid.New().String()[:10])
48+
}
49+
50+
func (d DefaultIDGenerator) GenerateInvalidNodeID() string {
51+
return "fake-nodeid"
52+
}

0 commit comments

Comments
 (0)